Debian Codegraph @

src/smploaders/pt2_load_aiff.c

raw symbols SCIP
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <stdbool.h>
5 #include <string.h>
6 #include <math.h>
7 #include "../pt2_header.h"
8 #include "../pt2_config.h"
9 #include "../pt2_structs.h"
10 #include "../pt2_textout.h"
11 #include "../pt2_visuals.h"
12 #include "../pt2_helpers.h"
13 #include "../pt2_replayer.h"
14 #include "../pt2_askbox.h"
15 #include "../pt2_downsample2x.h"
16 #include "../pt2_audio.h"
17
18 static uint32_t getAIFFSampleRate(uint8_t *in)
19 {
20 /* 80-bit IEEE-754 to unsigned 32-bit integer (rounded).
21 ** Sign bit is ignored.
22 */
23
24 #define EXP_BIAS 16383
25
26 const uint16_t exp15 = ((in[0] & 0x7F) << 8) | in[1];
27 const uint64_t mantissaBits = *(uint64_t *)&in[2];
28 const uint64_t mantissa63 = SWAP64(mantissaBits) & INT64_MAX;
29
30 double dExp = exp15 - EXP_BIAS;
31 double dMantissa = mantissa63 / ((double)INT64_MAX+1.0);
32
33 double dResult = (1.0 + dMantissa) * exp2(dExp);
34 return (uint32_t)round(dResult);
35 }
36
37 bool loadAIFFSample(FILE *f, uint32_t filesize, moduleSample_t *s)
38 {
39 uint8_t sampleRateBytes[10];
40 uint16_t bitDepth, numChannels;
41 uint32_t offset, sampleRate;
42
43 // zero out chunk pointers and lengths
44 uint32_t commPtr = 0; uint32_t commLen = 0;
45 uint32_t ssndPtr = 0; uint32_t ssndLen = 0;
46
47 bool unsigned8bit = false;
48
49 fseek(f, 12, SEEK_SET);
50 while (!feof(f) && (uint32_t)ftell(f) < filesize-12)
51 {
52 uint32_t blockName, blockSize;
53
54 fread(&blockName, 4, 1, f); if (feof(f)) break;
55 fread(&blockSize, 4, 1, f); if (feof(f)) break;
56
57 blockName = SWAP32(blockName);
58 blockSize = SWAP32(blockSize);
59
60 switch (blockName)
61 {
62 case 0x434F4D4D: // "COMM"
63 {
64 commPtr = ftell(f);
65 commLen = blockSize;
66 }
67 break;
68
69 case 0x53534E44: // "SSND"
70 {
71 ssndPtr = ftell(f);
72 ssndLen = blockSize;
73 }
74 break;
75
76 default: break;
77 }
78
79 fseek(f, blockSize + (blockSize & 1), SEEK_CUR);
80 }
81
82 if (commPtr == 0 || commLen < 18 || ssndPtr == 0)
83 {
84 displayErrorMsg("NOT A VALID AIFF!");
85 return false;
86 }
87
88 // kludge for some really strange AIFFs
89 if (ssndLen == 0)
90 ssndLen = filesize - ssndPtr;
91
92 if (ssndPtr+ssndLen > (uint32_t)filesize)
93 ssndLen = filesize - ssndPtr;
94
95 fseek(f, commPtr, SEEK_SET);
96 fread(&numChannels, 2, 1, f); numChannels = SWAP16(numChannels);
97 fseek(f, 4, SEEK_CUR);
98 fread(&bitDepth, 2, 1, f); bitDepth = SWAP16(bitDepth);
99 fread(sampleRateBytes, 1, 10, f);
100
101 fseek(f, 4 + 2 + 1, SEEK_CUR);
102
103 if (numChannels != 1 && numChannels != 2) // sample type
104 {
105 displayErrorMsg("UNSUPPORTED AIFF!");
106 return false;
107 }
108
109 if (bitDepth != 8 && bitDepth != 16 && bitDepth != 24 && bitDepth != 32)
110 {
111 displayErrorMsg("UNSUPPORTED AIFF!");
112 return false;
113 }
114
115 // read compression type (if present)
116 if (commLen > 18)
117 {
118 char compType[4];
119 fread(&compType, 1, 4, f);
120 if (memcmp(compType, "NONE", 4))
121 {
122 displayErrorMsg("UNSUPPORTED AIFF!");
123 return false;
124 }
125 }
126
127 sampleRate = getAIFFSampleRate(sampleRateBytes);
128
129 // sample data chunk
130
131 fseek(f, ssndPtr, SEEK_SET);
132
133 fread(&offset, 4, 1, f);
134 if (offset > 0)
135 {
136 displayErrorMsg("UNSUPPORTED AIFF!");
137 return false;
138 }
139
140 fseek(f, 4, SEEK_CUR);
141
142 ssndLen -= 8; // don't include offset and blockSize datas
143
144 int32_t sampleLength = ssndLen;
145 if (sampleLength == 0)
146 {
147 displayErrorMsg("NOT A VALID AIFF!");
148 return false;
149 }
150
151 bool downSample = false;
152 if (sampleRate > 22050 && !config.noDownsampleOnSmpLoad)
153 {
154 if (askBox(ASKBOX_DOWNSAMPLE, "DOWNSAMPLE ?"))
155 downSample = true;
156 }
157
158 int8_t *smpDataPtr = &song->sampleData[s->offset];
159
160 if (bitDepth == 8) // 8-BIT INTEGER SAMPLE
161 {
162 if (sampleLength > config.maxSampleLength*4)
163 sampleLength = config.maxSampleLength*4;
164
165 int8_t *audioDataS8 = (int8_t *)malloc(sampleLength * sizeof (int8_t));
166 if (audioDataS8 == NULL)
167 {
168 statusOutOfMemory();
169 return false;
170 }
171
172 // read sample data
173 if (fread(audioDataS8, 1, sampleLength, f) != (size_t)sampleLength)
174 {
175 free(audioDataS8);
176 displayErrorMsg("I/O ERROR !");
177 return false;
178 }
179
180 if (unsigned8bit)
181 {
182 for (int32_t i = 0; i < sampleLength; i++)
183 audioDataS8[i] ^= 0x80;
184 }
185
186 // convert from stereo to mono (if needed)
187 if (numChannels == 2)
188 {
189 sampleLength >>= 1;
190 for (int32_t i = 0; i < sampleLength; i++) // add right channel to left channel
191 audioDataS8[i] = (audioDataS8[(i * 2) + 0] + audioDataS8[(i * 2) + 1]) >> 1;
192 }
193
194 // 2x downsampling
195 if (downSample)
196 {
197 downsample2x8Bit(audioDataS8, sampleLength);
198 sampleLength >>= 1;
199 }
200
201 if (sampleLength > config.maxSampleLength)
202 sampleLength = config.maxSampleLength;
203
204 turnOffVoices();
205 for (int32_t i = 0; i < sampleLength; i++)
206 smpDataPtr[i] = audioDataS8[i];
207
208 free(audioDataS8);
209 }
210 else if (bitDepth == 16) // 16-BIT INTEGER SAMPLE
211 {
212 sampleLength >>= 1;
213 if (sampleLength > config.maxSampleLength*4)
214 sampleLength = config.maxSampleLength*4;
215
216 int16_t *audioDataS16 = (int16_t *)malloc(sampleLength * sizeof (int16_t));
217 if (audioDataS16 == NULL)
218 {
219 statusOutOfMemory();
220 return false;
221 }
222
223 // read sample data
224 if (fread(audioDataS16, 2, sampleLength, f) != (size_t)sampleLength)
225 {
226 free(audioDataS16);
227 displayErrorMsg("I/O ERROR !");
228 return false;
229 }
230
231 // fix endianness
232 for (int32_t i = 0; i < sampleLength; i++)
233 audioDataS16[i] = SWAP16(audioDataS16[i]);
234
235 // convert from stereo to mono (if needed)
236 if (numChannels == 2)
237 {
238 sampleLength >>= 1;
239 for (int32_t i = 0; i < sampleLength; i++) // add right channel to left channel
240 audioDataS16[i] = (audioDataS16[(i << 1) + 0] + audioDataS16[(i << 1) + 1]) >> 1;
241 }
242
243 // 2x downsampling
244 if (downSample)
245 {
246 downsample2x16Bit(audioDataS16, sampleLength);
247 sampleLength >>= 1;
248 }
249
250 if (sampleLength > config.maxSampleLength)
251 sampleLength = config.maxSampleLength;
252
253 double dAmp = 1.0;
254 if (downSample) // we already normalized
255 {
256 dAmp = INT8_MAX / (double)INT16_MAX;
257 }
258 else
259 {
260 const double dPeak = get16BitPeak(audioDataS16, sampleLength);
261 if (dPeak > 0.0)
262 dAmp = INT8_MAX / dPeak;
263 }
264
265 turnOffVoices();
266 for (int32_t i = 0; i < sampleLength; i++)
267 {
268 int32_t smp32 = (int32_t)round(audioDataS16[i] * dAmp);
269 ASSERT(smp32 >= -128 && smp32 <= 127); // shouldn't happen according to dAmp (but just in case)
270 smpDataPtr[i] = (int8_t)smp32;
271 }
272
273 free(audioDataS16);
274 }
275 else if (bitDepth == 24) // 24-BIT INTEGER SAMPLE
276 {
277 sampleLength /= 3;
278 if (sampleLength > config.maxSampleLength*4)
279 sampleLength = config.maxSampleLength*4;
280
281 int32_t *audioDataS32 = (int32_t *)malloc(sampleLength * sizeof (int32_t));
282 if (audioDataS32 == NULL)
283 {
284 statusOutOfMemory();
285 return false;
286 }
287
288 // read sample data
289 if (fread(&audioDataS32[sampleLength >> 2], 3, sampleLength, f) != (size_t)sampleLength)
290 {
291 free(audioDataS32);
292 displayErrorMsg("I/O ERROR !");
293 return false;
294 }
295
296 // convert to 32-bit
297 uint8_t *audioDataU8 = (uint8_t *)audioDataS32 + sampleLength;
298 for (int32_t i = 0; i < sampleLength; i++)
299 {
300 audioDataS32[i] = (audioDataU8[0] << 24) | (audioDataU8[1] << 16) | (audioDataU8[2] << 8);
301 audioDataU8 += 3;
302 }
303
304 // convert from stereo to mono (if needed)
305 if (numChannels == 2)
306 {
307 sampleLength >>= 1;
308 for (int32_t i = 0; i < sampleLength; i++) // add right channel to left channel
309 {
310 int64_t smp = ((int64_t)audioDataS32[(i << 1) + 0] + audioDataS32[(i << 1) + 1]) >> 1;
311 audioDataS32[i] = (int32_t)smp;
312 }
313 }
314
315 // 2x downsampling
316 if (downSample)
317 {
318 downsample2x32Bit(audioDataS32, sampleLength);
319 sampleLength >>= 1;
320 }
321
322 if (sampleLength > config.maxSampleLength)
323 sampleLength = config.maxSampleLength;
324
325 double dAmp = 1.0;
326 if (downSample) // we already normalized
327 {
328 dAmp = INT8_MAX / (double)INT32_MAX;
329 }
330 else
331 {
332 const double dPeak = get32BitPeak(audioDataS32, sampleLength);
333 if (dPeak > 0.0)
334 dAmp = INT8_MAX / dPeak;
335 }
336
337 turnOffVoices();
338 for (int32_t i = 0; i < sampleLength; i++)
339 {
340 int32_t smp32 = (int32_t)round(audioDataS32[i] * dAmp);
341 smpDataPtr[i] = (int8_t)smp32;
342 }
343
344 free(audioDataS32);
345 }
346 else if (bitDepth == 32) // 32-BIT INTEGER SAMPLE
347 {
348 sampleLength >>= 2;
349 if (sampleLength > config.maxSampleLength*4)
350 sampleLength = config.maxSampleLength*4;
351
352 int32_t *audioDataS32 = (int32_t *)malloc(sampleLength * sizeof (int32_t));
353 if (audioDataS32 == NULL)
354 {
355 statusOutOfMemory();
356 return false;
357 }
358
359 // read sample data
360 if (fread(audioDataS32, 4, sampleLength, f) != (size_t)sampleLength)
361 {
362 free(audioDataS32);
363 displayErrorMsg("I/O ERROR !");
364 return false;
365 }
366
367 // fix endianness
368 for (int32_t i = 0; i < sampleLength; i++)
369 audioDataS32[i] = SWAP32(audioDataS32[i]);
370
371 // convert from stereo to mono (if needed)
372 if (numChannels == 2)
373 {
374 sampleLength >>= 1;
375 for (int32_t i = 0; i < sampleLength; i++) // add right channel to left channel
376 {
377 int64_t smp = ((int64_t)audioDataS32[(i << 1) + 0] + audioDataS32[(i << 1) + 1]) >> 1;
378 audioDataS32[i] = (int32_t)smp;
379 }
380 }
381
382 // 2x downsampling
383 if (downSample)
384 {
385 downsample2x32Bit(audioDataS32, sampleLength);
386 sampleLength >>= 1;
387 }
388
389 if (sampleLength > config.maxSampleLength)
390 sampleLength = config.maxSampleLength;
391
392 double dAmp = 1.0;
393 if (downSample) // we already normalized
394 {
395 dAmp = INT8_MAX / (double)INT32_MAX;
396 }
397 else
398 {
399 const double dPeak = get32BitPeak(audioDataS32, sampleLength);
400 if (dPeak > 0.0)
401 dAmp = INT8_MAX / dPeak;
402 }
403
404 turnOffVoices();
405 for (int32_t i = 0; i < sampleLength; i++)
406 {
407 int32_t smp32 = (int32_t)round(audioDataS32[i] * dAmp);
408 smpDataPtr[i] = (int8_t)smp32;
409 }
410
411 free(audioDataS32);
412 }
413
414 if (sampleLength & 1)
415 {
416 if (++sampleLength > config.maxSampleLength)
417 sampleLength = config.maxSampleLength;
418 else
419 smpDataPtr[sampleLength-1] = 0;
420 }
421
422 s->length = sampleLength;
423 s->fineTune = 0;
424 s->volume = 64;
425 s->loopStart = 0;
426 s->loopLength = 2;
427
428 return true;
429 }