Mercurial > SDL_sound_CoreAudio
comparison decoders/aiff.c @ 34:938ef560c7bf
Initial add. Thanks, Torbj�rn!
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Thu, 20 Sep 2001 07:51:42 +0000 |
parents | |
children | 0d5ff5679523 |
comparison
equal
deleted
inserted
replaced
33:662bacccfd2c | 34:938ef560c7bf |
---|---|
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 * AIFF decoder for SDL_sound | |
22 * | |
23 * [Insert something profound about the AIFF file format here.] | |
24 * | |
25 * This code was ripped from a decoder I had written for SDL_mixer, which was | |
26 * based on SDL_mixer's old AIFF music loader. (This loader was unfortunately | |
27 * completely broken, but it was still useful because all the pieces were | |
28 * still there, so to speak.) | |
29 * | |
30 * When rewriting it for SDL_sound, I changed its structure to be more like | |
31 * the WAV loader Ryan wrote. Had they not both been part of the same project | |
32 * it would have been embarrassing how similar they are. | |
33 * | |
34 * It is not the most feature-complete AIFF loader the world has ever seen. | |
35 * For instance, it only makes a token attempt at implementing the AIFF-C | |
36 * standard; basically the parts of it that I can easily understand and test. | |
37 * It's a start, though. | |
38 * | |
39 * Please see the file LICENSE in the source's root directory. | |
40 * | |
41 * This file was written by Torbjörn Andersson. (d91tan@Update.UU.SE) | |
42 */ | |
43 | |
44 #include <stdio.h> | |
45 #include <stdlib.h> | |
46 #include <string.h> | |
47 #include <assert.h> | |
48 | |
49 #include "SDL.h" | |
50 #include "SDL_endian.h" | |
51 #include "SDL_sound.h" | |
52 | |
53 #define __SDL_SOUND_INTERNAL__ | |
54 #include "SDL_sound_internal.h" | |
55 | |
56 #if (!defined SOUND_SUPPORTS_AIFF) | |
57 #error SOUND_SUPPORTS_AIFF must be defined. | |
58 #endif | |
59 | |
60 | |
61 static int AIFF_open(Sound_Sample *sample, const char *ext); | |
62 static void AIFF_close(Sound_Sample *sample); | |
63 static Uint32 AIFF_read(Sound_Sample *sample); | |
64 | |
65 const Sound_DecoderFunctions __Sound_DecoderFunctions_AIFF = | |
66 { | |
67 { | |
68 "AIFF", | |
69 "Audio Interchange File Format", | |
70 "Torbjörn Andersson <d91tan@Update.UU.SE>", | |
71 "http://www.icculus.org/SDL_sound/" | |
72 }, | |
73 | |
74 AIFF_open, /* open() method */ | |
75 AIFF_close, /* close() method */ | |
76 AIFF_read /* read() method */ | |
77 }; | |
78 | |
79 | |
80 /* this is what we store in our internal->decoder_private field... */ | |
81 typedef struct { | |
82 Sint32 bytesLeft; | |
83 } aiff_t; | |
84 | |
85 | |
86 /* Chunk management code... */ | |
87 | |
88 #define formID 0x4d524f46 /* "FORM", in ascii. */ | |
89 #define aiffID 0x46464941 /* "AIFF", in ascii. */ | |
90 #define aifcID 0x43464941 /* "AIFC", in ascii. */ | |
91 #define ssndID 0x444e5353 /* "SSND", in ascii. */ | |
92 #define commID 0x4d4d4f43 /* "COMM", in ascii. */ | |
93 | |
94 #define noneID 0x454e4f4e /* "NONE", in ascii. */ | |
95 | |
96 typedef struct | |
97 { | |
98 Uint32 ckID; | |
99 Uint32 ckDataSize; | |
100 Uint16 numChannels; | |
101 Uint32 numSampleFrames; | |
102 Uint16 sampleSize; | |
103 Uint32 sampleRate; | |
104 /* | |
105 * We don't handle AIFF-C compressed audio yet, but for those | |
106 * interested the allowed compression types are supposed to be | |
107 * | |
108 * compressionType compressionName meaning | |
109 * --------------------------------------------------------------- | |
110 * 'NONE' "not compressed" uncompressed, that is, | |
111 * straight digitized samples | |
112 * 'ACE2' "ACE 2-to-1" 2-to-1 IIGS ACE (Audio | |
113 * Compression / Expansion) | |
114 * 'ACE8' "ACE 8-to-3" 8-to-3 IIGS ACE (Audio | |
115 * Compression / Expansion) | |
116 * 'MAC3' "MACE 3-to-1" 3-to-1 Macintosh Audio | |
117 * Compression / Expansion | |
118 * 'MAC6' "MACE 6-to-1" 6-to-1 Macintosh Audio | |
119 * Compression / Expansion | |
120 * | |
121 * A pstring is a "Pascal-style string", that is, "one byte followed | |
122 * by test bytes followed when needed by one pad byte. The total | |
123 * number of bytes in a pstring must be even. The pad byte is | |
124 * included when the number of text bytes is even, so the total of | |
125 * text bytes + one count byte + one pad byte will be even. This pad | |
126 * byte is not reflected in the count." | |
127 * | |
128 * As for how these compression algorithms work, your guess is as | |
129 * good as mine. | |
130 */ | |
131 Uint32 compressionType; | |
132 #if 0 | |
133 pstring compressionName; | |
134 #endif | |
135 } comm_t; | |
136 | |
137 | |
138 /* | |
139 * Sample rate is encoded as an "80 bit IEEE Standard 754 floating point | |
140 * number (Standard Apple Numeric Environment [SANE] data type Extended)". | |
141 * Whose bright idea was that? | |
142 * | |
143 * This function was adapted from libsndfile, and while I do know a little | |
144 * bit about the IEEE floating point standard I don't pretend to fully | |
145 * understand this. | |
146 */ | |
147 | |
148 static Uint32 SANE_to_Uint32 (Uint8 *sanebuf) | |
149 { | |
150 /* Is the frequency outside of what we can represent with Uint32? */ | |
151 if ( (sanebuf[0] & 0x80) | |
152 || (sanebuf[0] <= 0x3F) | |
153 || (sanebuf[0] > 0x40) | |
154 || (sanebuf[0] == 0x40 && sanebuf[1] > 0x1C) ) | |
155 return 0; | |
156 | |
157 return ((sanebuf[2] << 23) | (sanebuf[3] << 15) | (sanebuf[4] << 7) | |
158 | (sanebuf[5] >> 1)) >> (29 - sanebuf[1]); | |
159 } /* SANE_to_Uint32 */ | |
160 | |
161 | |
162 /* | |
163 * Read in a comm_t from disk. This makes this process safe regardless of | |
164 * the processor's byte order or how the comm_t structure is packed. | |
165 */ | |
166 | |
167 static int read_comm_chunk(SDL_RWops *rw, comm_t *comm) | |
168 { | |
169 Uint8 sampleRate[10]; | |
170 | |
171 /* skip reading the chunk ID, since it was already read at this point... */ | |
172 comm->ckID = commID; | |
173 | |
174 if (SDL_RWread(rw, &comm->ckDataSize, sizeof (comm->ckDataSize), 1) != 1) | |
175 return(0); | |
176 comm->ckDataSize = SDL_SwapBE32(comm->ckDataSize); | |
177 | |
178 if (SDL_RWread(rw, &comm->numChannels, sizeof (comm->numChannels), 1) != 1) | |
179 return(0); | |
180 comm->numChannels = SDL_SwapBE16(comm->numChannels); | |
181 | |
182 if (SDL_RWread(rw, &comm->numSampleFrames, sizeof (comm->numSampleFrames), 1) != 1) | |
183 return(0); | |
184 comm->numSampleFrames = SDL_SwapBE32(comm->numSampleFrames); | |
185 | |
186 if (SDL_RWread(rw, &comm->sampleSize, sizeof (comm->sampleSize), 1) != 1) | |
187 return(0); | |
188 comm->sampleSize = SDL_SwapBE16(comm->sampleSize); | |
189 | |
190 if (SDL_RWread(rw, sampleRate, sizeof(sampleRate), 1) != 1) | |
191 return(0); | |
192 comm->sampleRate = SANE_to_Uint32(sampleRate); | |
193 | |
194 if (comm->ckDataSize > sizeof(comm->numChannels) | |
195 + sizeof(comm->numSampleFrames) | |
196 + sizeof(comm->sampleSize) | |
197 + sizeof(sampleRate)) | |
198 { | |
199 if (SDL_RWread(rw, &comm->compressionType, sizeof (comm->compressionType), 1) != 1) | |
200 return(0); | |
201 comm->compressionType = SDL_SwapBE32(comm->compressionType); | |
202 } /* if */ | |
203 else | |
204 comm->compressionType = noneID; | |
205 | |
206 return(1); | |
207 } /* read_comm_chunk */ | |
208 | |
209 typedef struct | |
210 { | |
211 Uint32 ckID; | |
212 Uint32 ckDataSize; | |
213 Uint32 offset; | |
214 Uint32 blockSize; | |
215 /* | |
216 * Then, comm->numSampleFrames sample frames. (It's better to get the | |
217 * length from numSampleFrames than from ckDataSize.) | |
218 */ | |
219 } ssnd_t; | |
220 | |
221 | |
222 static int read_ssnd_chunk(SDL_RWops *rw, ssnd_t *ssnd) | |
223 { | |
224 /* skip reading the chunk ID, since it was already read at this point... */ | |
225 ssnd->ckID = ssndID; | |
226 | |
227 if (SDL_RWread(rw, &ssnd->ckDataSize, sizeof (ssnd->ckDataSize), 1) != 1) | |
228 return(0); | |
229 ssnd->ckDataSize = SDL_SwapBE32(ssnd->ckDataSize); | |
230 | |
231 if (SDL_RWread(rw, &ssnd->offset, sizeof(ssnd->offset), 1) != 1) | |
232 return(0); | |
233 ssnd->offset = SDL_SwapBE32(ssnd->offset); | |
234 | |
235 if (SDL_RWread(rw, &ssnd->blockSize, sizeof(ssnd->blockSize), 1) != 1) | |
236 return(0); | |
237 ssnd->blockSize = SDL_SwapBE32(ssnd->blockSize); | |
238 | |
239 /* Leave the SDL_RWops position indicator at the start of the samples */ | |
240 if (SDL_RWseek(rw, (int) ssnd->offset, SEEK_CUR) == -1) /* !!! FIXME: Int? Really? */ | |
241 return(0); | |
242 | |
243 return(1); | |
244 } /* read_ssnd_chunk */ | |
245 | |
246 | |
247 static int find_chunk(SDL_RWops *rw, Uint32 id) | |
248 { | |
249 Sint32 siz = 0; | |
250 Uint32 _id = 0; | |
251 | |
252 while (1) | |
253 { | |
254 BAIL_IF_MACRO(SDL_RWread(rw, &_id, sizeof (_id), 1) != 1, NULL, 0); | |
255 if (SDL_SwapLE32(_id) == id) | |
256 return(1); | |
257 | |
258 BAIL_IF_MACRO(SDL_RWread(rw, &siz, sizeof (siz), 1) != 1, NULL, 0); | |
259 siz = SDL_SwapBE32(siz); | |
260 assert(siz > 0); | |
261 BAIL_IF_MACRO(SDL_RWseek(rw, siz, SEEK_CUR) == -1, NULL, 0); | |
262 } /* while */ | |
263 | |
264 return(0); /* shouldn't hit this, but just in case... */ | |
265 } /* find_chunk */ | |
266 | |
267 | |
268 static int AIFF_open(Sound_Sample *sample, const char *ext) | |
269 { | |
270 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
271 SDL_RWops *rw = internal->rw; | |
272 Uint32 chunk_id; | |
273 int bytes_per_sample; | |
274 long pos; | |
275 comm_t c; | |
276 ssnd_t s; | |
277 aiff_t *a; | |
278 | |
279 BAIL_IF_MACRO(SDL_ReadLE32(rw) != formID, "AIFF: Not a FORM file.", 0); | |
280 SDL_ReadBE32(rw); /* throw the length away; we don't need it. */ | |
281 | |
282 chunk_id = SDL_ReadLE32(rw); | |
283 BAIL_IF_MACRO(chunk_id != aiffID && chunk_id != aifcID, | |
284 "AIFF: Not an AIFF or AIFC file.", 0); | |
285 | |
286 /* Chunks may appear in any order, so we establish base camp here. */ | |
287 pos = SDL_RWtell(rw); | |
288 | |
289 BAIL_IF_MACRO(!find_chunk(rw, commID), "AIFF: No common chunk.", 0); | |
290 BAIL_IF_MACRO(!read_comm_chunk(rw, &c), "AIFF: Can't read common chunk.", 0); | |
291 | |
292 /* !!! FIXME: This will have to change for compression types... */ | |
293 BAIL_IF_MACRO(c.compressionType != noneID, "AIFF: Unsupported encoding.", 0); | |
294 | |
295 BAIL_IF_MACRO(c.sampleRate == 0, "AIFF: Unsupported sample rate.", 0); | |
296 | |
297 sample->actual.channels = (Uint8) c.numChannels; | |
298 sample->actual.rate = c.sampleRate; | |
299 | |
300 if (c.sampleSize <= 8) | |
301 { | |
302 sample->actual.format = AUDIO_S8; | |
303 bytes_per_sample = 1; | |
304 } /* if */ | |
305 else if (c.sampleSize <= 16) | |
306 { | |
307 sample->actual.format = AUDIO_S16MSB; | |
308 bytes_per_sample = 2; | |
309 } /* if */ | |
310 else | |
311 BAIL_MACRO("AIFF: Unsupported sample size.", 0); | |
312 | |
313 SDL_RWseek(rw, pos, SEEK_SET); | |
314 | |
315 BAIL_IF_MACRO(!find_chunk(rw, ssndID), "AIFF: No sound data chunk.", 0); | |
316 BAIL_IF_MACRO(!read_ssnd_chunk(rw, &s), "AIFF: Can't read sound data chunk.", 0); | |
317 | |
318 a = (aiff_t *) malloc(sizeof(aiff_t)); | |
319 BAIL_IF_MACRO(a == NULL, ERR_OUT_OF_MEMORY, 0); | |
320 a->bytesLeft = bytes_per_sample * c.numSampleFrames; | |
321 internal->decoder_private = (void *) a; | |
322 | |
323 _D(("AIFF: Accepting data stream.\n")); | |
324 return(1); /* we'll handle this data. */ | |
325 } /* AIFF_open */ | |
326 | |
327 | |
328 static void AIFF_close(Sound_Sample *sample) | |
329 { | |
330 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
331 free(internal->decoder_private); | |
332 } /* WAV_close */ | |
333 | |
334 | |
335 static Uint32 AIFF_read(Sound_Sample *sample) | |
336 { | |
337 Uint32 retval; | |
338 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
339 aiff_t *a = (aiff_t *) internal->decoder_private; | |
340 Uint32 max = (internal->buffer_size < (Uint32) a->bytesLeft) ? | |
341 internal->buffer_size : (Uint32) a->bytesLeft; | |
342 | |
343 assert(max > 0); | |
344 | |
345 /* | |
346 * We don't actually do any decoding, so we read the AIFF data | |
347 * directly into the internal buffer... | |
348 */ | |
349 retval = SDL_RWread(internal->rw, internal->buffer, 1, max); | |
350 | |
351 a->bytesLeft -= retval; | |
352 | |
353 /* Make sure the read went smoothly... */ | |
354 if ((retval == 0) || (a->bytesLeft == 0)) | |
355 sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
356 | |
357 else if (retval == -1) | |
358 sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
359 | |
360 /* (next call this EAGAIN may turn into an EOF or error.) */ | |
361 else if (retval < internal->buffer_size) | |
362 sample->flags |= SOUND_SAMPLEFLAG_EAGAIN; | |
363 | |
364 return(retval); | |
365 } /* AIFF_read */ | |
366 | |
367 | |
368 /* end of aiff.c ... */ |