| 1 | #cxx . . $ `<file>/src/smploaders/pt2_load_wav.c`/include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdint.h> |
| 4 | #include <stdbool.h> |
| 5 | #include <math.h> |
| 6 | #include "../pt2_header.h"cxx . . $ `<file>/src/pt2_header.h`/ |
| 7 | #include "../pt2_config.h"cxx . . $ `<file>/src/pt2_config.h`/ |
| 8 | #include "../pt2_structs.h"cxx . . $ `<file>/src/pt2_structs.h`/ |
| 9 | #include "../pt2_textout.h"cxx . . $ `<file>/src/pt2_textout.h`/ |
| 10 | #include "../pt2_visuals.h"cxx . . $ `<file>/src/pt2_visuals.h`/ |
| 11 | #include "../pt2_helpers.h"cxx . . $ `<file>/src/pt2_helpers.h`/ |
| 12 | #include "../pt2_replayer.h"cxx . . $ `<file>/src/pt2_replayer.h`/ |
| 13 | #include "../pt2_askbox.h"cxx . . $ `<file>/src/pt2_askbox.h`/ |
| 14 | #include "../pt2_downsample2x.h"cxx . . $ `<file>/src/pt2_downsample2x.h`/ |
| 15 | #include "../pt2_audio.h"cxx . . $ `<file>/src/pt2_audio.h`/ |
| 16 | |
| 17 | enumcxx . . $ $anonymous_type_150bf8407b45ab74_0# |
| 18 | { |
| 19 | WAV_FORMAT_PCMcxx . . $ WAV_FORMAT_PCM. = 1, |
| 20 | WAV_FORMAT_IEEE_FLOATcxx . . $ WAV_FORMAT_IEEE_FLOAT. = 3, |
| 21 | WAV_FORMAT_EXTENSIBLEcxx . . $ WAV_FORMAT_EXTENSIBLE. = 65534 |
| 22 | }; |
| 23 | |
| 24 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! loadWAVSamplecxx . . $ loadWAVSample(85a5b89df4a007d2).(FILEcxx . . $ FILE# *f, uint32_tcxx . . $ uint32_t# filesize, moduleSample_tcxx . . $ moduleSample_t# *s) |
| 25 | { |
| 26 | uint16_tcxx . . $ uint16_t# audioFormat, numChannels, bitsPerSample; |
| 27 | uint32_tcxx . . $ uint32_t# sampleRate; |
| 28 | |
| 29 | // zero out chunk pointers and lengths |
| 30 | uint32_tcxx . . $ uint32_t# fmtPtr = 0; uint32_tcxx . . $ uint32_t# fmtLen = 0; |
| 31 | uint32_tcxx . . $ uint32_t# dataPtr = 0; uint32_tcxx . . $ uint32_t# dataLen = 0; |
| 32 | uint32_tcxx . . $ uint32_t# inamPtr = 0; int32_tcxx . . $ int32_t# inamLen = 0; |
| 33 | uint32_tcxx . . $ uint32_t# xtraPtr = 0; uint32_tcxx . . $ uint32_t# xtraLen = 0; |
| 34 | uint32_tcxx . . $ uint32_t# smplPtr = 0; uint32_tcxx . . $ uint32_t# smplLen = 0; |
| 35 | |
| 36 | // look for wanted chunks and set up pointers + lengths |
| 37 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, 12, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 38 | |
| 39 | uint32_tcxx . . $ uint32_t# bytesRead = 0; |
| 40 | while (!feofcxx . . $ feof(89bc1efcdac0ca22).(f) && bytesRead < (uint32_tcxx . . $ uint32_t#)filesize-12) |
| 41 | { |
| 42 | uint32_tcxx . . $ uint32_t# chunkID, chunkSize; |
| 43 | |
| 44 | freadcxx . . $ fread(738fc8ac1d711e58).(&chunkID, 4, 1, f); if (feofcxx . . $ feof(89bc1efcdac0ca22).(f)) break; |
| 45 | freadcxx . . $ fread(738fc8ac1d711e58).(&chunkSize, 4, 1, f); if (feofcxx . . $ feof(89bc1efcdac0ca22).(f)) break; |
| 46 | |
| 47 | uint32_tcxx . . $ uint32_t# endOfChunk = (ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(f) + chunkSize) + (chunkSize & 1); |
| 48 | switch (chunkID) |
| 49 | { |
| 50 | case 0x20746D66: // "fmt " |
| 51 | { |
| 52 | fmtPtr = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(f); |
| 53 | fmtLen = chunkSize; |
| 54 | } |
| 55 | break; |
| 56 | |
| 57 | case 0x61746164: // "data" |
| 58 | { |
| 59 | dataPtr = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(f); |
| 60 | dataLen = chunkSize; |
| 61 | } |
| 62 | break; |
| 63 | |
| 64 | case 0x5453494C: // "LIST" |
| 65 | { |
| 66 | if (chunkSize >= 4) |
| 67 | { |
| 68 | freadcxx . . $ fread(738fc8ac1d711e58).(&chunkID, 4, 1, f); |
| 69 | if (chunkID == 0x4F464E49) // "INFO" |
| 70 | { |
| 71 | bytesRead = 0; |
| 72 | while (!feofcxx . . $ feof(89bc1efcdac0ca22).(f) && bytesRead < chunkSize) |
| 73 | { |
| 74 | freadcxx . . $ fread(738fc8ac1d711e58).(&chunkID, 4, 1, f); |
| 75 | freadcxx . . $ fread(738fc8ac1d711e58).(&chunkSize, 4, 1, f); |
| 76 | |
| 77 | switch (chunkID) |
| 78 | { |
| 79 | case 0x4D414E49: // "INAM" |
| 80 | { |
| 81 | inamPtr = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(f); |
| 82 | inamLen = chunkSize; |
| 83 | } |
| 84 | break; |
| 85 | |
| 86 | default: break; |
| 87 | } |
| 88 | |
| 89 | bytesRead += chunkSize + (chunkSize & 1); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | break; |
| 95 | |
| 96 | case 0x61727478: // "xtra" |
| 97 | { |
| 98 | xtraPtr = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(f); |
| 99 | xtraLen = chunkSize; |
| 100 | } |
| 101 | break; |
| 102 | |
| 103 | case 0x6C706D73: // "smpl" |
| 104 | { |
| 105 | smplPtr = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(f); |
| 106 | smplLen = chunkSize; |
| 107 | } |
| 108 | break; |
| 109 | |
| 110 | default: break; |
| 111 | } |
| 112 | |
| 113 | bytesRead += chunkSize + (chunkSize & 1); |
| 114 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, endOfChunk, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 115 | } |
| 116 | |
| 117 | // we need at least "fmt " and "data" - check if we found them sanely |
| 118 | if (fmtPtr == 0 || fmtLen < 16 || dataPtr == 0 || dataLen == 0) |
| 119 | { |
| 120 | displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("NOT A WAV !"); |
| 121 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 122 | } |
| 123 | |
| 124 | // ---- READ "fmt " CHUNK ---- |
| 125 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, fmtPtr, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 126 | freadcxx . . $ fread(738fc8ac1d711e58).(&audioFormat, 2, 1, f); |
| 127 | freadcxx . . $ fread(738fc8ac1d711e58).(&numChannels, 2, 1, f); |
| 128 | freadcxx . . $ fread(738fc8ac1d711e58).(&sampleRate, 4, 1, f); |
| 129 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, 6, SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!); |
| 130 | freadcxx . . $ fread(738fc8ac1d711e58).(&bitsPerSample, 2, 1, f); |
| 131 | |
| 132 | int32_tcxx . . $ int32_t# sampleLength = dataLen; |
| 133 | |
| 134 | if (audioFormat == WAV_FORMAT_EXTENSIBLEcxx . . $ WAV_FORMAT_EXTENSIBLE.) |
| 135 | { |
| 136 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, 8, SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!); |
| 137 | freadcxx . . $ fread(738fc8ac1d711e58).(&audioFormat, 2, 1, f); |
| 138 | } |
| 139 | // --------------------------- |
| 140 | |
| 141 | if (sampleRate == 0 || sampleLength == 0 || sampleLength >= (int32_tcxx . . $ int32_t#)filesize*(bitsPerSample/8)) |
| 142 | { |
| 143 | displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("WAV CORRUPT !"); |
| 144 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 145 | } |
| 146 | |
| 147 | if (audioFormat != WAV_FORMAT_PCMcxx . . $ WAV_FORMAT_PCM. && audioFormat != WAV_FORMAT_IEEE_FLOATcxx . . $ WAV_FORMAT_IEEE_FLOAT.) |
| 148 | { |
| 149 | displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("WAV UNSUPPORTED !"); |
| 150 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 151 | } |
| 152 | |
| 153 | if (numChannels == 0 || numChannels > 2) |
| 154 | { |
| 155 | displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("WAV UNSUPPORTED !"); |
| 156 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 157 | } |
| 158 | |
| 159 | if (audioFormat == WAV_FORMAT_IEEE_FLOATcxx . . $ WAV_FORMAT_IEEE_FLOAT. && bitsPerSample != 32 && bitsPerSample != 64) |
| 160 | { |
| 161 | displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("WAV UNSUPPORTED !"); |
| 162 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 163 | } |
| 164 | |
| 165 | if (bitsPerSample != 8 && bitsPerSample != 16 && bitsPerSample != 24 && bitsPerSample != 32 && bitsPerSample != 64) |
| 166 | { |
| 167 | displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("WAV UNSUPPORTED !"); |
| 168 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 169 | } |
| 170 | |
| 171 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! downSample = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 172 | if (sampleRate > 22050 && !configcxx . . $ config..noDownsampleOnSmpLoadcxx . . $ config_t#noDownsampleOnSmpLoad.) |
| 173 | { |
| 174 | if (askBoxcxx . . $ askBox(7371ee129cebb502).(ASKBOX_DOWNSAMPLEcxx . . $ ASKBOX_DOWNSAMPLE., "DOWNSAMPLE ?")) |
| 175 | downSample = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 176 | } |
| 177 | |
| 178 | // ---- READ SAMPLE DATA ---- |
| 179 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, dataPtr, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 180 | |
| 181 | int8_tcxx . . $ int8_t# *smpDataPtr = &songcxx . . $ song.->sampleDatacxx . . $ module_t#sampleData.[s->offsetcxx . . $ moduleSample_t#offset.]; |
| 182 | |
| 183 | if (bitsPerSample == 8) // 8-BIT INTEGER SAMPLE |
| 184 | { |
| 185 | if (sampleLength > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.*4) |
| 186 | sampleLength = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.*4; |
| 187 | |
| 188 | uint8_tcxx . . $ uint8_t# *audioDataU8 = (uint8_tcxx . . $ uint8_t# *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sampleLength * sizeof (uint8_tcxx . . $ uint8_t#)); |
| 189 | if (audioDataU8 == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 190 | { |
| 191 | statusOutOfMemorycxx . . $ statusOutOfMemory(49f6e7a06ebc5aa8).(); |
| 192 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 193 | } |
| 194 | |
| 195 | // read sample data |
| 196 | if (freadcxx . . $ fread(738fc8ac1d711e58).(audioDataU8, 1, sampleLength, f) != (size_tcxx . . $ size_t#)sampleLength) |
| 197 | { |
| 198 | freecxx . . $ free(5d22bdacc48458e8).(audioDataU8); |
| 199 | displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("I/O ERROR !"); |
| 200 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 201 | } |
| 202 | |
| 203 | // convert from stereo to mono (if needed) |
| 204 | if (numChannels == 2) |
| 205 | { |
| 206 | sampleLength >>= 1; |
| 207 | for (int32_tcxx . . $ int32_t# i = 0; i < sampleLength; i++) // add right channel to left channel |
| 208 | { |
| 209 | int32_tcxx . . $ int32_t# smp32 = (audioDataU8[(i << 1) + 0] - 128) + (audioDataU8[(i << 1) + 1] - 128); |
| 210 | smp32 = 128 + (smp32 >> 1); |
| 211 | audioDataU8[i] = (uint8_tcxx . . $ uint8_t#)smp32; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // 2x downsampling |
| 216 | if (downSample) |
| 217 | { |
| 218 | downsample2x8BitUcxx . . $ downsample2x8BitU(f3e7299796ab6c6f).(audioDataU8, sampleLength); |
| 219 | sampleLength >>= 1; |
| 220 | } |
| 221 | |
| 222 | if (sampleLength > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.) |
| 223 | sampleLength = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.; |
| 224 | |
| 225 | turnOffVoicescxx . . $ turnOffVoices(49f6e7a06ebc5aa8).(); |
| 226 | for (int32_tcxx . . $ int32_t# i = 0; i < sampleLength; i++) |
| 227 | smpDataPtr[i] = audioDataU8[i] - 128; |
| 228 | |
| 229 | freecxx . . $ free(5d22bdacc48458e8).(audioDataU8); |
| 230 | } |
| 231 | else if (bitsPerSample == 16) // 16-BIT INTEGER SAMPLE |
| 232 | { |
| 233 | sampleLength >>= 1; |
| 234 | if (sampleLength > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.*4) |
| 235 | sampleLength = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.*4; |
| 236 | |
| 237 | int16_tcxx . . $ int16_t# *audioDataS16 = (int16_tcxx . . $ int16_t# *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sampleLength * sizeof (int16_tcxx . . $ int16_t#)); |
| 238 | if (audioDataS16 == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 239 | { |
| 240 | statusOutOfMemorycxx . . $ statusOutOfMemory(49f6e7a06ebc5aa8).(); |
| 241 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 242 | } |
| 243 | |
| 244 | // read sample data |
| 245 | if (freadcxx . . $ fread(738fc8ac1d711e58).(audioDataS16, 2, sampleLength, f) != (size_tcxx . . $ size_t#)sampleLength) |
| 246 | { |
| 247 | freecxx . . $ free(5d22bdacc48458e8).(audioDataS16); |
| 248 | displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("I/O ERROR !"); |
| 249 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 250 | } |
| 251 | |
| 252 | // convert from stereo to mono (if needed) |
| 253 | if (numChannels == 2) |
| 254 | { |
| 255 | sampleLength >>= 1; |
| 256 | for (int32_tcxx . . $ int32_t# i = 0; i < sampleLength; i++) // add right channel to left channel |
| 257 | audioDataS16[i] = (audioDataS16[(i << 1) + 0] + audioDataS16[(i << 1) + 1]) >> 1; |
| 258 | } |
| 259 | |
| 260 | // 2x downsampling |
| 261 | if (downSample) |
| 262 | { |
| 263 | downsample2x16Bitcxx . . $ downsample2x16Bit(6f0a599713eb5608).(audioDataS16, sampleLength); |
| 264 | sampleLength >>= 1; |
| 265 | } |
| 266 | |
| 267 | if (sampleLength > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.) |
| 268 | sampleLength = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.; |
| 269 | |
| 270 | double dAmp = 1.0; |
| 271 | if (downSample) // we already normalized |
| 272 | { |
| 273 | dAmp = INT8_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:110:10`! / (double)INT16_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:111:10`!; |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | const double dPeak = get16BitPeakcxx . . $ get16BitPeak(5bbf7f3ea503fca5).(audioDataS16, sampleLength); |
| 278 | if (dPeak > 0.0) |
| 279 | dAmp = INT8_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:110:10`! / dPeak; |
| 280 | } |
| 281 | |
| 282 | turnOffVoicescxx . . $ turnOffVoices(49f6e7a06ebc5aa8).(); |
| 283 | for (int32_tcxx . . $ int32_t# i = 0; i < sampleLength; i++) |
| 284 | { |
| 285 | int32_tcxx . . $ int32_t# smp32 = (int32_tcxx . . $ int32_t#)round(audioDataS16[i] * dAmp); |
| 286 | ASSERTcxx . . $ `src/pt2_header.h:25:9`!(smp32 >= -128 && smp32 <= 127); // shouldn't happen according to dAmp (but just in case) |
| 287 | smpDataPtr[i] = (int8_tcxx . . $ int8_t#)smp32; |
| 288 | } |
| 289 | |
| 290 | freecxx . . $ free(5d22bdacc48458e8).(audioDataS16); |
| 291 | } |
| 292 | else if (bitsPerSample == 24) // 24-BIT INTEGER SAMPLE |
| 293 | { |
| 294 | sampleLength /= 3; |
| 295 | if (sampleLength > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.*4) |
| 296 | sampleLength = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.*4; |
| 297 | |
| 298 | int32_tcxx . . $ int32_t# *audioDataS32 = (int32_tcxx . . $ int32_t# *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sampleLength * sizeof (int32_tcxx . . $ int32_t#)); |
| 299 | if (audioDataS32 == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 300 | { |
| 301 | statusOutOfMemorycxx . . $ statusOutOfMemory(49f6e7a06ebc5aa8).(); |
| 302 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 303 | } |
| 304 | |
| 305 | // read sample data |
| 306 | uint8_tcxx . . $ uint8_t# *audioDataU8 = (uint8_tcxx . . $ uint8_t# *)audioDataS32; |
| 307 | for (int32_tcxx . . $ int32_t# i = 0; i < sampleLength; i++) |
| 308 | { |
| 309 | audioDataU8[0] = 0; |
| 310 | freadcxx . . $ fread(738fc8ac1d711e58).(&audioDataU8[1], 3, 1, f); |
| 311 | audioDataU8 += sizeof (int32_tcxx . . $ int32_t#); |
| 312 | } |
| 313 | |
| 314 | // convert from stereo to mono (if needed) |
| 315 | if (numChannels == 2) |
| 316 | { |
| 317 | sampleLength >>= 1; |
| 318 | for (int32_tcxx . . $ int32_t# i = 0; i < sampleLength; i++) // add right channel to left channel |
| 319 | { |
| 320 | int64_tcxx . . $ int64_t# smp = ((int64_tcxx . . $ int64_t#)audioDataS32[(i << 1) + 0] + audioDataS32[(i << 1) + 1]) >> 1; |
| 321 | audioDataS32[i] = (int32_tcxx . . $ int32_t#)smp; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // 2x downsampling |
| 326 | if (downSample) |
| 327 | { |
| 328 | downsample2x32Bitcxx . . $ downsample2x32Bit(fdfc7b0a9f333dff).(audioDataS32, sampleLength); |
| 329 | sampleLength >>= 1; |
| 330 | } |
| 331 | |
| 332 | if (sampleLength > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.) |
| 333 | sampleLength = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.; |
| 334 | |
| 335 | double dAmp = 1.0; |
| 336 | if (downSample) // we already normalized |
| 337 | { |
| 338 | dAmp = INT8_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:110:10`! / (double)INT32_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:112:10`!; |
| 339 | } |
| 340 | else |
| 341 | { |
| 342 | const double dPeak = get32BitPeakcxx . . $ get32BitPeak(7ca14e0a6faf8061).(audioDataS32, sampleLength); |
| 343 | if (dPeak > 0.0) |
| 344 | dAmp = INT8_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:110:10`! / dPeak; |
| 345 | } |
| 346 | |
| 347 | turnOffVoicescxx . . $ turnOffVoices(49f6e7a06ebc5aa8).(); |
| 348 | for (int32_tcxx . . $ int32_t# i = 0; i < sampleLength; i++) |
| 349 | { |
| 350 | int32_tcxx . . $ int32_t# smp32 = (int32_tcxx . . $ int32_t#)round(audioDataS32[i] * dAmp); |
| 351 | ASSERTcxx . . $ `src/pt2_header.h:25:9`!(smp32 >= -128 && smp32 <= 127); // shouldn't happen according to dAmp (but just in case) |
| 352 | smpDataPtr[i] = (int8_tcxx . . $ int8_t#)smp32; |
| 353 | } |
| 354 | |
| 355 | freecxx . . $ free(5d22bdacc48458e8).(audioDataS32); |
| 356 | } |
| 357 | else if (audioFormat == WAV_FORMAT_PCMcxx . . $ WAV_FORMAT_PCM. && bitsPerSample == 32) // 32-BIT INTEGER SAMPLE |
| 358 | { |
| 359 | sampleLength >>= 2; |
| 360 | if (sampleLength > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.*4) |
| 361 | sampleLength = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.*4; |
| 362 | |
| 363 | int32_tcxx . . $ int32_t# *audioDataS32 = (int32_tcxx . . $ int32_t# *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sampleLength * sizeof (int32_tcxx . . $ int32_t#)); |
| 364 | if (audioDataS32 == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 365 | { |
| 366 | statusOutOfMemorycxx . . $ statusOutOfMemory(49f6e7a06ebc5aa8).(); |
| 367 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 368 | } |
| 369 | |
| 370 | // read sample data |
| 371 | if (freadcxx . . $ fread(738fc8ac1d711e58).(audioDataS32, 4, sampleLength, f) != (size_tcxx . . $ size_t#)sampleLength) |
| 372 | { |
| 373 | freecxx . . $ free(5d22bdacc48458e8).(audioDataS32); |
| 374 | displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("I/O ERROR !"); |
| 375 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 376 | } |
| 377 | |
| 378 | // convert from stereo to mono (if needed) |
| 379 | if (numChannels == 2) |
| 380 | { |
| 381 | sampleLength >>= 1; |
| 382 | for (int32_tcxx . . $ int32_t# i = 0; i < sampleLength; i++) // add right channel to left channel |
| 383 | { |
| 384 | int64_tcxx . . $ int64_t# smp = ((int64_tcxx . . $ int64_t#)audioDataS32[(i << 1) + 0] + audioDataS32[(i << 1) + 1]) >> 1; |
| 385 | audioDataS32[i] = (int32_tcxx . . $ int32_t#)smp; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | // 2x downsampling |
| 390 | if (downSample) |
| 391 | { |
| 392 | downsample2x32Bitcxx . . $ downsample2x32Bit(fdfc7b0a9f333dff).(audioDataS32, sampleLength); |
| 393 | sampleLength >>= 1; |
| 394 | } |
| 395 | |
| 396 | if (sampleLength > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.) |
| 397 | sampleLength = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.; |
| 398 | |
| 399 | double dAmp = 1.0; |
| 400 | if (downSample) // we already normalized |
| 401 | { |
| 402 | dAmp = INT8_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:110:10`! / (double)INT32_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:112:10`!; |
| 403 | } |
| 404 | else |
| 405 | { |
| 406 | const double dPeak = get32BitPeakcxx . . $ get32BitPeak(7ca14e0a6faf8061).(audioDataS32, sampleLength); |
| 407 | if (dPeak > 0.0) |
| 408 | dAmp = INT8_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:110:10`! / dPeak; |
| 409 | } |
| 410 | |
| 411 | turnOffVoicescxx . . $ turnOffVoices(49f6e7a06ebc5aa8).(); |
| 412 | for (int32_tcxx . . $ int32_t# i = 0; i < sampleLength; i++) |
| 413 | { |
| 414 | int32_tcxx . . $ int32_t# smp32 = (int32_tcxx . . $ int32_t#)round(audioDataS32[i] * dAmp); |
| 415 | ASSERTcxx . . $ `src/pt2_header.h:25:9`!(smp32 >= -128 && smp32 <= 127); // shouldn't happen according to dAmp (but just in case) |
| 416 | smpDataPtr[i] = (int8_tcxx . . $ int8_t#)smp32; |
| 417 | } |
| 418 | |
| 419 | freecxx . . $ free(5d22bdacc48458e8).(audioDataS32); |
| 420 | } |
| 421 | else if (audioFormat == WAV_FORMAT_IEEE_FLOATcxx . . $ WAV_FORMAT_IEEE_FLOAT. && bitsPerSample == 32) // 32-BIT FLOATING POINT SAMPLE |
| 422 | { |
| 423 | sampleLength >>= 2; |
| 424 | if (sampleLength > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.*4) |
| 425 | sampleLength = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.*4; |
| 426 | |
| 427 | uint32_tcxx . . $ uint32_t# *audioDataU32 = (uint32_tcxx . . $ uint32_t# *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sampleLength * sizeof (uint32_tcxx . . $ uint32_t#)); |
| 428 | if (audioDataU32 == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 429 | { |
| 430 | statusOutOfMemorycxx . . $ statusOutOfMemory(49f6e7a06ebc5aa8).(); |
| 431 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 432 | } |
| 433 | |
| 434 | // read sample data |
| 435 | if (freadcxx . . $ fread(738fc8ac1d711e58).(audioDataU32, 4, sampleLength, f) != (size_tcxx . . $ size_t#)sampleLength) |
| 436 | { |
| 437 | freecxx . . $ free(5d22bdacc48458e8).(audioDataU32); |
| 438 | displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("I/O ERROR !"); |
| 439 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 440 | } |
| 441 | |
| 442 | float *fAudioDataFloat = (float *)audioDataU32; |
| 443 | |
| 444 | // convert from stereo to mono (if needed) |
| 445 | if (numChannels == 2) |
| 446 | { |
| 447 | sampleLength >>= 1; |
| 448 | for (int32_tcxx . . $ int32_t# i = 0; i < sampleLength; i++) // add right channel to left channel |
| 449 | fAudioDataFloat[i] = (fAudioDataFloat[(i * 2) + 0] + fAudioDataFloat[(i * 2) + 1]) * 0.5f; |
| 450 | } |
| 451 | |
| 452 | // 2x downsampling |
| 453 | if (downSample) |
| 454 | { |
| 455 | downsample2xFloatcxx . . $ downsample2xFloat(ac1d28a1859f2825).(fAudioDataFloat, sampleLength); |
| 456 | sampleLength >>= 1; |
| 457 | } |
| 458 | |
| 459 | if (sampleLength > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.) |
| 460 | sampleLength = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.; |
| 461 | |
| 462 | float fAmp = 1.0f; |
| 463 | const float fPeak = getFloatPeakcxx . . $ getFloatPeak(5c3cfcb0fda3feba).(fAudioDataFloat, sampleLength); |
| 464 | if (fPeak > 0.0f) |
| 465 | fAmp = INT8_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:110:10`! / fPeak; |
| 466 | |
| 467 | turnOffVoicescxx . . $ turnOffVoices(49f6e7a06ebc5aa8).(); |
| 468 | for (int32_tcxx . . $ int32_t# i = 0; i < sampleLength; i++) |
| 469 | { |
| 470 | int32_tcxx . . $ int32_t# smp32 = (int32_tcxx . . $ int32_t#)roundfcxx . . $ roundf(fbcf3211dfe1bfd1).(fAudioDataFloat[i] * fAmp); |
| 471 | ASSERTcxx . . $ `src/pt2_header.h:25:9`!(smp32 >= -128 && smp32 <= 127); // shouldn't happen according to dAmp (but just in case) |
| 472 | smpDataPtr[i] = (int8_tcxx . . $ int8_t#)smp32; |
| 473 | } |
| 474 | |
| 475 | freecxx . . $ free(5d22bdacc48458e8).(audioDataU32); |
| 476 | } |
| 477 | else if (audioFormat == WAV_FORMAT_IEEE_FLOATcxx . . $ WAV_FORMAT_IEEE_FLOAT. && bitsPerSample == 64) // 64-BIT FLOATING POINT SAMPLE |
| 478 | { |
| 479 | sampleLength >>= 3; |
| 480 | if (sampleLength > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.*4) |
| 481 | sampleLength = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.*4; |
| 482 | |
| 483 | uint32_tcxx . . $ uint32_t# *audioDataU32 = (uint32_tcxx . . $ uint32_t# *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sampleLength * (sizeof (uint32_tcxx . . $ uint32_t#) * 2)); |
| 484 | if (audioDataU32 == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 485 | { |
| 486 | statusOutOfMemorycxx . . $ statusOutOfMemory(49f6e7a06ebc5aa8).(); |
| 487 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 488 | } |
| 489 | |
| 490 | // read sample data |
| 491 | if (freadcxx . . $ fread(738fc8ac1d711e58).(audioDataU32, 8, sampleLength, f) != (size_tcxx . . $ size_t#)sampleLength) |
| 492 | { |
| 493 | freecxx . . $ free(5d22bdacc48458e8).(audioDataU32); |
| 494 | displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("I/O ERROR !"); |
| 495 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 496 | } |
| 497 | |
| 498 | double *dAudioDataDouble = (double *)audioDataU32; |
| 499 | |
| 500 | // convert from stereo to mono (if needed) |
| 501 | if (numChannels == 2) |
| 502 | { |
| 503 | sampleLength >>= 1; |
| 504 | for (int32_tcxx . . $ int32_t# i = 0; i < sampleLength; i++) // add right channel to left channel |
| 505 | dAudioDataDouble[i] = (dAudioDataDouble[(i * 2) + 0] + dAudioDataDouble[(i * 2) + 1]) * 0.5; |
| 506 | } |
| 507 | |
| 508 | // 2x downsampling |
| 509 | if (downSample) |
| 510 | { |
| 511 | downsample2xDoublecxx . . $ downsample2xDouble(b832380d3925cf6).(dAudioDataDouble, sampleLength); |
| 512 | sampleLength >>= 1; |
| 513 | } |
| 514 | |
| 515 | if (sampleLength > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.) |
| 516 | sampleLength = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.; |
| 517 | |
| 518 | double dAmp = 1.0; |
| 519 | const double dPeak = getDoublePeakcxx . . $ getDoublePeak(fe75d5ffdcd76c3).(dAudioDataDouble, sampleLength); |
| 520 | if (dPeak > 0.0) |
| 521 | dAmp = INT8_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:110:10`! / dPeak; |
| 522 | |
| 523 | turnOffVoicescxx . . $ turnOffVoices(49f6e7a06ebc5aa8).(); |
| 524 | for (int32_tcxx . . $ int32_t# i = 0; i < sampleLength; i++) |
| 525 | { |
| 526 | int32_tcxx . . $ int32_t# smp32 = (int32_tcxx . . $ int32_t#)round(dAudioDataDouble[i] * dAmp); |
| 527 | ASSERTcxx . . $ `src/pt2_header.h:25:9`!(smp32 >= -128 && smp32 <= 127); // shouldn't happen according to dAmp (but just in case) |
| 528 | smpDataPtr[i] = (int8_tcxx . . $ int8_t#)smp32; |
| 529 | } |
| 530 | |
| 531 | freecxx . . $ free(5d22bdacc48458e8).(audioDataU32); |
| 532 | } |
| 533 | |
| 534 | if (sampleLength & 1) |
| 535 | { |
| 536 | if (++sampleLength > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.) |
| 537 | sampleLength = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.; |
| 538 | else |
| 539 | smpDataPtr[sampleLength-1] = 0; |
| 540 | } |
| 541 | |
| 542 | s->lengthcxx . . $ moduleSample_t#length. = sampleLength; |
| 543 | s->fineTunecxx . . $ moduleSample_t#fineTune. = 0; |
| 544 | s->volumecxx . . $ moduleSample_t#volume. = 64; |
| 545 | s->loopStartcxx . . $ moduleSample_t#loopStart. = 0; |
| 546 | s->loopLengthcxx . . $ moduleSample_t#loopLength. = 2; |
| 547 | |
| 548 | // ---- READ "smpl" chunk ---- |
| 549 | if (smplPtr != 0 && smplLen > 52) |
| 550 | { |
| 551 | int32_tcxx . . $ int32_t# loopStart, loopEnd; |
| 552 | uint32_tcxx . . $ uint32_t# loopFlags; |
| 553 | |
| 554 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, smplPtr + 28, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); // seek to first wanted byte |
| 555 | |
| 556 | freadcxx . . $ fread(738fc8ac1d711e58).(&loopFlags, 4, 1, f); |
| 557 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, 12, SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!); |
| 558 | freadcxx . . $ fread(738fc8ac1d711e58).(&loopStart, 4, 1, f); |
| 559 | freadcxx . . $ fread(738fc8ac1d711e58).(&loopEnd, 4, 1, f); |
| 560 | loopEnd++; |
| 561 | |
| 562 | if (loopFlags) // loop enabled? |
| 563 | { |
| 564 | int32_tcxx . . $ int32_t# loopLength = loopEnd - loopStart; |
| 565 | |
| 566 | if (downSample) |
| 567 | { |
| 568 | // we already downsampled 2x, so we're half the original length |
| 569 | loopStart >>= 1; |
| 570 | loopLength >>= 1; |
| 571 | } |
| 572 | |
| 573 | loopStart &= ~1; |
| 574 | loopLength &= ~1; |
| 575 | |
| 576 | if (loopLength < 2 || loopStart+loopLength > s->lengthcxx . . $ moduleSample_t#length.) |
| 577 | { |
| 578 | loopStart = 0; |
| 579 | loopLength = 2; |
| 580 | } |
| 581 | |
| 582 | s->loopStartcxx . . $ moduleSample_t#loopStart. = loopStart; |
| 583 | s->loopLengthcxx . . $ moduleSample_t#loopLength. = loopLength; |
| 584 | } |
| 585 | } |
| 586 | // --------------------------- |
| 587 | |
| 588 | // ---- READ "xtra" chunk ---- |
| 589 | if (xtraPtr != 0 && xtraLen >= 8) |
| 590 | { |
| 591 | uint16_tcxx . . $ uint16_t# tempVol; |
| 592 | |
| 593 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, xtraPtr + 4, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); // seek to first wanted byte |
| 594 | |
| 595 | // volume (0..256) |
| 596 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, 2, SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!); |
| 597 | freadcxx . . $ fread(738fc8ac1d711e58).(&tempVol, 2, 1, f); |
| 598 | if (tempVol > 256) |
| 599 | tempVol = 256; |
| 600 | |
| 601 | tempVol >>= 2; // 0..256 -> 0..64 |
| 602 | |
| 603 | s->volumecxx . . $ moduleSample_t#volume. = (int8_tcxx . . $ int8_t#)tempVol; |
| 604 | } |
| 605 | // --------------------------- |
| 606 | |
| 607 | // ---- READ "INAM" chunk ---- |
| 608 | if (inamPtr != 0 && inamLen > 0) |
| 609 | { |
| 610 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, inamPtr, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); // seek to first wanted byte |
| 611 | |
| 612 | for (int32_tcxx . . $ int32_t# i = 0; i < 21; i++) |
| 613 | { |
| 614 | if (i < inamLen) |
| 615 | s->textcxx . . $ moduleSample_t#text.[i] = (char)fgetccxx . . $ fgetc(89bc1efcdac0ca22).(f); |
| 616 | else |
| 617 | s->textcxx . . $ moduleSample_t#text.[i] = '\0'; |
| 618 | } |
| 619 | |
| 620 | s->textcxx . . $ moduleSample_t#text.[21] = '\0'; |
| 621 | s->textcxx . . $ moduleSample_t#text.[22] = '\0'; |
| 622 | } |
| 623 | // --------------------------- |
| 624 | |
| 625 | return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 626 | } |