Skip to content

Latest commit

 

History

History
508 lines (441 loc) · 14.4 KB

wavestream.c

File metadata and controls

508 lines (441 loc) · 14.4 KB
 
Oct 21, 1999
Oct 21, 1999
1
/*
Dec 31, 2011
Dec 31, 2011
2
SDL_mixer: An audio mixer library based on the SDL library
Feb 15, 2013
Feb 15, 2013
3
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
Dec 31, 2011
Dec 31, 2011
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Oct 21, 1999
Oct 21, 1999
20
21
*/
Dec 14, 2001
Dec 14, 2001
22
/* $Id$ */
Dec 14, 2001
Dec 14, 2001
23
Oct 21, 1999
Oct 21, 1999
24
25
26
27
28
/* This file supports streaming WAV files, without volume adjustment */
#include <stdlib.h>
#include <string.h>
Dec 21, 1999
Dec 21, 1999
29
30
31
32
#include "SDL_audio.h"
#include "SDL_mutex.h"
#include "SDL_rwops.h"
#include "SDL_endian.h"
Oct 21, 1999
Oct 21, 1999
33
Nov 30, 2001
Nov 30, 2001
34
#include "SDL_mixer.h"
Oct 21, 1999
Oct 21, 1999
35
36
#include "wavestream.h"
Sep 11, 2001
Sep 11, 2001
37
38
39
40
41
42
43
44
45
46
47
/*
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 */
/*******************************************/
May 22, 2013
May 22, 2013
48
49
50
51
#define RIFF 0x46464952 /* "RIFF" */
#define WAVE 0x45564157 /* "WAVE" */
#define FACT 0x74636166 /* "fact" */
#define LIST 0x5453494c /* "LIST" */
Jun 22, 2014
Jun 22, 2014
52
53
#define BEXT 0x74786562 /* "bext" */
#define FMT 0x20746D66 /* "fmt " */
May 22, 2013
May 22, 2013
54
55
56
57
58
#define DATA 0x61746164 /* "data" */
#define PCM_CODE 1
#define ADPCM_CODE 2
#define WAVE_MONO 1
#define WAVE_STEREO 2
Sep 11, 2001
Sep 11, 2001
59
60
61
62
/* Normally, these three chunks come consecutively in a WAVE file */
typedef struct WaveFMT {
/* Not saved in the chunk we read:
May 22, 2013
May 22, 2013
63
64
Uint32 FMTchunk;
Uint32 fmtlen;
Sep 11, 2001
Sep 11, 2001
65
*/
May 22, 2013
May 22, 2013
66
67
68
69
70
71
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 */
Sep 11, 2001
Sep 11, 2001
72
73
74
75
} WaveFMT;
/* The general chunk found in the WAVE file */
typedef struct Chunk {
May 22, 2013
May 22, 2013
76
77
78
Uint32 magic;
Uint32 length;
Uint8 *data; /* Data includes magic and length */
Sep 11, 2001
Sep 11, 2001
79
80
81
82
83
} Chunk;
/*********************************************/
/* Define values for AIFF (IFF audio) format */
/*********************************************/
May 22, 2013
May 22, 2013
84
85
86
87
#define FORM 0x4d524f46 /* "FORM" */
#define AIFF 0x46464941 /* "AIFF" */
#define SSND 0x444e5353 /* "SSND" */
#define COMM 0x4d4d4f43 /* "COMM" */
Sep 11, 2001
Sep 11, 2001
88
89
Jul 3, 2000
Jul 3, 2000
90
/* Currently we only support a single stream at a time */
May 16, 2002
May 16, 2002
91
static WAVStream *music = NULL;
Jul 3, 2000
Jul 3, 2000
92
Oct 21, 1999
Oct 21, 1999
93
94
/* This is the format of the audio mixer data */
static SDL_AudioSpec mixer;
Feb 12, 2003
Feb 12, 2003
95
static int wavestream_volume = MIX_MAX_VOLUME;
Oct 21, 1999
Oct 21, 1999
96
97
/* Function to load the WAV/AIFF stream */
Feb 26, 2008
Feb 26, 2008
98
static SDL_RWops *LoadWAVStream (SDL_RWops *rw, SDL_AudioSpec *spec,
May 22, 2013
May 22, 2013
99
long *start, long *stop);
Feb 26, 2008
Feb 26, 2008
100
static SDL_RWops *LoadAIFFStream (SDL_RWops *rw, SDL_AudioSpec *spec,
May 22, 2013
May 22, 2013
101
long *start, long *stop);
Oct 21, 1999
Oct 21, 1999
102
103
104
105
106
107
/* 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)
{
May 22, 2013
May 22, 2013
108
109
mixer = *mixerfmt;
return(0);
Oct 21, 1999
Oct 21, 1999
110
111
}
Feb 12, 2003
Feb 12, 2003
112
void WAVStream_SetVolume(int volume)
Oct 21, 1999
Oct 21, 1999
113
{
May 22, 2013
May 22, 2013
114
wavestream_volume = volume;
Oct 21, 1999
Oct 21, 1999
115
116
}
Jan 4, 2012
Jan 4, 2012
117
/* Load a WAV stream from the given RWops object */
Jun 2, 2013
Jun 2, 2013
118
WAVStream *WAVStream_LoadSong_RW(SDL_RWops *src, int freesrc)
Oct 21, 1999
Oct 21, 1999
119
{
May 22, 2013
May 22, 2013
120
121
122
123
124
125
126
127
128
WAVStream *wave;
SDL_AudioSpec wavespec;
if ( ! mixer.format ) {
Mix_SetError("WAV music output not started");
return(NULL);
}
wave = (WAVStream *)SDL_malloc(sizeof *wave);
if ( wave ) {
Jun 2, 2013
Jun 2, 2013
129
130
131
132
133
134
135
136
137
138
Uint32 magic;
SDL_zerop(wave);
wave->freesrc = freesrc;
magic = SDL_ReadLE32(src);
if ( magic == RIFF || magic == WAVE ) {
wave->src = LoadWAVStream(src, &wavespec, &wave->start, &wave->stop);
} else if ( magic == FORM ) {
wave->src = LoadAIFFStream(src, &wavespec, &wave->start, &wave->stop);
May 22, 2013
May 22, 2013
139
140
141
} else {
Mix_SetError("Unknown WAVE format");
}
Jun 2, 2013
Jun 2, 2013
142
if ( wave->src == NULL ) {
May 22, 2013
May 22, 2013
143
144
145
146
147
148
149
150
151
152
153
SDL_free(wave);
return(NULL);
}
SDL_BuildAudioCVT(&wave->cvt,
wavespec.format, wavespec.channels, wavespec.freq,
mixer.format, mixer.channels, mixer.freq);
} else {
SDL_OutOfMemory();
return(NULL);
}
return(wave);
Oct 21, 1999
Oct 21, 1999
154
155
156
}
/* Start playback of a given WAV stream */
Feb 12, 2003
Feb 12, 2003
157
void WAVStream_Start(WAVStream *wave)
Oct 21, 1999
Oct 21, 1999
158
{
Jun 2, 2013
Jun 2, 2013
159
SDL_RWseek (wave->src, wave->start, RW_SEEK_SET);
May 22, 2013
May 22, 2013
160
music = wave;
Oct 21, 1999
Oct 21, 1999
161
162
}
May 16, 2002
May 16, 2002
163
/* Play some of a stream previously started with WAVStream_Start() */
Oct 2, 2009
Oct 2, 2009
164
int WAVStream_PlaySome(Uint8 *stream, int len)
Oct 21, 1999
Oct 21, 1999
165
{
Jun 1, 2013
Jun 1, 2013
166
167
Sint64 pos;
Sint64 left = 0;
May 22, 2013
May 22, 2013
168
Jun 2, 2013
Jun 2, 2013
169
if ( music && ((pos=SDL_RWtell(music->src)) < music->stop) ) {
May 22, 2013
May 22, 2013
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
if ( music->cvt.needed ) {
int original_len;
original_len=(int)((double)len/music->cvt.len_ratio);
if ( music->cvt.len != original_len ) {
int worksize;
if ( music->cvt.buf != NULL ) {
SDL_free(music->cvt.buf);
}
worksize = original_len*music->cvt.len_mult;
music->cvt.buf=(Uint8 *)SDL_malloc(worksize);
if ( music->cvt.buf == NULL ) {
return 0;
}
music->cvt.len = original_len;
}
if ( (music->stop - pos) < original_len ) {
left = (original_len - (music->stop - pos));
Jun 12, 2013
Jun 12, 2013
188
original_len -= (int)left;
May 22, 2013
May 22, 2013
189
190
left = (int)((double)left*music->cvt.len_ratio);
}
Jun 2, 2013
Jun 2, 2013
191
original_len = SDL_RWread(music->src, music->cvt.buf,1,original_len);
May 22, 2013
May 22, 2013
192
193
194
195
196
197
198
199
200
201
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.
*/
if ( (music->cvt.src_format & 0x0010) && (original_len & 1) ) {
original_len--;
}
music->cvt.len = original_len;
SDL_ConvertAudio(&music->cvt);
SDL_MixAudio(stream, music->cvt.buf, music->cvt.len_cvt, wavestream_volume);
} else {
Uint8 *data;
if ( (music->stop - pos) < len ) {
left = (len - (music->stop - pos));
Jun 12, 2013
Jun 12, 2013
208
len -= (int)left;
May 22, 2013
May 22, 2013
209
210
211
212
}
data = SDL_stack_alloc(Uint8, len);
if (data)
{
Jun 2, 2013
Jun 2, 2013
213
SDL_RWread(music->src, data, len, 1);
May 22, 2013
May 22, 2013
214
215
216
217
218
SDL_MixAudio(stream, data, len, wavestream_volume);
SDL_stack_free(data);
}
}
}
Jun 12, 2013
Jun 12, 2013
219
return (int)left;
Oct 21, 1999
Oct 21, 1999
220
221
222
}
/* Stop playback of a stream previously started with WAVStream_Start() */
Feb 12, 2003
Feb 12, 2003
223
void WAVStream_Stop(void)
Oct 21, 1999
Oct 21, 1999
224
{
May 22, 2013
May 22, 2013
225
music = NULL;
Oct 21, 1999
Oct 21, 1999
226
227
228
}
/* Close the given WAV stream */
Feb 12, 2003
Feb 12, 2003
229
void WAVStream_FreeSong(WAVStream *wave)
Oct 21, 1999
Oct 21, 1999
230
{
May 22, 2013
May 22, 2013
231
232
233
234
235
if ( wave ) {
/* Clean up associated data */
if ( wave->cvt.buf ) {
SDL_free(wave->cvt.buf);
}
Jun 2, 2013
Jun 2, 2013
236
237
if ( wave->freesrc ) {
SDL_RWclose(wave->src);
May 22, 2013
May 22, 2013
238
239
240
}
SDL_free(wave);
}
Oct 21, 1999
Oct 21, 1999
241
242
243
}
/* Return non-zero if a stream is currently playing */
Feb 12, 2003
Feb 12, 2003
244
int WAVStream_Active(void)
Oct 21, 1999
Oct 21, 1999
245
{
May 22, 2013
May 22, 2013
246
int active;
Oct 21, 1999
Oct 21, 1999
247
May 22, 2013
May 22, 2013
248
active = 0;
Jun 2, 2013
Jun 2, 2013
249
if ( music && (SDL_RWtell(music->src) < music->stop) ) {
May 22, 2013
May 22, 2013
250
251
252
active = 1;
}
return(active);
Oct 21, 1999
Oct 21, 1999
253
254
255
256
}
static int ReadChunk(SDL_RWops *src, Chunk *chunk, int read_data)
{
May 22, 2013
May 22, 2013
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
chunk->magic = SDL_ReadLE32(src);
chunk->length = SDL_ReadLE32(src);
if ( read_data ) {
chunk->data = (Uint8 *)SDL_malloc(chunk->length);
if ( chunk->data == NULL ) {
Mix_SetError("Out of memory");
return(-1);
}
if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) {
Mix_SetError("Couldn't read chunk");
SDL_free(chunk->data);
return(-1);
}
} else {
SDL_RWseek(src, chunk->length, RW_SEEK_CUR);
}
return(chunk->length);
Oct 21, 1999
Oct 21, 1999
274
275
}
Feb 26, 2008
Feb 26, 2008
276
static SDL_RWops *LoadWAVStream (SDL_RWops *src, SDL_AudioSpec *spec,
May 22, 2013
May 22, 2013
277
long *start, long *stop)
Oct 21, 1999
Oct 21, 1999
278
{
May 22, 2013
May 22, 2013
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
int was_error;
Chunk chunk;
int lenread;
/* WAV magic header */
Uint32 wavelen;
Uint32 WAVEmagic;
/* FMT chunk */
WaveFMT *format = NULL;
was_error = 0;
/* Check the magic header */
wavelen = SDL_ReadLE32(src);
WAVEmagic = SDL_ReadLE32(src);
/* Read the audio data format chunk */
chunk.data = NULL;
do {
/* FIXME! Add this logic to SDL_LoadWAV_RW() */
if ( chunk.data ) {
SDL_free(chunk.data);
}
lenread = ReadChunk(src, &chunk, 1);
if ( lenread < 0 ) {
was_error = 1;
goto done;
}
Jun 22, 2014
Jun 22, 2014
308
} while ((chunk.magic == FACT) || (chunk.magic == LIST) || (chunk.magic == BEXT));
May 22, 2013
May 22, 2013
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* Decode the audio data format */
format = (WaveFMT *)chunk.data;
if ( chunk.magic != FMT ) {
SDL_free(chunk.data);
Mix_SetError("Complex WAVE files not supported");
was_error = 1;
goto done;
}
switch (SDL_SwapLE16(format->encoding)) {
case PCM_CODE:
/* We can understand this */
break;
default:
Mix_SetError("Unknown WAVE data format");
was_error = 1;
goto done;
}
Jun 1, 2013
Jun 1, 2013
327
SDL_memset(spec, 0, (sizeof *spec));
May 22, 2013
May 22, 2013
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
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:
Mix_SetError("Unknown PCM data format");
was_error = 1;
goto done;
}
spec->channels = (Uint8) SDL_SwapLE16(format->channels);
spec->samples = 4096; /* Good default buffer size */
/* Set the file offset to the DATA chunk data */
chunk.data = NULL;
do {
Jun 12, 2013
Jun 12, 2013
347
*start = (long)SDL_RWtell(src) + 2*sizeof(Uint32);
May 22, 2013
May 22, 2013
348
349
350
351
352
353
lenread = ReadChunk(src, &chunk, 0);
if ( lenread < 0 ) {
was_error = 1;
goto done;
}
} while ( chunk.magic != DATA );
Jun 12, 2013
Jun 12, 2013
354
*stop = (long)SDL_RWtell(src);
Oct 21, 1999
Oct 21, 1999
355
356
done:
May 22, 2013
May 22, 2013
357
358
359
360
361
362
363
if ( format != NULL ) {
SDL_free(format);
}
if ( was_error ) {
return NULL;
}
return(src);
Oct 21, 1999
Oct 21, 1999
364
365
}
Aug 19, 2001
Aug 19, 2001
366
367
368
369
370
/* 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)
Oct 21, 1999
Oct 21, 1999
371
{
May 22, 2013
May 22, 2013
372
373
374
/* Negative number? */
if (sanebuf[0] & 0x80)
return 0;
Aug 19, 2001
Aug 19, 2001
375
May 22, 2013
May 22, 2013
376
377
378
/* Less than 1? */
if (sanebuf[0] <= 0x3F)
return 1;
Aug 19, 2001
Aug 19, 2001
379
May 22, 2013
May 22, 2013
380
381
382
/* Way too big? */
if (sanebuf[0] > 0x40)
return 0x4000000;
Aug 19, 2001
Aug 19, 2001
383
May 22, 2013
May 22, 2013
384
385
386
/* Still too big? */
if (sanebuf[0] == 0x40 && sanebuf[1] > 0x1C)
return 800000000;
Aug 19, 2001
Aug 19, 2001
387
May 22, 2013
May 22, 2013
388
389
return ((sanebuf[2] << 23) | (sanebuf[3] << 15) | (sanebuf[4] << 7)
| (sanebuf[5] >> 1)) >> (29 - sanebuf[1]);
Oct 21, 1999
Oct 21, 1999
390
391
}
Feb 26, 2008
Feb 26, 2008
392
static SDL_RWops *LoadAIFFStream (SDL_RWops *src, SDL_AudioSpec *spec,
May 22, 2013
May 22, 2013
393
long *start, long *stop)
Oct 21, 1999
Oct 21, 1999
394
{
May 22, 2013
May 22, 2013
395
396
397
398
399
400
int was_error;
int found_SSND;
int found_COMM;
Uint32 chunk_type;
Uint32 chunk_length;
Jun 1, 2013
Jun 1, 2013
401
Sint64 next_chunk;
May 22, 2013
May 22, 2013
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* AIFF magic header */
Uint32 AIFFmagic;
/* SSND chunk */
Uint32 offset;
Uint32 blocksize;
/* COMM format chunk */
Uint16 channels = 0;
Uint32 numsamples = 0;
Uint16 samplesize = 0;
Uint8 sane_freq[10];
Uint32 frequency = 0;
was_error = 0;
/* Check the magic header */
chunk_length = SDL_ReadBE32(src);
AIFFmagic = SDL_ReadLE32(src);
Jun 2, 2013
Jun 2, 2013
420
if ( AIFFmagic != AIFF ) {
May 22, 2013
May 22, 2013
421
422
423
424
425
426
Mix_SetError("Unrecognized file type (not AIFF)");
was_error = 1;
goto done;
}
/* From what I understand of the specification, chunks may appear in
Aug 19, 2001
Aug 19, 2001
427
* any order, and we should just ignore unknown ones.
May 22, 2013
May 22, 2013
428
429
430
*
* TODO: Better sanity-checking. E.g. what happens if the AIFF file
* contains compressed sound data?
Jun 2, 2013
Jun 2, 2013
431
*/
Oct 21, 1999
Oct 21, 1999
432
May 22, 2013
May 22, 2013
433
434
found_SSND = 0;
found_COMM = 0;
Oct 21, 1999
Oct 21, 1999
435
May 22, 2013
May 22, 2013
436
437
438
439
do {
chunk_type = SDL_ReadLE32(src);
chunk_length = SDL_ReadBE32(src);
next_chunk = SDL_RWtell(src) + chunk_length;
Aug 19, 2001
Aug 19, 2001
440
May 22, 2013
May 22, 2013
441
442
443
/* Paranoia to avoid infinite loops */
if (chunk_length == 0)
break;
Aug 19, 2001
Aug 19, 2001
444
Jun 2, 2013
Jun 2, 2013
445
switch (chunk_type) {
May 22, 2013
May 22, 2013
446
447
448
449
case SSND:
found_SSND = 1;
offset = SDL_ReadBE32(src);
blocksize = SDL_ReadBE32(src);
Jun 12, 2013
Jun 12, 2013
450
*start = (long)SDL_RWtell(src) + offset;
May 22, 2013
May 22, 2013
451
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
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, RW_SEEK_SET) != -1);
if (!found_SSND) {
Mix_SetError("Bad AIFF file (no SSND chunk)");
was_error = 1;
goto done;
}
if (!found_COMM) {
Mix_SetError("Bad AIFF file (no COMM chunk)");
was_error = 1;
goto done;
}
*stop = *start + channels * numsamples * (samplesize / 8);
/* Decode the audio data format */
Jun 1, 2013
Jun 1, 2013
485
SDL_memset(spec, 0, (sizeof *spec));
May 22, 2013
May 22, 2013
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
spec->freq = frequency;
switch (samplesize) {
case 8:
spec->format = AUDIO_S8;
break;
case 16:
spec->format = AUDIO_S16MSB;
break;
default:
Mix_SetError("Unknown samplesize in data format");
was_error = 1;
goto done;
}
spec->channels = (Uint8) channels;
spec->samples = 4096; /* Good default buffer size */
Oct 21, 1999
Oct 21, 1999
501
502
done:
May 22, 2013
May 22, 2013
503
504
505
506
if ( was_error ) {
return NULL;
}
return(src);
Oct 21, 1999
Oct 21, 1999
507
}