Skip to content

Latest commit

 

History

History
895 lines (702 loc) · 23.7 KB

SDL_sound.c

File metadata and controls

895 lines (702 loc) · 23.7 KB
 
Sep 17, 2001
Sep 17, 2001
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. :)
*
Dec 26, 2001
Dec 26, 2001
26
* Please see the file COPYING in the source's root directory.
Sep 17, 2001
Sep 17, 2001
27
28
29
30
*
* This file written by Ryan C. Gordon. (icculus@clutteredmind.org)
*/
Oct 3, 2001
Oct 3, 2001
31
32
33
#if HAVE_CONFIG_H
# include <config.h>
#endif
Sep 17, 2001
Sep 17, 2001
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "SDL.h"
#include "SDL_sound.h"
#define __SDL_SOUND_INTERNAL__
#include "SDL_sound_internal.h"
/* The various decoder drivers... */
Feb 21, 2002
Feb 21, 2002
49
50
51
52
53
54
#if (defined SOUND_SUPPORTS_SMPEG)
extern const Sound_DecoderFunctions __Sound_DecoderFunctions_SMPEG;
#endif
#if (defined SOUND_SUPPORTS_MPGLIB)
extern const Sound_DecoderFunctions __Sound_DecoderFunctions_MPGLIB;
Sep 18, 2001
Sep 18, 2001
55
56
#endif
Jan 10, 2002
Jan 10, 2002
57
58
59
60
61
62
#if (defined SOUND_SUPPORTS_MIKMOD)
extern const Sound_DecoderFunctions __Sound_DecoderFunctions_MIKMOD;
#endif
#if (defined SOUND_SUPPORTS_MODPLUG)
extern const Sound_DecoderFunctions __Sound_DecoderFunctions_MODPLUG;
Sep 22, 2001
Sep 22, 2001
63
64
#endif
Sep 19, 2001
Sep 19, 2001
65
66
67
68
#if (defined SOUND_SUPPORTS_WAV)
extern const Sound_DecoderFunctions __Sound_DecoderFunctions_WAV;
#endif
Sep 20, 2001
Sep 20, 2001
69
70
71
72
#if (defined SOUND_SUPPORTS_AIFF)
extern const Sound_DecoderFunctions __Sound_DecoderFunctions_AIFF;
#endif
Jan 13, 2002
Jan 13, 2002
73
74
75
76
#if (defined SOUND_SUPPORTS_AU)
extern const Sound_DecoderFunctions __Sound_DecoderFunctions_AU;
#endif
Sep 19, 2001
Sep 19, 2001
77
78
79
80
#if (defined SOUND_SUPPORTS_OGG)
extern const Sound_DecoderFunctions __Sound_DecoderFunctions_OGG;
#endif
Sep 17, 2001
Sep 17, 2001
81
82
83
84
85
86
87
88
#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
Oct 3, 2001
Oct 3, 2001
89
90
91
92
#if (defined SOUND_SUPPORTS_SHN)
extern const Sound_DecoderFunctions __Sound_DecoderFunctions_SHN;
#endif
Oct 3, 2001
Oct 3, 2001
93
94
95
96
#if (defined SOUND_SUPPORTS_MIDI)
extern const Sound_DecoderFunctions __Sound_DecoderFunctions_MIDI;
#endif
Nov 9, 2001
Nov 9, 2001
97
98
99
100
#if (defined SOUND_SUPPORTS_FLAC)
extern const Sound_DecoderFunctions __Sound_DecoderFunctions_FLAC;
#endif
Apr 29, 2002
Apr 29, 2002
101
102
103
#if (defined SOUND_SUPPORTS_QUICKTIME)
extern const Sound_DecoderFunctions __Sound_DecoderFunctions_QuickTime;
#endif
Sep 22, 2001
Sep 22, 2001
104
105
106
107
108
109
110
111
typedef struct
{
int available;
const Sound_DecoderFunctions *funcs;
} decoder_element;
static decoder_element decoders[] =
Sep 17, 2001
Sep 17, 2001
112
{
Feb 21, 2002
Feb 21, 2002
113
114
115
116
117
118
#if (defined SOUND_SUPPORTS_SMPEG)
{ 0, &__Sound_DecoderFunctions_SMPEG },
#endif
#if (defined SOUND_SUPPORTS_MPGLIB)
{ 0, &__Sound_DecoderFunctions_MPGLIB },
Sep 22, 2001
Sep 22, 2001
119
120
#endif
Jan 10, 2002
Jan 10, 2002
121
122
#if (defined SOUND_SUPPORTS_MODPLUG)
{ 0, &__Sound_DecoderFunctions_MODPLUG },
Sep 18, 2001
Sep 18, 2001
123
124
#endif
Jan 19, 2002
Jan 19, 2002
125
126
127
128
#if (defined SOUND_SUPPORTS_MIKMOD)
{ 0, &__Sound_DecoderFunctions_MIKMOD },
#endif
Sep 19, 2001
Sep 19, 2001
129
#if (defined SOUND_SUPPORTS_WAV)
Sep 22, 2001
Sep 22, 2001
130
{ 0, &__Sound_DecoderFunctions_WAV },
Sep 19, 2001
Sep 19, 2001
131
132
#endif
Sep 20, 2001
Sep 20, 2001
133
#if (defined SOUND_SUPPORTS_AIFF)
Sep 22, 2001
Sep 22, 2001
134
{ 0, &__Sound_DecoderFunctions_AIFF },
Sep 20, 2001
Sep 20, 2001
135
136
#endif
Jan 13, 2002
Jan 13, 2002
137
138
139
140
#if (defined SOUND_SUPPORTS_AU)
{ 0, &__Sound_DecoderFunctions_AU },
#endif
Sep 19, 2001
Sep 19, 2001
141
#if (defined SOUND_SUPPORTS_OGG)
Sep 22, 2001
Sep 22, 2001
142
{ 0, &__Sound_DecoderFunctions_OGG },
Sep 19, 2001
Sep 19, 2001
143
144
#endif
Sep 17, 2001
Sep 17, 2001
145
#if (defined SOUND_SUPPORTS_VOC)
Sep 22, 2001
Sep 22, 2001
146
{ 0, &__Sound_DecoderFunctions_VOC },
Sep 17, 2001
Sep 17, 2001
147
148
149
#endif
#if (defined SOUND_SUPPORTS_RAW)
Sep 22, 2001
Sep 22, 2001
150
{ 0, &__Sound_DecoderFunctions_RAW },
Sep 17, 2001
Sep 17, 2001
151
#endif
Oct 3, 2001
Oct 3, 2001
152
153
154
155
#if (defined SOUND_SUPPORTS_SHN)
{ 0, &__Sound_DecoderFunctions_SHN },
#endif
Oct 3, 2001
Oct 3, 2001
156
Nov 9, 2001
Nov 9, 2001
157
158
159
160
#if (defined SOUND_SUPPORTS_FLAC)
{ 0, &__Sound_DecoderFunctions_FLAC },
#endif
Nov 19, 2001
Nov 19, 2001
161
162
163
164
#if (defined SOUND_SUPPORTS_MIDI)
{ 0, &__Sound_DecoderFunctions_MIDI },
#endif
Apr 29, 2002
Apr 29, 2002
165
166
167
168
#if (defined SOUND_SUPPORTS_QUICKTIME)
{ 0, &__Sound_DecoderFunctions_QuickTime },
#endif
Oct 6, 2001
Oct 6, 2001
169
{ 0, NULL }
Sep 17, 2001
Sep 17, 2001
170
171
172
173
174
175
};
/* General SDL_sound state ... */
Jan 13, 2002
Jan 13, 2002
176
177
178
typedef struct __SOUND_ERRMSGTYPE__
{
Uint32 tid;
Jan 19, 2002
Jan 19, 2002
179
180
int error_available;
char error_string[128];
Jan 13, 2002
Jan 13, 2002
181
182
183
struct __SOUND_ERRMSGTYPE__ *next;
} ErrMsg;
Jan 19, 2002
Jan 19, 2002
184
static ErrMsg *error_msgs = NULL;
Jan 13, 2002
Jan 13, 2002
185
186
static SDL_mutex *errorlist_mutex = NULL;
Jan 19, 2002
Jan 19, 2002
187
188
189
static Sound_Sample *sample_list = NULL; /* this is a linked list. */
static SDL_mutex *samplelist_mutex = NULL;
Sep 17, 2001
Sep 17, 2001
190
static const Sound_DecoderInfo **available_decoders = NULL;
Jan 13, 2002
Jan 13, 2002
191
static int initialized = 0;
Sep 17, 2001
Sep 17, 2001
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* 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;
Sep 22, 2001
Sep 22, 2001
210
211
size_t pos = 0;
size_t total = sizeof (decoders) / sizeof (decoders[0]);
Sep 17, 2001
Sep 17, 2001
212
BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
Jan 13, 2002
Jan 13, 2002
213
Jan 19, 2002
Jan 19, 2002
214
215
sample_list = NULL;
error_msgs = NULL;
Sep 17, 2001
Sep 17, 2001
216
217
available_decoders = (const Sound_DecoderInfo **)
Oct 6, 2001
Oct 6, 2001
218
malloc((total) * sizeof (Sound_DecoderInfo *));
Sep 17, 2001
Sep 17, 2001
219
220
BAIL_IF_MACRO(available_decoders == NULL, ERR_OUT_OF_MEMORY, 0);
Jan 19, 2002
Jan 19, 2002
221
SDL_Init(SDL_INIT_AUDIO);
Jan 19, 2002
Jan 19, 2002
222
Jan 19, 2002
Jan 19, 2002
223
224
errorlist_mutex = SDL_CreateMutex();
Oct 6, 2001
Oct 6, 2001
225
for (i = 0; decoders[i].funcs != NULL; i++)
Sep 22, 2001
Sep 22, 2001
226
227
228
229
230
231
232
233
{
decoders[i].available = decoders[i].funcs->init();
if (decoders[i].available)
{
available_decoders[pos] = &(decoders[i].funcs->info);
pos++;
} /* if */
} /* for */
Sep 17, 2001
Sep 17, 2001
234
Sep 22, 2001
Sep 22, 2001
235
available_decoders[pos] = NULL;
Sep 19, 2001
Sep 19, 2001
236
Sep 17, 2001
Sep 17, 2001
237
238
239
240
241
242
243
initialized = 1;
return(1);
} /* Sound_Init */
int Sound_Quit(void)
{
Jan 13, 2002
Jan 13, 2002
244
245
ErrMsg *err;
ErrMsg *nexterr = NULL;
Sep 22, 2001
Sep 22, 2001
246
247
size_t i;
Sep 17, 2001
Sep 17, 2001
248
249
BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
Jan 19, 2002
Jan 19, 2002
250
251
while (((volatile Sound_Sample *) sample_list) != NULL)
Sound_FreeSample(sample_list);
Sep 17, 2001
Sep 17, 2001
252
Jan 19, 2002
Jan 19, 2002
253
254
255
256
257
initialized = 0;
SDL_DestroyMutex(samplelist_mutex);
samplelist_mutex = NULL;
sample_list = NULL;
Jan 13, 2002
Jan 13, 2002
258
Oct 6, 2001
Oct 6, 2001
259
for (i = 0; decoders[i].funcs != NULL; i++)
Sep 22, 2001
Sep 22, 2001
260
261
262
263
264
265
266
267
{
if (decoders[i].available)
{
decoders[i].funcs->quit();
decoders[i].available = 0;
} /* if */
} /* for */
Sep 17, 2001
Sep 17, 2001
268
if (available_decoders != NULL)
Sep 25, 2001
Sep 25, 2001
269
free((void *) available_decoders);
Sep 17, 2001
Sep 17, 2001
270
271
available_decoders = NULL;
Jan 13, 2002
Jan 13, 2002
272
273
/* clean up error state for each thread... */
SDL_LockMutex(errorlist_mutex);
Jan 19, 2002
Jan 19, 2002
274
for (err = error_msgs; err != NULL; err = nexterr)
Jan 13, 2002
Jan 13, 2002
275
276
277
278
{
nexterr = err->next;
free(err);
} /* for */
Jan 19, 2002
Jan 19, 2002
279
error_msgs = NULL;
Jan 13, 2002
Jan 13, 2002
280
281
282
SDL_UnlockMutex(errorlist_mutex);
SDL_DestroyMutex(errorlist_mutex);
errorlist_mutex = NULL;
Sep 17, 2001
Sep 17, 2001
283
284
285
286
287
288
289
290
291
292
293
return(1);
} /* Sound_Quit */
const Sound_DecoderInfo **Sound_AvailableDecoders(void)
{
return(available_decoders); /* READ. ONLY. */
} /* Sound_AvailableDecoders */
Jan 13, 2002
Jan 13, 2002
294
295
296
297
298
static ErrMsg *findErrorForCurrentThread(void)
{
ErrMsg *i;
Uint32 tid;
Jan 19, 2002
Jan 19, 2002
299
if (error_msgs != NULL)
Jan 13, 2002
Jan 13, 2002
300
301
302
303
{
tid = SDL_ThreadID();
SDL_LockMutex(errorlist_mutex);
Jan 19, 2002
Jan 19, 2002
304
for (i = error_msgs; i != NULL; i = i->next)
Jan 13, 2002
Jan 13, 2002
305
306
307
308
309
310
311
312
313
314
315
316
317
318
{
if (i->tid == tid)
{
SDL_UnlockMutex(errorlist_mutex);
return(i);
} /* if */
} /* for */
SDL_UnlockMutex(errorlist_mutex);
} /* if */
return(NULL); /* no error available. */
} /* findErrorForCurrentThread */
Sep 17, 2001
Sep 17, 2001
319
320
const char *Sound_GetError(void)
{
Jan 13, 2002
Jan 13, 2002
321
const char *retval = NULL;
Jan 19, 2002
Jan 19, 2002
322
323
324
325
326
327
ErrMsg *err;
if (!initialized)
return(ERR_NOT_INITIALIZED);
err = findErrorForCurrentThread();
Jan 19, 2002
Jan 19, 2002
328
if ((err != NULL) && (err->error_available))
Jan 13, 2002
Jan 13, 2002
329
{
Jan 19, 2002
Jan 19, 2002
330
331
retval = err->error_string;
err->error_available = 0;
Jan 13, 2002
Jan 13, 2002
332
333
334
} /* if */
return(retval);
Sep 17, 2001
Sep 17, 2001
335
336
337
338
339
} /* Sound_GetError */
void Sound_ClearError(void)
{
Jan 19, 2002
Jan 19, 2002
340
341
342
343
344
345
ErrMsg *err;
if (!initialized)
return;
err = findErrorForCurrentThread();
Jan 13, 2002
Jan 13, 2002
346
if (err != NULL)
Jan 19, 2002
Jan 19, 2002
347
err->error_available = 0;
Sep 17, 2001
Sep 17, 2001
348
349
350
351
352
353
} /* Sound_ClearError */
/*
* This is declared in the internal header.
*/
Apr 21, 2002
Apr 21, 2002
354
void __Sound_SetError(const char *str)
Sep 17, 2001
Sep 17, 2001
355
{
Jan 13, 2002
Jan 13, 2002
356
357
358
359
360
ErrMsg *err;
if (str == NULL)
return;
Jul 5, 2002
Jul 5, 2002
361
SNDDBG(("__Sound_SetError(\"%s\");%s\n", str,
Jan 19, 2002
Jan 19, 2002
362
363
364
365
(initialized) ? "" : " [NOT INITIALIZED!]"));
if (!initialized)
return;
Jan 13, 2002
Jan 13, 2002
366
367
368
err = findErrorForCurrentThread();
if (err == NULL)
Nov 19, 2001
Nov 19, 2001
369
{
Jan 13, 2002
Jan 13, 2002
370
371
372
373
374
375
376
377
err = (ErrMsg *) malloc(sizeof (ErrMsg));
if (err == NULL)
return; /* uhh...? */
memset((void *) err, '\0', sizeof (ErrMsg));
err->tid = SDL_ThreadID();
SDL_LockMutex(errorlist_mutex);
Jan 19, 2002
Jan 19, 2002
378
379
err->next = error_msgs;
error_msgs = err;
Jan 13, 2002
Jan 13, 2002
380
SDL_UnlockMutex(errorlist_mutex);
Nov 19, 2001
Nov 19, 2001
381
} /* if */
Jan 13, 2002
Jan 13, 2002
382
Jan 19, 2002
Jan 19, 2002
383
384
385
err->error_available = 1;
strncpy(err->error_string, str, sizeof (err->error_string));
err->error_string[sizeof (err->error_string) - 1] = '\0';
Apr 21, 2002
Apr 21, 2002
386
387
388
389
390
391
392
393
394
395
396
} /* __Sound_SetError */
Uint32 __Sound_convertMsToBytePos(Sound_AudioInfo *info, Uint32 ms)
{
/* "frames" == "sample frames" */
float frames_per_ms = ((float) info->rate) / 1000.0;
Uint32 frame_offset = (Uint32) (frames_per_ms * ((float) ms));
Uint32 frame_size = (Uint32) ((info->format & 0xFF) / 8) * info->channels;
return(frame_offset * frame_size);
} /* __Sound_convertMsToBytePos */
Sep 17, 2001
Sep 17, 2001
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* -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;
Oct 2, 2001
Oct 2, 2001
411
412
413
414
415
416
417
418
419
if (x == y) /* same pointer? Both NULL? */
return(0);
if (x == NULL)
return(-1);
if (y == NULL)
return(1);
Sep 17, 2001
Sep 17, 2001
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
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))
{
Jul 5, 2002
Jul 5, 2002
447
__Sound_SetError(ERR_OUT_OF_MEMORY);
Sep 17, 2001
Sep 17, 2001
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
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)
{
Jul 5, 2002
Jul 5, 2002
463
__Sound_SetError(ERR_OUT_OF_MEMORY);
Sep 17, 2001
Sep 17, 2001
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
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 */
Sep 18, 2001
Sep 18, 2001
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#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
Sep 17, 2001
Sep 17, 2001
504
505
506
507
508
509
510
511
512
513
514
515
/*
* 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.
*/
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;
Jul 11, 2002
Jul 11, 2002
516
int pos = SDL_RWtell(internal->rw);
Sep 17, 2001
Sep 17, 2001
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/* 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));
May 20, 2002
May 20, 2002
534
535
536
537
538
539
540
if (desired.format == 0)
desired.format = sample->actual.format;
if (desired.channels == 0)
desired.channels = sample->actual.channels;
if (desired.rate == 0)
desired.rate = sample->actual.rate;
Oct 15, 2001
Oct 15, 2001
541
542
543
544
545
546
if (Sound_BuildAudioCVT(&internal->sdlcvt,
sample->actual.format,
sample->actual.channels,
sample->actual.rate,
desired.format,
desired.channels,
Jul 3, 2002
Jul 3, 2002
547
548
desired.rate,
sample->buffer_size) == -1)
Sep 17, 2001
Sep 17, 2001
549
{
Jul 5, 2002
Jul 5, 2002
550
__Sound_SetError(SDL_GetError());
Sep 17, 2001
Sep 17, 2001
551
552
553
554
555
556
557
558
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,
Oct 15, 2001
Oct 15, 2001
559
sample->buffer_size * internal->sdlcvt.len_mult);
Sep 17, 2001
Sep 17, 2001
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
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;
Jan 19, 2002
Jan 19, 2002
576
577
578
579
580
581
582
/* Prepend our new Sound_Sample to the sample_list... */
SDL_LockMutex(samplelist_mutex);
internal->next = sample_list;
if (sample_list != NULL)
((Sound_SampleInternal *) sample_list->opaque)->prev = sample;
sample_list = sample;
SDL_UnlockMutex(samplelist_mutex);
Sep 17, 2001
Sep 17, 2001
583
Sep 24, 2001
Sep 24, 2001
584
585
586
587
SNDDBG(("New sample DESIRED format: %s format, %d rate, %d channels.\n",
fmt_to_str(sample->desired.format),
sample->desired.rate,
sample->desired.channels));
Sep 18, 2001
Sep 18, 2001
588
Sep 24, 2001
Sep 24, 2001
589
590
591
592
SNDDBG(("New sample ACTUAL format: %s format, %d rate, %d channels.\n",
fmt_to_str(sample->actual.format),
sample->actual.rate,
sample->actual.channels));
Sep 18, 2001
Sep 18, 2001
593
Sep 24, 2001
Sep 24, 2001
594
595
SNDDBG(("On-the-fly conversion: %s.\n",
internal->sdlcvt.needed ? "ENABLED" : "DISABLED"));
Sep 18, 2001
Sep 18, 2001
596
Sep 17, 2001
Sep 17, 2001
597
598
599
600
601
602
603
604
return(1);
} /* init_sample */
Sound_Sample *Sound_NewSample(SDL_RWops *rw, const char *ext,
Sound_AudioInfo *desired, Uint32 bSize)
{
Sound_Sample *retval;
Nov 1, 2001
Nov 1, 2001
605
decoder_element *decoder;
Sep 17, 2001
Sep 17, 2001
606
607
608
609
610
611
612
613
614
615
616
/* 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)
{
Nov 1, 2001
Nov 1, 2001
617
for (decoder = &decoders[0]; decoder->funcs != NULL; decoder++)
Sep 17, 2001
Sep 17, 2001
618
{
Nov 1, 2001
Nov 1, 2001
619
if (decoder->available)
Sep 17, 2001
Sep 17, 2001
620
{
Nov 1, 2001
Nov 1, 2001
621
622
const char **decoderExt = decoder->funcs->info.extensions;
while (*decoderExt)
Sep 22, 2001
Sep 22, 2001
623
{
Nov 1, 2001
Nov 1, 2001
624
625
626
627
if (__Sound_strcasecmp(*decoderExt, ext) == 0)
{
if (init_sample(decoder->funcs, retval, ext, desired))
return(retval);
Jan 19, 2002
Jan 19, 2002
628
break; /* done with this decoder either way. */
Nov 1, 2001
Nov 1, 2001
629
630
631
} /* if */
decoderExt++;
} /* while */
Sep 17, 2001
Sep 17, 2001
632
633
634
635
636
} /* if */
} /* for */
} /* if */
/* no direct extension match? Try everything we've got... */
Nov 1, 2001
Nov 1, 2001
637
for (decoder = &decoders[0]; decoder->funcs != NULL; decoder++)
Sep 17, 2001
Sep 17, 2001
638
{
Nov 1, 2001
Nov 1, 2001
639
if (decoder->available)
Sep 22, 2001
Sep 22, 2001
640
{
Jan 19, 2002
Jan 19, 2002
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
int should_try = 1;
const char **decoderExt = decoder->funcs->info.extensions;
/* skip if we would have tried decoder above... */
while (*decoderExt)
{
if (__Sound_strcasecmp(*decoderExt, ext) == 0)
{
should_try = 0;
break;
} /* if */
decoderExt++;
} /* while */
if (should_try)
{
if (init_sample(decoder->funcs, retval, ext, desired))
return(retval);
} /* if */
Sep 22, 2001
Sep 22, 2001
660
} /* if */
Sep 17, 2001
Sep 17, 2001
661
662
663
664
665
666
667
668
} /* for */
/* nothing could handle the sound data... */
free(retval->opaque);
if (retval->buffer != NULL)
free(retval->buffer);
free(retval);
SDL_RWclose(rw);
Jul 5, 2002
Jul 5, 2002
669
__Sound_SetError(ERR_UNSUPPORTED_FORMAT);
Sep 17, 2001
Sep 17, 2001
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
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)
{
Jul 5, 2002
Jul 5, 2002
701
__Sound_SetError(ERR_NOT_INITIALIZED);
Sep 17, 2001
Sep 17, 2001
702
703
704
705
706
return;
} /* if */
if (sample == NULL)
{
Jul 5, 2002
Jul 5, 2002
707
__Sound_SetError(ERR_INVALID_ARGUMENT);
Sep 17, 2001
Sep 17, 2001
708
709
710
711
712
return;
} /* if */
internal = (Sound_SampleInternal *) sample->opaque;
Jan 19, 2002
Jan 19, 2002
713
SDL_LockMutex(samplelist_mutex);
Sep 17, 2001
Sep 17, 2001
714
Jan 19, 2002
Jan 19, 2002
715
/* update the sample_list... */
Sep 17, 2001
Sep 17, 2001
716
717
718
719
720
721
722
723
if (internal->prev != NULL)
{
Sound_SampleInternal *prevInternal;
prevInternal = (Sound_SampleInternal *) internal->prev->opaque;
prevInternal->next = internal->next;
} /* if */
else
{
Jan 19, 2002
Jan 19, 2002
724
725
assert(sample_list == sample);
sample_list = internal->next;
Sep 17, 2001
Sep 17, 2001
726
727
728
729
730
731
732
733
734
} /* else */
if (internal->next != NULL)
{
Sound_SampleInternal *nextInternal;
nextInternal = (Sound_SampleInternal *) internal->next->opaque;
nextInternal->prev = internal->prev;
} /* if */
Jan 19, 2002
Jan 19, 2002
735
736
SDL_UnlockMutex(samplelist_mutex);
Sep 17, 2001
Sep 17, 2001
737
/* nuke it... */
Jan 19, 2002
Jan 19, 2002
738
739
internal->funcs->close(sample);
Sep 17, 2001
Sep 17, 2001
740
741
if (internal->rw != NULL) /* this condition is a "just in case" thing. */
SDL_RWclose(internal->rw);
Nov 26, 2001
Nov 26, 2001
742
743
if ((internal->buffer != NULL) && (internal->buffer != sample->buffer))
Sep 17, 2001
Sep 17, 2001
744
free(internal->buffer);
Nov 26, 2001
Nov 26, 2001
745
Sep 17, 2001
Sep 17, 2001
746
free(internal);
Nov 26, 2001
Nov 26, 2001
747
Sep 17, 2001
Sep 17, 2001
748
749
if (sample->buffer != NULL)
free(sample->buffer);
Nov 26, 2001
Nov 26, 2001
750
Sep 17, 2001
Sep 17, 2001
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
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. */
Apr 24, 2002
Apr 24, 2002
794
sample->flags &= ~SOUND_SAMPLEFLAG_EAGAIN;
Sep 17, 2001
Sep 17, 2001
795
retval = internal->funcs->read(sample);
Sep 20, 2001
Sep 20, 2001
796
Nov 19, 2001
Nov 19, 2001
797
if (retval > 0 && internal->sdlcvt.needed)
Sep 17, 2001
Sep 17, 2001
798
799
{
internal->sdlcvt.len = retval;
Oct 15, 2001
Oct 15, 2001
800
801
Sound_ConvertAudio(&internal->sdlcvt);
retval = internal->sdlcvt.len_cvt;
Sep 17, 2001
Sep 17, 2001
802
803
804
805
806
807
808
809
810
811
812
813
814
} /* 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);
Dec 1, 2001
Dec 1, 2001
815
816
BAIL_IF_MACRO(sample->flags & SOUND_SAMPLEFLAG_EOF, ERR_PREV_EOF, 0);
BAIL_IF_MACRO(sample->flags & SOUND_SAMPLEFLAG_ERROR, ERR_PREV_ERROR, 0);
Sep 17, 2001
Sep 17, 2001
817
818
819
820
821
822
internal = (Sound_SampleInternal *) sample->opaque;
while ( ((sample->flags & SOUND_SAMPLEFLAG_EOF) == 0) &&
((sample->flags & SOUND_SAMPLEFLAG_ERROR) == 0) )
{
Dec 1, 2001
Dec 1, 2001
823
824
Uint32 br = Sound_Decode(sample);
void *ptr = realloc(buf, newBufSize + br);
Sep 17, 2001
Sep 17, 2001
825
826
827
if (ptr == NULL)
{
sample->flags |= SOUND_SAMPLEFLAG_ERROR;
Jul 5, 2002
Jul 5, 2002
828
__Sound_SetError(ERR_OUT_OF_MEMORY);
Sep 17, 2001
Sep 17, 2001
829
830
831
} /* if */
else
{
Nov 26, 2001
Nov 26, 2001
832
buf = ptr;
Sep 17, 2001
Sep 17, 2001
833
834
835
836
837
memcpy( ((char *) buf) + newBufSize, sample->buffer, br );
newBufSize += br;
} /* else */
} /* while */
Dec 1, 2001
Dec 1, 2001
838
839
840
if (buf == NULL) /* ...in case first call to realloc() fails... */
return(sample->buffer_size);
Nov 26, 2001
Nov 26, 2001
841
842
843
if (internal->buffer != sample->buffer)
free(internal->buffer);
Sep 17, 2001
Sep 17, 2001
844
free(sample->buffer);
Nov 26, 2001
Nov 26, 2001
845
846
internal->sdlcvt.buf = internal->buffer = sample->buffer = buf;
Sep 17, 2001
Sep 17, 2001
847
848
sample->buffer_size = newBufSize;
internal->buffer_size = newBufSize / internal->sdlcvt.len_mult;
Nov 26, 2001
Nov 26, 2001
849
internal->sdlcvt.len = internal->buffer_size;
Sep 17, 2001
Sep 17, 2001
850
851
852
853
return(newBufSize);
} /* Sound_DecodeAll */
Jan 17, 2002
Jan 17, 2002
854
855
856
857
858
859
860
861
862
863
864
865
866
int Sound_Rewind(Sound_Sample *sample)
{
Sound_SampleInternal *internal;
BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
internal = (Sound_SampleInternal *) sample->opaque;
if (!internal->funcs->rewind(sample))
{
sample->flags |= SOUND_SAMPLEFLAG_ERROR;
return(0);
} /* if */
Apr 24, 2002
Apr 24, 2002
867
868
869
sample->flags &= ~SOUND_SAMPLEFLAG_EAGAIN;
sample->flags &= ~SOUND_SAMPLEFLAG_ERROR;
sample->flags &= ~SOUND_SAMPLEFLAG_EOF;
Apr 21, 2002
Apr 21, 2002
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
return(1);
} /* Sound_Rewind */
int Sound_Seek(Sound_Sample *sample, Uint32 ms)
{
Sound_SampleInternal *internal;
BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
if (!(sample->flags & SOUND_SAMPLEFLAG_CANSEEK))
BAIL_MACRO(ERR_CANNOT_SEEK, 0);
internal = (Sound_SampleInternal *) sample->opaque;
BAIL_IF_MACRO(!internal->funcs->seek(sample, ms), NULL, 0);
Apr 24, 2002
Apr 24, 2002
886
887
888
sample->flags &= ~SOUND_SAMPLEFLAG_EAGAIN;
sample->flags &= ~SOUND_SAMPLEFLAG_ERROR;
sample->flags &= ~SOUND_SAMPLEFLAG_EOF;
Apr 21, 2002
Apr 21, 2002
889
Jan 17, 2002
Jan 17, 2002
890
891
892
893
return(1);
} /* Sound_Rewind */
Sep 17, 2001
Sep 17, 2001
894
/* end of SDL_sound.c ... */