Mercurial > sdl-ios-xcode
annotate src/audio/SDL_wave.c @ 1312:c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 01 Feb 2006 06:32:25 +0000 |
parents | 80f8c94b5199 |
children | 450721ad5436 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1260
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1260
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1260
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 9 |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1260
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1260
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1260
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1260
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
171
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 #ifndef DISABLE_FILE | |
24 | |
25 /* Microsoft WAVE file loading routines */ | |
26 | |
27 #include <stdlib.h> | |
28 #include <string.h> | |
29 | |
30 #include "SDL_error.h" | |
31 #include "SDL_audio.h" | |
32 #include "SDL_wave.h" | |
33 #include "SDL_endian.h" | |
34 | |
35 #ifndef NELEMS | |
36 #define NELEMS(array) ((sizeof array)/(sizeof array[0])) | |
37 #endif | |
38 | |
39 static int ReadChunk(SDL_RWops *src, Chunk *chunk); | |
40 | |
41 struct MS_ADPCM_decodestate { | |
42 Uint8 hPredictor; | |
43 Uint16 iDelta; | |
44 Sint16 iSamp1; | |
45 Sint16 iSamp2; | |
46 }; | |
47 static struct MS_ADPCM_decoder { | |
48 WaveFMT wavefmt; | |
49 Uint16 wSamplesPerBlock; | |
50 Uint16 wNumCoef; | |
51 Sint16 aCoeff[7][2]; | |
52 /* * * */ | |
53 struct MS_ADPCM_decodestate state[2]; | |
54 } MS_ADPCM_state; | |
55 | |
56 static int InitMS_ADPCM(WaveFMT *format) | |
57 { | |
58 Uint8 *rogue_feel; | |
59 Uint16 extra_info; | |
60 int i; | |
61 | |
62 /* Set the rogue pointer to the MS_ADPCM specific data */ | |
63 MS_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding); | |
64 MS_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels); | |
65 MS_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency); | |
66 MS_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate); | |
67 MS_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign); | |
68 MS_ADPCM_state.wavefmt.bitspersample = | |
69 SDL_SwapLE16(format->bitspersample); | |
70 rogue_feel = (Uint8 *)format+sizeof(*format); | |
71 if ( sizeof(*format) == 16 ) { | |
72 extra_info = ((rogue_feel[1]<<8)|rogue_feel[0]); | |
73 rogue_feel += sizeof(Uint16); | |
74 } | |
75 MS_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1]<<8)|rogue_feel[0]); | |
76 rogue_feel += sizeof(Uint16); | |
77 MS_ADPCM_state.wNumCoef = ((rogue_feel[1]<<8)|rogue_feel[0]); | |
78 rogue_feel += sizeof(Uint16); | |
79 if ( MS_ADPCM_state.wNumCoef != 7 ) { | |
80 SDL_SetError("Unknown set of MS_ADPCM coefficients"); | |
81 return(-1); | |
82 } | |
83 for ( i=0; i<MS_ADPCM_state.wNumCoef; ++i ) { | |
84 MS_ADPCM_state.aCoeff[i][0] = ((rogue_feel[1]<<8)|rogue_feel[0]); | |
85 rogue_feel += sizeof(Uint16); | |
86 MS_ADPCM_state.aCoeff[i][1] = ((rogue_feel[1]<<8)|rogue_feel[0]); | |
87 rogue_feel += sizeof(Uint16); | |
88 } | |
89 return(0); | |
90 } | |
91 | |
92 static Sint32 MS_ADPCM_nibble(struct MS_ADPCM_decodestate *state, | |
93 Uint8 nybble, Sint16 *coeff) | |
94 { | |
95 const Sint32 max_audioval = ((1<<(16-1))-1); | |
96 const Sint32 min_audioval = -(1<<(16-1)); | |
97 const Sint32 adaptive[] = { | |
98 230, 230, 230, 230, 307, 409, 512, 614, | |
99 768, 614, 512, 409, 307, 230, 230, 230 | |
100 }; | |
101 Sint32 new_sample, delta; | |
102 | |
103 new_sample = ((state->iSamp1 * coeff[0]) + | |
104 (state->iSamp2 * coeff[1]))/256; | |
105 if ( nybble & 0x08 ) { | |
106 new_sample += state->iDelta * (nybble-0x10); | |
107 } else { | |
108 new_sample += state->iDelta * nybble; | |
109 } | |
110 if ( new_sample < min_audioval ) { | |
111 new_sample = min_audioval; | |
112 } else | |
113 if ( new_sample > max_audioval ) { | |
114 new_sample = max_audioval; | |
115 } | |
116 delta = ((Sint32)state->iDelta * adaptive[nybble])/256; | |
117 if ( delta < 16 ) { | |
118 delta = 16; | |
119 } | |
120 state->iDelta = delta; | |
121 state->iSamp2 = state->iSamp1; | |
122 state->iSamp1 = new_sample; | |
123 return(new_sample); | |
124 } | |
125 | |
126 static int MS_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len) | |
127 { | |
128 struct MS_ADPCM_decodestate *state[2]; | |
129 Uint8 *freeable, *encoded, *decoded; | |
130 Sint32 encoded_len, samplesleft; | |
131 Sint8 nybble, stereo; | |
132 Sint16 *coeff[2]; | |
133 Sint32 new_sample; | |
134 | |
135 /* Allocate the proper sized output buffer */ | |
136 encoded_len = *audio_len; | |
137 encoded = *audio_buf; | |
138 freeable = *audio_buf; | |
139 *audio_len = (encoded_len/MS_ADPCM_state.wavefmt.blockalign) * | |
140 MS_ADPCM_state.wSamplesPerBlock* | |
141 MS_ADPCM_state.wavefmt.channels*sizeof(Sint16); | |
142 *audio_buf = (Uint8 *)malloc(*audio_len); | |
143 if ( *audio_buf == NULL ) { | |
144 SDL_Error(SDL_ENOMEM); | |
145 return(-1); | |
146 } | |
147 decoded = *audio_buf; | |
148 | |
149 /* Get ready... Go! */ | |
150 stereo = (MS_ADPCM_state.wavefmt.channels == 2); | |
151 state[0] = &MS_ADPCM_state.state[0]; | |
152 state[1] = &MS_ADPCM_state.state[stereo]; | |
153 while ( encoded_len >= MS_ADPCM_state.wavefmt.blockalign ) { | |
154 /* Grab the initial information for this block */ | |
155 state[0]->hPredictor = *encoded++; | |
156 if ( stereo ) { | |
157 state[1]->hPredictor = *encoded++; | |
158 } | |
159 state[0]->iDelta = ((encoded[1]<<8)|encoded[0]); | |
160 encoded += sizeof(Sint16); | |
161 if ( stereo ) { | |
162 state[1]->iDelta = ((encoded[1]<<8)|encoded[0]); | |
163 encoded += sizeof(Sint16); | |
164 } | |
165 state[0]->iSamp1 = ((encoded[1]<<8)|encoded[0]); | |
166 encoded += sizeof(Sint16); | |
167 if ( stereo ) { | |
168 state[1]->iSamp1 = ((encoded[1]<<8)|encoded[0]); | |
169 encoded += sizeof(Sint16); | |
170 } | |
171 state[0]->iSamp2 = ((encoded[1]<<8)|encoded[0]); | |
172 encoded += sizeof(Sint16); | |
173 if ( stereo ) { | |
174 state[1]->iSamp2 = ((encoded[1]<<8)|encoded[0]); | |
175 encoded += sizeof(Sint16); | |
176 } | |
177 coeff[0] = MS_ADPCM_state.aCoeff[state[0]->hPredictor]; | |
178 coeff[1] = MS_ADPCM_state.aCoeff[state[1]->hPredictor]; | |
179 | |
180 /* Store the two initial samples we start with */ | |
181 decoded[0] = state[0]->iSamp2&0xFF; | |
182 decoded[1] = state[0]->iSamp2>>8; | |
183 decoded += 2; | |
184 if ( stereo ) { | |
185 decoded[0] = state[1]->iSamp2&0xFF; | |
186 decoded[1] = state[1]->iSamp2>>8; | |
187 decoded += 2; | |
188 } | |
189 decoded[0] = state[0]->iSamp1&0xFF; | |
190 decoded[1] = state[0]->iSamp1>>8; | |
191 decoded += 2; | |
192 if ( stereo ) { | |
193 decoded[0] = state[1]->iSamp1&0xFF; | |
194 decoded[1] = state[1]->iSamp1>>8; | |
195 decoded += 2; | |
196 } | |
197 | |
198 /* Decode and store the other samples in this block */ | |
199 samplesleft = (MS_ADPCM_state.wSamplesPerBlock-2)* | |
200 MS_ADPCM_state.wavefmt.channels; | |
201 while ( samplesleft > 0 ) { | |
202 nybble = (*encoded)>>4; | |
203 new_sample = MS_ADPCM_nibble(state[0],nybble,coeff[0]); | |
204 decoded[0] = new_sample&0xFF; | |
205 new_sample >>= 8; | |
206 decoded[1] = new_sample&0xFF; | |
207 decoded += 2; | |
208 | |
209 nybble = (*encoded)&0x0F; | |
210 new_sample = MS_ADPCM_nibble(state[1],nybble,coeff[1]); | |
211 decoded[0] = new_sample&0xFF; | |
212 new_sample >>= 8; | |
213 decoded[1] = new_sample&0xFF; | |
214 decoded += 2; | |
215 | |
216 ++encoded; | |
217 samplesleft -= 2; | |
218 } | |
219 encoded_len -= MS_ADPCM_state.wavefmt.blockalign; | |
220 } | |
221 free(freeable); | |
222 return(0); | |
223 } | |
224 | |
225 struct IMA_ADPCM_decodestate { | |
226 Sint32 sample; | |
227 Sint8 index; | |
228 }; | |
229 static struct IMA_ADPCM_decoder { | |
230 WaveFMT wavefmt; | |
231 Uint16 wSamplesPerBlock; | |
232 /* * * */ | |
233 struct IMA_ADPCM_decodestate state[2]; | |
234 } IMA_ADPCM_state; | |
235 | |
236 static int InitIMA_ADPCM(WaveFMT *format) | |
237 { | |
238 Uint8 *rogue_feel; | |
239 Uint16 extra_info; | |
240 | |
241 /* Set the rogue pointer to the IMA_ADPCM specific data */ | |
242 IMA_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding); | |
243 IMA_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels); | |
244 IMA_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency); | |
245 IMA_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate); | |
246 IMA_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign); | |
247 IMA_ADPCM_state.wavefmt.bitspersample = | |
248 SDL_SwapLE16(format->bitspersample); | |
249 rogue_feel = (Uint8 *)format+sizeof(*format); | |
250 if ( sizeof(*format) == 16 ) { | |
251 extra_info = ((rogue_feel[1]<<8)|rogue_feel[0]); | |
252 rogue_feel += sizeof(Uint16); | |
253 } | |
254 IMA_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1]<<8)|rogue_feel[0]); | |
255 return(0); | |
256 } | |
257 | |
258 static Sint32 IMA_ADPCM_nibble(struct IMA_ADPCM_decodestate *state,Uint8 nybble) | |
259 { | |
260 const Sint32 max_audioval = ((1<<(16-1))-1); | |
261 const Sint32 min_audioval = -(1<<(16-1)); | |
262 const int index_table[16] = { | |
263 -1, -1, -1, -1, | |
264 2, 4, 6, 8, | |
265 -1, -1, -1, -1, | |
266 2, 4, 6, 8 | |
267 }; | |
268 const Sint32 step_table[89] = { | |
269 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, | |
270 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, | |
271 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, | |
272 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, | |
273 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, | |
274 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, | |
275 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350, | |
276 22385, 24623, 27086, 29794, 32767 | |
277 }; | |
278 Sint32 delta, step; | |
279 | |
280 /* Compute difference and new sample value */ | |
281 step = step_table[state->index]; | |
282 delta = step >> 3; | |
283 if ( nybble & 0x04 ) delta += step; | |
284 if ( nybble & 0x02 ) delta += (step >> 1); | |
285 if ( nybble & 0x01 ) delta += (step >> 2); | |
286 if ( nybble & 0x08 ) delta = -delta; | |
287 state->sample += delta; | |
288 | |
289 /* Update index value */ | |
290 state->index += index_table[nybble]; | |
291 if ( state->index > 88 ) { | |
292 state->index = 88; | |
293 } else | |
294 if ( state->index < 0 ) { | |
295 state->index = 0; | |
296 } | |
297 | |
298 /* Clamp output sample */ | |
299 if ( state->sample > max_audioval ) { | |
300 state->sample = max_audioval; | |
301 } else | |
302 if ( state->sample < min_audioval ) { | |
303 state->sample = min_audioval; | |
304 } | |
305 return(state->sample); | |
306 } | |
307 | |
308 /* Fill the decode buffer with a channel block of data (8 samples) */ | |
309 static void Fill_IMA_ADPCM_block(Uint8 *decoded, Uint8 *encoded, | |
310 int channel, int numchannels, struct IMA_ADPCM_decodestate *state) | |
311 { | |
312 int i; | |
313 Sint8 nybble; | |
314 Sint32 new_sample; | |
315 | |
316 decoded += (channel * 2); | |
317 for ( i=0; i<4; ++i ) { | |
318 nybble = (*encoded)&0x0F; | |
319 new_sample = IMA_ADPCM_nibble(state, nybble); | |
320 decoded[0] = new_sample&0xFF; | |
321 new_sample >>= 8; | |
322 decoded[1] = new_sample&0xFF; | |
323 decoded += 2 * numchannels; | |
324 | |
325 nybble = (*encoded)>>4; | |
326 new_sample = IMA_ADPCM_nibble(state, nybble); | |
327 decoded[0] = new_sample&0xFF; | |
328 new_sample >>= 8; | |
329 decoded[1] = new_sample&0xFF; | |
330 decoded += 2 * numchannels; | |
331 | |
332 ++encoded; | |
333 } | |
334 } | |
335 | |
336 static int IMA_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len) | |
337 { | |
338 struct IMA_ADPCM_decodestate *state; | |
339 Uint8 *freeable, *encoded, *decoded; | |
340 Sint32 encoded_len, samplesleft; | |
341 int c, channels; | |
342 | |
343 /* Check to make sure we have enough variables in the state array */ | |
344 channels = IMA_ADPCM_state.wavefmt.channels; | |
345 if ( channels > NELEMS(IMA_ADPCM_state.state) ) { | |
346 SDL_SetError("IMA ADPCM decoder can only handle %d channels", | |
347 NELEMS(IMA_ADPCM_state.state)); | |
348 return(-1); | |
349 } | |
350 state = IMA_ADPCM_state.state; | |
351 | |
352 /* Allocate the proper sized output buffer */ | |
353 encoded_len = *audio_len; | |
354 encoded = *audio_buf; | |
355 freeable = *audio_buf; | |
356 *audio_len = (encoded_len/IMA_ADPCM_state.wavefmt.blockalign) * | |
357 IMA_ADPCM_state.wSamplesPerBlock* | |
358 IMA_ADPCM_state.wavefmt.channels*sizeof(Sint16); | |
359 *audio_buf = (Uint8 *)malloc(*audio_len); | |
360 if ( *audio_buf == NULL ) { | |
361 SDL_Error(SDL_ENOMEM); | |
362 return(-1); | |
363 } | |
364 decoded = *audio_buf; | |
365 | |
366 /* Get ready... Go! */ | |
367 while ( encoded_len >= IMA_ADPCM_state.wavefmt.blockalign ) { | |
368 /* Grab the initial information for this block */ | |
369 for ( c=0; c<channels; ++c ) { | |
370 /* Fill the state information for this block */ | |
371 state[c].sample = ((encoded[1]<<8)|encoded[0]); | |
372 encoded += 2; | |
373 if ( state[c].sample & 0x8000 ) { | |
374 state[c].sample -= 0x10000; | |
375 } | |
376 state[c].index = *encoded++; | |
377 /* Reserved byte in buffer header, should be 0 */ | |
378 if ( *encoded++ != 0 ) { | |
379 /* Uh oh, corrupt data? Buggy code? */; | |
380 } | |
381 | |
382 /* Store the initial sample we start with */ | |
383 decoded[0] = state[c].sample&0xFF; | |
384 decoded[1] = state[c].sample>>8; | |
385 decoded += 2; | |
386 } | |
387 | |
388 /* Decode and store the other samples in this block */ | |
389 samplesleft = (IMA_ADPCM_state.wSamplesPerBlock-1)*channels; | |
390 while ( samplesleft > 0 ) { | |
391 for ( c=0; c<channels; ++c ) { | |
392 Fill_IMA_ADPCM_block(decoded, encoded, | |
393 c, channels, &state[c]); | |
394 encoded += 4; | |
395 samplesleft -= 8; | |
396 } | |
397 decoded += (channels * 8 * 2); | |
398 } | |
399 encoded_len -= IMA_ADPCM_state.wavefmt.blockalign; | |
400 } | |
401 free(freeable); | |
402 return(0); | |
403 } | |
404 | |
405 SDL_AudioSpec * SDL_LoadWAV_RW (SDL_RWops *src, int freesrc, | |
406 SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len) | |
407 { | |
408 int was_error; | |
409 Chunk chunk; | |
410 int lenread; | |
411 int MS_ADPCM_encoded, IMA_ADPCM_encoded; | |
412 int samplesize; | |
413 | |
414 /* WAV magic header */ | |
415 Uint32 RIFFchunk; | |
1260
80f8c94b5199
Date: 10 Jun 2003 15:30:59 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
416 Uint32 wavelen = 0; |
0 | 417 Uint32 WAVEmagic; |
1260
80f8c94b5199
Date: 10 Jun 2003 15:30:59 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
418 Uint32 headerDiff = 0; |
0 | 419 |
420 /* FMT chunk */ | |
421 WaveFMT *format = NULL; | |
422 | |
423 /* Make sure we are passed a valid data source */ | |
424 was_error = 0; | |
425 if ( src == NULL ) { | |
426 was_error = 1; | |
427 goto done; | |
428 } | |
429 | |
430 /* Check the magic header */ | |
431 RIFFchunk = SDL_ReadLE32(src); | |
432 wavelen = SDL_ReadLE32(src); | |
171
02e27b705645
Handle the case where the WAVE magic number was already read in a non-seekable
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
433 if ( wavelen == WAVE ) { /* The RIFFchunk has already been read */ |
02e27b705645
Handle the case where the WAVE magic number was already read in a non-seekable
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
434 WAVEmagic = wavelen; |
02e27b705645
Handle the case where the WAVE magic number was already read in a non-seekable
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
435 wavelen = RIFFchunk; |
02e27b705645
Handle the case where the WAVE magic number was already read in a non-seekable
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
436 RIFFchunk = RIFF; |
02e27b705645
Handle the case where the WAVE magic number was already read in a non-seekable
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
437 } else { |
02e27b705645
Handle the case where the WAVE magic number was already read in a non-seekable
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
438 WAVEmagic = SDL_ReadLE32(src); |
02e27b705645
Handle the case where the WAVE magic number was already read in a non-seekable
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
439 } |
0 | 440 if ( (RIFFchunk != RIFF) || (WAVEmagic != WAVE) ) { |
441 SDL_SetError("Unrecognized file type (not WAVE)"); | |
442 was_error = 1; | |
443 goto done; | |
444 } | |
1260
80f8c94b5199
Date: 10 Jun 2003 15:30:59 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
445 headerDiff += sizeof(Uint32); // for WAVE |
0 | 446 |
447 /* Read the audio data format chunk */ | |
448 chunk.data = NULL; | |
449 do { | |
450 if ( chunk.data != NULL ) { | |
451 free(chunk.data); | |
452 } | |
453 lenread = ReadChunk(src, &chunk); | |
454 if ( lenread < 0 ) { | |
455 was_error = 1; | |
456 goto done; | |
457 } | |
1260
80f8c94b5199
Date: 10 Jun 2003 15:30:59 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
458 // 2 Uint32's for chunk header+len, plus the lenread |
80f8c94b5199
Date: 10 Jun 2003 15:30:59 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
459 headerDiff += lenread + 2 * sizeof(Uint32); |
0 | 460 } while ( (chunk.magic == FACT) || (chunk.magic == LIST) ); |
461 | |
462 /* Decode the audio data format */ | |
463 format = (WaveFMT *)chunk.data; | |
464 if ( chunk.magic != FMT ) { | |
465 SDL_SetError("Complex WAVE files not supported"); | |
466 was_error = 1; | |
467 goto done; | |
468 } | |
469 MS_ADPCM_encoded = IMA_ADPCM_encoded = 0; | |
470 switch (SDL_SwapLE16(format->encoding)) { | |
471 case PCM_CODE: | |
472 /* We can understand this */ | |
473 break; | |
474 case MS_ADPCM_CODE: | |
475 /* Try to understand this */ | |
476 if ( InitMS_ADPCM(format) < 0 ) { | |
477 was_error = 1; | |
478 goto done; | |
479 } | |
480 MS_ADPCM_encoded = 1; | |
481 break; | |
482 case IMA_ADPCM_CODE: | |
483 /* Try to understand this */ | |
484 if ( InitIMA_ADPCM(format) < 0 ) { | |
485 was_error = 1; | |
486 goto done; | |
487 } | |
488 IMA_ADPCM_encoded = 1; | |
489 break; | |
490 default: | |
491 SDL_SetError("Unknown WAVE data format: 0x%.4x", | |
492 SDL_SwapLE16(format->encoding)); | |
493 was_error = 1; | |
494 goto done; | |
495 } | |
496 memset(spec, 0, (sizeof *spec)); | |
497 spec->freq = SDL_SwapLE32(format->frequency); | |
498 switch (SDL_SwapLE16(format->bitspersample)) { | |
499 case 4: | |
500 if ( MS_ADPCM_encoded || IMA_ADPCM_encoded ) { | |
501 spec->format = AUDIO_S16; | |
502 } else { | |
503 was_error = 1; | |
504 } | |
505 break; | |
506 case 8: | |
507 spec->format = AUDIO_U8; | |
508 break; | |
509 case 16: | |
510 spec->format = AUDIO_S16; | |
511 break; | |
512 default: | |
513 was_error = 1; | |
514 break; | |
515 } | |
516 if ( was_error ) { | |
517 SDL_SetError("Unknown %d-bit PCM data format", | |
518 SDL_SwapLE16(format->bitspersample)); | |
519 goto done; | |
520 } | |
521 spec->channels = (Uint8)SDL_SwapLE16(format->channels); | |
522 spec->samples = 4096; /* Good default buffer size */ | |
523 | |
524 /* Read the audio data chunk */ | |
525 *audio_buf = NULL; | |
526 do { | |
527 if ( *audio_buf != NULL ) { | |
528 free(*audio_buf); | |
529 } | |
530 lenread = ReadChunk(src, &chunk); | |
531 if ( lenread < 0 ) { | |
532 was_error = 1; | |
533 goto done; | |
534 } | |
535 *audio_len = lenread; | |
536 *audio_buf = chunk.data; | |
1260
80f8c94b5199
Date: 10 Jun 2003 15:30:59 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
537 if(chunk.magic != DATA) headerDiff += lenread + 2 * sizeof(Uint32); |
0 | 538 } while ( chunk.magic != DATA ); |
1260
80f8c94b5199
Date: 10 Jun 2003 15:30:59 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
539 headerDiff += 2 * sizeof(Uint32); // for the data chunk and len |
0 | 540 |
541 if ( MS_ADPCM_encoded ) { | |
542 if ( MS_ADPCM_decode(audio_buf, audio_len) < 0 ) { | |
543 was_error = 1; | |
544 goto done; | |
545 } | |
546 } | |
547 if ( IMA_ADPCM_encoded ) { | |
548 if ( IMA_ADPCM_decode(audio_buf, audio_len) < 0 ) { | |
549 was_error = 1; | |
550 goto done; | |
551 } | |
552 } | |
553 | |
554 /* Don't return a buffer that isn't a multiple of samplesize */ | |
555 samplesize = ((spec->format & 0xFF)/8)*spec->channels; | |
556 *audio_len &= ~(samplesize-1); | |
557 | |
558 done: | |
559 if ( format != NULL ) { | |
560 free(format); | |
561 } | |
562 if ( freesrc && src ) { | |
563 SDL_RWclose(src); | |
564 } | |
1260
80f8c94b5199
Date: 10 Jun 2003 15:30:59 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
565 else { |
80f8c94b5199
Date: 10 Jun 2003 15:30:59 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
566 // seek to the end of the file (given by the RIFF chunk) |
80f8c94b5199
Date: 10 Jun 2003 15:30:59 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
567 SDL_RWseek(src, wavelen - chunk.length - headerDiff, SEEK_CUR); |
80f8c94b5199
Date: 10 Jun 2003 15:30:59 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
568 } |
0 | 569 if ( was_error ) { |
570 spec = NULL; | |
571 } | |
572 return(spec); | |
573 } | |
574 | |
575 /* Since the WAV memory is allocated in the shared library, it must also | |
576 be freed here. (Necessary under Win32, VC++) | |
577 */ | |
578 void SDL_FreeWAV(Uint8 *audio_buf) | |
579 { | |
580 if ( audio_buf != NULL ) { | |
581 free(audio_buf); | |
582 } | |
583 } | |
584 | |
585 static int ReadChunk(SDL_RWops *src, Chunk *chunk) | |
586 { | |
587 chunk->magic = SDL_ReadLE32(src); | |
588 chunk->length = SDL_ReadLE32(src); | |
589 chunk->data = (Uint8 *)malloc(chunk->length); | |
590 if ( chunk->data == NULL ) { | |
591 SDL_Error(SDL_ENOMEM); | |
592 return(-1); | |
593 } | |
594 if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) { | |
595 SDL_Error(SDL_EFREAD); | |
596 free(chunk->data); | |
597 return(-1); | |
598 } | |
599 return(chunk->length); | |
600 } | |
601 | |
602 #endif /* ENABLE_FILE */ |