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