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