Mercurial > almixer_isolated
annotate Isolated/SoundDecoder.c @ 66:5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
If Apple, Core Audio will always be picked for WAV/MP3. This could be more flexible, but for shipping apps, there are not a lot of compelling reasons to not use Core Audio when it is available.
Ogg Vorbis/Tremor are in common areas so it is directly selectable since Core Audio does not support Ogg. You are responsible for not trying to compile/link both Vorbis/Tremor at the same time. They are mutually exclusive.
author | Eric Wing <ewing . public |-at-| gmail . com> |
---|---|
date | Tue, 19 Jun 2012 00:40:32 -0700 |
parents | 71b465ff0622 |
children | be97ae4f30c0 |
rev | line source |
---|---|
38 | 1 #ifndef ALMIXER_COMPILED_WITH_SDL |
2 | |
3 #include "SoundDecoder.h" | |
4 #include "SoundDecoder_Internal.h" | |
5 #include "tErrorLib.h" | |
6 #include "LinkedList.h" | |
7 #include <stdlib.h> | |
8 #include <string.h> | |
9 #include <assert.h> | |
10 | |
11 #ifdef ANDROID_NDK | |
12 #include <android/log.h> | |
13 #endif | |
14 | |
15 /* A partial shim reimplementation of SDL_sound to work around the LGPL issues. | |
16 * This implementation is more limited than SDL_sound. | |
17 * For example, there is no generic software conversion routines. | |
18 * Functions are also not necessarily thread safe. | |
19 * This implementation relies on the back-end decoder much more heavily | |
20 * than SDL_sound. (For example, I bypass the internal->buffer.) | |
21 */ | |
22 | |
23 static LinkedList* s_listOfLoadedSamples = NULL; | |
24 | |
25 static signed char s_isInitialized = 0; | |
26 static TErrorPool* s_errorPool = NULL; | |
27 | |
28 static const SoundDecoder_DecoderInfo** s_availableDecoders = NULL; | |
29 | |
66
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
30 #ifdef __APPLE__ /* I'm making Apple use the Core Audio backend. */ |
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
31 //extern const SoundDecoder_DecoderFunctions __SoundDecoder_DecoderFunctions_CoreAudio; |
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
32 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_CoreAudio; |
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
33 #else /* Not Apple */ |
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
34 #ifdef SOUND_SUPPORTS_WAV |
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
35 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_WAV; |
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
36 #endif |
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
37 #ifdef SOUND_SUPPORTS_MPG123 |
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
38 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_MPG123; |
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
39 #endif |
38 | 40 #endif |
66
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
41 |
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
42 /* Note: Make sure to compile only Vorbis xor Tremor, not both. */ |
38 | 43 #ifdef SOUND_SUPPORTS_OGG |
66
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
44 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_OGG; |
38 | 45 #endif |
46 | |
47 typedef struct | |
48 { | |
49 int available; | |
50 const SoundDecoder_DecoderFunctions* funcs; | |
51 } SoundElement; | |
52 | |
53 static SoundElement s_linkedDecoders[] = | |
54 { | |
55 #if defined(__APPLE__) | |
66
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
56 { 0, &__Sound_DecoderFunctions_CoreAudio }, |
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
57 #else /* Not Apple */ |
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
58 #ifdef SOUND_SUPPORTS_WAV |
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
59 { 0, &__Sound_DecoderFunctions_WAV }, |
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
60 #endif |
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
61 #ifdef SOUND_SUPPORTS_MPG123 |
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
62 { 0, &__Sound_DecoderFunctions_MPG123 }, |
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
63 #endif |
38 | 64 #endif |
66
5c9eaf0cc385
Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
38
diff
changeset
|
65 /* Note: Make sure to link only Vorbis xor Tremor, not both. */ |
38 | 66 #ifdef SOUND_SUPPORTS_OGG |
67 { 0, &__Sound_DecoderFunctions_OGG }, | |
68 #endif | |
69 { 0, NULL } | |
70 }; | |
71 | |
72 | |
73 #include <ctype.h> | |
74 int SoundDecoder_strcasecmp(const char* str1, const char* str2) | |
75 { | |
76 int the_char1; | |
77 int the_char2; | |
78 int i = 0; | |
79 if(str1 == str2) | |
80 { | |
81 return 0; | |
82 } | |
83 if(NULL == str1) | |
84 { | |
85 return -1; | |
86 } | |
87 if(NULL == str2) | |
88 { | |
89 return 1; | |
90 } | |
91 | |
92 do | |
93 { | |
94 the_char1 = tolower(str1[i]); | |
95 the_char2 = tolower(str2[i]); | |
96 if(the_char1 < the_char2) | |
97 { | |
98 return -1; | |
99 } | |
100 else if(the_char1 > the_char2) | |
101 { | |
102 return 1; | |
103 } | |
104 i++; | |
105 } while( (0 != the_char1) && (0 != the_char2) ); | |
106 | |
107 return 0; | |
108 } | |
109 | |
110 | |
111 #ifdef ANDROID_NDK | |
112 #include <stdarg.h> | |
113 #include <android/log.h> | |
114 int SoundDecoder_DebugPrint(const char* format, ...) | |
115 { | |
116 va_list arg_list; | |
117 int ret_val; | |
118 | |
119 va_start(arg_list, format); | |
120 ret_val = __android_log_vprint(ANDROID_LOG_INFO, "SoundDecoder", format, arg_list); | |
121 va_end(arg_list); | |
122 return ret_val; | |
123 } | |
124 #endif | |
125 | |
126 const char* SoundDecoder_GetError() | |
127 { | |
128 const char* error_string = NULL; | |
129 if(NULL == s_errorPool) | |
130 { | |
131 return "Error: You should not call SoundDecoder_GetError while Sound is not initialized"; | |
132 } | |
133 error_string = TError_GetLastErrorStr(s_errorPool); | |
134 /* SDL returns empty strings instead of NULL */ | |
135 if(NULL == error_string) | |
136 { | |
137 return ""; | |
138 } | |
139 else | |
140 { | |
141 return error_string; | |
142 } | |
143 } | |
144 | |
145 void SoundDecoder_ClearError() | |
146 { | |
147 if(NULL == s_errorPool) | |
148 { | |
149 return; | |
150 } | |
151 TError_SetError(s_errorPool, 0, NULL); | |
152 } | |
153 | |
154 void SoundDecoder_SetError(const char* err_str, ...) | |
155 { | |
156 if(NULL == s_errorPool) | |
157 { | |
158 fprintf(stderr, "Error: You should not call SoundDecoder_SetError while Sound is not initialized\n"); | |
159 return; | |
160 } | |
161 va_list argp; | |
162 va_start(argp, err_str); | |
163 // SDL_SetError which I'm emulating has no number parameter. | |
164 TError_SetErrorv(s_errorPool, 1, err_str, argp); | |
165 va_end(argp); | |
166 #ifdef ANDROID_NDK | |
167 __android_log_print(ANDROID_LOG_INFO, "SoundDecoder_SetError", TError_GetLastErrorStr(s_errorPool)); | |
168 #endif | |
169 } | |
170 | |
171 | |
172 void SoundDecoder_GetLinkedVersion(SoundDecoder_Version* the_version) | |
173 { | |
174 if(NULL != the_version) | |
175 { | |
176 the_version->major = SOUNDDECODER_VER_MAJOR; | |
177 the_version->minor = SOUNDDECODER_VER_MINOR; | |
178 the_version->patch = SOUNDDECODER_VER_PATCH; | |
179 } | |
180 } | |
181 | |
182 | |
183 const SoundDecoder_DecoderInfo** SoundDecoder_AvailableDecoders() | |
184 { | |
185 return(s_availableDecoders); | |
186 } | |
187 | |
188 int SoundDecoder_Init() | |
189 { | |
190 size_t total_number_of_decoders; | |
191 size_t i; | |
192 size_t current_pos = 0; | |
193 if(1 == s_isInitialized) | |
194 { | |
195 return 1; | |
196 } | |
197 if(NULL == s_errorPool) | |
198 { | |
199 s_errorPool = TError_CreateErrorPool(); | |
200 } | |
201 if(NULL == s_errorPool) | |
202 { | |
203 return 0; | |
204 } | |
205 | |
206 total_number_of_decoders = sizeof(s_linkedDecoders) / sizeof(s_linkedDecoders[0]); | |
207 s_availableDecoders = (const SoundDecoder_DecoderInfo **)malloc((total_number_of_decoders) * sizeof(SoundDecoder_DecoderInfo*)); | |
208 if(NULL == s_availableDecoders) | |
209 { | |
210 SoundDecoder_SetError(ERR_OUT_OF_MEMORY); | |
211 return 0; | |
212 } | |
213 | |
214 /* Allocate memory for linked list of sound samples. */ | |
215 s_listOfLoadedSamples = LinkedList_Create(); | |
216 if(NULL == s_listOfLoadedSamples) | |
217 { | |
218 free(s_availableDecoders); | |
219 s_availableDecoders = NULL; | |
220 SoundDecoder_SetError(ERR_OUT_OF_MEMORY); | |
221 return 0; | |
222 } | |
223 | |
224 for(i = 0; s_linkedDecoders[i].funcs != NULL; i++) | |
225 { | |
226 s_linkedDecoders[i].available = s_linkedDecoders[i].funcs->init(); | |
227 if(s_linkedDecoders[i].available) | |
228 { | |
229 s_availableDecoders[current_pos] = &(s_linkedDecoders[i].funcs->info); | |
230 current_pos++; | |
231 } | |
232 } | |
233 | |
234 s_availableDecoders[current_pos] = NULL; | |
235 s_isInitialized = 1; | |
236 return 1; | |
237 } | |
238 | |
239 void SoundDecoder_Quit() | |
240 { | |
241 size_t i; | |
242 if(0 == s_isInitialized) | |
243 { | |
244 return; | |
245 } | |
246 | |
247 /* | |
248 * SDL_sound actually embeds the linked list in the internal data structure. | |
249 * So any sample can potentially reach any other sample. | |
250 * But I'm keeping my own separate list. | |
251 */ | |
252 while(LinkedList_Size(s_listOfLoadedSamples) > 0) | |
253 { | |
254 SoundDecoder_Sample* sound_sample = (SoundDecoder_Sample*)LinkedList_PopBack(s_listOfLoadedSamples); | |
255 SoundDecoder_FreeSample(sound_sample); | |
256 } | |
257 LinkedList_Free(s_listOfLoadedSamples); | |
258 s_listOfLoadedSamples = NULL; | |
259 | |
260 | |
261 for(i = 0; s_linkedDecoders[i].funcs != NULL; i++) | |
262 { | |
263 if (s_linkedDecoders[i].available) | |
264 { | |
265 s_linkedDecoders[i].funcs->quit(); | |
266 s_linkedDecoders[i].available = 0; | |
267 } | |
268 } | |
269 | |
270 if(NULL != s_availableDecoders) | |
271 { | |
272 free(s_availableDecoders); | |
273 } | |
274 s_availableDecoders = NULL; | |
275 | |
276 | |
277 /* Remember: ALmixer_SetError/GetError calls will not work while this is gone. */ | |
278 TError_FreeErrorPool(s_errorPool); | |
279 s_errorPool = NULL; | |
280 | |
281 s_isInitialized = 0; | |
282 } | |
283 | |
284 | |
285 void SoundDecoder_FreeSample(SoundDecoder_Sample* sound_sample) | |
286 { | |
287 SoundDecoder_SampleInternal* sample_internal; | |
288 | |
289 /* Quit unloads all samples, so it is not possible to free a sample | |
290 * when not initialized. | |
291 */ | |
292 if(0 == s_isInitialized) | |
293 { | |
294 return; | |
295 } | |
296 | |
297 if(sound_sample == NULL) | |
298 { | |
299 return; | |
300 } | |
301 | |
302 /* SDL_sound keeps a linked list of all the loaded samples. | |
303 * We want to remove the current sample from that list. | |
304 */ | |
305 LinkedListNode* the_node = LinkedList_Find(s_listOfLoadedSamples, sound_sample, NULL); | |
306 if(NULL == the_node) | |
307 { | |
308 SoundDecoder_SetError("SoundDecoder_FreeSample: Internal Error, sample does not exist in linked list."); | |
309 return; | |
310 } | |
311 LinkedList_Remove(s_listOfLoadedSamples, the_node); | |
312 | |
313 sample_internal = (SoundDecoder_SampleInternal*)sound_sample->opaque; | |
314 | |
315 /* Ugh...SDL_sound has a lot of pointers. | |
316 * I hope I didn't miss any dynamic memory. | |
317 */ | |
318 | |
319 /* Call close on the decoder */ | |
320 sample_internal->funcs->close(sound_sample); | |
321 | |
322 if(NULL != sample_internal->rw) | |
323 { | |
324 sample_internal->rw->close(sample_internal->rw); | |
325 } | |
326 | |
327 /* Ooops. The public buffer might be shared with the internal buffer. | |
328 * Make sure to not accidentally double delete. | |
329 */ | |
330 if((NULL != sample_internal->buffer) | |
331 && (sample_internal->buffer != sound_sample->buffer) | |
332 ) | |
333 { | |
334 free(sample_internal->buffer); | |
335 } | |
336 free(sample_internal); | |
337 | |
338 if(NULL != sound_sample->buffer) | |
339 { | |
340 free(sound_sample->buffer); | |
341 } | |
342 free(sound_sample); | |
343 } | |
344 | |
345 | |
346 | |
347 static int Internal_LoadSample(const SoundDecoder_DecoderFunctions *funcs, | |
348 SoundDecoder_Sample* sound_sample, const char *ext | |
349 ) | |
350 { | |
351 SoundDecoder_SampleInternal* internal_sample = (SoundDecoder_SampleInternal*)sound_sample->opaque; | |
352 long current_file_position = internal_sample->rw->seek(internal_sample->rw, 0, SEEK_CUR); | |
353 | |
354 /* fill in the funcs for this decoder... */ | |
355 sound_sample->decoder = &funcs->info; | |
356 internal_sample->funcs = funcs; | |
357 if (!funcs->open(sound_sample, ext)) | |
358 { | |
359 internal_sample->rw->seek(internal_sample->rw, current_file_position, SEEK_SET); | |
360 return 0; | |
361 } | |
362 | |
363 /* we found a compatible decoder */ | |
364 | |
365 /* SDL_sound normally goes on to setup a bunch of things to | |
366 * support format conversion via SDL APIs. | |
367 * I am not porting any of that stuff. | |
368 * My goal is to simply setup the struct properties to values | |
369 * that will not cause any confusion with other parts of the implementation. | |
370 */ | |
371 | |
372 | |
373 if(0 == sound_sample->desired.format) | |
374 { | |
375 sound_sample->desired.format = sound_sample->actual.format; | |
376 } | |
377 if(0 == sound_sample->desired.channels) | |
378 { | |
379 sound_sample->desired.channels = sound_sample->actual.channels; | |
380 } | |
381 if(0 == sound_sample->desired.rate) | |
382 { | |
383 sound_sample->desired.rate = sound_sample->actual.rate; | |
384 } | |
385 | |
386 /* I'm a little confused at the difference between the internal | |
387 * public buffer. I am going to make them the same. | |
388 * I assume I already allocated the public buffer. | |
389 */ | |
390 internal_sample->buffer = sound_sample->buffer; | |
391 internal_sample->buffer_size = sound_sample->buffer_size; | |
392 | |
393 /* Insert the new sample into the linked list of samples. */ | |
394 LinkedList_PushBack(s_listOfLoadedSamples, sound_sample); | |
395 | |
396 return 1; | |
397 } | |
398 | |
399 | |
400 | |
401 SoundDecoder_Sample* SoundDecoder_NewSampleFromFile(const char* file_name, | |
402 SoundDecoder_AudioInfo* desired_format, | |
403 size_t buffer_size) | |
404 { | |
405 | |
406 const char* file_extension; | |
407 ALmixer_RWops* rw_ops; | |
408 | |
409 | |
410 if(0 == s_isInitialized) | |
411 { | |
412 SoundDecoder_SetError(ERR_NOT_INITIALIZED); | |
413 return NULL; | |
414 } | |
415 if(NULL == file_name) | |
416 { | |
417 SoundDecoder_SetError("No file specified"); | |
418 return NULL; | |
419 } | |
420 | |
421 file_extension = strrchr(file_name, '.'); | |
422 if(NULL != file_extension) | |
423 { | |
424 file_extension++; | |
425 } | |
426 | |
427 rw_ops = ALmixer_RWFromFile(file_name, "rb"); | |
428 | |
429 return SoundDecoder_NewSample(rw_ops, file_extension, desired_format, buffer_size); | |
430 } | |
431 | |
432 | |
433 SoundDecoder_Sample* SoundDecoder_NewSample(ALmixer_RWops* rw_ops, const char* file_extension, SoundDecoder_AudioInfo* desired_format, size_t buffer_size) | |
434 { | |
435 SoundDecoder_Sample* new_sample; | |
436 SoundDecoder_SampleInternal* internal_sample; | |
437 SoundElement* current_decoder; | |
438 | |
439 if(0 == s_isInitialized) | |
440 { | |
441 SoundDecoder_SetError(ERR_NOT_INITIALIZED); | |
442 return NULL; | |
443 } | |
444 if(NULL == rw_ops) | |
445 { | |
446 SoundDecoder_SetError("No file specified"); | |
447 return NULL; | |
448 } | |
449 | |
450 new_sample = (SoundDecoder_Sample*)calloc(1, sizeof(SoundDecoder_Sample)); | |
451 if(NULL == new_sample) | |
452 { | |
453 SoundDecoder_SetError(ERR_OUT_OF_MEMORY); | |
454 return NULL; | |
455 } | |
456 internal_sample = (SoundDecoder_SampleInternal*)calloc(1, sizeof(SoundDecoder_SampleInternal)); | |
457 if(NULL == internal_sample) | |
458 { | |
459 free(new_sample); | |
460 SoundDecoder_SetError(ERR_OUT_OF_MEMORY); | |
461 return NULL; | |
462 } | |
463 new_sample->buffer = calloc(1, buffer_size); | |
464 if(NULL == new_sample->buffer) | |
465 { | |
466 free(internal_sample); | |
467 free(new_sample); | |
468 SoundDecoder_SetError(ERR_OUT_OF_MEMORY); | |
469 return NULL; | |
470 } | |
471 | |
472 new_sample->buffer_size = buffer_size; | |
473 | |
474 if(NULL != desired_format) | |
475 { | |
476 memcpy(&new_sample->desired, desired_format, sizeof(SoundDecoder_AudioInfo)); | |
477 } | |
478 | |
479 internal_sample->rw = rw_ops; | |
480 new_sample->opaque = internal_sample; | |
481 | |
482 | |
483 if(NULL != file_extension) | |
484 { | |
485 for(current_decoder = &s_linkedDecoders[0]; current_decoder->funcs != NULL; current_decoder++) | |
486 { | |
487 if(current_decoder->available) | |
488 { | |
489 const char** decoder_file_extension = current_decoder->funcs->info.extensions; | |
490 while(*decoder_file_extension) | |
491 { | |
492 if(0 == (SoundDecoder_strcasecmp(*decoder_file_extension, file_extension))) | |
493 { | |
494 if(Internal_LoadSample(current_decoder->funcs, new_sample, file_extension)) | |
495 { | |
496 return(new_sample); | |
497 } | |
498 break; /* go to the next decoder */ | |
499 } | |
500 decoder_file_extension++; | |
501 } | |
502 } | |
503 } | |
504 } | |
505 | |
506 /* no direct file_extensionension match? Try everything we've got... */ | |
507 for(current_decoder = &s_linkedDecoders[0]; current_decoder->funcs != NULL; current_decoder++) | |
508 { | |
509 if(current_decoder->available) | |
510 { | |
511 int already_tried_decoder = 0; | |
512 const char** decoder_file_extension = current_decoder->funcs->info.extensions; | |
513 | |
514 /* skip decoders we already tried from the above loop */ | |
515 while(*decoder_file_extension) | |
516 { | |
517 if(SoundDecoder_strcasecmp(*decoder_file_extension, file_extension) == 0) | |
518 { | |
519 already_tried_decoder = 1; | |
520 break; | |
521 } | |
522 decoder_file_extension++; | |
523 } | |
524 | |
525 if(0 == already_tried_decoder) | |
526 { | |
527 if (Internal_LoadSample(current_decoder->funcs, new_sample, file_extension)) | |
528 { | |
529 return new_sample; | |
530 } | |
531 } | |
532 } | |
533 } | |
534 | |
535 /* could not find a decoder */ | |
536 SoundDecoder_SetError(ERR_UNSUPPORTED_FORMAT); | |
537 /* clean up the memory */ | |
538 free(new_sample->opaque); | |
539 if(NULL != new_sample->buffer) | |
540 { | |
541 free(new_sample->buffer); | |
542 } | |
543 free(new_sample); | |
544 | |
545 rw_ops->close(rw_ops); | |
546 return NULL; | |
547 } | |
548 | |
549 | |
550 int SoundDecoder_SetBufferSize(SoundDecoder_Sample* sound_sample, size_t new_buffer_size) | |
551 { | |
552 SoundDecoder_SampleInternal* internal_sample = NULL; | |
553 void* new_buffer_ptr = NULL; | |
554 | |
555 BAIL_IF_MACRO(!s_isInitialized, ERR_NOT_INITIALIZED, 0); | |
556 BAIL_IF_MACRO(NULL == sound_sample, ERR_NULL_SAMPLE, 0); | |
557 | |
558 internal_sample = ((SoundDecoder_SampleInternal*)sound_sample->opaque); | |
559 | |
560 | |
561 new_buffer_ptr = realloc(sound_sample->buffer, new_buffer_size); | |
562 BAIL_IF_MACRO(NULL == new_buffer_ptr, ERR_OUT_OF_MEMORY, 0); | |
563 | |
564 sound_sample->buffer = new_buffer_ptr; | |
565 sound_sample->buffer_size = new_buffer_size; | |
566 internal_sample->buffer = sound_sample->buffer; | |
567 internal_sample->buffer_size = sound_sample->buffer_size; | |
568 | |
569 return 1; | |
570 } | |
571 | |
572 | |
573 size_t SoundDecoder_Decode(SoundDecoder_Sample* sound_sample) | |
574 { | |
575 SoundDecoder_SampleInternal* internal_sample = NULL; | |
576 size_t bytes_read = 0; | |
577 | |
578 BAIL_IF_MACRO(!s_isInitialized, ERR_NOT_INITIALIZED, 0); | |
579 BAIL_IF_MACRO(NULL == sound_sample, ERR_NULL_SAMPLE, 0); | |
580 BAIL_IF_MACRO(sound_sample->flags & SOUND_SAMPLEFLAG_ERROR, ERR_PREVIOUS_SAMPLE_ERROR, 0); | |
581 BAIL_IF_MACRO(sound_sample->flags & SOUND_SAMPLEFLAG_EOF, ERR_ALREADY_AT_EOF_ERROR, 0); | |
582 | |
583 internal_sample = (SoundDecoder_SampleInternal*)sound_sample->opaque; | |
584 | |
585 assert(sound_sample->buffer != NULL); | |
586 assert(sound_sample->buffer_size > 0); | |
587 assert(internal_sample->buffer != NULL); | |
588 assert(internal_sample->buffer_size > 0); | |
589 | |
590 /* reset EAGAIN. Decoder can flip it back on if it needs to. */ | |
591 sound_sample->flags &= ~SOUND_SAMPLEFLAG_EAGAIN; | |
592 bytes_read = internal_sample->funcs->read(sound_sample); | |
593 return bytes_read; | |
594 } | |
595 | |
596 | |
597 size_t SoundDecoder_DecodeAll(SoundDecoder_Sample* sound_sample) | |
598 { | |
599 SoundDecoder_SampleInternal* internal_sample = NULL; | |
600 void* data_buffer = NULL; | |
601 size_t updated_buffer_size = 0; | |
602 | |
603 BAIL_IF_MACRO(!s_isInitialized, ERR_NOT_INITIALIZED, 0); | |
604 BAIL_IF_MACRO(NULL == sound_sample, ERR_NULL_SAMPLE, 0); | |
605 | |
606 /* My original thought was to call SetBufferSize and resize to | |
607 * the size needed to hold the entire decoded file utilizing total_time, | |
608 * but it appears SDL_sound simply loops on SoundDecoder_Decode. | |
609 * I suppose it is possible to partially decode or seek a file, and then | |
610 * call DecodeAll so you won't have the whole thing in which case | |
611 * my idea would waste memory. | |
612 */ | |
613 while( (0 == (sound_sample->flags & SOUND_SAMPLEFLAG_EOF) ) | |
614 && (0 == (sound_sample->flags & SOUND_SAMPLEFLAG_ERROR)) | |
615 ) | |
616 { | |
617 size_t bytes_decoded = SoundDecoder_Decode(sound_sample); | |
618 void* realloced_ptr = realloc(data_buffer, updated_buffer_size + bytes_decoded); | |
619 if(NULL == realloced_ptr) | |
620 { | |
621 sound_sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
622 SoundDecoder_SetError(ERR_OUT_OF_MEMORY); | |
623 if(NULL != data_buffer) | |
624 { | |
625 free(data_buffer); | |
626 } | |
627 return bytes_decoded; | |
628 } | |
629 data_buffer = realloced_ptr; | |
630 /* copy the chunk of decoded PCM to the end of our new data buffer */ | |
631 memcpy( ((char*)data_buffer) + updated_buffer_size, sound_sample->buffer, bytes_decoded ); | |
632 updated_buffer_size += bytes_decoded; | |
633 } | |
634 | |
635 internal_sample = (SoundDecoder_SampleInternal*)sound_sample->opaque; | |
636 if(internal_sample->buffer != sound_sample->buffer) | |
637 { | |
638 free(internal_sample->buffer); | |
639 } | |
640 free(sound_sample->buffer); | |
641 | |
642 sound_sample->buffer = data_buffer; | |
643 sound_sample->buffer_size = updated_buffer_size; | |
644 internal_sample->buffer = sound_sample->buffer; | |
645 internal_sample->buffer_size = sound_sample->buffer_size; | |
646 | |
647 return sound_sample->buffer_size; | |
648 } | |
649 | |
650 | |
651 int SoundDecoder_Rewind(SoundDecoder_Sample* sound_sample) | |
652 { | |
653 SoundDecoder_SampleInternal* internal_sample; | |
654 int ret_val; | |
655 | |
656 BAIL_IF_MACRO(!s_isInitialized, ERR_NOT_INITIALIZED, 0); | |
657 BAIL_IF_MACRO(NULL == sound_sample, ERR_NULL_SAMPLE, 0); | |
658 | |
659 internal_sample = (SoundDecoder_SampleInternal*)sound_sample->opaque; | |
660 ret_val = internal_sample->funcs->rewind(sound_sample); | |
661 if(0 == ret_val) | |
662 { | |
663 sound_sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
664 SoundDecoder_SetError("Rewind failed"); | |
665 return 0; | |
666 } | |
667 /* reset flags */ | |
668 sound_sample->flags &= ~SOUND_SAMPLEFLAG_EAGAIN; | |
669 sound_sample->flags &= ~SOUND_SAMPLEFLAG_ERROR; | |
670 sound_sample->flags &= ~SOUND_SAMPLEFLAG_EOF; | |
671 | |
672 return 1; | |
673 } | |
674 | |
675 | |
676 int SoundDecoder_Seek(SoundDecoder_Sample* sound_sample, size_t ms) | |
677 { | |
678 SoundDecoder_SampleInternal* internal_sample; | |
679 int ret_val; | |
680 | |
681 BAIL_IF_MACRO(!s_isInitialized, ERR_NOT_INITIALIZED, 0); | |
682 BAIL_IF_MACRO(NULL == sound_sample, ERR_NULL_SAMPLE, 0); | |
683 | |
684 BAIL_IF_MACRO(!(sound_sample->flags & SOUND_SAMPLEFLAG_CANSEEK), "Sound sample is not seekable", 0); | |
685 | |
686 internal_sample = (SoundDecoder_SampleInternal*)sound_sample->opaque; | |
687 ret_val = internal_sample->funcs->seek(sound_sample, ms); | |
688 if(0 == ret_val) | |
689 { | |
690 sound_sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
691 SoundDecoder_SetError("Seek failed"); | |
692 return 0; | |
693 } | |
694 /* reset flags */ | |
695 sound_sample->flags &= ~SOUND_SAMPLEFLAG_EAGAIN; | |
696 sound_sample->flags &= ~SOUND_SAMPLEFLAG_ERROR; | |
697 sound_sample->flags &= ~SOUND_SAMPLEFLAG_EOF; | |
698 | |
699 return 1; | |
700 } | |
701 | |
702 | |
703 ptrdiff_t SoundDecoder_GetDuration(SoundDecoder_Sample* sound_sample) | |
704 { | |
705 SoundDecoder_SampleInternal* internal_sample; | |
706 BAIL_IF_MACRO(!s_isInitialized, ERR_NOT_INITIALIZED, -1); | |
707 BAIL_IF_MACRO(NULL == sound_sample, ERR_NULL_SAMPLE, 0); | |
708 internal_sample = (SoundDecoder_SampleInternal*)sound_sample->opaque; | |
709 return internal_sample->total_time; | |
710 } | |
711 | |
712 #endif |