Mercurial > SDL_sound_CoreAudio
annotate decoders/au.c @ 252:c54eae85f5f1
Upped version to 0.1.5.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Fri, 08 Feb 2002 01:42:00 +0000 |
parents | c9772a9f5271 |
children | 7c4f6ee02cd0 |
rev | line source |
---|---|
213 | 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 * Sun/NeXT .au decoder for SDL_sound. | |
22 * Formats supported: 8 and 16 bit linear PCM, 8 bit µ-law. | |
23 * Files without valid header are assumed to be 8 bit µ-law, 8kHz, mono. | |
24 * | |
25 * Please see the file COPYING in the source's root directory. | |
26 * | |
27 * This file written by Mattias Engdegård. (f91-men@nada.kth.se) | |
28 */ | |
29 | |
30 #if HAVE_CONFIG_H | |
31 # include <config.h> | |
32 #endif | |
33 | |
34 #ifdef SOUND_SUPPORTS_AU | |
35 | |
36 #include <stdio.h> | |
37 #include <stdlib.h> | |
38 #include <string.h> | |
39 #include <assert.h> | |
40 | |
41 #include "SDL_sound.h" | |
42 | |
43 #define __SDL_SOUND_INTERNAL__ | |
44 #include "SDL_sound_internal.h" | |
45 | |
46 static int AU_init(void); | |
47 static void AU_quit(void); | |
48 static int AU_open(Sound_Sample *sample, const char *ext); | |
49 static void AU_close(Sound_Sample *sample); | |
50 static Uint32 AU_read(Sound_Sample *sample); | |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
51 static int AU_rewind(Sound_Sample *sample); |
213 | 52 |
53 /* | |
54 * Sometimes the extension ".snd" is used for these files (mostly on the NeXT), | |
55 * and the magic number comes from this. However it may clash with other | |
56 * formats and is somewhat of an anachronism, so only .au is used here. | |
57 */ | |
58 static const char *extensions_au[] = { "AU", NULL }; | |
59 const Sound_DecoderFunctions __Sound_DecoderFunctions_AU = | |
60 { | |
61 { | |
62 extensions_au, | |
63 "Sun/NeXT audio file format", | |
64 "Mattias Engdegård <f91-men@nada.kth.se>", | |
65 "http://www.icculus.org/SDL_sound/" | |
66 }, | |
67 | |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
68 AU_init, /* init() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
69 AU_quit, /* quit() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
70 AU_open, /* open() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
71 AU_close, /* close() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
72 AU_read, /* read() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
73 AU_rewind /* rewind() method */ |
213 | 74 }; |
75 | |
76 /* no init/deinit needed */ | |
77 static int AU_init(void) | |
78 { | |
79 return(1); | |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
80 } /* AU_init */ |
213 | 81 |
82 static void AU_quit(void) | |
83 { | |
84 /* no-op. */ | |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
85 } /* AU_quit */ |
213 | 86 |
87 struct au_file_hdr | |
88 { | |
89 Uint32 magic; | |
90 Uint32 hdr_size; | |
91 Uint32 data_size; | |
92 Uint32 encoding; | |
93 Uint32 sample_rate; | |
94 Uint32 channels; | |
95 }; | |
96 | |
97 #define HDR_SIZE 24 | |
98 | |
99 enum | |
100 { | |
101 AU_ENC_ULAW_8 = 1, /* 8-bit ISDN µ-law */ | |
102 AU_ENC_LINEAR_8 = 2, /* 8-bit linear PCM */ | |
103 AU_ENC_LINEAR_16 = 3, /* 16-bit linear PCM */ | |
104 | |
105 /* the rest are unsupported (I have never seen them in the wild) */ | |
106 AU_ENC_LINEAR_24 = 4, /* 24-bit linear PCM */ | |
107 AU_ENC_LINEAR_32 = 5, /* 32-bit linear PCM */ | |
108 AU_ENC_FLOAT = 6, /* 32-bit IEEE floating point */ | |
109 AU_ENC_DOUBLE = 7, /* 64-bit IEEE floating point */ | |
110 /* more Sun formats, not supported either */ | |
111 AU_ENC_ADPCM_G721 = 23, | |
112 AU_ENC_ADPCM_G722 = 24, | |
113 AU_ENC_ADPCM_G723_3 = 25, | |
114 AU_ENC_ADPCM_G723_5 = 26, | |
115 AU_ENC_ALAW_8 = 27 | |
116 }; | |
117 | |
118 struct audec | |
119 { | |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
120 Uint32 total; |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
121 Uint32 remaining; |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
122 Uint32 start_offset; |
213 | 123 int encoding; |
124 }; | |
125 | |
126 | |
127 #define AU_MAGIC 0x646E732E /* ".snd", in ASCII */ | |
128 | |
129 static int AU_open(Sound_Sample *sample, const char *ext) | |
130 { | |
131 Sound_SampleInternal *internal = sample->opaque; | |
132 SDL_RWops *rw = internal->rw; | |
133 int r, skip, hsize, i; | |
134 struct au_file_hdr hdr; | |
135 char c; | |
136 struct audec *dec = malloc(sizeof *dec); | |
137 BAIL_IF_MACRO(dec == NULL, ERR_OUT_OF_MEMORY, 0); | |
138 internal->decoder_private = dec; | |
139 | |
140 r = SDL_RWread(rw, &hdr, 1, HDR_SIZE); | |
141 if (r < HDR_SIZE) | |
142 { | |
143 Sound_SetError("No .au file (bad header)"); | |
144 free(dec); | |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
145 return(0); |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
146 } /* if */ |
213 | 147 |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
148 /* |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
149 * !!! FIXME: For correctness, we should calculate this as a bigendian |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
150 * !!! FIXME: number, which means swapping AU_MAGIC around. |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
151 */ |
213 | 152 if (SDL_SwapLE32(hdr.magic) == AU_MAGIC) |
153 { | |
154 /* valid magic */ | |
155 dec->encoding = SDL_SwapBE32(hdr.encoding); | |
156 switch(dec->encoding) | |
157 { | |
158 case AU_ENC_ULAW_8: | |
159 /* Convert 8-bit µ-law to 16-bit linear on the fly. This is | |
160 slightly wasteful if the audio driver must convert them | |
161 back, but µ-law only devices are rare (mostly _old_ Suns) */ | |
162 sample->actual.format = AUDIO_S16SYS; | |
163 break; | |
164 | |
165 case AU_ENC_LINEAR_8: | |
166 sample->actual.format = AUDIO_S8; | |
167 break; | |
168 | |
169 case AU_ENC_LINEAR_16: | |
170 sample->actual.format = AUDIO_S16MSB; | |
171 break; | |
172 | |
173 default: | |
174 Sound_SetError("Unsupported .au encoding"); | |
175 free(dec); | |
176 return 0; | |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
177 } /* switch */ |
213 | 178 |
179 sample->actual.rate = SDL_SwapBE32(hdr.sample_rate); | |
180 sample->actual.channels = SDL_SwapBE32(hdr.channels); | |
181 dec->remaining = SDL_SwapBE32(hdr.data_size); | |
182 hsize = SDL_SwapBE32(hdr.hdr_size); | |
183 | |
184 /* skip remaining part of header (input may be unseekable) */ | |
185 for (i = HDR_SIZE; i < hsize; i++) | |
186 SDL_RWread(rw, &c, 1, 1); | |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
187 } /* if */ |
213 | 188 |
189 else if (__Sound_strcasecmp(ext, "au") == 0) | |
190 { | |
191 /* | |
192 * A number of files in the wild have the .au extension but no valid | |
193 * header; these are traditionally assumed to be 8kHz µ-law. Handle | |
194 * them here only if the extension is recognized. | |
195 */ | |
196 | |
197 SNDDBG(("AU: Invalid header, assuming raw 8kHz µ-law.\n")); | |
198 /* if seeking fails, we lose 24 samples. big deal */ | |
199 SDL_RWseek(rw, -HDR_SIZE, SEEK_CUR); | |
200 dec->encoding = AU_ENC_ULAW_8; | |
201 dec->remaining = (Uint32)-1; /* no limit */ | |
202 sample->actual.format = AUDIO_S16SYS; | |
203 sample->actual.rate = 8000; | |
204 sample->actual.channels = 1; | |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
205 } /* else if */ |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
206 |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
207 else |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
208 { |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
209 SNDDBG(("AU: Not an .AU stream.\n")); |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
210 free(dec); |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
211 return(0); |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
212 } /* else */ |
213 | 213 |
214 sample->flags = SOUND_SAMPLEFLAG_NONE; | |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
215 dec->total = dec->remaining; |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
216 dec->start_offset = SDL_RWtell(rw); |
213 | 217 |
218 SNDDBG(("AU: Accepting data stream.\n")); | |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
219 return(1); |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
220 } /* AU_open */ |
213 | 221 |
222 | |
223 static void AU_close(Sound_Sample *sample) | |
224 { | |
225 Sound_SampleInternal *internal = sample->opaque; | |
226 free(internal->decoder_private); | |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
227 } /* AU_close */ |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
228 |
213 | 229 |
230 /* table to convert from µ-law encoding to signed 16-bit samples, | |
231 generated by a throwaway perl script */ | |
232 static Sint16 ulaw_to_linear[256] = { | |
233 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956, | |
234 -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764, | |
235 -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412, | |
236 -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316, | |
237 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140, | |
238 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, | |
239 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004, | |
240 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980, | |
241 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436, | |
242 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924, | |
243 -876, -844, -812, -780, -748, -716, -684, -652, | |
244 -620, -588, -556, -524, -492, -460, -428, -396, | |
245 -372, -356, -340, -324, -308, -292, -276, -260, | |
246 -244, -228, -212, -196, -180, -164, -148, -132, | |
247 -120, -112, -104, -96, -88, -80, -72, -64, | |
248 -56, -48, -40, -32, -24, -16, -8, 0, | |
249 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956, | |
250 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764, | |
251 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412, | |
252 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316, | |
253 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140, | |
254 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, | |
255 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004, | |
256 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980, | |
257 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436, | |
258 1372, 1308, 1244, 1180, 1116, 1052, 988, 924, | |
259 876, 844, 812, 780, 748, 716, 684, 652, | |
260 620, 588, 556, 524, 492, 460, 428, 396, | |
261 372, 356, 340, 324, 308, 292, 276, 260, | |
262 244, 228, 212, 196, 180, 164, 148, 132, | |
263 120, 112, 104, 96, 88, 80, 72, 64, | |
264 56, 48, 40, 32, 24, 16, 8, 0 | |
265 }; | |
266 | |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
267 |
213 | 268 static Uint32 AU_read(Sound_Sample *sample) |
269 { | |
270 int ret; | |
271 Sound_SampleInternal *internal = sample->opaque; | |
272 struct audec *dec = internal->decoder_private; | |
273 int maxlen; | |
274 Uint8 *buf; | |
275 | |
276 maxlen = internal->buffer_size; | |
277 buf = internal->buffer; | |
278 if (dec->encoding == AU_ENC_ULAW_8) | |
279 { | |
280 /* We read µ-law samples into the second half of the buffer, so | |
281 we can expand them to 16-bit samples afterwards */ | |
282 maxlen >>= 1; | |
283 buf += maxlen; | |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
284 } /* if */ |
213 | 285 |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
286 if (maxlen > dec->remaining) |
213 | 287 maxlen = dec->remaining; |
288 ret = SDL_RWread(internal->rw, buf, 1, maxlen); | |
289 if (ret == 0) | |
290 sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
291 else if (ret == -1) | |
292 sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
293 else | |
294 { | |
295 dec->remaining -= ret; | |
296 if (ret < maxlen) | |
297 sample->flags |= SOUND_SAMPLEFLAG_EAGAIN; | |
298 | |
299 if (dec->encoding == AU_ENC_ULAW_8) | |
300 { | |
301 int i; | |
302 Sint16 *dst = internal->buffer; | |
303 for (i = 0; i < ret; i++) | |
304 dst[i] = ulaw_to_linear[buf[i]]; | |
305 ret <<= 1; /* return twice as much as read */ | |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
306 } /* if */ |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
307 } /* else */ |
213 | 308 |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
309 return(ret); |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
310 } /* AU_read */ |
213 | 311 |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
312 |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
313 static int AU_rewind(Sound_Sample *sample) |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
314 { |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
315 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
316 struct audec *dec = (struct audec *) internal->decoder_private; |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
317 int rc = SDL_RWseek(internal->rw, dec->start_offset, SEEK_SET); |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
318 BAIL_IF_MACRO(rc != dec->start_offset, ERR_IO_ERROR, 0); |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
319 dec->remaining = dec->total; |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
320 return(1); |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
321 } /* AU_rewind */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
322 |
213 | 323 #endif /* SOUND_SUPPORTS_AU */ |
324 |