| 1 | /* Tnis IFF loader intentionally avoids reading the sample volume from the |
| 2 | ** IFF header as the IFF vol range can differ depending on the software used. |
| 3 | */ |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <stdint.h> |
| 8 | #include <stdbool.h> |
| 9 | #include <string.h> |
| 10 | #include <math.h> |
| 11 | #include "../pt2_header.h"cxx . . $ `<file>/src/pt2_header.h`/ |
| 12 | #include "../pt2_config.h"cxx . . $ `<file>/src/pt2_config.h`/ |
| 13 | #include "../pt2_structs.h"cxx . . $ `<file>/src/pt2_structs.h`/ |
| 14 | #include "../pt2_textout.h"cxx . . $ `<file>/src/pt2_textout.h`/ |
| 15 | #include "../pt2_visuals.h"cxx . . $ `<file>/src/pt2_visuals.h`/ |
| 16 | #include "../pt2_helpers.h"cxx . . $ `<file>/src/pt2_helpers.h`/ |
| 17 | #include "../pt2_replayer.h"cxx . . $ `<file>/src/pt2_replayer.h`/ |
| 18 | #include "../pt2_askbox.h"cxx . . $ `<file>/src/pt2_askbox.h`/ |
| 19 | #include "../pt2_downsample2x.h"cxx . . $ `<file>/src/pt2_downsample2x.h`/ |
| 20 | #include "../pt2_audio.h"cxx . . $ `<file>/src/pt2_audio.h`/ |
| 21 | |
| 22 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! loadIFFSamplecxx . . $ loadIFFSample(85a5b89df4a007d2).(FILEcxx . . $ FILE# *f, uint32_tcxx . . $ uint32_t# filesize, moduleSample_tcxx . . $ moduleSample_t# *s) |
| 23 | { |
| 24 | char tmpCharBuf[23]; |
| 25 | uint16_tcxx . . $ uint16_t# sampleRate; |
| 26 | int32_tcxx . . $ int32_t# loopStart, loopLength; |
| 27 | |
| 28 | // zero out chunk pointers and lengths |
| 29 | uint32_tcxx . . $ uint32_t# vhdrPtr = 0; uint32_tcxx . . $ uint32_t# vhdrLen = 0; |
| 30 | uint32_tcxx . . $ uint32_t# bodyPtr = 0; uint32_tcxx . . $ uint32_t# bodyLen = 0; |
| 31 | uint32_tcxx . . $ uint32_t# namePtr = 0; int32_tcxx . . $ int32_t# nameLen = 0; |
| 32 | |
| 33 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, 8, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 34 | freadcxx . . $ fread(738fc8ac1d711e58).(tmpCharBuf, 1, 4, f); |
| 35 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! is16Bit = !strncmpcxx . . $ strncmp(fd278af2f295fa2e).(tmpCharBuf, "16SV", 4); |
| 36 | |
| 37 | int32_tcxx . . $ int32_t# sampleLength = 0; |
| 38 | |
| 39 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, 12, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 40 | while (!feofcxx . . $ feof(89bc1efcdac0ca22).(f) && (uint32_tcxx . . $ uint32_t#)ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(f) < filesize-12) |
| 41 | { |
| 42 | uint32_tcxx . . $ uint32_t# blockName, blockSize; |
| 43 | |
| 44 | freadcxx . . $ fread(738fc8ac1d711e58).(&blockName, 4, 1, f); if (feofcxx . . $ feof(89bc1efcdac0ca22).(f)) break; |
| 45 | freadcxx . . $ fread(738fc8ac1d711e58).(&blockSize, 4, 1, f); if (feofcxx . . $ feof(89bc1efcdac0ca22).(f)) break; |
| 46 | |
| 47 | blockName = SWAP32cxx . . $ `src/pt2_helpers.h:12:9`!(blockName); |
| 48 | blockSize = SWAP32cxx . . $ `src/pt2_helpers.h:12:9`!(blockSize); |
| 49 | |
| 50 | switch (blockName) |
| 51 | { |
| 52 | case 0x56484452: // VHDR |
| 53 | { |
| 54 | vhdrPtr = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(f); |
| 55 | vhdrLen = blockSize; |
| 56 | } |
| 57 | break; |
| 58 | |
| 59 | case 0x4E414D45: // NAME |
| 60 | { |
| 61 | namePtr = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(f); |
| 62 | nameLen = blockSize; |
| 63 | } |
| 64 | break; |
| 65 | |
| 66 | case 0x424F4459: // BODY |
| 67 | { |
| 68 | bodyPtr = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(f); |
| 69 | bodyLen = blockSize; |
| 70 | } |
| 71 | break; |
| 72 | |
| 73 | default: break; |
| 74 | } |
| 75 | |
| 76 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, blockSize + (blockSize & 1), SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!); |
| 77 | } |
| 78 | |
| 79 | if (vhdrPtr == 0 || vhdrLen < 20 || bodyPtr == 0) |
| 80 | { |
| 81 | displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("NOT A VALID IFF !"); |
| 82 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 83 | } |
| 84 | |
| 85 | // kludge for some really strange IFFs |
| 86 | if (bodyLen == 0) |
| 87 | bodyLen = filesize - bodyPtr; |
| 88 | |
| 89 | if (bodyPtr+bodyLen > (uint32_tcxx . . $ uint32_t#)filesize) |
| 90 | bodyLen = filesize - bodyPtr; |
| 91 | |
| 92 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, vhdrPtr, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 93 | freadcxx . . $ fread(738fc8ac1d711e58).(&loopStart, 4, 1, f); loopStart = SWAP32cxx . . $ `src/pt2_helpers.h:12:9`!(loopStart); |
| 94 | freadcxx . . $ fread(738fc8ac1d711e58).(&loopLength, 4, 1, f); loopLength = SWAP32cxx . . $ `src/pt2_helpers.h:12:9`!(loopLength); |
| 95 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, 4, SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!); |
| 96 | freadcxx . . $ fread(738fc8ac1d711e58).(&sampleRate, 2, 1, f); sampleRate = SWAP16cxx . . $ `src/pt2_helpers.h:6:9`!(sampleRate); |
| 97 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, 1, SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!); |
| 98 | |
| 99 | if (fgetccxx . . $ fgetc(89bc1efcdac0ca22).(f) != 0) // sample type |
| 100 | { |
| 101 | displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("UNSUPPORTED IFF !"); |
| 102 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 103 | } |
| 104 | |
| 105 | sampleLength = bodyLen; |
| 106 | |
| 107 | if (sampleLength == 0) |
| 108 | { |
| 109 | displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("NOT A VALID IFF !"); |
| 110 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 111 | } |
| 112 | |
| 113 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! downSample = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 114 | if (sampleRate > 22050 && !configcxx . . $ config..noDownsampleOnSmpLoadcxx . . $ config_t#noDownsampleOnSmpLoad.) |
| 115 | { |
| 116 | if (askBoxcxx . . $ askBox(7371ee129cebb502).(ASKBOX_DOWNSAMPLEcxx . . $ ASKBOX_DOWNSAMPLE., "DOWNSAMPLE ?")) |
| 117 | downSample = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 118 | } |
| 119 | |
| 120 | int32_tcxx . . $ int32_t# maxSampleLength = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.; |
| 121 | if (is16Bit) |
| 122 | maxSampleLength *= 2; |
| 123 | |
| 124 | if (downSample) |
| 125 | maxSampleLength *= 2; |
| 126 | |
| 127 | if (sampleLength > maxSampleLength) |
| 128 | sampleLength = maxSampleLength; |
| 129 | |
| 130 | int8_tcxx . . $ int8_t# *sampleData = (int8_tcxx . . $ int8_t# *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sampleLength); |
| 131 | if (sampleData == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 132 | { |
| 133 | statusOutOfMemorycxx . . $ statusOutOfMemory(49f6e7a06ebc5aa8).(); |
| 134 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 135 | } |
| 136 | |
| 137 | if (is16Bit) |
| 138 | { |
| 139 | sampleLength >>= 1; |
| 140 | loopStart >>= 1; |
| 141 | loopLength >>= 1; |
| 142 | } |
| 143 | |
| 144 | if (downSample) |
| 145 | { |
| 146 | loopStart >>= 1; |
| 147 | loopLength >>= 1; |
| 148 | } |
| 149 | |
| 150 | turnOffVoicescxx . . $ turnOffVoices(49f6e7a06ebc5aa8).(); |
| 151 | |
| 152 | int8_tcxx . . $ int8_t# *smpDataPtr = &songcxx . . $ song.->sampleDatacxx . . $ module_t#sampleData.[s->offsetcxx . . $ moduleSample_t#offset.]; |
| 153 | |
| 154 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, bodyPtr, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 155 | if (is16Bit) // FT2-specific 16SV format (little-endian samples) |
| 156 | { |
| 157 | freadcxx . . $ fread(738fc8ac1d711e58).(sampleData, 1, sampleLength << 1, f); |
| 158 | int16_tcxx . . $ int16_t# *ptr16 = (int16_tcxx . . $ int16_t# *)sampleData; |
| 159 | |
| 160 | // 2x downsampling |
| 161 | if (downSample) |
| 162 | { |
| 163 | downsample2x16Bitcxx . . $ downsample2x16Bit(6f0a599713eb5608).(ptr16, sampleLength); |
| 164 | sampleLength >>= 1; |
| 165 | } |
| 166 | |
| 167 | if (sampleLength > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.) |
| 168 | sampleLength = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.; |
| 169 | |
| 170 | double dAmp = 1.0; |
| 171 | if (downSample) // we already normalized |
| 172 | { |
| 173 | dAmp = INT8_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:110:10`! / (double)INT16_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:111:10`!; |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | const double dPeak = get16BitPeakcxx . . $ get16BitPeak(5bbf7f3ea503fca5).(ptr16, sampleLength); |
| 178 | if (dPeak > 0.0) |
| 179 | dAmp = INT8_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:110:10`! / dPeak; |
| 180 | } |
| 181 | |
| 182 | for (int32_tcxx . . $ int32_t# i = 0; i < sampleLength; i++) |
| 183 | { |
| 184 | int32_tcxx . . $ int32_t# smp32 = (int32_tcxx . . $ int32_t#)round(ptr16[i] * dAmp); |
| 185 | ASSERTcxx . . $ `src/pt2_header.h:25:9`!(smp32 >= -128 && smp32 <= 127); // shouldn't happen according to dAmp (but just in case) |
| 186 | smpDataPtr[i] = (int8_tcxx . . $ int8_t#)smp32; |
| 187 | } |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | freadcxx . . $ fread(738fc8ac1d711e58).(sampleData, 1, sampleLength, f); |
| 192 | |
| 193 | // 2x downsampling |
| 194 | if (downSample) |
| 195 | { |
| 196 | downsample2x8Bitcxx . . $ downsample2x8Bit(9b8781d28ee69655).(sampleData, sampleLength); |
| 197 | sampleLength >>= 1; |
| 198 | } |
| 199 | |
| 200 | if (sampleLength > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.) |
| 201 | sampleLength = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.; |
| 202 | |
| 203 | memcpycxx . . $ memcpy(cc817cb175f282d).(smpDataPtr, sampleData, sampleLength); |
| 204 | } |
| 205 | |
| 206 | freecxx . . $ free(5d22bdacc48458e8).(sampleData); |
| 207 | |
| 208 | if (sampleLength & 1) |
| 209 | { |
| 210 | if (++sampleLength > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.) |
| 211 | sampleLength = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.; |
| 212 | else |
| 213 | smpDataPtr[sampleLength-1] = 0; |
| 214 | } |
| 215 | |
| 216 | loopStart &= ~1; |
| 217 | loopLength &= ~1; |
| 218 | |
| 219 | if (loopLength < 2 || loopStart+loopLength > sampleLength) |
| 220 | { |
| 221 | loopStart = 0; |
| 222 | loopLength = 2; |
| 223 | } |
| 224 | |
| 225 | // set sample attributes |
| 226 | s->volumecxx . . $ moduleSample_t#volume. = 64; |
| 227 | s->fineTunecxx . . $ moduleSample_t#fineTune. = 0; |
| 228 | s->lengthcxx . . $ moduleSample_t#length. = sampleLength; |
| 229 | s->loopStartcxx . . $ moduleSample_t#loopStart. = loopStart; |
| 230 | s->loopLengthcxx . . $ moduleSample_t#loopLength. = loopLength; |
| 231 | |
| 232 | if (namePtr != 0 && nameLen > 0) |
| 233 | { |
| 234 | // read name |
| 235 | |
| 236 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, namePtr, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 237 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(tmpCharBuf, 0, sizeof (tmpCharBuf)); |
| 238 | |
| 239 | if (nameLen > 21) |
| 240 | { |
| 241 | freadcxx . . $ fread(738fc8ac1d711e58).(tmpCharBuf, 1, 21, f); |
| 242 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, nameLen - 21, SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!); |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | freadcxx . . $ fread(738fc8ac1d711e58).(tmpCharBuf, 1, nameLen, f); |
| 247 | } |
| 248 | |
| 249 | // copy over sample name |
| 250 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(s->textcxx . . $ moduleSample_t#text., '\0', sizeof (s->textcxx . . $ moduleSample_t#text.)); |
| 251 | |
| 252 | nameLen = (uint32_tcxx . . $ uint32_t#)strlencxx . . $ strlen(751346f8406fd082).(tmpCharBuf); |
| 253 | if (nameLen > 21) |
| 254 | nameLen = 21; |
| 255 | |
| 256 | memcpycxx . . $ memcpy(cc817cb175f282d).(s->textcxx . . $ moduleSample_t#text., tmpCharBuf, nameLen); |
| 257 | } |
| 258 | |
| 259 | return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 260 | } |