Mercurial > SDL_sound_CoreAudio
annotate decoders/wav.c @ 90:a292285ae155
Added shn module.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Tue, 25 Sep 2001 22:48:24 +0000 |
parents | 40006625142a |
children | 6d9fdec2f708 |
rev | line source |
---|---|
17 | 1 /* |
2 * SDL_sound -- An abstract sound format decoding API. | |
3 * Copyright (C) 2001 Ryan C. Gordon. | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2.1 of the License, or (at your option) any later version. | |
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 | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 */ | |
19 | |
20 /* | |
21 * WAV decoder for SDL_sound. | |
22 * | |
23 * This driver handles Microsoft .WAVs, in as many of the thousands of | |
24 * variations as we can. | |
25 * | |
26 * Please see the file LICENSE in the source's root directory. | |
27 * | |
28 * This file written by Ryan C. Gordon. (icculus@clutteredmind.org) | |
29 */ | |
30 | |
64
40006625142a
Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents:
62
diff
changeset
|
31 #ifdef SOUND_SUPPORTS_WAV |
40006625142a
Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents:
62
diff
changeset
|
32 |
17 | 33 #include <stdio.h> |
34 #include <stdlib.h> | |
35 #include <string.h> | |
36 #include <assert.h> | |
37 #include "SDL_sound.h" | |
38 | |
39 #define __SDL_SOUND_INTERNAL__ | |
40 #include "SDL_sound_internal.h" | |
41 | |
47
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
40
diff
changeset
|
42 static int WAV_init(void); |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
40
diff
changeset
|
43 static void WAV_quit(void); |
17 | 44 static int WAV_open(Sound_Sample *sample, const char *ext); |
45 static void WAV_close(Sound_Sample *sample); | |
46 static Uint32 WAV_read(Sound_Sample *sample); | |
47 | |
48 const Sound_DecoderFunctions __Sound_DecoderFunctions_WAV = | |
49 { | |
50 { | |
51 "WAV", | |
52 "Microsoft WAVE audio format", | |
53 "Ryan C. Gordon <icculus@clutteredmind.org>", | |
54 "http://www.icculus.org/SDL_sound/" | |
55 }, | |
56 | |
47
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
40
diff
changeset
|
57 WAV_init, /* init() method */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
40
diff
changeset
|
58 WAV_quit, /* quit() method */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
40
diff
changeset
|
59 WAV_open, /* open() method */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
40
diff
changeset
|
60 WAV_close, /* close() method */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
40
diff
changeset
|
61 WAV_read /* read() method */ |
17 | 62 }; |
63 | |
64 | |
65 /* this is what we store in our internal->decoder_private field... */ | |
66 typedef struct | |
67 { | |
68 Sint32 bytesLeft; | |
69 } wav_t; | |
70 | |
71 | |
72 /* Chunk management code... */ | |
73 | |
74 #define riffID 0x46464952 /* "RIFF", in ascii. */ | |
75 #define waveID 0x45564157 /* "WAVE", in ascii. */ | |
76 | |
77 | |
78 #define fmtID 0x20746D66 /* "fmt ", in ascii. */ | |
79 | |
80 #define FMT_NORMAL 1 /* Uncompressed waveform data. */ | |
81 | |
82 typedef struct | |
83 { | |
84 Uint32 chunkID; | |
85 Sint32 chunkSize; | |
86 Sint16 wFormatTag; | |
87 Uint16 wChannels; | |
88 Uint32 dwSamplesPerSec; | |
89 Uint32 dwAvgBytesPerSec; | |
90 Uint16 wBlockAlign; | |
91 Uint16 wBitsPerSample; | |
92 } fmt_t; | |
93 | |
94 | |
47
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
40
diff
changeset
|
95 static int WAV_init(void) |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
40
diff
changeset
|
96 { |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
40
diff
changeset
|
97 return(1); /* always succeeds. */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
40
diff
changeset
|
98 } /* WAV_init */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
40
diff
changeset
|
99 |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
40
diff
changeset
|
100 |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
40
diff
changeset
|
101 static void WAV_quit(void) |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
40
diff
changeset
|
102 { |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
40
diff
changeset
|
103 /* it's a no-op. */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
40
diff
changeset
|
104 } /* WAV_quit */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
40
diff
changeset
|
105 |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
40
diff
changeset
|
106 |
17 | 107 /* |
108 * Read in a fmt_t from disk. This makes this process safe regardless of | |
109 * the processor's byte order or how the fmt_t structure is packed. | |
110 */ | |
111 static int read_fmt_chunk(SDL_RWops *rw, fmt_t *fmt) | |
112 { | |
113 /* skip reading the chunk ID, since it was already read at this point... */ | |
114 fmt->chunkID = fmtID; | |
115 | |
116 if (SDL_RWread(rw, &fmt->chunkSize, sizeof (fmt->chunkSize), 1) != 1) | |
117 return(0); | |
118 fmt->chunkSize = SDL_SwapLE32(fmt->chunkSize); | |
119 | |
120 if (SDL_RWread(rw, &fmt->wFormatTag, sizeof (fmt->wFormatTag), 1) != 1) | |
121 return(0); | |
122 fmt->wFormatTag = SDL_SwapLE16(fmt->wFormatTag); | |
123 | |
124 if (SDL_RWread(rw, &fmt->wChannels, sizeof (fmt->wChannels), 1) != 1) | |
125 return(0); | |
126 fmt->wChannels = SDL_SwapLE16(fmt->wChannels); | |
127 | |
128 if (SDL_RWread(rw, &fmt->dwSamplesPerSec, | |
129 sizeof (fmt->dwSamplesPerSec), 1) != 1) | |
130 return(0); | |
131 fmt->dwSamplesPerSec = SDL_SwapLE32(fmt->dwSamplesPerSec); | |
132 | |
133 if (SDL_RWread(rw, &fmt->dwAvgBytesPerSec, | |
134 sizeof (fmt->dwAvgBytesPerSec), 1) != 1) | |
135 return(0); | |
136 fmt->dwAvgBytesPerSec = SDL_SwapLE32(fmt->dwAvgBytesPerSec); | |
137 | |
138 if (SDL_RWread(rw, &fmt->wBlockAlign, sizeof (fmt->wBlockAlign), 1) != 1) | |
139 return(0); | |
140 fmt->wBlockAlign = SDL_SwapLE16(fmt->wBlockAlign); | |
141 | |
142 if (SDL_RWread(rw, &fmt->wBitsPerSample, | |
143 sizeof (fmt->wBitsPerSample), 1) != 1) | |
144 return(0); | |
145 fmt->wBitsPerSample = SDL_SwapLE16(fmt->wBitsPerSample); | |
146 | |
147 return(1); | |
148 } /* read_fmt_chunk */ | |
149 | |
150 | |
151 #define dataID 0x61746164 /* "data", in ascii. */ | |
152 | |
153 typedef struct | |
154 { | |
155 Uint32 chunkID; | |
156 Sint32 chunkSize; | |
157 /* Then, (chunkSize) bytes of waveform data... */ | |
158 } data_t; | |
159 | |
160 | |
161 /* | |
162 * Read in a fmt_t from disk. This makes this process safe regardless of | |
163 * the processor's byte order or how the fmt_t structure is packed. | |
164 */ | |
165 static int read_data_chunk(SDL_RWops *rw, data_t *data) | |
166 { | |
167 /* skip reading the chunk ID, since it was already read at this point... */ | |
168 data->chunkID = dataID; | |
169 | |
170 if (SDL_RWread(rw, &data->chunkSize, sizeof (data->chunkSize), 1) != 1) | |
171 return(0); | |
172 data->chunkSize = SDL_SwapLE32(data->chunkSize); | |
173 | |
174 return(1); | |
40
c15396fc0e55
Fixed incorrect comment, by Torbj�rn Andersson. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
17
diff
changeset
|
175 } /* read_data_chunk */ |
17 | 176 |
177 | |
178 static int find_chunk(SDL_RWops *rw, Uint32 id) | |
179 { | |
180 Sint32 siz = 0; | |
181 Uint32 _id = 0; | |
182 | |
183 while (1) | |
184 { | |
185 BAIL_IF_MACRO(SDL_RWread(rw, &_id, sizeof (_id), 1) != 1, NULL, 0); | |
186 if (SDL_SwapLE32(_id) == id) | |
187 return(1); | |
188 | |
189 BAIL_IF_MACRO(SDL_RWread(rw, &siz, sizeof (siz), 1) != 1, NULL, 0); | |
190 siz = SDL_SwapLE32(siz); | |
191 assert(siz > 0); | |
192 BAIL_IF_MACRO(SDL_RWseek(rw, siz, SEEK_SET) != siz, NULL, 0); | |
193 } /* while */ | |
194 | |
195 return(0); /* shouldn't hit this, but just in case... */ | |
196 } /* find_chunk */ | |
197 | |
198 | |
199 static int WAV_open(Sound_Sample *sample, const char *ext) | |
200 { | |
201 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
202 SDL_RWops *rw = internal->rw; | |
203 fmt_t f; | |
204 data_t d; | |
205 wav_t *w; | |
206 | |
207 BAIL_IF_MACRO(SDL_ReadLE32(rw) != riffID, "WAV: Not a RIFF file.", 0); | |
208 SDL_ReadLE32(rw); /* throw the length away; we get this info later. */ | |
209 BAIL_IF_MACRO(SDL_ReadLE32(rw) != waveID, "WAV: Not a WAVE file.", 0); | |
210 BAIL_IF_MACRO(!find_chunk(rw, fmtID), "WAV: No format chunk.", 0); | |
211 BAIL_IF_MACRO(!read_fmt_chunk(rw, &f), "WAV: Can't read format chunk.", 0); | |
212 | |
213 /* !!! FIXME: This will have to change for compression types... */ | |
214 BAIL_IF_MACRO(f.wFormatTag != FMT_NORMAL, "WAV: Unsupported encoding.", 0); | |
215 | |
216 sample->actual.channels = (Uint8) f.wChannels; | |
217 sample->actual.rate = f.dwSamplesPerSec; | |
218 | |
219 if (f.wBitsPerSample <= 8) | |
220 sample->actual.format = AUDIO_U8; | |
221 else if (f.wBitsPerSample <= 16) | |
222 sample->actual.format = AUDIO_S16LSB; | |
223 else | |
224 BAIL_MACRO("WAV: Unsupported sample size.", 0); | |
225 | |
226 BAIL_IF_MACRO(!find_chunk(rw, dataID), "WAV: No data chunk.", 0); | |
227 BAIL_IF_MACRO(!read_data_chunk(rw, &d), "WAV: Can't read data chunk.", 0); | |
228 | |
229 w = (wav_t *) malloc(sizeof(wav_t)); | |
230 BAIL_IF_MACRO(w == NULL, ERR_OUT_OF_MEMORY, 0); | |
231 w->bytesLeft = d.chunkSize; | |
232 internal->decoder_private = (void *) w; | |
233 | |
234 sample->flags = SOUND_SAMPLEFLAG_NONE; | |
235 | |
62
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
236 SNDDBG(("WAV: Accepting data stream.\n")); |
17 | 237 return(1); /* we'll handle this data. */ |
238 } /* WAV_open */ | |
239 | |
240 | |
241 static void WAV_close(Sound_Sample *sample) | |
242 { | |
243 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
244 free(internal->decoder_private); | |
245 } /* WAV_close */ | |
246 | |
247 | |
248 static Uint32 WAV_read(Sound_Sample *sample) | |
249 { | |
250 Uint32 retval; | |
251 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
252 wav_t *w = (wav_t *) internal->decoder_private; | |
253 Uint32 max = (internal->buffer_size < (Uint32) w->bytesLeft) ? | |
254 internal->buffer_size : (Uint32) w->bytesLeft; | |
255 | |
256 assert(max > 0); | |
257 | |
258 /* | |
259 * We don't actually do any decoding, so we read the wav data | |
260 * directly into the internal buffer... | |
261 */ | |
262 retval = SDL_RWread(internal->rw, internal->buffer, 1, max); | |
263 | |
264 w->bytesLeft -= retval; | |
265 | |
266 /* Make sure the read went smoothly... */ | |
267 if ((retval == 0) || (w->bytesLeft == 0)) | |
268 sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
269 | |
270 else if (retval == -1) | |
271 sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
272 | |
273 /* (next call this EAGAIN may turn into an EOF or error.) */ | |
274 else if (retval < internal->buffer_size) | |
275 sample->flags |= SOUND_SAMPLEFLAG_EAGAIN; | |
276 | |
277 return(retval); | |
278 } /* WAV_read */ | |
279 | |
64
40006625142a
Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents:
62
diff
changeset
|
280 #endif /* SOUND_SUPPORTS_WAV */ |
17 | 281 |
282 /* end of wav.c ... */ | |
283 |