Skip to content

Latest commit

 

History

History
562 lines (494 loc) · 13.4 KB

wavestream.c

File metadata and controls

562 lines (494 loc) · 13.4 KB
 
Oct 21, 1999
Oct 21, 1999
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
26
27
28
29
/*
MIXERLIB: An audio mixer library based on the SDL library
Copyright (C) 1997-1999 Sam Lantinga
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
5635-34 Springhouse Dr.
Pleasanton, CA 94588 (USA)
slouken@devolution.com
*/
/* This file supports streaming WAV files, without volume adjustment */
#include <stdlib.h>
#include <string.h>
Dec 21, 1999
Dec 21, 1999
30
31
32
33
#include "SDL_audio.h"
#include "SDL_mutex.h"
#include "SDL_rwops.h"
#include "SDL_endian.h"
Oct 21, 1999
Oct 21, 1999
34
35
36
#include "wavestream.h"
Sep 11, 2001
Sep 11, 2001
37
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
/*
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" */
Jul 3, 2000
Jul 3, 2000
89
90
91
92
93
94
/* Currently we only support a single stream at a time */
static WAVStream *theWave = NULL;
/* This is initialized by the music mixer */
static SDL_mutex *music_lock = NULL;
Oct 21, 1999
Oct 21, 1999
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* This is the format of the audio mixer data */
static SDL_AudioSpec mixer;
/* Function to load the WAV/AIFF stream */
static FILE *LoadWAVStream (const char *file, SDL_AudioSpec *spec,
long *start, long *stop);
static FILE *LoadAIFFStream (const char *file, SDL_AudioSpec *spec,
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)
{
Jul 3, 2000
Jul 3, 2000
109
110
111
112
113
114
115
/* FIXME: clean up the mutex, or move it into music.c */
music_lock = SDL_CreateMutex();
#ifndef macintosh /* Hmm.. */
if ( music_lock == NULL ) {
return(-1);
}
#endif
Oct 21, 1999
Oct 21, 1999
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
mixer = *mixerfmt;
return(0);
}
/* Unimplemented */
extern void WAVStream_SetVolume(int volume)
{
}
/* Load a WAV stream from the given file */
extern WAVStream *WAVStream_LoadSong(const char *file, const char *magic)
{
WAVStream *wave;
SDL_AudioSpec wavespec;
if ( ! mixer.format ) {
Nov 30, 2001
Nov 30, 2001
132
Mix_SetError("WAV music output not started");
Oct 21, 1999
Oct 21, 1999
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
return(NULL);
}
wave = (WAVStream *)malloc(sizeof *wave);
if ( wave ) {
memset(wave, 0, (sizeof *wave));
if ( strcmp(magic, "RIFF") == 0 ) {
wave->wavefp = LoadWAVStream(file, &wavespec,
&wave->start, &wave->stop);
} else
if ( strcmp(magic, "FORM") == 0 ) {
wave->wavefp = LoadAIFFStream(file, &wavespec,
&wave->start, &wave->stop);
}
if ( wave->wavefp == NULL ) {
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 */
extern void WAVStream_Start(WAVStream *wave)
{
Jul 3, 2000
Jul 3, 2000
160
SDL_mutexP(music_lock);
Oct 21, 1999
Oct 21, 1999
161
162
clearerr(wave->wavefp);
fseek(wave->wavefp, wave->start, SEEK_SET);
Jul 3, 2000
Jul 3, 2000
163
164
theWave = wave;
SDL_mutexV(music_lock);
Oct 21, 1999
Oct 21, 1999
165
166
167
}
/* Play some of a stream previously started with WAVStream_Start()
Jul 3, 2000
Jul 3, 2000
168
The music_lock is held while this function is called.
Oct 21, 1999
Oct 21, 1999
169
*/
Jul 3, 2000
Jul 3, 2000
170
extern void WAVStream_PlaySome(Uint8 *stream, int len)
Oct 21, 1999
Oct 21, 1999
171
172
173
{
long pos;
Jul 3, 2000
Jul 3, 2000
174
175
176
SDL_mutexP(music_lock);
if ( theWave && ((pos=ftell(theWave->wavefp)) < theWave->stop) ) {
if ( theWave->cvt.needed ) {
Oct 21, 1999
Oct 21, 1999
177
178
int original_len;
Jul 3, 2000
Jul 3, 2000
179
180
original_len=(int)((double)len/theWave->cvt.len_ratio);
if ( theWave->cvt.len != original_len ) {
Oct 21, 1999
Oct 21, 1999
181
int worksize;
Jul 3, 2000
Jul 3, 2000
182
183
if ( theWave->cvt.buf != NULL ) {
free(theWave->cvt.buf);
Oct 21, 1999
Oct 21, 1999
184
}
Jul 3, 2000
Jul 3, 2000
185
186
187
188
worksize = original_len*theWave->cvt.len_mult;
theWave->cvt.buf=(Uint8 *)malloc(worksize);
if ( theWave->cvt.buf == NULL ) {
SDL_mutexV(music_lock);
Oct 21, 1999
Oct 21, 1999
189
190
return;
}
Jul 3, 2000
Jul 3, 2000
191
theWave->cvt.len = original_len;
Oct 21, 1999
Oct 21, 1999
192
}
Jul 3, 2000
Jul 3, 2000
193
194
if ( (theWave->stop - pos) < original_len ) {
original_len = (theWave->stop - pos);
Oct 21, 1999
Oct 21, 1999
195
}
Sep 6, 2001
Sep 6, 2001
196
197
198
199
200
201
202
203
204
205
original_len = fread(theWave->cvt.buf,1,original_len,theWave->wavefp);
/* 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 ( (theWave->cvt.src_format & 0x0010) && (original_len & 1) ) {
original_len--;
}
Jul 3, 2000
Jul 3, 2000
206
207
208
theWave->cvt.len = original_len;
SDL_ConvertAudio(&theWave->cvt);
memcpy(stream, theWave->cvt.buf, theWave->cvt.len_cvt);
Oct 21, 1999
Oct 21, 1999
209
} else {
Jul 3, 2000
Jul 3, 2000
210
211
if ( (theWave->stop - pos) < len ) {
len = (theWave->stop - pos);
Oct 21, 1999
Oct 21, 1999
212
}
Jul 3, 2000
Jul 3, 2000
213
fread(stream, len, 1, theWave->wavefp);
Oct 21, 1999
Oct 21, 1999
214
215
}
}
Jul 3, 2000
Jul 3, 2000
216
SDL_mutexV(music_lock);
Oct 21, 1999
Oct 21, 1999
217
218
219
220
221
}
/* Stop playback of a stream previously started with WAVStream_Start() */
extern void WAVStream_Stop(void)
{
Jul 3, 2000
Jul 3, 2000
222
223
224
SDL_mutexP(music_lock);
theWave = NULL;
SDL_mutexV(music_lock);
Oct 21, 1999
Oct 21, 1999
225
226
227
228
229
230
}
/* Close the given WAV stream */
extern void WAVStream_FreeSong(WAVStream *wave)
{
if ( wave ) {
Jul 3, 2000
Jul 3, 2000
231
232
233
234
235
236
237
/* Remove song from the currently playing list */
SDL_mutexP(music_lock);
if ( wave == theWave ) {
theWave = NULL;
}
SDL_mutexV(music_lock);
Oct 21, 1999
Oct 21, 1999
238
239
240
241
242
243
244
245
246
247
248
249
/* Clean up associated data */
if ( wave->wavefp ) {
fclose(wave->wavefp);
}
if ( wave->cvt.buf ) {
free(wave->cvt.buf);
}
free(wave);
}
}
/* Return non-zero if a stream is currently playing */
Jul 3, 2000
Jul 3, 2000
250
extern int WAVStream_Active(void)
Oct 21, 1999
Oct 21, 1999
251
252
253
{
int active;
Jul 3, 2000
Jul 3, 2000
254
SDL_mutexP(music_lock);
Oct 21, 1999
Oct 21, 1999
255
active = 0;
Jul 3, 2000
Jul 3, 2000
256
if ( theWave && (ftell(theWave->wavefp) < theWave->stop) ) {
Oct 21, 1999
Oct 21, 1999
257
258
active = 1;
}
Jul 3, 2000
Jul 3, 2000
259
SDL_mutexV(music_lock);
Oct 21, 1999
Oct 21, 1999
260
261
262
263
264
265
266
267
268
269
270
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 ) {
Nov 30, 2001
Nov 30, 2001
271
Mix_SetError("Out of memory");
Oct 21, 1999
Oct 21, 1999
272
273
274
return(-1);
}
if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) {
Nov 30, 2001
Nov 30, 2001
275
Mix_SetError("Couldn't read chunk");
Oct 21, 1999
Oct 21, 1999
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
free(chunk->data);
return(-1);
}
} else {
SDL_RWseek(src, chunk->length, SEEK_CUR);
}
return(chunk->length);
}
static FILE *LoadWAVStream (const char *file, SDL_AudioSpec *spec,
long *start, long *stop)
{
int was_error;
FILE *wavefp;
SDL_RWops *src;
Chunk chunk;
int lenread;
/* WAV magic header */
Uint32 RIFFchunk;
Uint32 wavelen;
Uint32 WAVEmagic;
/* FMT chunk */
WaveFMT *format = NULL;
/* Make sure we are passed a valid data source */
was_error = 0;
wavefp = fopen(file, "rb");
src = NULL;
if ( wavefp ) {
src = SDL_RWFromFP(wavefp, 0);
}
if ( src == NULL ) {
was_error = 1;
goto done;
}
Dec 27, 1999
Dec 27, 1999
313
Oct 21, 1999
Oct 21, 1999
314
315
316
317
318
/* Check the magic header */
RIFFchunk = SDL_ReadLE32(src);
wavelen = SDL_ReadLE32(src);
WAVEmagic = SDL_ReadLE32(src);
if ( (RIFFchunk != RIFF) || (WAVEmagic != WAVE) ) {
Nov 30, 2001
Nov 30, 2001
319
Mix_SetError("Unrecognized file type (not WAVE)");
Oct 21, 1999
Oct 21, 1999
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
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);
Nov 30, 2001
Nov 30, 2001
342
Mix_SetError("Complex WAVE files not supported");
Oct 21, 1999
Oct 21, 1999
343
344
345
346
347
348
349
350
was_error = 1;
goto done;
}
switch (SDL_SwapLE16(format->encoding)) {
case PCM_CODE:
/* We can understand this */
break;
default:
Nov 30, 2001
Nov 30, 2001
351
Mix_SetError("Unknown WAVE data format");
Oct 21, 1999
Oct 21, 1999
352
353
354
355
356
357
358
359
360
361
362
363
364
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:
Nov 30, 2001
Nov 30, 2001
365
Mix_SetError("Unknown PCM data format");
Oct 21, 1999
Oct 21, 1999
366
367
368
was_error = 1;
goto done;
}
Feb 1, 2000
Feb 1, 2000
369
spec->channels = (Uint8) SDL_SwapLE16(format->channels);
Oct 21, 1999
Oct 21, 1999
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
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 ( src ) {
SDL_RWclose(src);
}
if ( was_error ) {
if ( wavefp ) {
fclose(wavefp);
wavefp = NULL;
}
}
return(wavefp);
}
Aug 19, 2001
Aug 19, 2001
400
401
402
403
404
/* 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
405
{
Aug 19, 2001
Aug 19, 2001
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* 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]);
Oct 21, 1999
Oct 21, 1999
424
425
426
427
428
429
}
static FILE *LoadAIFFStream (const char *file, SDL_AudioSpec *spec,
long *start, long *stop)
{
int was_error;
Aug 19, 2001
Aug 19, 2001
430
431
int found_SSND;
int found_COMM;
Oct 21, 1999
Oct 21, 1999
432
433
434
FILE *wavefp;
SDL_RWops *src;
Aug 19, 2001
Aug 19, 2001
435
436
437
438
Uint32 chunk_type;
Uint32 chunk_length;
long next_chunk;
Oct 21, 1999
Oct 21, 1999
439
440
441
442
443
444
445
446
447
448
/* AIFF magic header */
Uint32 FORMchunk;
Uint32 AIFFmagic;
/* SSND chunk */
Uint32 offset;
Uint32 blocksize;
/* COMM format chunk */
Uint16 channels;
Uint32 numsamples;
Uint16 samplesize;
Aug 19, 2001
Aug 19, 2001
449
Uint8 sane_freq[10];
Oct 21, 1999
Oct 21, 1999
450
451
452
453
454
455
456
457
458
459
460
461
462
463
Uint32 frequency;
/* Make sure we are passed a valid data source */
was_error = 0;
wavefp = fopen(file, "rb");
src = NULL;
if ( wavefp ) {
src = SDL_RWFromFP(wavefp, 0);
}
if ( src == NULL ) {
was_error = 1;
goto done;
}
Dec 27, 1999
Dec 27, 1999
464
Oct 21, 1999
Oct 21, 1999
465
466
/* Check the magic header */
FORMchunk = SDL_ReadLE32(src);
Aug 19, 2001
Aug 19, 2001
467
chunk_length = SDL_ReadBE32(src);
Oct 21, 1999
Oct 21, 1999
468
469
AIFFmagic = SDL_ReadLE32(src);
if ( (FORMchunk != FORM) || (AIFFmagic != AIFF) ) {
Nov 30, 2001
Nov 30, 2001
470
Mix_SetError("Unrecognized file type (not AIFF)");
Oct 21, 1999
Oct 21, 1999
471
472
473
474
was_error = 1;
goto done;
}
Aug 19, 2001
Aug 19, 2001
475
476
/* From what I understand of the specification, chunks may appear in
* any order, and we should just ignore unknown ones.
Sep 6, 2001
Sep 6, 2001
477
478
479
*
* TODO: Better sanity-checking. E.g. what happens if the AIFF file
* contains compressed sound data?
Aug 19, 2001
Aug 19, 2001
480
*/
Oct 21, 1999
Oct 21, 1999
481
Aug 19, 2001
Aug 19, 2001
482
483
found_SSND = 0;
found_COMM = 0;
Oct 21, 1999
Oct 21, 1999
484
Aug 19, 2001
Aug 19, 2001
485
486
487
488
489
490
491
492
493
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
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) {
Nov 30, 2001
Nov 30, 2001
520
Mix_SetError("Bad AIFF file (no SSND chunk)");
Aug 19, 2001
Aug 19, 2001
521
522
523
524
525
was_error = 1;
goto done;
}
if (!found_COMM) {
Nov 30, 2001
Nov 30, 2001
526
Mix_SetError("Bad AIFF file (no COMM chunk)");
Aug 19, 2001
Aug 19, 2001
527
528
was_error = 1;
goto done;
Oct 21, 1999
Oct 21, 1999
529
}
Aug 19, 2001
Aug 19, 2001
530
Sep 6, 2001
Sep 6, 2001
531
*stop = *start + channels * numsamples * (samplesize / 8);
Oct 21, 1999
Oct 21, 1999
532
533
534
535
536
537
/* Decode the audio data format */
memset(spec, 0, (sizeof *spec));
spec->freq = frequency;
switch (samplesize) {
case 8:
Sep 6, 2001
Sep 6, 2001
538
spec->format = AUDIO_S8;
Oct 21, 1999
Oct 21, 1999
539
540
break;
case 16:
Aug 19, 2001
Aug 19, 2001
541
spec->format = AUDIO_S16MSB;
Oct 21, 1999
Oct 21, 1999
542
543
break;
default:
Nov 30, 2001
Nov 30, 2001
544
Mix_SetError("Unknown samplesize in data format");
Oct 21, 1999
Oct 21, 1999
545
546
547
was_error = 1;
goto done;
}
Feb 1, 2000
Feb 1, 2000
548
spec->channels = (Uint8) channels;
Oct 21, 1999
Oct 21, 1999
549
550
551
552
553
554
555
556
557
558
559
560
561
562
spec->samples = 4096; /* Good default buffer size */
done:
if ( src ) {
SDL_RWclose(src);
}
if ( was_error ) {
if ( wavefp ) {
fclose(wavefp);
wavefp = NULL;
}
}
return(wavefp);
}