/
wavestream.c
525 lines (457 loc) · 12.6 KB
1
/*
2
SDL_mixer: An audio mixer library based on the SDL library
3
Copyright (C) 1997-2009 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
20
slouken@libsdl.org
21
22
*/
23
/* $Id$ */
24
25
26
27
28
29
/* This file supports streaming WAV files, without volume adjustment */
#include <stdlib.h>
#include <string.h>
30
31
32
33
#include "SDL_audio.h"
#include "SDL_mutex.h"
#include "SDL_rwops.h"
#include "SDL_endian.h"
34
35
#include "SDL_mixer.h"
36
37
#include "wavestream.h"
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Taken with permission from SDL_wave.h, part of the SDL library,
available at: http://www.libsdl.org/
and placed under the same license as this mixer library.
*/
/* WAVE files are little-endian */
/*******************************************/
/* Define values for Microsoft WAVE format */
/*******************************************/
#define RIFF 0x46464952 /* "RIFF" */
#define WAVE 0x45564157 /* "WAVE" */
#define FACT 0x74636166 /* "fact" */
#define LIST 0x5453494c /* "LIST" */
#define FMT 0x20746D66 /* "fmt " */
#define DATA 0x61746164 /* "data" */
#define PCM_CODE 1
#define ADPCM_CODE 2
#define WAVE_MONO 1
#define WAVE_STEREO 2
/* Normally, these three chunks come consecutively in a WAVE file */
typedef struct WaveFMT {
/* Not saved in the chunk we read:
Uint32 FMTchunk;
Uint32 fmtlen;
*/
Uint16 encoding;
Uint16 channels; /* 1 = mono, 2 = stereo */
Uint32 frequency; /* One of 11025, 22050, or 44100 Hz */
Uint32 byterate; /* Average bytes per second */
Uint16 blockalign; /* Bytes per sample block */
Uint16 bitspersample; /* One of 8, 12, 16, or 4 for ADPCM */
} WaveFMT;
/* The general chunk found in the WAVE file */
typedef struct Chunk {
Uint32 magic;
Uint32 length;
Uint8 *data; /* Data includes magic and length */
} Chunk;
/*********************************************/
/* Define values for AIFF (IFF audio) format */
/*********************************************/
#define FORM 0x4d524f46 /* "FORM" */
#define AIFF 0x46464941 /* "AIFF" */
#define SSND 0x444e5353 /* "SSND" */
#define COMM 0x4d4d4f43 /* "COMM" */
90
/* Currently we only support a single stream at a time */
91
static WAVStream *music = NULL;
92
93
94
/* This is the format of the audio mixer data */
static SDL_AudioSpec mixer;
95
static int wavestream_volume = MIX_MAX_VOLUME;
96
97
/* Function to load the WAV/AIFF stream */
98
static SDL_RWops *LoadWAVStream (SDL_RWops *rw, SDL_AudioSpec *spec,
99
long *start, long *stop);
100
static SDL_RWops *LoadAIFFStream (SDL_RWops *rw, SDL_AudioSpec *spec,
101
102
103
104
105
106
107
108
109
110
111
long *start, long *stop);
/* Initialize the WAVStream player, with the given mixer settings
This function returns 0, or -1 if there was an error.
*/
int WAVStream_Init(SDL_AudioSpec *mixerfmt)
{
mixer = *mixerfmt;
return(0);
}
112
void WAVStream_SetVolume(int volume)
113
{
114
wavestream_volume = volume;
115
116
}
117
WAVStream *WAVStream_LoadSong(const char *file, const char *magic)
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
{
SDL_RWops *rw;
WAVStream *wave;
rw = SDL_RWFromFile(file, "rb");
if ( rw == NULL ) {
SDL_SetError("Couldn't open %s", file);
return NULL;
}
wave = WAVStream_LoadSong_RW(rw, magic);
if ( wave == NULL ) {
SDL_FreeRW(rw);
return NULL;
}
return wave;
}
/* Load a WAV stream from the given file */
WAVStream *WAVStream_LoadSong_RW(SDL_RWops *rw, const char *magic)
137
138
139
140
141
{
WAVStream *wave;
SDL_AudioSpec wavespec;
if ( ! mixer.format ) {
142
Mix_SetError("WAV music output not started");
143
144
145
146
147
148
return(NULL);
}
wave = (WAVStream *)malloc(sizeof *wave);
if ( wave ) {
memset(wave, 0, (sizeof *wave));
if ( strcmp(magic, "RIFF") == 0 ) {
149
wave->rw = LoadWAVStream(rw, &wavespec,
150
151
152
&wave->start, &wave->stop);
} else
if ( strcmp(magic, "FORM") == 0 ) {
153
wave->rw = LoadAIFFStream(rw, &wavespec,
154
&wave->start, &wave->stop);
155
156
} else {
Mix_SetError("Unknown WAVE format");
157
}
158
if ( wave->rw == NULL ) {
159
160
161
162
163
164
165
166
167
168
169
free(wave);
return(NULL);
}
SDL_BuildAudioCVT(&wave->cvt,
wavespec.format, wavespec.channels, wavespec.freq,
mixer.format, mixer.channels, mixer.freq);
}
return(wave);
}
/* Start playback of a given WAV stream */
170
void WAVStream_Start(WAVStream *wave)
171
{
172
SDL_RWseek (wave->rw, wave->start, SEEK_SET);
173
music = wave;
174
175
}
176
/* Play some of a stream previously started with WAVStream_Start() */
177
void WAVStream_PlaySome(Uint8 *stream, int len)
178
179
180
{
long pos;
181
if ( music && ((pos=SDL_RWtell(music->rw)) < music->stop) ) {
182
if ( music->cvt.needed ) {
183
184
int original_len;
185
186
original_len=(int)((double)len/music->cvt.len_ratio);
if ( music->cvt.len != original_len ) {
187
int worksize;
188
189
if ( music->cvt.buf != NULL ) {
free(music->cvt.buf);
190
}
191
192
193
worksize = original_len*music->cvt.len_mult;
music->cvt.buf=(Uint8 *)malloc(worksize);
if ( music->cvt.buf == NULL ) {
194
195
return;
}
196
music->cvt.len = original_len;
197
}
198
199
if ( (music->stop - pos) < original_len ) {
original_len = (music->stop - pos);
200
}
201
original_len = SDL_RWread(music->rw, music->cvt.buf,1,original_len);
202
203
204
205
206
207
/* At least at the time of writing, SDL_ConvertAudio()
does byte-order swapping starting at the end of the
buffer. Thus, if we are reading 16-bit samples, we
had better make damn sure that we get an even
number of bytes, or we'll get garbage.
*/
208
if ( (music->cvt.src_format & 0x0010) && (original_len & 1) ) {
209
210
original_len--;
}
211
212
music->cvt.len = original_len;
SDL_ConvertAudio(&music->cvt);
213
SDL_MixAudio(stream, music->cvt.buf, music->cvt.len_cvt, wavestream_volume);
214
} else {
215
Uint8 *data;
216
217
if ( (music->stop - pos) < len ) {
len = (music->stop - pos);
218
}
219
220
221
data = SDL_stack_alloc(Uint8, len);
if (data)
{
222
SDL_RWread(music->rw, data, len, 1);
223
224
225
SDL_MixAudio(stream, data, len, wavestream_volume);
SDL_stack_free(data);
}
226
227
228
229
230
}
}
}
/* Stop playback of a stream previously started with WAVStream_Start() */
231
void WAVStream_Stop(void)
232
{
233
music = NULL;
234
235
236
}
/* Close the given WAV stream */
237
void WAVStream_FreeSong(WAVStream *wave)
238
239
240
{
if ( wave ) {
/* Clean up associated data */
241
242
if ( wave->freerw ) {
SDL_FreeRW(wave->rw);
243
244
245
246
247
248
249
250
251
}
if ( wave->cvt.buf ) {
free(wave->cvt.buf);
}
free(wave);
}
}
/* Return non-zero if a stream is currently playing */
252
int WAVStream_Active(void)
253
254
255
256
{
int active;
active = 0;
257
if ( music && (SDL_RWtell(music->rw) < music->stop) ) {
258
259
260
261
262
263
264
265
266
267
268
269
active = 1;
}
return(active);
}
static int ReadChunk(SDL_RWops *src, Chunk *chunk, int read_data)
{
chunk->magic = SDL_ReadLE32(src);
chunk->length = SDL_ReadLE32(src);
if ( read_data ) {
chunk->data = (Uint8 *)malloc(chunk->length);
if ( chunk->data == NULL ) {
270
Mix_SetError("Out of memory");
271
272
273
return(-1);
}
if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) {
274
Mix_SetError("Couldn't read chunk");
275
276
277
278
279
280
281
282
283
free(chunk->data);
return(-1);
}
} else {
SDL_RWseek(src, chunk->length, SEEK_CUR);
}
return(chunk->length);
}
284
static SDL_RWops *LoadWAVStream (SDL_RWops *src, SDL_AudioSpec *spec,
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
long *start, long *stop)
{
int was_error;
Chunk chunk;
int lenread;
/* WAV magic header */
Uint32 RIFFchunk;
Uint32 wavelen;
Uint32 WAVEmagic;
/* FMT chunk */
WaveFMT *format = NULL;
was_error = 0;
300
301
302
303
304
305
/* Check the magic header */
RIFFchunk = SDL_ReadLE32(src);
wavelen = SDL_ReadLE32(src);
WAVEmagic = SDL_ReadLE32(src);
if ( (RIFFchunk != RIFF) || (WAVEmagic != WAVE) ) {
306
Mix_SetError("Unrecognized file type (not WAVE)");
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
was_error = 1;
goto done;
}
/* Read the audio data format chunk */
chunk.data = NULL;
do {
/* FIXME! Add this logic to SDL_LoadWAV_RW() */
if ( chunk.data ) {
free(chunk.data);
}
lenread = ReadChunk(src, &chunk, 1);
if ( lenread < 0 ) {
was_error = 1;
goto done;
}
} while ( (chunk.magic == FACT) || (chunk.magic == LIST) );
/* Decode the audio data format */
format = (WaveFMT *)chunk.data;
if ( chunk.magic != FMT ) {
free(chunk.data);
329
Mix_SetError("Complex WAVE files not supported");
330
331
332
333
334
335
336
337
was_error = 1;
goto done;
}
switch (SDL_SwapLE16(format->encoding)) {
case PCM_CODE:
/* We can understand this */
break;
default:
338
Mix_SetError("Unknown WAVE data format");
339
340
341
342
343
344
345
346
347
348
349
350
351
was_error = 1;
goto done;
}
memset(spec, 0, (sizeof *spec));
spec->freq = SDL_SwapLE32(format->frequency);
switch (SDL_SwapLE16(format->bitspersample)) {
case 8:
spec->format = AUDIO_U8;
break;
case 16:
spec->format = AUDIO_S16;
break;
default:
352
Mix_SetError("Unknown PCM data format");
353
354
355
was_error = 1;
goto done;
}
356
spec->channels = (Uint8) SDL_SwapLE16(format->channels);
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
spec->samples = 4096; /* Good default buffer size */
/* Set the file offset to the DATA chunk data */
chunk.data = NULL;
do {
*start = SDL_RWtell(src) + 2*sizeof(Uint32);
lenread = ReadChunk(src, &chunk, 0);
if ( lenread < 0 ) {
was_error = 1;
goto done;
}
} while ( chunk.magic != DATA );
*stop = SDL_RWtell(src);
done:
if ( format != NULL ) {
free(format);
}
if ( was_error ) {
376
return NULL;
377
}
378
return(src);
379
380
}
381
382
383
384
385
/* I couldn't get SANE_to_double() to work, so I stole this from libsndfile.
* I don't pretend to fully understand it.
*/
static Uint32 SANE_to_Uint32 (Uint8 *sanebuf)
386
{
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* Negative number? */
if (sanebuf[0] & 0x80)
return 0;
/* Less than 1? */
if (sanebuf[0] <= 0x3F)
return 1;
/* Way too big? */
if (sanebuf[0] > 0x40)
return 0x4000000;
/* Still too big? */
if (sanebuf[0] == 0x40 && sanebuf[1] > 0x1C)
return 800000000;
return ((sanebuf[2] << 23) | (sanebuf[3] << 15) | (sanebuf[4] << 7)
| (sanebuf[5] >> 1)) >> (29 - sanebuf[1]);
405
406
}
407
static SDL_RWops *LoadAIFFStream (SDL_RWops *src, SDL_AudioSpec *spec,
408
409
410
long *start, long *stop)
{
int was_error;
411
412
int found_SSND;
int found_COMM;
413
414
415
416
417
Uint32 chunk_type;
Uint32 chunk_length;
long next_chunk;
418
419
420
421
422
423
424
/* AIFF magic header */
Uint32 FORMchunk;
Uint32 AIFFmagic;
/* SSND chunk */
Uint32 offset;
Uint32 blocksize;
/* COMM format chunk */
425
426
427
Uint16 channels = 0;
Uint32 numsamples = 0;
Uint16 samplesize = 0;
428
Uint8 sane_freq[10];
429
Uint32 frequency = 0;
430
431
was_error = 0;
432
433
434
/* Check the magic header */
FORMchunk = SDL_ReadLE32(src);
435
chunk_length = SDL_ReadBE32(src);
436
437
AIFFmagic = SDL_ReadLE32(src);
if ( (FORMchunk != FORM) || (AIFFmagic != AIFF) ) {
438
Mix_SetError("Unrecognized file type (not AIFF)");
439
440
441
442
was_error = 1;
goto done;
}
443
444
/* From what I understand of the specification, chunks may appear in
* any order, and we should just ignore unknown ones.
445
446
447
*
* TODO: Better sanity-checking. E.g. what happens if the AIFF file
* contains compressed sound data?
448
*/
449
450
451
found_SSND = 0;
found_COMM = 0;
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
do {
chunk_type = SDL_ReadLE32(src);
chunk_length = SDL_ReadBE32(src);
next_chunk = SDL_RWtell(src) + chunk_length;
/* Paranoia to avoid infinite loops */
if (chunk_length == 0)
break;
switch (chunk_type) {
case SSND:
found_SSND = 1;
offset = SDL_ReadBE32(src);
blocksize = SDL_ReadBE32(src);
*start = SDL_RWtell(src) + offset;
break;
case COMM:
found_COMM = 1;
/* Read the audio data format chunk */
channels = SDL_ReadBE16(src);
numsamples = SDL_ReadBE32(src);
samplesize = SDL_ReadBE16(src);
SDL_RWread(src, sane_freq, sizeof(sane_freq), 1);
frequency = SANE_to_Uint32(sane_freq);
break;
default:
break;
}
} while ((!found_SSND || !found_COMM)
&& SDL_RWseek(src, next_chunk, SEEK_SET) != -1);
if (!found_SSND) {
488
Mix_SetError("Bad AIFF file (no SSND chunk)");
489
490
491
492
493
was_error = 1;
goto done;
}
if (!found_COMM) {
494
Mix_SetError("Bad AIFF file (no COMM chunk)");
495
496
was_error = 1;
goto done;
497
}
498
499
*stop = *start + channels * numsamples * (samplesize / 8);
500
501
502
503
504
505
/* Decode the audio data format */
memset(spec, 0, (sizeof *spec));
spec->freq = frequency;
switch (samplesize) {
case 8:
506
spec->format = AUDIO_S8;
507
508
break;
case 16:
509
spec->format = AUDIO_S16MSB;
510
511
break;
default:
512
Mix_SetError("Unknown samplesize in data format");
513
514
515
was_error = 1;
goto done;
}
516
spec->channels = (Uint8) channels;
517
518
519
520
spec->samples = 4096; /* Good default buffer size */
done:
if ( was_error ) {
521
return NULL;
522
}
523
return(src);
524
}