Mercurial > SDL_sound_CoreAudio
annotate SDL_sound.c @ 172:705dcbf94639
Sound_DecodeAll() is now more robust.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Sat, 01 Dec 2001 01:12:58 +0000 |
parents | 82acaa7107c2 |
children | 47cc2de2ae36 |
rev | line source |
---|---|
4 | 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 * This file implements the core API, which is relatively simple. | |
22 * The real meat of SDL_sound is in the decoders directory. | |
23 * | |
24 * Documentation is in SDL_sound.h ... It's verbose, honest. :) | |
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 | |
107
427541211bfd
config.h management updates.
Ryan C. Gordon <icculus@icculus.org>
parents:
102
diff
changeset
|
31 #if HAVE_CONFIG_H |
427541211bfd
config.h management updates.
Ryan C. Gordon <icculus@icculus.org>
parents:
102
diff
changeset
|
32 # include <config.h> |
427541211bfd
config.h management updates.
Ryan C. Gordon <icculus@icculus.org>
parents:
102
diff
changeset
|
33 #endif |
4 | 34 |
35 #include <stdio.h> | |
36 #include <stdlib.h> | |
37 #include <string.h> | |
38 #include <assert.h> | |
39 #include <ctype.h> | |
40 | |
41 #include "SDL.h" | |
42 #include "SDL_sound.h" | |
43 | |
44 #define __SDL_SOUND_INTERNAL__ | |
45 #include "SDL_sound_internal.h" | |
46 | |
47 | |
48 /* The various decoder drivers... */ | |
49 | |
10
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
50 #if (defined SOUND_SUPPORTS_MP3) |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
51 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_MP3; |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
52 #endif |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
53 |
48
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
54 #if (defined SOUND_SUPPORTS_MOD) |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
55 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_MOD; |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
56 #endif |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
57 |
20 | 58 #if (defined SOUND_SUPPORTS_WAV) |
59 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_WAV; | |
60 #endif | |
61 | |
37
f27bcbcaeab1
Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset
|
62 #if (defined SOUND_SUPPORTS_AIFF) |
f27bcbcaeab1
Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset
|
63 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_AIFF; |
f27bcbcaeab1
Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset
|
64 #endif |
f27bcbcaeab1
Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset
|
65 |
26
ddc3614c9042
Bugfix (thanks Tsuyoshi Iguchi!), and Ogg Vorbis decoder entry.
Ryan C. Gordon <icculus@icculus.org>
parents:
20
diff
changeset
|
66 #if (defined SOUND_SUPPORTS_OGG) |
ddc3614c9042
Bugfix (thanks Tsuyoshi Iguchi!), and Ogg Vorbis decoder entry.
Ryan C. Gordon <icculus@icculus.org>
parents:
20
diff
changeset
|
67 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_OGG; |
ddc3614c9042
Bugfix (thanks Tsuyoshi Iguchi!), and Ogg Vorbis decoder entry.
Ryan C. Gordon <icculus@icculus.org>
parents:
20
diff
changeset
|
68 #endif |
ddc3614c9042
Bugfix (thanks Tsuyoshi Iguchi!), and Ogg Vorbis decoder entry.
Ryan C. Gordon <icculus@icculus.org>
parents:
20
diff
changeset
|
69 |
4 | 70 #if (defined SOUND_SUPPORTS_VOC) |
71 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_VOC; | |
72 #endif | |
73 | |
74 #if (defined SOUND_SUPPORTS_RAW) | |
75 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_RAW; | |
76 #endif | |
77 | |
102 | 78 #if (defined SOUND_SUPPORTS_SHN) |
79 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_SHN; | |
80 #endif | |
81 | |
110 | 82 #if (defined SOUND_SUPPORTS_MIDI) |
83 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_MIDI; | |
84 #endif | |
85 | |
157 | 86 #if (defined SOUND_SUPPORTS_FLAC) |
87 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_FLAC; | |
88 #endif | |
89 | |
48
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
90 |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
91 |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
92 typedef struct |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
93 { |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
94 int available; |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
95 const Sound_DecoderFunctions *funcs; |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
96 } decoder_element; |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
97 |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
98 static decoder_element decoders[] = |
4 | 99 { |
10
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
100 #if (defined SOUND_SUPPORTS_MP3) |
48
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
101 { 0, &__Sound_DecoderFunctions_MP3 }, |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
102 #endif |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
103 |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
104 #if (defined SOUND_SUPPORTS_MOD) |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
105 { 0, &__Sound_DecoderFunctions_MOD }, |
10
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
106 #endif |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
107 |
20 | 108 #if (defined SOUND_SUPPORTS_WAV) |
48
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
109 { 0, &__Sound_DecoderFunctions_WAV }, |
20 | 110 #endif |
111 | |
37
f27bcbcaeab1
Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset
|
112 #if (defined SOUND_SUPPORTS_AIFF) |
48
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
113 { 0, &__Sound_DecoderFunctions_AIFF }, |
37
f27bcbcaeab1
Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset
|
114 #endif |
f27bcbcaeab1
Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset
|
115 |
26
ddc3614c9042
Bugfix (thanks Tsuyoshi Iguchi!), and Ogg Vorbis decoder entry.
Ryan C. Gordon <icculus@icculus.org>
parents:
20
diff
changeset
|
116 #if (defined SOUND_SUPPORTS_OGG) |
48
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
117 { 0, &__Sound_DecoderFunctions_OGG }, |
26
ddc3614c9042
Bugfix (thanks Tsuyoshi Iguchi!), and Ogg Vorbis decoder entry.
Ryan C. Gordon <icculus@icculus.org>
parents:
20
diff
changeset
|
118 #endif |
ddc3614c9042
Bugfix (thanks Tsuyoshi Iguchi!), and Ogg Vorbis decoder entry.
Ryan C. Gordon <icculus@icculus.org>
parents:
20
diff
changeset
|
119 |
4 | 120 #if (defined SOUND_SUPPORTS_VOC) |
48
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
121 { 0, &__Sound_DecoderFunctions_VOC }, |
4 | 122 #endif |
123 | |
124 #if (defined SOUND_SUPPORTS_RAW) | |
48
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
125 { 0, &__Sound_DecoderFunctions_RAW }, |
4 | 126 #endif |
102 | 127 |
128 #if (defined SOUND_SUPPORTS_SHN) | |
129 { 0, &__Sound_DecoderFunctions_SHN }, | |
130 #endif | |
110 | 131 |
163
b6d6f994e970
Moved MIDI decoder to end of array. Fixed Sound_SetError() so that it
Ryan C. Gordon <icculus@icculus.org>
parents:
157
diff
changeset
|
132 #if (defined SOUND_SUPPORTS_FLAC) |
b6d6f994e970
Moved MIDI decoder to end of array. Fixed Sound_SetError() so that it
Ryan C. Gordon <icculus@icculus.org>
parents:
157
diff
changeset
|
133 { 0, &__Sound_DecoderFunctions_FLAC }, |
b6d6f994e970
Moved MIDI decoder to end of array. Fixed Sound_SetError() so that it
Ryan C. Gordon <icculus@icculus.org>
parents:
157
diff
changeset
|
134 #endif |
b6d6f994e970
Moved MIDI decoder to end of array. Fixed Sound_SetError() so that it
Ryan C. Gordon <icculus@icculus.org>
parents:
157
diff
changeset
|
135 |
110 | 136 #if (defined SOUND_SUPPORTS_MIDI) |
137 { 0, &__Sound_DecoderFunctions_MIDI }, | |
138 #endif | |
118
fd942c1433f8
Fixed for win32 compile.
Ryan C. Gordon <icculus@icculus.org>
parents:
110
diff
changeset
|
139 |
fd942c1433f8
Fixed for win32 compile.
Ryan C. Gordon <icculus@icculus.org>
parents:
110
diff
changeset
|
140 { 0, NULL } |
4 | 141 }; |
142 | |
143 | |
144 | |
145 /* General SDL_sound state ... */ | |
146 | |
147 static int initialized = 0; | |
148 static Sound_Sample *samplesList = NULL; /* this is a linked list. */ | |
149 static const Sound_DecoderInfo **available_decoders = NULL; | |
150 | |
151 | |
152 /* functions ... */ | |
153 | |
154 void Sound_GetLinkedVersion(Sound_Version *ver) | |
155 { | |
156 if (ver != NULL) | |
157 { | |
158 ver->major = SOUND_VER_MAJOR; | |
159 ver->minor = SOUND_VER_MINOR; | |
160 ver->patch = SOUND_VER_PATCH; | |
161 } /* if */ | |
162 } /* Sound_GetLinkedVersion */ | |
163 | |
164 | |
165 int Sound_Init(void) | |
166 { | |
167 size_t i; | |
48
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
168 size_t pos = 0; |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
169 size_t total = sizeof (decoders) / sizeof (decoders[0]); |
4 | 170 BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0); |
171 samplesList = NULL; | |
172 | |
173 SDL_Init(SDL_INIT_AUDIO); | |
174 | |
175 available_decoders = (const Sound_DecoderInfo **) | |
118
fd942c1433f8
Fixed for win32 compile.
Ryan C. Gordon <icculus@icculus.org>
parents:
110
diff
changeset
|
176 malloc((total) * sizeof (Sound_DecoderInfo *)); |
4 | 177 BAIL_IF_MACRO(available_decoders == NULL, ERR_OUT_OF_MEMORY, 0); |
178 | |
118
fd942c1433f8
Fixed for win32 compile.
Ryan C. Gordon <icculus@icculus.org>
parents:
110
diff
changeset
|
179 for (i = 0; decoders[i].funcs != NULL; i++) |
48
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
180 { |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
181 decoders[i].available = decoders[i].funcs->init(); |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
182 if (decoders[i].available) |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
183 { |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
184 available_decoders[pos] = &(decoders[i].funcs->info); |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
185 pos++; |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
186 } /* if */ |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
187 } /* for */ |
4 | 188 |
48
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
189 available_decoders[pos] = NULL; |
26
ddc3614c9042
Bugfix (thanks Tsuyoshi Iguchi!), and Ogg Vorbis decoder entry.
Ryan C. Gordon <icculus@icculus.org>
parents:
20
diff
changeset
|
190 |
4 | 191 initialized = 1; |
192 return(1); | |
193 } /* Sound_Init */ | |
194 | |
195 | |
196 int Sound_Quit(void) | |
197 { | |
48
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
198 size_t i; |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
199 |
4 | 200 BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0); |
201 | |
202 while (((volatile Sound_Sample *) samplesList) != NULL) | |
203 Sound_FreeSample(samplesList); | |
204 | |
118
fd942c1433f8
Fixed for win32 compile.
Ryan C. Gordon <icculus@icculus.org>
parents:
110
diff
changeset
|
205 for (i = 0; decoders[i].funcs != NULL; i++) |
48
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
206 { |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
207 if (decoders[i].available) |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
208 { |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
209 decoders[i].funcs->quit(); |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
210 decoders[i].available = 0; |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
211 } /* if */ |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
212 } /* for */ |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
213 |
4 | 214 if (available_decoders != NULL) |
80
25ee62c6b333
Fixed a const complaint from Visual C++ 6.0.
Ryan C. Gordon <icculus@icculus.org>
parents:
62
diff
changeset
|
215 free((void *) available_decoders); |
4 | 216 available_decoders = NULL; |
217 | |
218 initialized = 0; | |
219 | |
220 return(1); | |
221 } /* Sound_Quit */ | |
222 | |
223 | |
224 const Sound_DecoderInfo **Sound_AvailableDecoders(void) | |
225 { | |
226 return(available_decoders); /* READ. ONLY. */ | |
227 } /* Sound_AvailableDecoders */ | |
228 | |
229 | |
230 const char *Sound_GetError(void) | |
231 { | |
232 return(SDL_GetError()); | |
233 } /* Sound_GetError */ | |
234 | |
235 | |
236 void Sound_ClearError(void) | |
237 { | |
238 SDL_ClearError(); | |
239 } /* Sound_ClearError */ | |
240 | |
241 | |
242 /* | |
243 * This is declared in the internal header. | |
244 */ | |
245 void Sound_SetError(const char *err) | |
246 { | |
165
9b26ed9eaf04
Whoops; I should read the code before I change it next time; regressing
Ryan C. Gordon <icculus@icculus.org>
parents:
163
diff
changeset
|
247 if (err != NULL) |
9b26ed9eaf04
Whoops; I should read the code before I change it next time; regressing
Ryan C. Gordon <icculus@icculus.org>
parents:
163
diff
changeset
|
248 { |
9b26ed9eaf04
Whoops; I should read the code before I change it next time; regressing
Ryan C. Gordon <icculus@icculus.org>
parents:
163
diff
changeset
|
249 SNDDBG(("Sound_SetError(\"%s\");\n", err)); |
9b26ed9eaf04
Whoops; I should read the code before I change it next time; regressing
Ryan C. Gordon <icculus@icculus.org>
parents:
163
diff
changeset
|
250 SDL_SetError(err); |
9b26ed9eaf04
Whoops; I should read the code before I change it next time; regressing
Ryan C. Gordon <icculus@icculus.org>
parents:
163
diff
changeset
|
251 } /* if */ |
4 | 252 } /* Sound_SetError */ |
253 | |
254 | |
255 /* | |
256 * -ansi and -pedantic flags prevent use of strcasecmp() on Linux, and | |
257 * I honestly don't want to mess around with figuring out if a given | |
258 * platform has "strcasecmp", "stricmp", or | |
259 * "compare_two_damned_strings_case_insensitive", which I hear is in the | |
260 * next release of Carbon. :) This is exported so decoders may use it if | |
261 * they like. | |
262 */ | |
263 int __Sound_strcasecmp(const char *x, const char *y) | |
264 { | |
265 int ux, uy; | |
266 | |
98
dfdf7b4e05bd
Changed __Sound_strcasecmp() to handle NULL strings gracefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
80
diff
changeset
|
267 if (x == y) /* same pointer? Both NULL? */ |
dfdf7b4e05bd
Changed __Sound_strcasecmp() to handle NULL strings gracefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
80
diff
changeset
|
268 return(0); |
dfdf7b4e05bd
Changed __Sound_strcasecmp() to handle NULL strings gracefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
80
diff
changeset
|
269 |
dfdf7b4e05bd
Changed __Sound_strcasecmp() to handle NULL strings gracefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
80
diff
changeset
|
270 if (x == NULL) |
dfdf7b4e05bd
Changed __Sound_strcasecmp() to handle NULL strings gracefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
80
diff
changeset
|
271 return(-1); |
dfdf7b4e05bd
Changed __Sound_strcasecmp() to handle NULL strings gracefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
80
diff
changeset
|
272 |
dfdf7b4e05bd
Changed __Sound_strcasecmp() to handle NULL strings gracefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
80
diff
changeset
|
273 if (y == NULL) |
dfdf7b4e05bd
Changed __Sound_strcasecmp() to handle NULL strings gracefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
80
diff
changeset
|
274 return(1); |
dfdf7b4e05bd
Changed __Sound_strcasecmp() to handle NULL strings gracefully.
Ryan C. Gordon <icculus@icculus.org>
parents:
80
diff
changeset
|
275 |
4 | 276 do |
277 { | |
278 ux = toupper((int) *x); | |
279 uy = toupper((int) *y); | |
280 if (ux > uy) | |
281 return(1); | |
282 else if (ux < uy) | |
283 return(-1); | |
284 x++; | |
285 y++; | |
286 } while ((ux) && (uy)); | |
287 | |
288 return(0); | |
289 } /* __Sound_strcasecmp */ | |
290 | |
291 | |
292 /* | |
293 * Allocate a Sound_Sample, and fill in most of its fields. Those that need | |
294 * to be filled in later, by a decoder, will be initialized to zero. | |
295 */ | |
296 static Sound_Sample *alloc_sample(SDL_RWops *rw, Sound_AudioInfo *desired, | |
297 Uint32 bufferSize) | |
298 { | |
299 Sound_Sample *retval = malloc(sizeof (Sound_Sample)); | |
300 Sound_SampleInternal *internal = malloc(sizeof (Sound_SampleInternal)); | |
301 if ((retval == NULL) || (internal == NULL)) | |
302 { | |
303 Sound_SetError(ERR_OUT_OF_MEMORY); | |
304 if (retval) | |
305 free(retval); | |
306 if (internal) | |
307 free(internal); | |
308 | |
309 return(NULL); | |
310 } /* if */ | |
311 | |
312 memset(retval, '\0', sizeof (Sound_Sample)); | |
313 memset(internal, '\0', sizeof (Sound_SampleInternal)); | |
314 | |
315 assert(bufferSize > 0); | |
316 retval->buffer = malloc(bufferSize); /* pure ugly. */ | |
317 if (!retval->buffer) | |
318 { | |
319 Sound_SetError(ERR_OUT_OF_MEMORY); | |
320 free(internal); | |
321 free(retval); | |
322 return(NULL); | |
323 } /* if */ | |
324 memset(retval->buffer, '\0', bufferSize); | |
325 retval->buffer_size = bufferSize; | |
326 | |
327 if (desired != NULL) | |
328 memcpy(&retval->desired, desired, sizeof (Sound_AudioInfo)); | |
329 | |
330 internal->rw = rw; | |
331 retval->opaque = internal; | |
332 return(retval); | |
333 } /* alloc_sample */ | |
334 | |
335 | |
10
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
336 #if (defined DEBUG_CHATTER) |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
337 static __inline__ const char *fmt_to_str(Uint16 fmt) |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
338 { |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
339 switch(fmt) |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
340 { |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
341 case AUDIO_U8: |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
342 return("U8"); |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
343 case AUDIO_S8: |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
344 return("S8"); |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
345 case AUDIO_U16LSB: |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
346 return("U16LSB"); |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
347 case AUDIO_S16LSB: |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
348 return("S16LSB"); |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
349 case AUDIO_U16MSB: |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
350 return("U16MSB"); |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
351 case AUDIO_S16MSB: |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
352 return("S16MSB"); |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
353 } /* switch */ |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
354 |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
355 return("Unknown"); |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
356 } /* fmt_to_str */ |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
357 #endif |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
358 |
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
359 |
4 | 360 /* |
361 * The bulk of the Sound_NewSample() work is done here... | |
362 * Ask the specified decoder to handle the data in (rw), and if | |
363 * so, construct the Sound_Sample. Otherwise, try to wind (rw)'s stream | |
364 * back to where it was, and return false. | |
365 * | |
366 * !!! FIXME: This is big, ugly, nasty, and smelly. | |
367 */ | |
368 static int init_sample(const Sound_DecoderFunctions *funcs, | |
369 Sound_Sample *sample, const char *ext, | |
370 Sound_AudioInfo *_desired) | |
371 { | |
372 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
37
f27bcbcaeab1
Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset
|
373 Sound_AudioInfo desired; |
4 | 374 int pos = SDL_RWtell(internal->rw); /* !!! FIXME: Int? Really? */ |
375 | |
376 /* fill in the funcs for this decoder... */ | |
377 sample->decoder = &funcs->info; | |
378 internal->funcs = funcs; | |
379 if (!funcs->open(sample, ext)) | |
380 { | |
381 SDL_RWseek(internal->rw, pos, SEEK_SET); /* set for next try... */ | |
382 return(0); | |
383 } /* if */ | |
384 | |
385 /* success; we've got a decoder! */ | |
386 | |
387 /* Now we need to set up the conversion buffer... */ | |
388 | |
389 memcpy(&desired, (_desired != NULL) ? _desired : &sample->actual, | |
390 sizeof (Sound_AudioInfo)); | |
391 | |
143
3e60862fbd76
Start of audio converter work.
Ryan C. Gordon <icculus@icculus.org>
parents:
118
diff
changeset
|
392 if (Sound_BuildAudioCVT(&internal->sdlcvt, |
3e60862fbd76
Start of audio converter work.
Ryan C. Gordon <icculus@icculus.org>
parents:
118
diff
changeset
|
393 sample->actual.format, |
3e60862fbd76
Start of audio converter work.
Ryan C. Gordon <icculus@icculus.org>
parents:
118
diff
changeset
|
394 sample->actual.channels, |
3e60862fbd76
Start of audio converter work.
Ryan C. Gordon <icculus@icculus.org>
parents:
118
diff
changeset
|
395 sample->actual.rate, |
3e60862fbd76
Start of audio converter work.
Ryan C. Gordon <icculus@icculus.org>
parents:
118
diff
changeset
|
396 desired.format, |
3e60862fbd76
Start of audio converter work.
Ryan C. Gordon <icculus@icculus.org>
parents:
118
diff
changeset
|
397 desired.channels, |
3e60862fbd76
Start of audio converter work.
Ryan C. Gordon <icculus@icculus.org>
parents:
118
diff
changeset
|
398 desired.rate) == -1) |
4 | 399 { |
400 Sound_SetError(SDL_GetError()); | |
401 funcs->close(sample); | |
402 SDL_RWseek(internal->rw, pos, SEEK_SET); /* set for next try... */ | |
403 return(0); | |
404 } /* if */ | |
405 | |
406 if (internal->sdlcvt.len_mult > 1) | |
407 { | |
408 void *rc = realloc(sample->buffer, | |
143
3e60862fbd76
Start of audio converter work.
Ryan C. Gordon <icculus@icculus.org>
parents:
118
diff
changeset
|
409 sample->buffer_size * internal->sdlcvt.len_mult); |
4 | 410 if (rc == NULL) |
411 { | |
412 funcs->close(sample); | |
413 SDL_RWseek(internal->rw, pos, SEEK_SET); /* set for next try... */ | |
414 return(0); | |
415 } /* if */ | |
416 | |
417 sample->buffer = rc; | |
418 } /* if */ | |
419 | |
420 /* these pointers are all one and the same. */ | |
421 memcpy(&sample->desired, &desired, sizeof (Sound_AudioInfo)); | |
422 internal->sdlcvt.buf = internal->buffer = sample->buffer; | |
423 internal->buffer_size = sample->buffer_size / internal->sdlcvt.len_mult; | |
424 internal->sdlcvt.len = internal->buffer_size; | |
425 | |
426 /* Prepend our new Sound_Sample to the samplesList... */ | |
427 if (samplesList != NULL) | |
428 { | |
429 internal->next = samplesList; | |
430 if (samplesList != NULL) | |
431 internal->prev = sample; | |
432 } /* if */ | |
433 samplesList = sample; | |
434 | |
62
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
48
diff
changeset
|
435 SNDDBG(("New sample DESIRED format: %s format, %d rate, %d channels.\n", |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
48
diff
changeset
|
436 fmt_to_str(sample->desired.format), |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
48
diff
changeset
|
437 sample->desired.rate, |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
48
diff
changeset
|
438 sample->desired.channels)); |
10
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
439 |
62
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
48
diff
changeset
|
440 SNDDBG(("New sample ACTUAL format: %s format, %d rate, %d channels.\n", |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
48
diff
changeset
|
441 fmt_to_str(sample->actual.format), |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
48
diff
changeset
|
442 sample->actual.rate, |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
48
diff
changeset
|
443 sample->actual.channels)); |
10
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
444 |
62
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
48
diff
changeset
|
445 SNDDBG(("On-the-fly conversion: %s.\n", |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
48
diff
changeset
|
446 internal->sdlcvt.needed ? "ENABLED" : "DISABLED")); |
10
cc2c32349380
Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
447 |
4 | 448 return(1); |
449 } /* init_sample */ | |
450 | |
451 | |
452 Sound_Sample *Sound_NewSample(SDL_RWops *rw, const char *ext, | |
453 Sound_AudioInfo *desired, Uint32 bSize) | |
454 { | |
455 Sound_Sample *retval; | |
149
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
143
diff
changeset
|
456 decoder_element *decoder; |
4 | 457 |
458 /* sanity checks. */ | |
459 BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, NULL); | |
460 BAIL_IF_MACRO(rw == NULL, ERR_INVALID_ARGUMENT, NULL); | |
461 | |
462 retval = alloc_sample(rw, desired, bSize); | |
463 if (!retval) | |
464 return(NULL); /* alloc_sample() sets error message... */ | |
465 | |
466 if (ext != NULL) | |
467 { | |
149
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
143
diff
changeset
|
468 for (decoder = &decoders[0]; decoder->funcs != NULL; decoder++) |
4 | 469 { |
149
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
143
diff
changeset
|
470 if (decoder->available) |
4 | 471 { |
149
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
143
diff
changeset
|
472 const char **decoderExt = decoder->funcs->info.extensions; |
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
143
diff
changeset
|
473 while (*decoderExt) |
48
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
474 { |
149
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
143
diff
changeset
|
475 if (__Sound_strcasecmp(*decoderExt, ext) == 0) |
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
143
diff
changeset
|
476 { |
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
143
diff
changeset
|
477 if (init_sample(decoder->funcs, retval, ext, desired)) |
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
143
diff
changeset
|
478 return(retval); |
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
143
diff
changeset
|
479 } /* if */ |
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
143
diff
changeset
|
480 decoderExt++; |
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
143
diff
changeset
|
481 } /* while */ |
4 | 482 } /* if */ |
483 } /* for */ | |
484 } /* if */ | |
485 | |
486 /* no direct extension match? Try everything we've got... */ | |
149
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
143
diff
changeset
|
487 for (decoder = &decoders[0]; decoder->funcs != NULL; decoder++) |
4 | 488 { |
149
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
143
diff
changeset
|
489 if (decoder->available) |
48
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
490 { |
149
1df5c106504e
Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents:
143
diff
changeset
|
491 if (init_sample(decoder->funcs, retval, ext, desired)) |
48
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
492 return(retval); |
c4b8c39a9798
Individual decoders are now initialized during Sound_Init(), and deinitialized
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset
|
493 } /* if */ |
4 | 494 } /* for */ |
495 | |
496 /* nothing could handle the sound data... */ | |
497 free(retval->opaque); | |
498 if (retval->buffer != NULL) | |
499 free(retval->buffer); | |
500 free(retval); | |
501 SDL_RWclose(rw); | |
502 Sound_SetError(ERR_UNSUPPORTED_FORMAT); | |
503 return(NULL); | |
504 } /* Sound_NewSample */ | |
505 | |
506 | |
507 Sound_Sample *Sound_NewSampleFromFile(const char *filename, | |
508 Sound_AudioInfo *desired, | |
509 Uint32 bufferSize) | |
510 { | |
511 const char *ext; | |
512 SDL_RWops *rw; | |
513 | |
514 BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, NULL); | |
515 BAIL_IF_MACRO(filename == NULL, ERR_INVALID_ARGUMENT, NULL); | |
516 | |
517 ext = strrchr(filename, '.'); | |
518 rw = SDL_RWFromFile(filename, "rb"); | |
519 BAIL_IF_MACRO(rw == NULL, SDL_GetError(), NULL); | |
520 | |
521 if (ext != NULL) | |
522 ext++; | |
523 | |
524 return(Sound_NewSample(rw, ext, desired, bufferSize)); | |
525 } /* Sound_NewSampleFromFile */ | |
526 | |
527 | |
528 void Sound_FreeSample(Sound_Sample *sample) | |
529 { | |
530 Sound_SampleInternal *internal; | |
531 | |
532 if (!initialized) | |
533 { | |
534 Sound_SetError(ERR_NOT_INITIALIZED); | |
535 return; | |
536 } /* if */ | |
537 | |
538 if (sample == NULL) | |
539 { | |
540 Sound_SetError(ERR_INVALID_ARGUMENT); | |
541 return; | |
542 } /* if */ | |
543 | |
544 internal = (Sound_SampleInternal *) sample->opaque; | |
545 | |
546 internal->funcs->close(sample); | |
547 | |
548 /* update the samplesList... */ | |
549 if (internal->prev != NULL) | |
550 { | |
551 Sound_SampleInternal *prevInternal; | |
552 prevInternal = (Sound_SampleInternal *) internal->prev->opaque; | |
553 prevInternal->next = internal->next; | |
554 } /* if */ | |
555 else | |
556 { | |
557 assert(samplesList == sample); | |
558 samplesList = internal->next; | |
559 } /* else */ | |
560 | |
561 if (internal->next != NULL) | |
562 { | |
563 Sound_SampleInternal *nextInternal; | |
564 nextInternal = (Sound_SampleInternal *) internal->next->opaque; | |
565 nextInternal->prev = internal->prev; | |
566 } /* if */ | |
567 | |
568 /* nuke it... */ | |
569 if (internal->rw != NULL) /* this condition is a "just in case" thing. */ | |
570 SDL_RWclose(internal->rw); | |
167
82acaa7107c2
Fixed Sound_DecodeAll(), and made Sound_FreeSample() a little more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
165
diff
changeset
|
571 |
82acaa7107c2
Fixed Sound_DecodeAll(), and made Sound_FreeSample() a little more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
165
diff
changeset
|
572 if ((internal->buffer != NULL) && (internal->buffer != sample->buffer)) |
4 | 573 free(internal->buffer); |
167
82acaa7107c2
Fixed Sound_DecodeAll(), and made Sound_FreeSample() a little more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
165
diff
changeset
|
574 |
4 | 575 free(internal); |
167
82acaa7107c2
Fixed Sound_DecodeAll(), and made Sound_FreeSample() a little more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
165
diff
changeset
|
576 |
4 | 577 if (sample->buffer != NULL) |
578 free(sample->buffer); | |
167
82acaa7107c2
Fixed Sound_DecodeAll(), and made Sound_FreeSample() a little more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
165
diff
changeset
|
579 |
4 | 580 free(sample); |
581 } /* Sound_FreeSample */ | |
582 | |
583 | |
584 int Sound_SetBufferSize(Sound_Sample *sample, Uint32 newSize) | |
585 { | |
586 void *newBuf = NULL; | |
587 Sound_SampleInternal *internal = NULL; | |
588 | |
589 BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0); | |
590 BAIL_IF_MACRO(sample == NULL, ERR_INVALID_ARGUMENT, 0); | |
591 internal = ((Sound_SampleInternal *) sample->opaque); | |
592 newBuf = realloc(sample->buffer, newSize * internal->sdlcvt.len_mult); | |
593 BAIL_IF_MACRO(newBuf == NULL, ERR_OUT_OF_MEMORY, 0); | |
594 | |
595 internal->sdlcvt.buf = internal->buffer = sample->buffer = newBuf; | |
596 sample->buffer_size = newSize; | |
597 internal->buffer_size = newSize / internal->sdlcvt.len_mult; | |
598 internal->sdlcvt.len = internal->buffer_size; | |
599 | |
600 return(1); | |
601 } /* Sound_SetBufferSize */ | |
602 | |
603 | |
604 Uint32 Sound_Decode(Sound_Sample *sample) | |
605 { | |
606 Sound_SampleInternal *internal = NULL; | |
607 Uint32 retval = 0; | |
608 | |
609 /* a boatload of sanity checks... */ | |
610 BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0); | |
611 BAIL_IF_MACRO(sample == NULL, ERR_INVALID_ARGUMENT, 0); | |
612 BAIL_IF_MACRO(sample->flags & SOUND_SAMPLEFLAG_ERROR, ERR_PREV_ERROR, 0); | |
613 BAIL_IF_MACRO(sample->flags & SOUND_SAMPLEFLAG_EOF, ERR_PREV_EOF, 0); | |
614 | |
615 internal = (Sound_SampleInternal *) sample->opaque; | |
616 | |
617 assert(sample->buffer != NULL); | |
618 assert(sample->buffer_size > 0); | |
619 assert(internal->buffer != NULL); | |
620 assert(internal->buffer_size > 0); | |
621 | |
622 /* reset EAGAIN. Decoder can flip it back on if it needs to. */ | |
623 sample->flags &= !SOUND_SAMPLEFLAG_EAGAIN; | |
624 retval = internal->funcs->read(sample); | |
37
f27bcbcaeab1
Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset
|
625 |
163
b6d6f994e970
Moved MIDI decoder to end of array. Fixed Sound_SetError() so that it
Ryan C. Gordon <icculus@icculus.org>
parents:
157
diff
changeset
|
626 if (retval > 0 && internal->sdlcvt.needed) |
4 | 627 { |
628 internal->sdlcvt.len = retval; | |
143
3e60862fbd76
Start of audio converter work.
Ryan C. Gordon <icculus@icculus.org>
parents:
118
diff
changeset
|
629 Sound_ConvertAudio(&internal->sdlcvt); |
3e60862fbd76
Start of audio converter work.
Ryan C. Gordon <icculus@icculus.org>
parents:
118
diff
changeset
|
630 retval = internal->sdlcvt.len_cvt; |
4 | 631 } /* if */ |
632 | |
633 return(retval); | |
634 } /* Sound_Decode */ | |
635 | |
636 | |
637 Uint32 Sound_DecodeAll(Sound_Sample *sample) | |
638 { | |
639 Sound_SampleInternal *internal = NULL; | |
640 void *buf = NULL; | |
641 Uint32 newBufSize = 0; | |
642 | |
643 BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0); | |
172
705dcbf94639
Sound_DecodeAll() is now more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
167
diff
changeset
|
644 BAIL_IF_MACRO(sample->flags & SOUND_SAMPLEFLAG_EOF, ERR_PREV_EOF, 0); |
705dcbf94639
Sound_DecodeAll() is now more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
167
diff
changeset
|
645 BAIL_IF_MACRO(sample->flags & SOUND_SAMPLEFLAG_ERROR, ERR_PREV_ERROR, 0); |
4 | 646 |
647 internal = (Sound_SampleInternal *) sample->opaque; | |
648 | |
649 while ( ((sample->flags & SOUND_SAMPLEFLAG_EOF) == 0) && | |
650 ((sample->flags & SOUND_SAMPLEFLAG_ERROR) == 0) ) | |
651 { | |
172
705dcbf94639
Sound_DecodeAll() is now more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
167
diff
changeset
|
652 Uint32 br = Sound_Decode(sample); |
705dcbf94639
Sound_DecodeAll() is now more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
167
diff
changeset
|
653 void *ptr = realloc(buf, newBufSize + br); |
4 | 654 if (ptr == NULL) |
655 { | |
656 sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
657 Sound_SetError(ERR_OUT_OF_MEMORY); | |
658 } /* if */ | |
659 else | |
660 { | |
167
82acaa7107c2
Fixed Sound_DecodeAll(), and made Sound_FreeSample() a little more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
165
diff
changeset
|
661 buf = ptr; |
4 | 662 memcpy( ((char *) buf) + newBufSize, sample->buffer, br ); |
663 newBufSize += br; | |
664 } /* else */ | |
665 } /* while */ | |
666 | |
172
705dcbf94639
Sound_DecodeAll() is now more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
167
diff
changeset
|
667 if (buf == NULL) /* ...in case first call to realloc() fails... */ |
705dcbf94639
Sound_DecodeAll() is now more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
167
diff
changeset
|
668 return(sample->buffer_size); |
705dcbf94639
Sound_DecodeAll() is now more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
167
diff
changeset
|
669 |
167
82acaa7107c2
Fixed Sound_DecodeAll(), and made Sound_FreeSample() a little more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
165
diff
changeset
|
670 if (internal->buffer != sample->buffer) |
82acaa7107c2
Fixed Sound_DecodeAll(), and made Sound_FreeSample() a little more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
165
diff
changeset
|
671 free(internal->buffer); |
82acaa7107c2
Fixed Sound_DecodeAll(), and made Sound_FreeSample() a little more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
165
diff
changeset
|
672 |
4 | 673 free(sample->buffer); |
167
82acaa7107c2
Fixed Sound_DecodeAll(), and made Sound_FreeSample() a little more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
165
diff
changeset
|
674 |
82acaa7107c2
Fixed Sound_DecodeAll(), and made Sound_FreeSample() a little more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
165
diff
changeset
|
675 internal->sdlcvt.buf = internal->buffer = sample->buffer = buf; |
4 | 676 sample->buffer_size = newBufSize; |
677 internal->buffer_size = newBufSize / internal->sdlcvt.len_mult; | |
167
82acaa7107c2
Fixed Sound_DecodeAll(), and made Sound_FreeSample() a little more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
165
diff
changeset
|
678 internal->sdlcvt.len = internal->buffer_size; |
4 | 679 |
680 return(newBufSize); | |
681 } /* Sound_DecodeAll */ | |
682 | |
683 /* end of SDL_sound.c ... */ | |
684 |