Skip to content

Latest commit

 

History

History
675 lines (582 loc) · 21.5 KB

music_mad.c

File metadata and controls

675 lines (582 loc) · 21.5 KB
 
Jul 15, 2007
Jul 15, 2007
1
/*
Dec 31, 2011
Dec 31, 2011
2
SDL_mixer: An audio mixer library based on the SDL library
Jan 5, 2019
Jan 5, 2019
3
Copyright (C) 1997-2019 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.
Jul 15, 2007
Jul 15, 2007
20
21
*/
Oct 17, 2017
Oct 17, 2017
22
#ifdef MUSIC_MP3_MAD
Jul 15, 2007
Jul 15, 2007
23
24
25
#include "music_mad.h"
Oct 17, 2017
Oct 17, 2017
26
27
#include "mad.h"
Oct 21, 2017
Oct 21, 2017
28
29
30
31
32
33
34
35
36
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* NOTE: The dithering functions are GPL, which should be fine if your
application is GPL (which would need to be true if you enabled
libmad support in SDL_mixer). If you're using libmad under the
commercial license, you need to disable this code.
*/
/************************ dithering functions ***************************/
#ifdef MUSIC_MP3_MAD_GPL_DITHERING
/* All dithering done here is taken from the GPL'ed xmms-mad plugin. */
/* Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. */
/* Any feedback is very welcome. For any question, comments, */
/* see http://www.math.keio.ac.jp/matumoto/emt.html or email */
/* matumoto@math.keio.ac.jp */
/* Period parameters */
#define MP3_DITH_N 624
#define MP3_DITH_M 397
#define MATRIX_A 0x9908b0df /* constant vector a */
#define UPPER_MASK 0x80000000 /* most significant w-r bits */
#define LOWER_MASK 0x7fffffff /* least significant r bits */
/* Tempering parameters */
#define TEMPERING_MASK_B 0x9d2c5680
#define TEMPERING_MASK_C 0xefc60000
#define TEMPERING_SHIFT_U(y) (y >> 11)
#define TEMPERING_SHIFT_S(y) (y << 7)
#define TEMPERING_SHIFT_T(y) (y << 15)
#define TEMPERING_SHIFT_L(y) (y >> 18)
static unsigned long mt[MP3_DITH_N]; /* the array for the state vector */
static int mti=MP3_DITH_N+1; /* mti==MP3_DITH_N+1 means mt[MP3_DITH_N] is not initialized */
/* initializing the array with a NONZERO seed */
static void sgenrand(unsigned long seed)
{
/* setting initial seeds to mt[MP3_DITH_N] using */
/* the generator Line 25 of Table 1 in */
/* [KNUTH 1981, The Art of Computer Programming */
/* Vol. 2 (2nd Ed.), pp102] */
mt[0]= seed & 0xffffffff;
for (mti=1; mti<MP3_DITH_N; mti++)
mt[mti] = (69069 * mt[mti-1]) & 0xffffffff;
}
static unsigned long genrand(void)
{
unsigned long y;
static unsigned long mag01[2]={0x0, MATRIX_A};
/* mag01[x] = x * MATRIX_A for x=0,1 */
if (mti >= MP3_DITH_N) { /* generate MP3_DITH_N words at one time */
int kk;
if (mti == MP3_DITH_N+1) /* if sgenrand() has not been called, */
sgenrand(4357); /* a default initial seed is used */
for (kk=0;kk<MP3_DITH_N-MP3_DITH_M;kk++) {
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
mt[kk] = mt[kk+MP3_DITH_M] ^ (y >> 1) ^ mag01[y & 0x1];
}
for (;kk<MP3_DITH_N-1;kk++) {
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
mt[kk] = mt[kk+(MP3_DITH_M-MP3_DITH_N)] ^ (y >> 1) ^ mag01[y & 0x1];
}
y = (mt[MP3_DITH_N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
mt[MP3_DITH_N-1] = mt[MP3_DITH_M-1] ^ (y >> 1) ^ mag01[y & 0x1];
mti = 0;
}
y = mt[mti++];
y ^= TEMPERING_SHIFT_U(y);
y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
y ^= TEMPERING_SHIFT_L(y);
return y;
}
static long triangular_dither_noise(int nbits) {
/* parameter nbits : the peak-to-peak amplitude desired (in bits)
* use with nbits set to 2 + nber of bits to be trimmed.
* (because triangular is made from two uniformly distributed processes,
* it starts at 2 bits peak-to-peak amplitude)
* see The Theory of Dithered Quantization by Robert Alexander Wannamaker
* for complete proof of why that's optimal
*/
long v = (genrand()/2 - genrand()/2); /* in ]-2^31, 2^31[ */
long P = 1 << (32 - nbits); /* the power of 2 */
v /= P;
/* now v in ]-2^(nbits-1), 2^(nbits-1) [ */
return v;
}
#endif /* MUSIC_MP3_MAD_GPL_DITHERING */
Oct 17, 2017
Oct 17, 2017
129
130
131
132
133
#define MAD_INPUT_BUFFER_SIZE (5*8192)
enum {
MS_input_eof = 0x0001,
MS_input_error = 0x0001,
Oct 21, 2017
Oct 21, 2017
134
MS_decode_error = 0x0002,
Oct 17, 2017
Oct 17, 2017
135
136
137
138
MS_error_flags = 0x000f,
};
typedef struct {
Oct 21, 2017
Oct 21, 2017
139
int play_count;
Oct 17, 2017
Oct 17, 2017
140
SDL_RWops *src;
Jul 20, 2019
Jul 20, 2019
141
Sint64 start, length, pos;
Oct 17, 2017
Oct 17, 2017
142
143
144
145
146
147
148
int freesrc;
struct mad_stream stream;
struct mad_frame frame;
struct mad_synth synth;
mad_timer_t next_frame_start;
int volume;
int status;
Oct 21, 2017
Oct 21, 2017
149
SDL_AudioStream *audiostream;
Oct 17, 2017
Oct 17, 2017
150
151
unsigned char input_buffer[MAD_INPUT_BUFFER_SIZE + MAD_BUFFER_GUARD];
Oct 21, 2017
Oct 21, 2017
152
} MAD_Music;
Oct 17, 2017
Oct 17, 2017
153
154
Jul 20, 2019
Jul 20, 2019
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
static size_t MAD_RWread(MAD_Music *music, void *ptr, size_t size, size_t maxnum) {
size_t remaining = (size_t)(music->length - music->pos);
size_t ret;
maxnum *= size;
if (maxnum > remaining) maxnum = remaining;
ret = SDL_RWread(music->src, ptr, 1, maxnum);
music->pos += (Sint64)ret;
return ret;
}
static Sint64 MAD_RWseek(MAD_Music *music, Sint64 offset, int whence) {
Sint64 ret;
switch (whence) { /* assumes a legal whence value */
case RW_SEEK_CUR:
offset += music->pos;
break;
case RW_SEEK_END:
offset = music->length + offset;
break;
}
if (offset < 0) return -1;
if (offset > music->length)
offset = music->length;
ret = SDL_RWseek(music->src, music->start + offset, RW_SEEK_SET);
if (ret < 0) return ret;
music->pos = offset;
return (music->pos - music->start);
}
Oct 21, 2017
Oct 21, 2017
185
static int MAD_Seek(void *context, double position);
Jul 20, 2019
Jul 20, 2019
186
static int skip_tags(MAD_Music *music);
Oct 21, 2017
Oct 21, 2017
187
Oct 17, 2017
Oct 17, 2017
188
static void *MAD_CreateFromRW(SDL_RWops *src, int freesrc)
Dec 31, 2011
Dec 31, 2011
189
{
Oct 21, 2017
Oct 21, 2017
190
191
192
193
194
195
MAD_Music *music;
music = (MAD_Music *)SDL_calloc(1, sizeof(MAD_Music));
if (!music) {
SDL_OutOfMemory();
return NULL;
Oct 17, 2017
Oct 17, 2017
196
}
Oct 21, 2017
Oct 21, 2017
197
198
199
music->src = src;
music->volume = MIX_MAX_VOLUME;
Jul 20, 2019
Jul 20, 2019
200
201
202
203
204
205
206
music->length = SDL_RWsize(src);
if (skip_tags(music) < 0) {
SDL_free(music);
Mix_SetError("music_mad: corrupt mp3 file.");
return NULL;
}
Oct 21, 2017
Oct 21, 2017
207
208
209
210
211
212
213
mad_stream_init(&music->stream);
mad_frame_init(&music->frame);
mad_synth_init(&music->synth);
mad_timer_reset(&music->next_frame_start);
music->freesrc = freesrc;
return music;
Jul 15, 2007
Jul 15, 2007
214
215
}
Oct 17, 2017
Oct 17, 2017
216
static void MAD_SetVolume(void *context, int volume)
Dec 31, 2011
Dec 31, 2011
217
{
Oct 21, 2017
Oct 21, 2017
218
219
MAD_Music *music = (MAD_Music *)context;
music->volume = volume;
Jul 15, 2007
Jul 15, 2007
220
221
222
}
/* Starts the playback. */
Oct 21, 2017
Oct 21, 2017
223
static int MAD_Play(void *context, int play_count)
Oct 17, 2017
Oct 17, 2017
224
{
Oct 21, 2017
Oct 21, 2017
225
226
227
MAD_Music *music = (MAD_Music *)context;
music->play_count = play_count;
return MAD_Seek(music, 0.0);
Jul 15, 2007
Jul 15, 2007
228
229
}
Oct 7, 2018
Oct 7, 2018
230
231
232
/*************************** TAG HANDLING: ******************************/
Jul 20, 2019
Jul 20, 2019
233
static SDL_INLINE SDL_bool is_id3v1(const unsigned char *data, size_t length)
Oct 7, 2018
Oct 7, 2018
234
235
236
237
238
239
240
{
/* http://id3.org/ID3v1 : 3 bytes "TAG" identifier and 125 bytes tag data */
if (length < 3 || SDL_memcmp(data,"TAG",3) != 0) {
return SDL_FALSE;
}
return SDL_TRUE;
}
Jul 20, 2019
Jul 20, 2019
241
242
243
244
245
246
247
248
249
250
251
252
static SDL_INLINE SDL_bool is_id3v1ext(const unsigned char *data, size_t length)
{
/* ID3v1 extended tag: just before ID3v1, always 227 bytes.
* https://www.getid3.org/phpBB3/viewtopic.php?t=1202
* https://en.wikipedia.org/wiki/ID3v1#Enhanced_tag
* Not an official standard, is only supported by few programs. */
if (length < 4 || SDL_memcmp(data,"TAG+",4) != 0) {
return SDL_FALSE;
}
return SDL_TRUE;
}
static SDL_INLINE SDL_bool is_id3v2(const unsigned char *data, size_t length)
Oct 7, 2018
Oct 7, 2018
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
{
/* ID3v2 header is 10 bytes: http://id3.org/id3v2.4.0-structure */
/* bytes 0-2: "ID3" identifier */
if (length < 10 || SDL_memcmp(data,"ID3",3) != 0) {
return SDL_FALSE;
}
/* bytes 3-4: version num (major,revision), each byte always less than 0xff. */
if (data[3] == 0xff || data[4] == 0xff) {
return SDL_FALSE;
}
/* bytes 6-9 are the ID3v2 tag size: a 32 bit 'synchsafe' integer, i.e. the
* highest bit 7 in each byte zeroed. i.e.: 7 bit information in each byte ->
* effectively a 28 bit value. */
if (data[6] >= 0x80 || data[7] >= 0x80 || data[8] >= 0x80 || data[9] >= 0x80) {
return SDL_FALSE;
}
return SDL_TRUE;
}
Jul 20, 2019
Jul 20, 2019
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
static SDL_INLINE long get_id3v2_len(const unsigned char *data, long length)
{
/* size is a 'synchsafe' integer (see above) */
long size = (long)((data[6]<<21) + (data[7]<<14) + (data[8]<<7) + data[9]);
size += 10; /* header size */
/* ID3v2 header[5] is flags (bits 4-7 only, 0-3 are zero).
* bit 4 set: footer is present (a copy of the header but
* with "3DI" as ident.) */
if (data[5] & 0x10) {
size += 10; /* footer size */
}
/* optional padding (always zeroes) */
while (size < length && data[size] == 0) {
++size;
}
return size;
}
static SDL_INLINE SDL_bool is_apetag(const unsigned char *data, size_t length)
Oct 7, 2018
Oct 7, 2018
289
290
{
/* http://wiki.hydrogenaud.io/index.php?title=APEv2_specification
Jul 20, 2019
Jul 20, 2019
291
292
* Header/footer is 32 bytes: bytes 0-7 ident, bytes 8-11 version,
* bytes 12-17 size. bytes 24-31 are reserved: must be all zeroes. */
Oct 7, 2018
Oct 7, 2018
293
294
295
296
297
298
Uint32 v;
if (length < 32 || SDL_memcmp(data,"APETAGEX",8) != 0) {
return SDL_FALSE;
}
v = (data[11]<<24) | (data[10]<<16) | (data[9]<<8) | data[8]; /* version */
Jul 20, 2019
Jul 20, 2019
299
if (v != 2000U && v != 1000U) {
Oct 7, 2018
Oct 7, 2018
300
301
302
303
304
305
306
307
return SDL_FALSE;
}
v = 0; /* reserved bits : */
if (SDL_memcmp(&data[24],&v,4) != 0 || SDL_memcmp(&data[28],&v,4) != 0) {
return SDL_FALSE;
}
return SDL_TRUE;
}
Jul 20, 2019
Jul 20, 2019
308
static SDL_INLINE long get_ape_len(const unsigned char *data, long datalen, Uint32 *version)
Oct 7, 2018
Oct 7, 2018
309
{
Jul 20, 2019
Jul 20, 2019
310
311
312
313
long size = (long)((data[15]<<24) | (data[14]<<16) | (data[13]<<8) | data[12]);
*version = (data[11]<<24) | (data[10]<<16) | (data[9]<<8) | data[8];
return size; /* caller will handle the additional v2 header length */
}
Oct 7, 2018
Oct 7, 2018
314
Jul 20, 2019
Jul 20, 2019
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
static int skip_tags(MAD_Music *music)
{
long len; size_t readsize;
readsize = MAD_RWread(music, music->input_buffer, 1, MAD_INPUT_BUFFER_SIZE);
if (!readsize) return -1;
/* ID3v2 tag is at the start */
if (is_id3v2(music->input_buffer, readsize)) {
len = get_id3v2_len(music->input_buffer, (long)readsize);
if (len >= music->length) return -1;
music->start += len;
music->length -= len;
MAD_RWseek(music, 0, RW_SEEK_SET);
}
/* APE tag _might_ be at the start: read the header */
else if (is_apetag(music->input_buffer, readsize)) {
Uint32 v;
len = get_ape_len(music->input_buffer, (long)readsize, &v);
len += 32; /* we're at top: have a header. */
if (len >= music->length) return -1;
music->start += len;
music->length -= len;
MAD_RWseek(music, 0, RW_SEEK_SET);
Oct 7, 2018
Oct 7, 2018
339
}
Jul 20, 2019
Jul 20, 2019
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* ID3v1 tag is at the end */
if (music->length < 128) goto ape;
MAD_RWseek(music, -128, RW_SEEK_END);
readsize = MAD_RWread(music, music->input_buffer, 1, 128);
MAD_RWseek(music, 0, RW_SEEK_SET);
if (readsize != 128) return -1;
if (is_id3v1(music->input_buffer, 128)) {
music->length -= 128;
/* APE tag may be before the ID3v1: read the footer */
if (music->length < 32) goto end;
MAD_RWseek(music, -32, RW_SEEK_END);
readsize = MAD_RWread(music, music->input_buffer, 1, 32);
MAD_RWseek(music, 0, RW_SEEK_SET);
if (readsize != 32) return -1;
if (is_apetag(music->input_buffer, 32)) {
Uint32 v;
len = get_ape_len(music->input_buffer, (long)readsize, &v);
if (v == 2000U) len += 32; /* header */
if (len >= music->length) return -1;
if (v == 2000U) { /* verify header : */
MAD_RWseek(music, -len, RW_SEEK_END);
readsize = MAD_RWread(music, music->input_buffer, 1, 32);
MAD_RWseek(music, 0, RW_SEEK_SET);
if (readsize != 32) return -1;
if (!is_apetag(music->input_buffer, 32)) return -1;
}
music->length -= len;
goto end;
Oct 7, 2018
Oct 7, 2018
370
}
Jul 20, 2019
Jul 20, 2019
371
372
373
374
375
376
377
378
379
/* extended ID3v1 just before the ID3v1 tag? (unlikely) */
if (music->length < 227) goto end;
MAD_RWseek(music, -227, RW_SEEK_END);
readsize = MAD_RWread(music, music->input_buffer, 1, 227);
MAD_RWseek(music, 0, RW_SEEK_SET);
if (readsize != 227) return -1;
if (is_id3v1ext(music->input_buffer, 227)) {
music->length -= 227;
goto end;
Oct 7, 2018
Oct 7, 2018
380
381
}
}
Jul 20, 2019
Jul 20, 2019
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
ape: /* APE tag may be at the end: read the footer */
if (music->length >= 32) {
MAD_RWseek(music, -32, RW_SEEK_END);
readsize = MAD_RWread(music, music->input_buffer, 1, 32);
MAD_RWseek(music, 0, RW_SEEK_SET);
if (readsize != 32) return -1;
if (is_apetag(music->input_buffer, 32)) {
Uint32 v;
len = get_ape_len(music->input_buffer, (long)readsize, &v);
if (v == 2000U) len += 32; /* header */
if (len >= music->length) return -1;
if (v == 2000U) { /* verify header : */
MAD_RWseek(music, -len, RW_SEEK_END);
readsize = MAD_RWread(music, music->input_buffer, 1, 32);
MAD_RWseek(music, 0, RW_SEEK_SET);
if (readsize != 32) return -1;
if (!is_apetag(music->input_buffer, 32)) return -1;
}
music->length -= len;
}
Oct 7, 2018
Oct 7, 2018
402
403
}
Jul 20, 2019
Jul 20, 2019
404
405
end:
return (music->length > 0)? 0: -1;
Oct 7, 2018
Oct 7, 2018
406
407
}
Oct 21, 2017
Oct 21, 2017
408
409
410
411
/* Reads the next frame from the file.
Returns true on success or false on failure.
*/
static SDL_bool read_next_frame(MAD_Music *music)
Oct 17, 2017
Oct 17, 2017
412
{
Oct 21, 2017
Oct 21, 2017
413
414
if (music->stream.buffer == NULL ||
music->stream.error == MAD_ERROR_BUFLEN) {
Oct 17, 2017
Oct 17, 2017
415
416
417
418
419
420
421
size_t read_size;
size_t remaining;
unsigned char *read_start;
/* There might be some bytes in the buffer left over from last
time. If so, move them down and read more bytes following
them. */
Oct 21, 2017
Oct 21, 2017
422
423
424
425
if (music->stream.next_frame != NULL) {
remaining = music->stream.bufend - music->stream.next_frame;
memmove(music->input_buffer, music->stream.next_frame, remaining);
read_start = music->input_buffer + remaining;
Oct 17, 2017
Oct 17, 2017
426
427
428
429
read_size = MAD_INPUT_BUFFER_SIZE - remaining;
} else {
read_size = MAD_INPUT_BUFFER_SIZE;
Oct 21, 2017
Oct 21, 2017
430
read_start = music->input_buffer;
Oct 17, 2017
Oct 17, 2017
431
432
remaining = 0;
}
May 22, 2013
May 22, 2013
433
Oct 17, 2017
Oct 17, 2017
434
/* Now read additional bytes from the input file. */
Jul 20, 2019
Jul 20, 2019
435
read_size = MAD_RWread(music, read_start, 1, read_size);
May 22, 2013
May 22, 2013
436
Oct 17, 2017
Oct 17, 2017
437
if (read_size == 0) {
Oct 21, 2017
Oct 21, 2017
438
if ((music->status & (MS_input_eof | MS_input_error)) == 0) {
Oct 17, 2017
Oct 17, 2017
439
/* FIXME: how to detect error? */
Oct 21, 2017
Oct 21, 2017
440
music->status |= MS_input_eof;
May 22, 2013
May 22, 2013
441
Oct 17, 2017
Oct 17, 2017
442
443
444
445
446
447
/* At the end of the file, we must stuff MAD_BUFFER_GUARD
number of 0 bytes. */
SDL_memset(read_start + read_size, 0, MAD_BUFFER_GUARD);
read_size += MAD_BUFFER_GUARD;
}
}
May 22, 2013
May 22, 2013
448
Oct 17, 2017
Oct 17, 2017
449
/* Now feed those bytes into the libmad stream. */
Oct 21, 2017
Oct 21, 2017
450
mad_stream_buffer(&music->stream, music->input_buffer,
Oct 17, 2017
Oct 17, 2017
451
read_size + remaining);
Oct 21, 2017
Oct 21, 2017
452
music->stream.error = MAD_ERROR_NONE;
Oct 17, 2017
Oct 17, 2017
453
}
May 22, 2013
May 22, 2013
454
Oct 17, 2017
Oct 17, 2017
455
456
/* Now ask libmad to extract a frame from the data we just put in
its buffer. */
Oct 21, 2017
Oct 21, 2017
457
458
if (mad_frame_decode(&music->frame, &music->stream)) {
if (MAD_RECOVERABLE(music->stream.error)) {
Oct 7, 2018
Oct 7, 2018
459
mad_stream_sync(&music->stream); /* to frame seek mode */
Oct 21, 2017
Oct 21, 2017
460
return SDL_FALSE;
May 22, 2013
May 22, 2013
461
Oct 21, 2017
Oct 21, 2017
462
463
} else if (music->stream.error == MAD_ERROR_BUFLEN) {
return SDL_FALSE;
May 22, 2013
May 22, 2013
464
Oct 17, 2017
Oct 17, 2017
465
} else {
Oct 21, 2017
Oct 21, 2017
466
467
468
Mix_SetError("mad_frame_decode() failed, corrupt stream?");
music->status |= MS_decode_error;
return SDL_FALSE;
Oct 17, 2017
Oct 17, 2017
469
}
May 22, 2013
May 22, 2013
470
471
}
Oct 21, 2017
Oct 21, 2017
472
mad_timer_add(&music->next_frame_start, music->frame.header.duration);
Jul 15, 2007
Jul 15, 2007
473
Oct 21, 2017
Oct 21, 2017
474
return SDL_TRUE;
Jul 15, 2007
Jul 15, 2007
475
476
477
}
/* Scale a MAD sample to 16 bits for output. */
Oct 21, 2017
Oct 21, 2017
478
479
480
481
static Sint16 scale(mad_fixed_t sample)
{
const int n_bits_to_loose = MAD_F_FRACBITS + 1 - 16;
Oct 17, 2017
Oct 17, 2017
482
/* round */
Oct 21, 2017
Oct 21, 2017
483
484
485
486
487
sample += (1L << (n_bits_to_loose - 1));
#ifdef MUSIC_MP3_MAD_GPL_DITHERING
sample += triangular_dither_noise(n_bits_to_loose + 1);
#endif
Jul 15, 2007
Jul 15, 2007
488
Oct 17, 2017
Oct 17, 2017
489
490
491
492
493
/* clip */
if (sample >= MAD_F_ONE)
sample = MAD_F_ONE - 1;
else if (sample < -MAD_F_ONE)
sample = -MAD_F_ONE;
Jul 15, 2007
Jul 15, 2007
494
Oct 17, 2017
Oct 17, 2017
495
/* quantize */
Oct 21, 2017
Oct 21, 2017
496
return (Sint16)(sample >> n_bits_to_loose);
Jul 15, 2007
Jul 15, 2007
497
498
}
Oct 17, 2017
Oct 17, 2017
499
/* Once the frame has been read, copies its samples into the output buffer. */
Oct 21, 2017
Oct 21, 2017
500
501
static SDL_bool decode_frame(MAD_Music *music)
{
Oct 17, 2017
Oct 17, 2017
502
struct mad_pcm *pcm;
Oct 21, 2017
Oct 21, 2017
503
unsigned int i, nchannels, nsamples;
Oct 17, 2017
Oct 17, 2017
504
mad_fixed_t const *left_ch, *right_ch;
Oct 21, 2017
Oct 21, 2017
505
506
Sint16 *buffer, *dst;
int result;
Oct 17, 2017
Oct 17, 2017
507
Oct 21, 2017
Oct 21, 2017
508
509
mad_synth_frame(&music->synth, &music->frame);
pcm = &music->synth.pcm;
Oct 17, 2017
Oct 17, 2017
510
Oct 21, 2017
Oct 21, 2017
511
512
513
514
515
516
if (!music->audiostream) {
music->audiostream = SDL_NewAudioStream(AUDIO_S16, pcm->channels, pcm->samplerate, music_spec.format, music_spec.channels, music_spec.freq);
if (!music->audiostream) {
return SDL_FALSE;
}
}
Oct 17, 2017
Oct 17, 2017
517
Oct 21, 2017
Oct 21, 2017
518
519
520
521
522
523
524
525
nchannels = pcm->channels;
nsamples = pcm->length;
left_ch = pcm->samples[0];
right_ch = pcm->samples[1];
buffer = SDL_stack_alloc(Sint16, nsamples*nchannels);
if (!buffer) {
SDL_OutOfMemory();
return SDL_FALSE;
Oct 16, 2017
Oct 16, 2017
526
527
}
Oct 21, 2017
Oct 21, 2017
528
529
530
531
532
533
534
535
536
dst = buffer;
if (nchannels == 1) {
for (i = nsamples; i--;) {
*dst++ = scale(*left_ch++);
}
} else {
for (i = nsamples; i--;) {
*dst++ = scale(*left_ch++);
*dst++ = scale(*right_ch++);
Oct 17, 2017
Oct 17, 2017
537
538
}
}
Jul 15, 2007
Jul 15, 2007
539
Oct 21, 2017
Oct 21, 2017
540
541
result = SDL_AudioStreamPut(music->audiostream, buffer, (nsamples * nchannels * sizeof(Sint16)));
SDL_stack_free(buffer);
Jul 15, 2007
Jul 15, 2007
542
Oct 21, 2017
Oct 21, 2017
543
544
545
546
547
if (result < 0) {
return SDL_FALSE;
}
return SDL_TRUE;
}
Jul 15, 2007
Jul 15, 2007
548
Oct 21, 2017
Oct 21, 2017
549
550
551
552
static int MAD_GetSome(void *context, void *data, int bytes, SDL_bool *done)
{
MAD_Music *music = (MAD_Music *)context;
int filled;
Jul 15, 2007
Jul 15, 2007
553
Oct 21, 2017
Oct 21, 2017
554
555
556
557
if (music->audiostream) {
filled = SDL_AudioStreamGet(music->audiostream, data, bytes);
if (filled != 0) {
return filled;
Oct 17, 2017
Oct 17, 2017
558
}
Jul 15, 2007
Jul 15, 2007
559
560
}
Oct 21, 2017
Oct 21, 2017
561
562
563
if (!music->play_count) {
/* All done */
*done = SDL_TRUE;
Oct 17, 2017
Oct 17, 2017
564
565
566
return 0;
}
Oct 21, 2017
Oct 21, 2017
567
568
569
if (read_next_frame(music)) {
if (!decode_frame(music)) {
return -1;
Oct 17, 2017
Oct 17, 2017
570
}
Oct 21, 2017
Oct 21, 2017
571
} else if (music->status & MS_input_eof) {
Oct 21, 2017
Oct 21, 2017
572
573
574
575
576
577
int play_count = -1;
if (music->play_count > 0) {
play_count = (music->play_count - 1);
}
if (MAD_Play(music, play_count) < 0) {
return -1;
Oct 17, 2017
Oct 17, 2017
578
}
Oct 21, 2017
Oct 21, 2017
579
580
} else if (music->status & MS_decode_error) {
return -1;
May 22, 2013
May 22, 2013
581
}
Oct 17, 2017
Oct 17, 2017
582
583
return 0;
}
Oct 21, 2017
Oct 21, 2017
584
585
586
587
588
static int MAD_GetAudio(void *context, void *data, int bytes)
{
MAD_Music *music = (MAD_Music *)context;
return music_pcm_getaudio(context, data, bytes, music->volume, MAD_GetSome);
}
May 22, 2013
May 22, 2013
589
Oct 17, 2017
Oct 17, 2017
590
591
static int MAD_Seek(void *context, double position)
{
Oct 21, 2017
Oct 21, 2017
592
MAD_Music *music = (MAD_Music *)context;
Oct 17, 2017
Oct 17, 2017
593
594
595
596
597
598
mad_timer_t target;
int int_part;
int_part = (int)position;
mad_timer_set(&target, int_part, (int)((position - int_part) * 1000000), 1000000);
Oct 21, 2017
Oct 21, 2017
599
if (mad_timer_compare(music->next_frame_start, target) > 0) {
Oct 17, 2017
Oct 17, 2017
600
601
602
603
604
605
/* In order to seek backwards in a VBR file, we have to rewind and
start again from the beginning. This isn't necessary if the
file happens to be CBR, of course; in that case we could seek
directly to the frame we want. But I leave that little
optimization for the future developer who discovers she really
needs it. */
Oct 21, 2017
Oct 21, 2017
606
607
mad_timer_reset(&music->next_frame_start);
music->status &= ~MS_error_flags;
Oct 17, 2017
Oct 17, 2017
608
Jul 20, 2019
Jul 20, 2019
609
MAD_RWseek(music, 0, RW_SEEK_SET);
May 22, 2013
May 22, 2013
610
}
Jul 15, 2007
Jul 15, 2007
611
Oct 17, 2017
Oct 17, 2017
612
613
/* Now we have to skip frames until we come to the right one.
Again, only truly necessary if the file is VBR. */
Oct 21, 2017
Oct 21, 2017
614
615
616
while (mad_timer_compare(music->next_frame_start, target) < 0) {
if (!read_next_frame(music)) {
if ((music->status & MS_error_flags) != 0) {
Oct 17, 2017
Oct 17, 2017
617
618
619
620
621
/* Couldn't read a frame; either an error condition or
end-of-file. Stop. */
return Mix_SetError("Seek position out of range");
}
}
May 22, 2013
May 22, 2013
622
}
Jul 15, 2007
Jul 15, 2007
623
Oct 17, 2017
Oct 17, 2017
624
625
626
627
628
/* Here we are, at the beginning of the frame that contains the
target time. Ehh, I say that's close enough. If we wanted to,
we could get more precise by decoding the frame now and counting
the appropriate number of samples out of it. */
return 0;
Jul 15, 2007
Jul 15, 2007
629
630
}
Oct 17, 2017
Oct 17, 2017
631
632
static void MAD_Delete(void *context)
{
Oct 21, 2017
Oct 21, 2017
633
MAD_Music *music = (MAD_Music *)context;
Jul 15, 2007
Jul 15, 2007
634
Oct 21, 2017
Oct 21, 2017
635
636
637
638
639
640
641
642
643
mad_stream_finish(&music->stream);
mad_frame_finish(&music->frame);
mad_synth_finish(&music->synth);
if (music->audiostream) {
SDL_FreeAudioStream(music->audiostream);
}
if (music->freesrc) {
SDL_RWclose(music->src);
Oct 17, 2017
Oct 17, 2017
644
}
Oct 21, 2017
Oct 21, 2017
645
SDL_free(music);
Oct 17, 2017
Oct 17, 2017
646
647
648
649
650
651
652
653
654
655
}
Mix_MusicInterface Mix_MusicInterface_MAD =
{
"MAD",
MIX_MUSIC_MAD,
MUS_MP3,
SDL_FALSE,
SDL_FALSE,
Oct 21, 2017
Oct 21, 2017
656
657
NULL, /* Load */
NULL, /* Open */
Oct 17, 2017
Oct 17, 2017
658
MAD_CreateFromRW,
Oct 21, 2017
Oct 21, 2017
659
NULL, /* CreateFromFile */
Oct 17, 2017
Oct 17, 2017
660
661
MAD_SetVolume,
MAD_Play,
Oct 21, 2017
Oct 21, 2017
662
NULL, /* IsPlaying */
Oct 17, 2017
Oct 17, 2017
663
664
MAD_GetAudio,
MAD_Seek,
Oct 21, 2017
Oct 21, 2017
665
666
667
NULL, /* Pause */
NULL, /* Resume */
NULL, /* Stop */
Oct 17, 2017
Oct 17, 2017
668
MAD_Delete,
Oct 21, 2017
Oct 21, 2017
669
670
NULL, /* Close */
NULL, /* Unload */
Oct 17, 2017
Oct 17, 2017
671
672
673
674
675
};
#endif /* MUSIC_MP3_MAD */
/* vi: set ts=4 sw=4 expandtab: */