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