Debian Codegraph @

src/smploaders/pt2_load_wav.c

raw symbols SCIP
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <stdbool.h>
5 #include <math.h>
6 #include "../pt2_header.h"
7 #include "../pt2_config.h"
8 #include "../pt2_structs.h"
9 #include "../pt2_textout.h"
10 #include "../pt2_visuals.h"
11 #include "../pt2_helpers.h"
12 #include "../pt2_replayer.h"
13 #include "../pt2_askbox.h"
14 #include "../pt2_downsample2x.h"
15 #include "../pt2_audio.h"
16
17 enum
18 {
19 WAV_FORMAT_PCM = 1,
20 WAV_FORMAT_IEEE_FLOAT = 3,
21 WAV_FORMAT_EXTENSIBLE = 65534
22 };
23
24 bool loadWAVSample(FILE *f, uint32_t filesize, moduleSample_t *s)
25 {
26 uint16_t audioFormat, numChannels, bitsPerSample;
27 uint32_t sampleRate;
28
29 // zero out chunk pointers and lengths
30 uint32_t fmtPtr = 0; uint32_t fmtLen = 0;
31 uint32_t dataPtr = 0; uint32_t dataLen = 0;
32 uint32_t inamPtr = 0; int32_t inamLen = 0;
33 uint32_t xtraPtr = 0; uint32_t xtraLen = 0;
34 uint32_t smplPtr = 0; uint32_t smplLen = 0;
35
36 // look for wanted chunks and set up pointers + lengths
37 fseek(f, 12, SEEK_SET);
38
39 uint32_t bytesRead = 0;
40 while (!feof(f) && bytesRead < (uint32_t)filesize-12)
41 {
42 uint32_t chunkID, chunkSize;
43
44 fread(&chunkID, 4, 1, f); if (feof(f)) break;
45 fread(&chunkSize, 4, 1, f); if (feof(f)) break;
46
47 uint32_t endOfChunk = (ftell(f) + chunkSize) + (chunkSize & 1);
48 switch (chunkID)
49 {
50 case 0x20746D66: // "fmt "
51 {
52 fmtPtr = ftell(f);
53 fmtLen = chunkSize;
54 }
55 break;
56
57 case 0x61746164: // "data"
58 {
59 dataPtr = ftell(f);
60 dataLen = chunkSize;
61 }
62 break;
63
64 case 0x5453494C: // "LIST"
65 {
66 if (chunkSize >= 4)
67 {
68 fread(&chunkID, 4, 1, f);
69 if (chunkID == 0x4F464E49) // "INFO"
70 {
71 bytesRead = 0;
72 while (!feof(f) && bytesRead < chunkSize)
73 {
74 fread(&chunkID, 4, 1, f);
75 fread(&chunkSize, 4, 1, f);
76
77 switch (chunkID)
78 {
79 case 0x4D414E49: // "INAM"
80 {
81 inamPtr = ftell(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 = ftell(f);
99 xtraLen = chunkSize;
100 }
101 break;
102
103 case 0x6C706D73: // "smpl"
104 {
105 smplPtr = ftell(f);
106 smplLen = chunkSize;
107 }
108 break;
109
110 default: break;
111 }
112
113 bytesRead += chunkSize + (chunkSize & 1);
114 fseek(f, endOfChunk, SEEK_SET);
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 displayErrorMsg("NOT A WAV !");
121 return false;
122 }
123
124 // ---- READ "fmt " CHUNK ----
125 fseek(f, fmtPtr, SEEK_SET);
126 fread(&audioFormat, 2, 1, f);
127 fread(&numChannels, 2, 1, f);
128 fread(&sampleRate, 4, 1, f);
129 fseek(f, 6, SEEK_CUR);
130 fread(&bitsPerSample, 2, 1, f);
131
132 int32_t sampleLength = dataLen;
133
134 if (audioFormat == WAV_FORMAT_EXTENSIBLE)
135 {
136 fseek(f, 8, SEEK_CUR);
137 fread(&audioFormat, 2, 1, f);
138 }
139 // ---------------------------
140
141 if (sampleRate == 0 || sampleLength == 0 || sampleLength >= (int32_t)filesize*(bitsPerSample/8))
142 {
143 displayErrorMsg("WAV CORRUPT !");
144 return false;
145 }
146
147 if (audioFormat != WAV_FORMAT_PCM && audioFormat != WAV_FORMAT_IEEE_FLOAT)
148 {
149 displayErrorMsg("WAV UNSUPPORTED !");
150 return false;
151 }
152
153 if (numChannels == 0 || numChannels > 2)
154 {
155 displayErrorMsg("WAV UNSUPPORTED !");
156 return false;
157 }
158
159 if (audioFormat == WAV_FORMAT_IEEE_FLOAT && bitsPerSample != 32 && bitsPerSample != 64)
160 {
161 displayErrorMsg("WAV UNSUPPORTED !");
162 return false;
163 }
164
165 if (bitsPerSample != 8 && bitsPerSample != 16 && bitsPerSample != 24 && bitsPerSample != 32 && bitsPerSample != 64)
166 {
167 displayErrorMsg("WAV UNSUPPORTED !");
168 return false;
169 }
170
171 bool downSample = false;
172 if (sampleRate > 22050 && !config.noDownsampleOnSmpLoad)
173 {
174 if (askBox(ASKBOX_DOWNSAMPLE, "DOWNSAMPLE ?"))
175 downSample = true;
176 }
177
178 // ---- READ SAMPLE DATA ----
179 fseek(f, dataPtr, SEEK_SET);
180
181 int8_t *smpDataPtr = &song->sampleData[s->offset];
182
183 if (bitsPerSample == 8) // 8-BIT INTEGER SAMPLE
184 {
185 if (sampleLength > config.maxSampleLength*4)
186 sampleLength = config.maxSampleLength*4;
187
188 uint8_t *audioDataU8 = (uint8_t *)malloc(sampleLength * sizeof (uint8_t));
189 if (audioDataU8 == NULL)
190 {
191 statusOutOfMemory();
192 return false;
193 }
194
195 // read sample data
196 if (fread(audioDataU8, 1, sampleLength, f) != (size_t)sampleLength)
197 {
198 free(audioDataU8);
199 displayErrorMsg("I/O ERROR !");
200 return false;
201 }
202
203 // convert from stereo to mono (if needed)
204 if (numChannels == 2)
205 {
206 sampleLength >>= 1;
207 for (int32_t i = 0; i < sampleLength; i++) // add right channel to left channel
208 {
209 int32_t smp32 = (audioDataU8[(i << 1) + 0] - 128) + (audioDataU8[(i << 1) + 1] - 128);
210 smp32 = 128 + (smp32 >> 1);
211 audioDataU8[i] = (uint8_t)smp32;
212 }
213 }
214
215 // 2x downsampling
216 if (downSample)
217 {
218 downsample2x8BitU(audioDataU8, sampleLength);
219 sampleLength >>= 1;
220 }
221
222 if (sampleLength > config.maxSampleLength)
223 sampleLength = config.maxSampleLength;
224
225 turnOffVoices();
226 for (int32_t i = 0; i < sampleLength; i++)
227 smpDataPtr[i] = audioDataU8[i] - 128;
228
229 free(audioDataU8);
230 }
231 else if (bitsPerSample == 16) // 16-BIT INTEGER SAMPLE
232 {
233 sampleLength >>= 1;
234 if (sampleLength > config.maxSampleLength*4)
235 sampleLength = config.maxSampleLength*4;
236
237 int16_t *audioDataS16 = (int16_t *)malloc(sampleLength * sizeof (int16_t));
238 if (audioDataS16 == NULL)
239 {
240 statusOutOfMemory();
241 return false;
242 }
243
244 // read sample data
245 if (fread(audioDataS16, 2, sampleLength, f) != (size_t)sampleLength)
246 {
247 free(audioDataS16);
248 displayErrorMsg("I/O ERROR !");
249 return false;
250 }
251
252 // convert from stereo to mono (if needed)
253 if (numChannels == 2)
254 {
255 sampleLength >>= 1;
256 for (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 downsample2x16Bit(audioDataS16, sampleLength);
264 sampleLength >>= 1;
265 }
266
267 if (sampleLength > config.maxSampleLength)
268 sampleLength = config.maxSampleLength;
269
270 double dAmp = 1.0;
271 if (downSample) // we already normalized
272 {
273 dAmp = INT8_MAX / (double)INT16_MAX;
274 }
275 else
276 {
277 const double dPeak = get16BitPeak(audioDataS16, sampleLength);
278 if (dPeak > 0.0)
279 dAmp = INT8_MAX / dPeak;
280 }
281
282 turnOffVoices();
283 for (int32_t i = 0; i < sampleLength; i++)
284 {
285 int32_t smp32 = (int32_t)round(audioDataS16[i] * dAmp);
286 ASSERT(smp32 >= -128 && smp32 <= 127); // shouldn't happen according to dAmp (but just in case)
287 smpDataPtr[i] = (int8_t)smp32;
288 }
289
290 free(audioDataS16);
291 }
292 else if (bitsPerSample == 24) // 24-BIT INTEGER SAMPLE
293 {
294 sampleLength /= 3;
295 if (sampleLength > config.maxSampleLength*4)
296 sampleLength = config.maxSampleLength*4;
297
298 int32_t *audioDataS32 = (int32_t *)malloc(sampleLength * sizeof (int32_t));
299 if (audioDataS32 == NULL)
300 {
301 statusOutOfMemory();
302 return false;
303 }
304
305 // read sample data
306 uint8_t *audioDataU8 = (uint8_t *)audioDataS32;
307 for (int32_t i = 0; i < sampleLength; i++)
308 {
309 audioDataU8[0] = 0;
310 fread(&audioDataU8[1], 3, 1, f);
311 audioDataU8 += sizeof (int32_t);
312 }
313
314 // convert from stereo to mono (if needed)
315 if (numChannels == 2)
316 {
317 sampleLength >>= 1;
318 for (int32_t i = 0; i < sampleLength; i++) // add right channel to left channel
319 {
320 int64_t smp = ((int64_t)audioDataS32[(i << 1) + 0] + audioDataS32[(i << 1) + 1]) >> 1;
321 audioDataS32[i] = (int32_t)smp;
322 }
323 }
324
325 // 2x downsampling
326 if (downSample)
327 {
328 downsample2x32Bit(audioDataS32, sampleLength);
329 sampleLength >>= 1;
330 }
331
332 if (sampleLength > config.maxSampleLength)
333 sampleLength = config.maxSampleLength;
334
335 double dAmp = 1.0;
336 if (downSample) // we already normalized
337 {
338 dAmp = INT8_MAX / (double)INT32_MAX;
339 }
340 else
341 {
342 const double dPeak = get32BitPeak(audioDataS32, sampleLength);
343 if (dPeak > 0.0)
344 dAmp = INT8_MAX / dPeak;
345 }
346
347 turnOffVoices();
348 for (int32_t i = 0; i < sampleLength; i++)
349 {
350 int32_t smp32 = (int32_t)round(audioDataS32[i] * dAmp);
351 ASSERT(smp32 >= -128 && smp32 <= 127); // shouldn't happen according to dAmp (but just in case)
352 smpDataPtr[i] = (int8_t)smp32;
353 }
354
355 free(audioDataS32);
356 }
357 else if (audioFormat == WAV_FORMAT_PCM && bitsPerSample == 32) // 32-BIT INTEGER SAMPLE
358 {
359 sampleLength >>= 2;
360 if (sampleLength > config.maxSampleLength*4)
361 sampleLength = config.maxSampleLength*4;
362
363 int32_t *audioDataS32 = (int32_t *)malloc(sampleLength * sizeof (int32_t));
364 if (audioDataS32 == NULL)
365 {
366 statusOutOfMemory();
367 return false;
368 }
369
370 // read sample data
371 if (fread(audioDataS32, 4, sampleLength, f) != (size_t)sampleLength)
372 {
373 free(audioDataS32);
374 displayErrorMsg("I/O ERROR !");
375 return false;
376 }
377
378 // convert from stereo to mono (if needed)
379 if (numChannels == 2)
380 {
381 sampleLength >>= 1;
382 for (int32_t i = 0; i < sampleLength; i++) // add right channel to left channel
383 {
384 int64_t smp = ((int64_t)audioDataS32[(i << 1) + 0] + audioDataS32[(i << 1) + 1]) >> 1;
385 audioDataS32[i] = (int32_t)smp;
386 }
387 }
388
389 // 2x downsampling
390 if (downSample)
391 {
392 downsample2x32Bit(audioDataS32, sampleLength);
393 sampleLength >>= 1;
394 }
395
396 if (sampleLength > config.maxSampleLength)
397 sampleLength = config.maxSampleLength;
398
399 double dAmp = 1.0;
400 if (downSample) // we already normalized
401 {
402 dAmp = INT8_MAX / (double)INT32_MAX;
403 }
404 else
405 {
406 const double dPeak = get32BitPeak(audioDataS32, sampleLength);
407 if (dPeak > 0.0)
408 dAmp = INT8_MAX / dPeak;
409 }
410
411 turnOffVoices();
412 for (int32_t i = 0; i < sampleLength; i++)
413 {
414 int32_t smp32 = (int32_t)round(audioDataS32[i] * dAmp);
415 ASSERT(smp32 >= -128 && smp32 <= 127); // shouldn't happen according to dAmp (but just in case)
416 smpDataPtr[i] = (int8_t)smp32;
417 }
418
419 free(audioDataS32);
420 }
421 else if (audioFormat == WAV_FORMAT_IEEE_FLOAT && bitsPerSample == 32) // 32-BIT FLOATING POINT SAMPLE
422 {
423 sampleLength >>= 2;
424 if (sampleLength > config.maxSampleLength*4)
425 sampleLength = config.maxSampleLength*4;
426
427 uint32_t *audioDataU32 = (uint32_t *)malloc(sampleLength * sizeof (uint32_t));
428 if (audioDataU32 == NULL)
429 {
430 statusOutOfMemory();
431 return false;
432 }
433
434 // read sample data
435 if (fread(audioDataU32, 4, sampleLength, f) != (size_t)sampleLength)
436 {
437 free(audioDataU32);
438 displayErrorMsg("I/O ERROR !");
439 return false;
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_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 downsample2xFloat(fAudioDataFloat, sampleLength);
456 sampleLength >>= 1;
457 }
458
459 if (sampleLength > config.maxSampleLength)
460 sampleLength = config.maxSampleLength;
461
462 float fAmp = 1.0f;
463 const float fPeak = getFloatPeak(fAudioDataFloat, sampleLength);
464 if (fPeak > 0.0f)
465 fAmp = INT8_MAX / fPeak;
466
467 turnOffVoices();
468 for (int32_t i = 0; i < sampleLength; i++)
469 {
470 int32_t smp32 = (int32_t)roundf(fAudioDataFloat[i] * fAmp);
471 ASSERT(smp32 >= -128 && smp32 <= 127); // shouldn't happen according to dAmp (but just in case)
472 smpDataPtr[i] = (int8_t)smp32;
473 }
474
475 free(audioDataU32);
476 }
477 else if (audioFormat == WAV_FORMAT_IEEE_FLOAT && bitsPerSample == 64) // 64-BIT FLOATING POINT SAMPLE
478 {
479 sampleLength >>= 3;
480 if (sampleLength > config.maxSampleLength*4)
481 sampleLength = config.maxSampleLength*4;
482
483 uint32_t *audioDataU32 = (uint32_t *)malloc(sampleLength * (sizeof (uint32_t) * 2));
484 if (audioDataU32 == NULL)
485 {
486 statusOutOfMemory();
487 return false;
488 }
489
490 // read sample data
491 if (fread(audioDataU32, 8, sampleLength, f) != (size_t)sampleLength)
492 {
493 free(audioDataU32);
494 displayErrorMsg("I/O ERROR !");
495 return false;
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_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 downsample2xDouble(dAudioDataDouble, sampleLength);
512 sampleLength >>= 1;
513 }
514
515 if (sampleLength > config.maxSampleLength)
516 sampleLength = config.maxSampleLength;
517
518 double dAmp = 1.0;
519 const double dPeak = getDoublePeak(dAudioDataDouble, sampleLength);
520 if (dPeak > 0.0)
521 dAmp = INT8_MAX / dPeak;
522
523 turnOffVoices();
524 for (int32_t i = 0; i < sampleLength; i++)
525 {
526 int32_t smp32 = (int32_t)round(dAudioDataDouble[i] * dAmp);
527 ASSERT(smp32 >= -128 && smp32 <= 127); // shouldn't happen according to dAmp (but just in case)
528 smpDataPtr[i] = (int8_t)smp32;
529 }
530
531 free(audioDataU32);
532 }
533
534 if (sampleLength & 1)
535 {
536 if (++sampleLength > config.maxSampleLength)
537 sampleLength = config.maxSampleLength;
538 else
539 smpDataPtr[sampleLength-1] = 0;
540 }
541
542 s->length = sampleLength;
543 s->fineTune = 0;
544 s->volume = 64;
545 s->loopStart = 0;
546 s->loopLength = 2;
547
548 // ---- READ "smpl" chunk ----
549 if (smplPtr != 0 && smplLen > 52)
550 {
551 int32_t loopStart, loopEnd;
552 uint32_t loopFlags;
553
554 fseek(f, smplPtr + 28, SEEK_SET); // seek to first wanted byte
555
556 fread(&loopFlags, 4, 1, f);
557 fseek(f, 12, SEEK_CUR);
558 fread(&loopStart, 4, 1, f);
559 fread(&loopEnd, 4, 1, f);
560 loopEnd++;
561
562 if (loopFlags) // loop enabled?
563 {
564 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->length)
577 {
578 loopStart = 0;
579 loopLength = 2;
580 }
581
582 s->loopStart = loopStart;
583 s->loopLength = loopLength;
584 }
585 }
586 // ---------------------------
587
588 // ---- READ "xtra" chunk ----
589 if (xtraPtr != 0 && xtraLen >= 8)
590 {
591 uint16_t tempVol;
592
593 fseek(f, xtraPtr + 4, SEEK_SET); // seek to first wanted byte
594
595 // volume (0..256)
596 fseek(f, 2, SEEK_CUR);
597 fread(&tempVol, 2, 1, f);
598 if (tempVol > 256)
599 tempVol = 256;
600
601 tempVol >>= 2; // 0..256 -> 0..64
602
603 s->volume = (int8_t)tempVol;
604 }
605 // ---------------------------
606
607 // ---- READ "INAM" chunk ----
608 if (inamPtr != 0 && inamLen > 0)
609 {
610 fseek(f, inamPtr, SEEK_SET); // seek to first wanted byte
611
612 for (int32_t i = 0; i < 21; i++)
613 {
614 if (i < inamLen)
615 s->text[i] = (char)fgetc(f);
616 else
617 s->text[i] = '\0';
618 }
619
620 s->text[21] = '\0';
621 s->text[22] = '\0';
622 }
623 // ---------------------------
624
625 return true;
626 }