2 Simple DirectMedia Layer
3 Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 #include "../SDL_internal.h"
23 /* Microsoft WAVE file loading routines */
25 #include "SDL_audio.h"
29 static int ReadChunk(SDL_RWops * src, Chunk * chunk);
31 struct MS_ADPCM_decodestate
38 static struct MS_ADPCM_decoder
41 Uint16 wSamplesPerBlock;
45 struct MS_ADPCM_decodestate state[2];
49 InitMS_ADPCM(WaveFMT * format)
54 /* Set the rogue pointer to the MS_ADPCM specific data */
55 MS_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding);
56 MS_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels);
57 MS_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency);
58 MS_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate);
59 MS_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign);
60 MS_ADPCM_state.wavefmt.bitspersample =
61 SDL_SwapLE16(format->bitspersample);
62 rogue_feel = (Uint8 *) format + sizeof(*format);
63 if (sizeof(*format) == 16) {
64 /* const Uint16 extra_info = ((rogue_feel[1] << 8) | rogue_feel[0]); */
65 rogue_feel += sizeof(Uint16);
67 MS_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1] << 8) | rogue_feel[0]);
68 rogue_feel += sizeof(Uint16);
69 MS_ADPCM_state.wNumCoef = ((rogue_feel[1] << 8) | rogue_feel[0]);
70 rogue_feel += sizeof(Uint16);
71 if (MS_ADPCM_state.wNumCoef != 7) {
72 SDL_SetError("Unknown set of MS_ADPCM coefficients");
75 for (i = 0; i < MS_ADPCM_state.wNumCoef; ++i) {
76 MS_ADPCM_state.aCoeff[i][0] = ((rogue_feel[1] << 8) | rogue_feel[0]);
77 rogue_feel += sizeof(Uint16);
78 MS_ADPCM_state.aCoeff[i][1] = ((rogue_feel[1] << 8) | rogue_feel[0]);
79 rogue_feel += sizeof(Uint16);
85 MS_ADPCM_nibble(struct MS_ADPCM_decodestate *state,
86 Uint8 nybble, Sint16 * coeff)
88 const Sint32 max_audioval = ((1 << (16 - 1)) - 1);
89 const Sint32 min_audioval = -(1 << (16 - 1));
90 const Sint32 adaptive[] = {
91 230, 230, 230, 230, 307, 409, 512, 614,
92 768, 614, 512, 409, 307, 230, 230, 230
94 Sint32 new_sample, delta;
96 new_sample = ((state->iSamp1 * coeff[0]) +
97 (state->iSamp2 * coeff[1])) / 256;
99 new_sample += state->iDelta * (nybble - 0x10);
101 new_sample += state->iDelta * nybble;
103 if (new_sample < min_audioval) {
104 new_sample = min_audioval;
105 } else if (new_sample > max_audioval) {
106 new_sample = max_audioval;
108 delta = ((Sint32) state->iDelta * adaptive[nybble]) / 256;
112 state->iDelta = (Uint16) delta;
113 state->iSamp2 = state->iSamp1;
114 state->iSamp1 = (Sint16) new_sample;
119 MS_ADPCM_decode(Uint8 ** audio_buf, Uint32 * audio_len)
121 struct MS_ADPCM_decodestate *state[2];
122 Uint8 *freeable, *encoded, *decoded;
123 Sint32 encoded_len, samplesleft;
129 /* Allocate the proper sized output buffer */
130 encoded_len = *audio_len;
131 encoded = *audio_buf;
132 freeable = *audio_buf;
133 *audio_len = (encoded_len / MS_ADPCM_state.wavefmt.blockalign) *
134 MS_ADPCM_state.wSamplesPerBlock *
135 MS_ADPCM_state.wavefmt.channels * sizeof(Sint16);
136 *audio_buf = (Uint8 *) SDL_malloc(*audio_len);
137 if (*audio_buf == NULL) {
138 return SDL_OutOfMemory();
140 decoded = *audio_buf;
142 /* Get ready... Go! */
143 stereo = (MS_ADPCM_state.wavefmt.channels == 2);
144 state[0] = &MS_ADPCM_state.state[0];
145 state[1] = &MS_ADPCM_state.state[stereo];
146 while (encoded_len >= MS_ADPCM_state.wavefmt.blockalign) {
147 /* Grab the initial information for this block */
148 state[0]->hPredictor = *encoded++;
150 state[1]->hPredictor = *encoded++;
152 state[0]->iDelta = ((encoded[1] << 8) | encoded[0]);
153 encoded += sizeof(Sint16);
155 state[1]->iDelta = ((encoded[1] << 8) | encoded[0]);
156 encoded += sizeof(Sint16);
158 state[0]->iSamp1 = ((encoded[1] << 8) | encoded[0]);
159 encoded += sizeof(Sint16);
161 state[1]->iSamp1 = ((encoded[1] << 8) | encoded[0]);
162 encoded += sizeof(Sint16);
164 state[0]->iSamp2 = ((encoded[1] << 8) | encoded[0]);
165 encoded += sizeof(Sint16);
167 state[1]->iSamp2 = ((encoded[1] << 8) | encoded[0]);
168 encoded += sizeof(Sint16);
170 coeff[0] = MS_ADPCM_state.aCoeff[state[0]->hPredictor];
171 coeff[1] = MS_ADPCM_state.aCoeff[state[1]->hPredictor];
173 /* Store the two initial samples we start with */
174 decoded[0] = state[0]->iSamp2 & 0xFF;
175 decoded[1] = state[0]->iSamp2 >> 8;
178 decoded[0] = state[1]->iSamp2 & 0xFF;
179 decoded[1] = state[1]->iSamp2 >> 8;
182 decoded[0] = state[0]->iSamp1 & 0xFF;
183 decoded[1] = state[0]->iSamp1 >> 8;
186 decoded[0] = state[1]->iSamp1 & 0xFF;
187 decoded[1] = state[1]->iSamp1 >> 8;
191 /* Decode and store the other samples in this block */
192 samplesleft = (MS_ADPCM_state.wSamplesPerBlock - 2) *
193 MS_ADPCM_state.wavefmt.channels;
194 while (samplesleft > 0) {
195 nybble = (*encoded) >> 4;
196 new_sample = MS_ADPCM_nibble(state[0], nybble, coeff[0]);
197 decoded[0] = new_sample & 0xFF;
199 decoded[1] = new_sample & 0xFF;
202 nybble = (*encoded) & 0x0F;
203 new_sample = MS_ADPCM_nibble(state[1], nybble, coeff[1]);
204 decoded[0] = new_sample & 0xFF;
206 decoded[1] = new_sample & 0xFF;
212 encoded_len -= MS_ADPCM_state.wavefmt.blockalign;
218 struct IMA_ADPCM_decodestate
223 static struct IMA_ADPCM_decoder
226 Uint16 wSamplesPerBlock;
228 struct IMA_ADPCM_decodestate state[2];
232 InitIMA_ADPCM(WaveFMT * format)
236 /* Set the rogue pointer to the IMA_ADPCM specific data */
237 IMA_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding);
238 IMA_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels);
239 IMA_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency);
240 IMA_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate);
241 IMA_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign);
242 IMA_ADPCM_state.wavefmt.bitspersample =
243 SDL_SwapLE16(format->bitspersample);
244 rogue_feel = (Uint8 *) format + sizeof(*format);
245 if (sizeof(*format) == 16) {
246 /* const Uint16 extra_info = ((rogue_feel[1] << 8) | rogue_feel[0]); */
247 rogue_feel += sizeof(Uint16);
249 IMA_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1] << 8) | rogue_feel[0]);
254 IMA_ADPCM_nibble(struct IMA_ADPCM_decodestate *state, Uint8 nybble)
256 const Sint32 max_audioval = ((1 << (16 - 1)) - 1);
257 const Sint32 min_audioval = -(1 << (16 - 1));
258 const int index_table[16] = {
264 const Sint32 step_table[89] = {
265 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
266 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130,
267 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408,
268 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
269 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
270 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630,
271 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350,
272 22385, 24623, 27086, 29794, 32767
276 /* Compute difference and new sample value */
277 if (state->index > 88) {
279 } else if (state->index < 0) {
282 /* explicit cast to avoid gcc warning about using 'char' as array index */
283 step = step_table[(int)state->index];
288 delta += (step >> 1);
290 delta += (step >> 2);
293 state->sample += delta;
295 /* Update index value */
296 state->index += index_table[nybble];
298 /* Clamp output sample */
299 if (state->sample > max_audioval) {
300 state->sample = max_audioval;
301 } else if (state->sample < min_audioval) {
302 state->sample = min_audioval;
304 return (state->sample);
307 /* Fill the decode buffer with a channel block of data (8 samples) */
309 Fill_IMA_ADPCM_block(Uint8 * decoded, Uint8 * encoded,
310 int channel, int numchannels,
311 struct IMA_ADPCM_decodestate *state)
317 decoded += (channel * 2);
318 for (i = 0; i < 4; ++i) {
319 nybble = (*encoded) & 0x0F;
320 new_sample = IMA_ADPCM_nibble(state, nybble);
321 decoded[0] = new_sample & 0xFF;
323 decoded[1] = new_sample & 0xFF;
324 decoded += 2 * numchannels;
326 nybble = (*encoded) >> 4;
327 new_sample = IMA_ADPCM_nibble(state, nybble);
328 decoded[0] = new_sample & 0xFF;
330 decoded[1] = new_sample & 0xFF;
331 decoded += 2 * numchannels;
338 IMA_ADPCM_decode(Uint8 ** audio_buf, Uint32 * audio_len)
340 struct IMA_ADPCM_decodestate *state;
341 Uint8 *freeable, *encoded, *decoded;
342 Sint32 encoded_len, samplesleft;
343 unsigned int c, channels;
345 /* Check to make sure we have enough variables in the state array */
346 channels = IMA_ADPCM_state.wavefmt.channels;
347 if (channels > SDL_arraysize(IMA_ADPCM_state.state)) {
348 SDL_SetError("IMA ADPCM decoder can only handle %u channels",
349 (unsigned int)SDL_arraysize(IMA_ADPCM_state.state));
352 state = IMA_ADPCM_state.state;
354 /* Allocate the proper sized output buffer */
355 encoded_len = *audio_len;
356 encoded = *audio_buf;
357 freeable = *audio_buf;
358 *audio_len = (encoded_len / IMA_ADPCM_state.wavefmt.blockalign) *
359 IMA_ADPCM_state.wSamplesPerBlock *
360 IMA_ADPCM_state.wavefmt.channels * sizeof(Sint16);
361 *audio_buf = (Uint8 *) SDL_malloc(*audio_len);
362 if (*audio_buf == NULL) {
363 return SDL_OutOfMemory();
365 decoded = *audio_buf;
367 /* Get ready... Go! */
368 while (encoded_len >= IMA_ADPCM_state.wavefmt.blockalign) {
369 /* Grab the initial information for this block */
370 for (c = 0; c < channels; ++c) {
371 /* Fill the state information for this block */
372 state[c].sample = ((encoded[1] << 8) | encoded[0]);
374 if (state[c].sample & 0x8000) {
375 state[c].sample -= 0x10000;
377 state[c].index = *encoded++;
378 /* Reserved byte in buffer header, should be 0 */
379 if (*encoded++ != 0) {
380 /* Uh oh, corrupt data? Buggy code? */ ;
383 /* Store the initial sample we start with */
384 decoded[0] = (Uint8) (state[c].sample & 0xFF);
385 decoded[1] = (Uint8) (state[c].sample >> 8);
389 /* Decode and store the other samples in this block */
390 samplesleft = (IMA_ADPCM_state.wSamplesPerBlock - 1) * channels;
391 while (samplesleft > 0) {
392 for (c = 0; c < channels; ++c) {
393 Fill_IMA_ADPCM_block(decoded, encoded,
394 c, channels, &state[c]);
398 decoded += (channels * 8 * 2);
400 encoded_len -= IMA_ADPCM_state.wavefmt.blockalign;
408 ConvertSint24ToSint32(Uint8 ** audio_buf, Uint32 * audio_len)
410 const double DIVBY8388608 = 0.00000011920928955078125;
411 const Uint32 original_len = *audio_len;
412 const Uint32 samples = original_len / 3;
413 const Uint32 expanded_len = samples * sizeof (Uint32);
414 Uint8 *ptr = (Uint8 *) SDL_realloc(*audio_buf, expanded_len);
420 return SDL_OutOfMemory();
424 *audio_len = expanded_len;
426 /* work from end to start, since we're expanding in-place. */
427 src = (ptr + original_len) - 3;
428 dst = ((Uint32 *) (ptr + expanded_len)) - 1;
429 for (i = 0; i < samples; i++) {
430 /* There's probably a faster way to do all this. */
431 const Sint32 converted = ((Sint32) ( (((Uint32) src[2]) << 24) |
432 (((Uint32) src[1]) << 16) |
433 (((Uint32) src[0]) << 8) )) >> 8;
434 const double scaled = (((double) converted) * DIVBY8388608);
436 *(dst--) = (Sint32) (scaled * 2147483647.0);
443 /* GUIDs that are used by WAVE_FORMAT_EXTENSIBLE */
444 static const Uint8 extensible_pcm_guid[16] = { 1, 0, 0, 0, 0, 0, 16, 0, 128, 0, 0, 170, 0, 56, 155, 113 };
445 static const Uint8 extensible_ieee_guid[16] = { 3, 0, 0, 0, 0, 0, 16, 0, 128, 0, 0, 170, 0, 56, 155, 113 };
448 SDL_LoadWAV_RW(SDL_RWops * src, int freesrc,
449 SDL_AudioSpec * spec, Uint8 ** audio_buf, Uint32 * audio_len)
454 int IEEE_float_encoded, MS_ADPCM_encoded, IMA_ADPCM_encoded;
457 /* WAV magic header */
461 Uint32 headerDiff = 0;
464 WaveFMT *format = NULL;
465 WaveExtensibleFMT *ext = NULL;
469 /* Make sure we are passed a valid data source */
476 /* Check the magic header */
477 RIFFchunk = SDL_ReadLE32(src);
478 wavelen = SDL_ReadLE32(src);
479 if (wavelen == WAVE) { /* The RIFFchunk has already been read */
484 WAVEmagic = SDL_ReadLE32(src);
486 if ((RIFFchunk != RIFF) || (WAVEmagic != WAVE)) {
487 SDL_SetError("Unrecognized file type (not WAVE)");
491 headerDiff += sizeof(Uint32); /* for WAVE */
493 /* Read the audio data format chunk */
496 SDL_free(chunk.data);
498 lenread = ReadChunk(src, &chunk);
503 /* 2 Uint32's for chunk header+len, plus the lenread */
504 headerDiff += lenread + 2 * sizeof(Uint32);
505 } while ((chunk.magic == FACT) || (chunk.magic == LIST) || (chunk.magic == BEXT) || (chunk.magic == JUNK));
507 /* Decode the audio data format */
508 format = (WaveFMT *) chunk.data;
509 if (chunk.magic != FMT) {
510 SDL_SetError("Complex WAVE files not supported");
514 IEEE_float_encoded = MS_ADPCM_encoded = IMA_ADPCM_encoded = 0;
515 switch (SDL_SwapLE16(format->encoding)) {
517 /* We can understand this */
519 case IEEE_FLOAT_CODE:
520 IEEE_float_encoded = 1;
521 /* We can understand this */
524 /* Try to understand this */
525 if (InitMS_ADPCM(format) < 0) {
529 MS_ADPCM_encoded = 1;
532 /* Try to understand this */
533 if (InitIMA_ADPCM(format) < 0) {
537 IMA_ADPCM_encoded = 1;
539 case EXTENSIBLE_CODE:
540 /* note that this ignores channel masks, smaller valid bit counts
541 inside a larger container, and most subtypes. This is just enough
542 to get things that didn't really _need_ WAVE_FORMAT_EXTENSIBLE
543 to be useful working when they use this format flag. */
544 ext = (WaveExtensibleFMT *) format;
545 if (SDL_SwapLE16(ext->size) < 22) {
546 SDL_SetError("bogus extended .wav header");
550 if (SDL_memcmp(ext->subformat, extensible_pcm_guid, 16) == 0) {
552 } else if (SDL_memcmp(ext->subformat, extensible_ieee_guid, 16) == 0) {
553 IEEE_float_encoded = 1;
558 SDL_SetError("MPEG Layer 3 data not supported");
562 SDL_SetError("Unknown WAVE data format: 0x%.4x",
563 SDL_SwapLE16(format->encoding));
568 spec->freq = SDL_SwapLE32(format->frequency);
570 if (IEEE_float_encoded) {
571 if ((SDL_SwapLE16(format->bitspersample)) != 32) {
574 spec->format = AUDIO_F32;
577 switch (SDL_SwapLE16(format->bitspersample)) {
579 if (MS_ADPCM_encoded || IMA_ADPCM_encoded) {
580 spec->format = AUDIO_S16;
586 spec->format = AUDIO_U8;
589 spec->format = AUDIO_S16;
591 case 24: /* convert this. */
592 spec->format = AUDIO_S32;
595 spec->format = AUDIO_S32;
604 SDL_SetError("Unknown %d-bit PCM data format",
605 SDL_SwapLE16(format->bitspersample));
608 spec->channels = (Uint8) SDL_SwapLE16(format->channels);
609 spec->samples = 4096; /* Good default buffer size */
611 /* Read the audio data chunk */
614 SDL_free(*audio_buf);
616 lenread = ReadChunk(src, &chunk);
621 *audio_len = lenread;
622 *audio_buf = chunk.data;
623 if (chunk.magic != DATA)
624 headerDiff += lenread + 2 * sizeof(Uint32);
625 } while (chunk.magic != DATA);
626 headerDiff += 2 * sizeof(Uint32); /* for the data chunk and len */
628 if (MS_ADPCM_encoded) {
629 if (MS_ADPCM_decode(audio_buf, audio_len) < 0) {
634 if (IMA_ADPCM_encoded) {
635 if (IMA_ADPCM_decode(audio_buf, audio_len) < 0) {
641 if (SDL_SwapLE16(format->bitspersample) == 24) {
642 if (ConvertSint24ToSint32(audio_buf, audio_len) < 0) {
648 /* Don't return a buffer that isn't a multiple of samplesize */
649 samplesize = ((SDL_AUDIO_BITSIZE(spec->format)) / 8) * spec->channels;
650 *audio_len &= ~(samplesize - 1);
658 /* seek to the end of the file (given by the RIFF chunk) */
659 SDL_RWseek(src, wavelen - chunk.length - headerDiff, RW_SEEK_CUR);
668 /* Since the WAV memory is allocated in the shared library, it must also
669 be freed here. (Necessary under Win32, VC++)
672 SDL_FreeWAV(Uint8 * audio_buf)
678 ReadChunk(SDL_RWops * src, Chunk * chunk)
680 chunk->magic = SDL_ReadLE32(src);
681 chunk->length = SDL_ReadLE32(src);
682 chunk->data = (Uint8 *) SDL_malloc(chunk->length);
683 if (chunk->data == NULL) {
684 return SDL_OutOfMemory();
686 if (SDL_RWread(src, chunk->data, chunk->length, 1) != 1) {
687 SDL_free(chunk->data);
689 return SDL_Error(SDL_EFREAD);
691 return (chunk->length);
694 /* vi: set ts=4 sw=4 expandtab: */