Mercurial > SDL_sound_CoreAudio
annotate decoders/au.c @ 330:a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Mon, 20 May 2002 09:22:42 +0000 |
parents | c97be6e1bd27 |
children | e683cb99f88f |
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); |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
294
diff
changeset
|
52 static int AU_seek(Sound_Sample *sample, Uint32 ms); |
213 | 53 |
54 /* | |
55 * Sometimes the extension ".snd" is used for these files (mostly on the NeXT), | |
56 * and the magic number comes from this. However it may clash with other | |
57 * formats and is somewhat of an anachronism, so only .au is used here. | |
58 */ | |
59 static const char *extensions_au[] = { "AU", NULL }; | |
60 const Sound_DecoderFunctions __Sound_DecoderFunctions_AU = | |
61 { | |
62 { | |
63 extensions_au, | |
64 "Sun/NeXT audio file format", | |
65 "Mattias Engdegård <f91-men@nada.kth.se>", | |
66 "http://www.icculus.org/SDL_sound/" | |
67 }, | |
68 | |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
69 AU_init, /* init() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
70 AU_quit, /* quit() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
71 AU_open, /* open() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
72 AU_close, /* close() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
73 AU_read, /* read() method */ |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
294
diff
changeset
|
74 AU_rewind, /* rewind() method */ |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
294
diff
changeset
|
75 AU_seek /* seek() method */ |
213 | 76 }; |
77 | |
78 /* no init/deinit needed */ | |
79 static int AU_init(void) | |
80 { | |
81 return(1); | |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
82 } /* AU_init */ |
213 | 83 |
84 static void AU_quit(void) | |
85 { | |
86 /* no-op. */ | |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
87 } /* AU_quit */ |
213 | 88 |
89 struct au_file_hdr | |
90 { | |
91 Uint32 magic; | |
92 Uint32 hdr_size; | |
93 Uint32 data_size; | |
94 Uint32 encoding; | |
95 Uint32 sample_rate; | |
96 Uint32 channels; | |
97 }; | |
98 | |
99 #define HDR_SIZE 24 | |
100 | |
101 enum | |
102 { | |
103 AU_ENC_ULAW_8 = 1, /* 8-bit ISDN µ-law */ | |
104 AU_ENC_LINEAR_8 = 2, /* 8-bit linear PCM */ | |
105 AU_ENC_LINEAR_16 = 3, /* 16-bit linear PCM */ | |
106 | |
107 /* the rest are unsupported (I have never seen them in the wild) */ | |
108 AU_ENC_LINEAR_24 = 4, /* 24-bit linear PCM */ | |
109 AU_ENC_LINEAR_32 = 5, /* 32-bit linear PCM */ | |
110 AU_ENC_FLOAT = 6, /* 32-bit IEEE floating point */ | |
111 AU_ENC_DOUBLE = 7, /* 64-bit IEEE floating point */ | |
112 /* more Sun formats, not supported either */ | |
113 AU_ENC_ADPCM_G721 = 23, | |
114 AU_ENC_ADPCM_G722 = 24, | |
115 AU_ENC_ADPCM_G723_3 = 25, | |
116 AU_ENC_ADPCM_G723_5 = 26, | |
117 AU_ENC_ALAW_8 = 27 | |
118 }; | |
119 | |
120 struct audec | |
121 { | |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
122 Uint32 total; |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
123 Uint32 remaining; |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
124 Uint32 start_offset; |
213 | 125 int encoding; |
126 }; | |
127 | |
128 | |
330
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
129 /* |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
130 * Read in the AU header from disk. This makes this process safe |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
131 * regardless of the processor's byte order or how the au_file_hdr |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
132 * structure is packed. |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
133 */ |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
134 static int read_au_header(SDL_RWops *rw, struct au_file_hdr *hdr) |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
135 { |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
136 if (SDL_RWread(rw, &hdr->magic, sizeof (hdr->magic), 1) != 1) |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
137 return(0); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
138 hdr->magic = SDL_SwapBE32(hdr->magic); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
139 |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
140 if (SDL_RWread(rw, &hdr->hdr_size, sizeof (hdr->hdr_size), 1) != 1) |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
141 return(0); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
142 hdr->hdr_size = SDL_SwapBE32(hdr->hdr_size); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
143 |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
144 if (SDL_RWread(rw, &hdr->data_size, sizeof (hdr->data_size), 1) != 1) |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
145 return(0); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
146 hdr->data_size = SDL_SwapBE32(hdr->data_size); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
147 |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
148 if (SDL_RWread(rw, &hdr->encoding, sizeof (hdr->encoding), 1) != 1) |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
149 return(0); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
150 hdr->encoding = SDL_SwapBE32(hdr->encoding); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
151 |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
152 if (SDL_RWread(rw, &hdr->sample_rate, sizeof (hdr->sample_rate), 1) != 1) |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
153 return(0); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
154 hdr->sample_rate = SDL_SwapBE32(hdr->sample_rate); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
155 |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
156 if (SDL_RWread(rw, &hdr->channels, sizeof (hdr->channels), 1) != 1) |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
157 return(0); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
158 hdr->channels = SDL_SwapBE32(hdr->channels); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
159 |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
160 return(1); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
161 } /* read_au_header */ |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
162 |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
163 |
257
7c4f6ee02cd0
Changed magic number to be bigendian.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
164 #define AU_MAGIC 0x2E736E64 /* ".snd", in ASCII (bigendian number) */ |
213 | 165 |
166 static int AU_open(Sound_Sample *sample, const char *ext) | |
167 { | |
168 Sound_SampleInternal *internal = sample->opaque; | |
169 SDL_RWops *rw = internal->rw; | |
330
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
170 int skip, hsize, i; |
213 | 171 struct au_file_hdr hdr; |
330
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
172 struct audec *dec; |
213 | 173 char c; |
330
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
174 |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
175 if (!read_au_header(rw, &hdr)) /* does byte order swapping. */ |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
176 { |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
177 Sound_SetError("AU: Not an .au file (bad header)"); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
178 return(0); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
179 } /* if */ |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
180 |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
181 dec = malloc(sizeof *dec); |
213 | 182 BAIL_IF_MACRO(dec == NULL, ERR_OUT_OF_MEMORY, 0); |
183 internal->decoder_private = dec; | |
184 | |
330
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
185 if (hdr.magic == AU_MAGIC) |
213 | 186 { |
187 /* valid magic */ | |
330
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
188 dec->encoding = hdr.encoding; |
213 | 189 switch(dec->encoding) |
190 { | |
191 case AU_ENC_ULAW_8: | |
192 /* Convert 8-bit µ-law to 16-bit linear on the fly. This is | |
193 slightly wasteful if the audio driver must convert them | |
194 back, but µ-law only devices are rare (mostly _old_ Suns) */ | |
195 sample->actual.format = AUDIO_S16SYS; | |
196 break; | |
197 | |
198 case AU_ENC_LINEAR_8: | |
199 sample->actual.format = AUDIO_S8; | |
200 break; | |
201 | |
202 case AU_ENC_LINEAR_16: | |
203 sample->actual.format = AUDIO_S16MSB; | |
204 break; | |
205 | |
206 default: | |
294 | 207 Sound_SetError("AU: Unsupported .au encoding"); |
213 | 208 free(dec); |
209 return 0; | |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
210 } /* switch */ |
213 | 211 |
330
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
212 sample->actual.rate = hdr.sample_rate; |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
213 sample->actual.channels = hdr.channels; |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
214 dec->remaining = hdr.data_size; |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
215 hsize = hdr.hdr_size; |
213 | 216 |
217 /* skip remaining part of header (input may be unseekable) */ | |
218 for (i = HDR_SIZE; i < hsize; i++) | |
330
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
219 { |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
220 if (SDL_RWread(rw, &c, 1, 1) != 1) |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
221 { |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
222 free(dec); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
223 BAIL_MACRO(ERR_IO_ERROR, 0); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
224 } /* if */ |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
225 } /* for */ |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
226 } /* if */ |
213 | 227 |
228 else if (__Sound_strcasecmp(ext, "au") == 0) | |
229 { | |
230 /* | |
231 * A number of files in the wild have the .au extension but no valid | |
232 * header; these are traditionally assumed to be 8kHz µ-law. Handle | |
233 * them here only if the extension is recognized. | |
234 */ | |
235 | |
236 SNDDBG(("AU: Invalid header, assuming raw 8kHz µ-law.\n")); | |
237 /* if seeking fails, we lose 24 samples. big deal */ | |
238 SDL_RWseek(rw, -HDR_SIZE, SEEK_CUR); | |
239 dec->encoding = AU_ENC_ULAW_8; | |
240 dec->remaining = (Uint32)-1; /* no limit */ | |
241 sample->actual.format = AUDIO_S16SYS; | |
242 sample->actual.rate = 8000; | |
243 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
|
244 } /* else if */ |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
245 |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
246 else |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
247 { |
294 | 248 Sound_SetError("AU: Not an .AU stream."); |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
249 free(dec); |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
250 return(0); |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
251 } /* else */ |
213 | 252 |
330
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
253 sample->flags = SOUND_SAMPLEFLAG_CANSEEK; |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
254 dec->total = dec->remaining; |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
255 dec->start_offset = SDL_RWtell(rw); |
213 | 256 |
257 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
|
258 return(1); |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
259 } /* AU_open */ |
213 | 260 |
261 | |
262 static void AU_close(Sound_Sample *sample) | |
263 { | |
264 Sound_SampleInternal *internal = sample->opaque; | |
265 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
|
266 } /* AU_close */ |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
267 |
213 | 268 |
269 /* table to convert from µ-law encoding to signed 16-bit samples, | |
270 generated by a throwaway perl script */ | |
271 static Sint16 ulaw_to_linear[256] = { | |
272 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956, | |
273 -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764, | |
274 -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412, | |
275 -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316, | |
276 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140, | |
277 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, | |
278 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004, | |
279 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980, | |
280 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436, | |
281 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924, | |
282 -876, -844, -812, -780, -748, -716, -684, -652, | |
283 -620, -588, -556, -524, -492, -460, -428, -396, | |
284 -372, -356, -340, -324, -308, -292, -276, -260, | |
285 -244, -228, -212, -196, -180, -164, -148, -132, | |
286 -120, -112, -104, -96, -88, -80, -72, -64, | |
287 -56, -48, -40, -32, -24, -16, -8, 0, | |
288 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956, | |
289 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764, | |
290 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412, | |
291 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316, | |
292 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140, | |
293 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, | |
294 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004, | |
295 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980, | |
296 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436, | |
297 1372, 1308, 1244, 1180, 1116, 1052, 988, 924, | |
298 876, 844, 812, 780, 748, 716, 684, 652, | |
299 620, 588, 556, 524, 492, 460, 428, 396, | |
300 372, 356, 340, 324, 308, 292, 276, 260, | |
301 244, 228, 212, 196, 180, 164, 148, 132, | |
302 120, 112, 104, 96, 88, 80, 72, 64, | |
303 56, 48, 40, 32, 24, 16, 8, 0 | |
304 }; | |
305 | |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
306 |
213 | 307 static Uint32 AU_read(Sound_Sample *sample) |
308 { | |
309 int ret; | |
310 Sound_SampleInternal *internal = sample->opaque; | |
311 struct audec *dec = internal->decoder_private; | |
312 int maxlen; | |
313 Uint8 *buf; | |
314 | |
315 maxlen = internal->buffer_size; | |
316 buf = internal->buffer; | |
317 if (dec->encoding == AU_ENC_ULAW_8) | |
318 { | |
319 /* We read µ-law samples into the second half of the buffer, so | |
320 we can expand them to 16-bit samples afterwards */ | |
321 maxlen >>= 1; | |
322 buf += maxlen; | |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
323 } /* if */ |
213 | 324 |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
325 if (maxlen > dec->remaining) |
213 | 326 maxlen = dec->remaining; |
327 ret = SDL_RWread(internal->rw, buf, 1, maxlen); | |
328 if (ret == 0) | |
329 sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
330 else if (ret == -1) | |
331 sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
332 else | |
333 { | |
334 dec->remaining -= ret; | |
335 if (ret < maxlen) | |
336 sample->flags |= SOUND_SAMPLEFLAG_EAGAIN; | |
337 | |
338 if (dec->encoding == AU_ENC_ULAW_8) | |
339 { | |
340 int i; | |
341 Sint16 *dst = internal->buffer; | |
342 for (i = 0; i < ret; i++) | |
343 dst[i] = ulaw_to_linear[buf[i]]; | |
344 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
|
345 } /* if */ |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
346 } /* else */ |
213 | 347 |
217
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
348 return(ret); |
9bab949e2318
Fixed memory leak I introduced, mangled coding style some more. :)
Ryan C. Gordon <icculus@icculus.org>
parents:
213
diff
changeset
|
349 } /* AU_read */ |
213 | 350 |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
351 |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
352 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
|
353 { |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
354 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
|
355 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
|
356 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
|
357 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
|
358 dec->remaining = dec->total; |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
359 return(1); |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
360 } /* AU_rewind */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
217
diff
changeset
|
361 |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
294
diff
changeset
|
362 |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
294
diff
changeset
|
363 static int AU_seek(Sound_Sample *sample, Uint32 ms) |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
294
diff
changeset
|
364 { |
330
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
365 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
366 struct audec *dec = (struct audec *) internal->decoder_private; |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
367 int offset = dec->start_offset; |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
368 int frames = (int) (((float) sample->actual.rate / 1000.0) * ((float) ms)); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
369 int points = (int) (frames * sample->actual.channels); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
370 int rc; |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
371 |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
372 SNDDBG(("WARNING: AU_seek() may be buggy.\n")); /* !!! FIXME : remove this. */ |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
373 |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
374 switch (dec->encoding) |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
375 { |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
376 case AU_ENC_ULAW_8: /* halve the byte offset for compression. */ |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
377 SNDDBG(("uLaw8 encoding\n")); /* !!! FIXME : remove this. */ |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
378 offset += ((sizeof (Uint8) * points) >> 1); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
379 break; |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
380 |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
381 case AU_ENC_LINEAR_8: |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
382 SNDDBG(("linear8 encoding\n")); /* !!! FIXME : remove this. */ |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
383 offset += (sizeof (Uint8) * points); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
384 break; |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
385 |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
386 case AU_ENC_LINEAR_16: |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
387 SNDDBG(("linear16 encoding\n")); /* !!! FIXME : remove this. */ |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
388 offset += (sizeof (Uint16) * points); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
389 break; |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
390 |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
391 default: |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
392 BAIL_MACRO("Unexpected format. Something is very wrong.", 0); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
393 break; |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
394 } /* switch */ |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
395 |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
396 SNDDBG(("Seek to %d (edge is %d).\n", (int) offset, (int) dec->total)); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
397 |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
398 BAIL_IF_MACRO(offset >= dec->total, ERR_IO_ERROR, 0); /* seek past end? */ |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
399 |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
400 rc = SDL_RWseek(internal->rw, offset, SEEK_SET); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
401 BAIL_IF_MACRO(rc != offset, ERR_IO_ERROR, 0); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
402 dec->remaining = dec->total - (offset - 2); |
a81976ed5df7
Cleanups for robustness, potentially buggy seek method implementation.
Ryan C. Gordon <icculus@icculus.org>
parents:
306
diff
changeset
|
403 return(1); |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
294
diff
changeset
|
404 } /* AU_seek */ |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
294
diff
changeset
|
405 |
213 | 406 #endif /* SOUND_SUPPORTS_AU */ |
407 |