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