Mercurial > SDL_sound_CoreAudio
annotate decoders/wav.c @ 40:c15396fc0e55
Fixed incorrect comment, by Torbj�rn Andersson. :)
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Thu, 20 Sep 2001 07:57:52 +0000 |
parents | 102cf61c0816 |
children | ea58bc3b15d7 |
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 | |
31 #include <stdio.h> | |
32 #include <stdlib.h> | |
33 #include <string.h> | |
34 #include <assert.h> | |
35 #include "SDL_sound.h" | |
36 | |
37 #define __SDL_SOUND_INTERNAL__ | |
38 #include "SDL_sound_internal.h" | |
39 | |
40 #if (!defined SOUND_SUPPORTS_WAV) | |
41 #error SOUND_SUPPORTS_WAV must be defined. | |
42 #endif | |
43 | |
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 | |
57 WAV_open, /* open() method */ | |
58 WAV_close, /* close() method */ | |
59 WAV_read /* read() method */ | |
60 }; | |
61 | |
62 | |
63 /* this is what we store in our internal->decoder_private field... */ | |
64 typedef struct | |
65 { | |
66 Sint32 bytesLeft; | |
67 } wav_t; | |
68 | |
69 | |
70 /* Chunk management code... */ | |
71 | |
72 #define riffID 0x46464952 /* "RIFF", in ascii. */ | |
73 #define waveID 0x45564157 /* "WAVE", in ascii. */ | |
74 | |
75 | |
76 #define fmtID 0x20746D66 /* "fmt ", in ascii. */ | |
77 | |
78 #define FMT_NORMAL 1 /* Uncompressed waveform data. */ | |
79 | |
80 typedef struct | |
81 { | |
82 Uint32 chunkID; | |
83 Sint32 chunkSize; | |
84 Sint16 wFormatTag; | |
85 Uint16 wChannels; | |
86 Uint32 dwSamplesPerSec; | |
87 Uint32 dwAvgBytesPerSec; | |
88 Uint16 wBlockAlign; | |
89 Uint16 wBitsPerSample; | |
90 } fmt_t; | |
91 | |
92 | |
93 /* | |
94 * Read in a fmt_t from disk. This makes this process safe regardless of | |
95 * the processor's byte order or how the fmt_t structure is packed. | |
96 */ | |
97 static int read_fmt_chunk(SDL_RWops *rw, fmt_t *fmt) | |
98 { | |
99 /* skip reading the chunk ID, since it was already read at this point... */ | |
100 fmt->chunkID = fmtID; | |
101 | |
102 if (SDL_RWread(rw, &fmt->chunkSize, sizeof (fmt->chunkSize), 1) != 1) | |
103 return(0); | |
104 fmt->chunkSize = SDL_SwapLE32(fmt->chunkSize); | |
105 | |
106 if (SDL_RWread(rw, &fmt->wFormatTag, sizeof (fmt->wFormatTag), 1) != 1) | |
107 return(0); | |
108 fmt->wFormatTag = SDL_SwapLE16(fmt->wFormatTag); | |
109 | |
110 if (SDL_RWread(rw, &fmt->wChannels, sizeof (fmt->wChannels), 1) != 1) | |
111 return(0); | |
112 fmt->wChannels = SDL_SwapLE16(fmt->wChannels); | |
113 | |
114 if (SDL_RWread(rw, &fmt->dwSamplesPerSec, | |
115 sizeof (fmt->dwSamplesPerSec), 1) != 1) | |
116 return(0); | |
117 fmt->dwSamplesPerSec = SDL_SwapLE32(fmt->dwSamplesPerSec); | |
118 | |
119 if (SDL_RWread(rw, &fmt->dwAvgBytesPerSec, | |
120 sizeof (fmt->dwAvgBytesPerSec), 1) != 1) | |
121 return(0); | |
122 fmt->dwAvgBytesPerSec = SDL_SwapLE32(fmt->dwAvgBytesPerSec); | |
123 | |
124 if (SDL_RWread(rw, &fmt->wBlockAlign, sizeof (fmt->wBlockAlign), 1) != 1) | |
125 return(0); | |
126 fmt->wBlockAlign = SDL_SwapLE16(fmt->wBlockAlign); | |
127 | |
128 if (SDL_RWread(rw, &fmt->wBitsPerSample, | |
129 sizeof (fmt->wBitsPerSample), 1) != 1) | |
130 return(0); | |
131 fmt->wBitsPerSample = SDL_SwapLE16(fmt->wBitsPerSample); | |
132 | |
133 return(1); | |
134 } /* read_fmt_chunk */ | |
135 | |
136 | |
137 #define dataID 0x61746164 /* "data", in ascii. */ | |
138 | |
139 typedef struct | |
140 { | |
141 Uint32 chunkID; | |
142 Sint32 chunkSize; | |
143 /* Then, (chunkSize) bytes of waveform data... */ | |
144 } data_t; | |
145 | |
146 | |
147 /* | |
148 * Read in a fmt_t from disk. This makes this process safe regardless of | |
149 * the processor's byte order or how the fmt_t structure is packed. | |
150 */ | |
151 static int read_data_chunk(SDL_RWops *rw, data_t *data) | |
152 { | |
153 /* skip reading the chunk ID, since it was already read at this point... */ | |
154 data->chunkID = dataID; | |
155 | |
156 if (SDL_RWread(rw, &data->chunkSize, sizeof (data->chunkSize), 1) != 1) | |
157 return(0); | |
158 data->chunkSize = SDL_SwapLE32(data->chunkSize); | |
159 | |
160 return(1); | |
40
c15396fc0e55
Fixed incorrect comment, by Torbj�rn Andersson. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
17
diff
changeset
|
161 } /* read_data_chunk */ |
17 | 162 |
163 | |
164 static int find_chunk(SDL_RWops *rw, Uint32 id) | |
165 { | |
166 Sint32 siz = 0; | |
167 Uint32 _id = 0; | |
168 | |
169 while (1) | |
170 { | |
171 BAIL_IF_MACRO(SDL_RWread(rw, &_id, sizeof (_id), 1) != 1, NULL, 0); | |
172 if (SDL_SwapLE32(_id) == id) | |
173 return(1); | |
174 | |
175 BAIL_IF_MACRO(SDL_RWread(rw, &siz, sizeof (siz), 1) != 1, NULL, 0); | |
176 siz = SDL_SwapLE32(siz); | |
177 assert(siz > 0); | |
178 BAIL_IF_MACRO(SDL_RWseek(rw, siz, SEEK_SET) != siz, NULL, 0); | |
179 } /* while */ | |
180 | |
181 return(0); /* shouldn't hit this, but just in case... */ | |
182 } /* find_chunk */ | |
183 | |
184 | |
185 static int WAV_open(Sound_Sample *sample, const char *ext) | |
186 { | |
187 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
188 SDL_RWops *rw = internal->rw; | |
189 fmt_t f; | |
190 data_t d; | |
191 wav_t *w; | |
192 | |
193 BAIL_IF_MACRO(SDL_ReadLE32(rw) != riffID, "WAV: Not a RIFF file.", 0); | |
194 SDL_ReadLE32(rw); /* throw the length away; we get this info later. */ | |
195 BAIL_IF_MACRO(SDL_ReadLE32(rw) != waveID, "WAV: Not a WAVE file.", 0); | |
196 BAIL_IF_MACRO(!find_chunk(rw, fmtID), "WAV: No format chunk.", 0); | |
197 BAIL_IF_MACRO(!read_fmt_chunk(rw, &f), "WAV: Can't read format chunk.", 0); | |
198 | |
199 /* !!! FIXME: This will have to change for compression types... */ | |
200 BAIL_IF_MACRO(f.wFormatTag != FMT_NORMAL, "WAV: Unsupported encoding.", 0); | |
201 | |
202 sample->actual.channels = (Uint8) f.wChannels; | |
203 sample->actual.rate = f.dwSamplesPerSec; | |
204 | |
205 if (f.wBitsPerSample <= 8) | |
206 sample->actual.format = AUDIO_U8; | |
207 else if (f.wBitsPerSample <= 16) | |
208 sample->actual.format = AUDIO_S16LSB; | |
209 else | |
210 BAIL_MACRO("WAV: Unsupported sample size.", 0); | |
211 | |
212 BAIL_IF_MACRO(!find_chunk(rw, dataID), "WAV: No data chunk.", 0); | |
213 BAIL_IF_MACRO(!read_data_chunk(rw, &d), "WAV: Can't read data chunk.", 0); | |
214 | |
215 w = (wav_t *) malloc(sizeof(wav_t)); | |
216 BAIL_IF_MACRO(w == NULL, ERR_OUT_OF_MEMORY, 0); | |
217 w->bytesLeft = d.chunkSize; | |
218 internal->decoder_private = (void *) w; | |
219 | |
220 sample->flags = SOUND_SAMPLEFLAG_NONE; | |
221 | |
222 _D(("WAV: Accepting data stream.\n")); | |
223 return(1); /* we'll handle this data. */ | |
224 } /* WAV_open */ | |
225 | |
226 | |
227 static void WAV_close(Sound_Sample *sample) | |
228 { | |
229 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
230 free(internal->decoder_private); | |
231 } /* WAV_close */ | |
232 | |
233 | |
234 static Uint32 WAV_read(Sound_Sample *sample) | |
235 { | |
236 Uint32 retval; | |
237 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
238 wav_t *w = (wav_t *) internal->decoder_private; | |
239 Uint32 max = (internal->buffer_size < (Uint32) w->bytesLeft) ? | |
240 internal->buffer_size : (Uint32) w->bytesLeft; | |
241 | |
242 assert(max > 0); | |
243 | |
244 /* | |
245 * We don't actually do any decoding, so we read the wav data | |
246 * directly into the internal buffer... | |
247 */ | |
248 retval = SDL_RWread(internal->rw, internal->buffer, 1, max); | |
249 | |
250 w->bytesLeft -= retval; | |
251 | |
252 /* Make sure the read went smoothly... */ | |
253 if ((retval == 0) || (w->bytesLeft == 0)) | |
254 sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
255 | |
256 else if (retval == -1) | |
257 sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
258 | |
259 /* (next call this EAGAIN may turn into an EOF or error.) */ | |
260 else if (retval < internal->buffer_size) | |
261 sample->flags |= SOUND_SAMPLEFLAG_EAGAIN; | |
262 | |
263 return(retval); | |
264 } /* WAV_read */ | |
265 | |
266 | |
267 /* end of wav.c ... */ | |
268 |