Skip to content

Latest commit

 

History

History
334 lines (278 loc) · 9.77 KB

music_mad.c

File metadata and controls

334 lines (278 loc) · 9.77 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 2, 2017
Jan 2, 2017
3
Copyright (C) 1997-2017 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
22
23
24
25
26
27
28
*/
#ifdef MP3_MAD_MUSIC
#include <string.h>
#include "music_mad.h"
mad_data *
Jun 2, 2013
Jun 2, 2013
29
mad_openFileRW(SDL_RWops *src, SDL_AudioSpec *mixer, int freesrc)
Dec 31, 2011
Dec 31, 2011
30
{
Jul 15, 2007
Jul 15, 2007
31
32
mad_data *mp3_mad;
Jan 13, 2012
Jan 13, 2012
33
mp3_mad = (mad_data *)SDL_malloc(sizeof(mad_data));
Feb 26, 2008
Feb 26, 2008
34
if (mp3_mad) {
Jun 2, 2013
Jun 2, 2013
35
36
mp3_mad->src = src;
mp3_mad->freesrc = freesrc;
May 22, 2013
May 22, 2013
37
38
39
40
41
42
43
44
45
46
mad_stream_init(&mp3_mad->stream);
mad_frame_init(&mp3_mad->frame);
mad_synth_init(&mp3_mad->synth);
mp3_mad->frames_read = 0;
mad_timer_reset(&mp3_mad->next_frame_start);
mp3_mad->volume = MIX_MAX_VOLUME;
mp3_mad->status = 0;
mp3_mad->output_begin = 0;
mp3_mad->output_end = 0;
mp3_mad->mixer = *mixer;
Oct 16, 2017
Oct 16, 2017
47
mp3_mad->output_buffer = NULL;
Feb 26, 2008
Feb 26, 2008
48
}
Jul 15, 2007
Jul 15, 2007
49
50
51
52
return mp3_mad;
}
void
Dec 31, 2011
Dec 31, 2011
53
54
mad_closeFile(mad_data *mp3_mad)
{
Jul 15, 2007
Jul 15, 2007
55
56
57
58
mad_stream_finish(&mp3_mad->stream);
mad_frame_finish(&mp3_mad->frame);
mad_synth_finish(&mp3_mad->synth);
Jun 2, 2013
Jun 2, 2013
59
60
if (mp3_mad->freesrc) {
SDL_RWclose(mp3_mad->src);
Feb 26, 2008
Feb 26, 2008
61
}
Oct 16, 2017
Oct 16, 2017
62
SDL_free(mp3_mad->output_buffer);
Jan 13, 2012
Jan 13, 2012
63
SDL_free(mp3_mad);
Jul 15, 2007
Jul 15, 2007
64
65
66
67
68
69
70
71
72
}
/* Starts the playback. */
void
mad_start(mad_data *mp3_mad) {
mp3_mad->status |= MS_playing;
}
/* Stops the playback. */
May 22, 2013
May 22, 2013
73
void
Jul 15, 2007
Jul 15, 2007
74
75
76
77
78
79
80
81
82
83
84
85
86
87
mad_stop(mad_data *mp3_mad) {
mp3_mad->status &= ~MS_playing;
}
/* Returns true if the playing is engaged, false otherwise. */
int
mad_isPlaying(mad_data *mp3_mad) {
return ((mp3_mad->status & MS_playing) != 0);
}
/* Reads the next frame from the file. Returns true on success or
false on failure. */
static int
read_next_frame(mad_data *mp3_mad) {
May 22, 2013
May 22, 2013
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
if (mp3_mad->stream.buffer == NULL ||
mp3_mad->stream.error == MAD_ERROR_BUFLEN) {
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. */
if (mp3_mad->stream.next_frame != NULL) {
remaining = mp3_mad->stream.bufend - mp3_mad->stream.next_frame;
memmove(mp3_mad->input_buffer, mp3_mad->stream.next_frame, remaining);
read_start = mp3_mad->input_buffer + remaining;
read_size = MAD_INPUT_BUFFER_SIZE - remaining;
} else {
read_size = MAD_INPUT_BUFFER_SIZE;
read_start = mp3_mad->input_buffer;
remaining = 0;
}
/* Now read additional bytes from the input file. */
Jun 4, 2013
Jun 4, 2013
110
read_size = SDL_RWread(mp3_mad->src, read_start, 1, read_size);
May 22, 2013
May 22, 2013
111
Oct 16, 2017
Oct 16, 2017
112
if (read_size == 0) {
May 22, 2013
May 22, 2013
113
if ((mp3_mad->status & (MS_input_eof | MS_input_error)) == 0) {
Oct 16, 2017
Oct 16, 2017
114
115
/* FIXME: how to detect error? */
mp3_mad->status |= MS_input_eof;
May 22, 2013
May 22, 2013
116
117
118
/* At the end of the file, we must stuff MAD_BUFFER_GUARD
number of 0 bytes. */
Jun 1, 2013
Jun 1, 2013
119
SDL_memset(read_start + read_size, 0, MAD_BUFFER_GUARD);
May 22, 2013
May 22, 2013
120
121
122
123
124
125
126
127
read_size += MAD_BUFFER_GUARD;
}
}
/* Now feed those bytes into the libmad stream. */
mad_stream_buffer(&mp3_mad->stream, mp3_mad->input_buffer,
read_size + remaining);
mp3_mad->stream.error = MAD_ERROR_NONE;
Jul 15, 2007
Jul 15, 2007
128
}
May 22, 2013
May 22, 2013
129
Jul 15, 2007
Jul 15, 2007
130
/* Now ask libmad to extract a frame from the data we just put in
May 22, 2013
May 22, 2013
131
its buffer. */
Jul 15, 2007
Jul 15, 2007
132
if (mad_frame_decode(&mp3_mad->frame, &mp3_mad->stream)) {
May 22, 2013
May 22, 2013
133
134
135
136
137
138
139
140
141
142
if (MAD_RECOVERABLE(mp3_mad->stream.error)) {
return 0;
} else if (mp3_mad->stream.error == MAD_ERROR_BUFLEN) {
return 0;
} else {
mp3_mad->status |= MS_decode_error;
return 0;
}
Jul 15, 2007
Jul 15, 2007
143
}
May 22, 2013
May 22, 2013
144
Jul 15, 2007
Jul 15, 2007
145
146
147
148
149
150
151
152
153
154
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
mp3_mad->frames_read++;
mad_timer_add(&mp3_mad->next_frame_start, mp3_mad->frame.header.duration);
return 1;
}
/* Scale a MAD sample to 16 bits for output. */
static signed int
scale(mad_fixed_t sample) {
/* round */
sample += (1L << (MAD_F_FRACBITS - 16));
/* clip */
if (sample >= MAD_F_ONE)
sample = MAD_F_ONE - 1;
else if (sample < -MAD_F_ONE)
sample = -MAD_F_ONE;
/* quantize */
return sample >> (MAD_F_FRACBITS + 1 - 16);
}
/* Once the frame has been read, copies its samples into the output
buffer. */
static void
decode_frame(mad_data *mp3_mad) {
struct mad_pcm *pcm;
unsigned int nchannels, nsamples;
mad_fixed_t const *left_ch, *right_ch;
unsigned char *out;
int ret;
mad_synth_frame(&mp3_mad->synth, &mp3_mad->frame);
pcm = &mp3_mad->synth.pcm;
if ((mp3_mad->status & MS_cvt_decoded) == 0) {
May 22, 2013
May 22, 2013
181
mp3_mad->status |= MS_cvt_decoded;
Jul 15, 2007
Jul 15, 2007
182
May 22, 2013
May 22, 2013
183
184
185
186
/* The first frame determines some key properties of the stream.
In particular, it tells us enough to set up the convert
structure now. */
SDL_BuildAudioCVT(&mp3_mad->cvt, AUDIO_S16, pcm->channels, mp3_mad->frame.header.samplerate, mp3_mad->mixer.format, mp3_mad->mixer.channels, mp3_mad->mixer.freq);
Jul 15, 2007
Jul 15, 2007
187
188
}
Oct 16, 2017
Oct 16, 2017
189
190
191
192
193
194
195
196
197
if (!mp3_mad->output_buffer) {
size_t sz = MAD_OUTPUT_BUFFER_SIZE;
if (mp3_mad->cvt.len_mult > 1) {
sz *= mp3_mad->cvt.len_mult;
}
mp3_mad->output_buffer = (unsigned char *) SDL_malloc(sz);
}
out = mp3_mad->output_buffer + mp3_mad->output_end;
Jul 15, 2007
Jul 15, 2007
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* pcm->samplerate contains the sampling frequency */
nchannels = pcm->channels;
nsamples = pcm->length;
left_ch = pcm->samples[0];
right_ch = pcm->samples[1];
while (nsamples--) {
signed int sample;
/* output sample(s) in 16-bit signed little-endian PCM */
sample = scale(*left_ch++);
*out++ = ((sample >> 0) & 0xff);
*out++ = ((sample >> 8) & 0xff);
if (nchannels == 2) {
sample = scale(*right_ch++);
*out++ = ((sample >> 0) & 0xff);
*out++ = ((sample >> 8) & 0xff);
}
}
mp3_mad->output_end = out - mp3_mad->output_buffer;
/*assert(mp3_mad->output_end <= MAD_OUTPUT_BUFFER_SIZE);*/
}
Oct 2, 2009
Oct 2, 2009
225
int
Jul 15, 2007
Jul 15, 2007
226
227
228
229
230
231
mad_getSamples(mad_data *mp3_mad, Uint8 *stream, int len) {
int bytes_remaining;
int num_bytes;
Uint8 *out;
if ((mp3_mad->status & MS_playing) == 0) {
May 22, 2013
May 22, 2013
232
/* We're not supposed to be playing, so send silence instead. */
Jun 1, 2013
Jun 1, 2013
233
SDL_memset(stream, 0, len);
May 22, 2013
May 22, 2013
234
return 0;
Jul 15, 2007
Jul 15, 2007
235
236
237
238
239
}
out = stream;
bytes_remaining = len;
while (bytes_remaining > 0) {
May 22, 2013
May 22, 2013
240
241
242
243
244
245
246
247
if (mp3_mad->output_end == mp3_mad->output_begin) {
/* We need to get a new frame. */
mp3_mad->output_begin = 0;
mp3_mad->output_end = 0;
if (!read_next_frame(mp3_mad)) {
if ((mp3_mad->status & MS_error_flags) != 0) {
/* Couldn't read a frame; either an error condition or
end-of-file. Stop. */
Jun 1, 2013
Jun 1, 2013
248
SDL_memset(out, 0, bytes_remaining);
May 22, 2013
May 22, 2013
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
mp3_mad->status &= ~MS_playing;
return bytes_remaining;
}
} else {
decode_frame(mp3_mad);
/* Now convert the frame data to the appropriate format for
output. */
mp3_mad->cvt.buf = mp3_mad->output_buffer;
mp3_mad->cvt.len = mp3_mad->output_end;
mp3_mad->output_end = (int)(mp3_mad->output_end * mp3_mad->cvt.len_ratio);
/*assert(mp3_mad->output_end <= MAD_OUTPUT_BUFFER_SIZE);*/
SDL_ConvertAudio(&mp3_mad->cvt);
}
}
num_bytes = mp3_mad->output_end - mp3_mad->output_begin;
if (bytes_remaining < num_bytes) {
num_bytes = bytes_remaining;
}
if (mp3_mad->volume == MIX_MAX_VOLUME) {
Jun 1, 2013
Jun 1, 2013
272
SDL_memcpy(out, mp3_mad->output_buffer + mp3_mad->output_begin, num_bytes);
May 22, 2013
May 22, 2013
273
} else {
Jan 20, 2017
Jan 20, 2017
274
SDL_MixAudioFormat(out, mp3_mad->output_buffer + mp3_mad->output_begin,
Aug 26, 2017
Aug 26, 2017
275
mp3_mad->mixer.format, num_bytes, mp3_mad->volume);
May 22, 2013
May 22, 2013
276
277
278
279
}
out += num_bytes;
mp3_mad->output_begin += num_bytes;
bytes_remaining -= num_bytes;
Jul 15, 2007
Jul 15, 2007
280
}
Oct 2, 2009
Oct 2, 2009
281
return 0;
Jul 15, 2007
Jul 15, 2007
282
283
284
285
286
287
288
289
}
void
mad_seek(mad_data *mp3_mad, double position) {
mad_timer_t target;
int int_part;
int_part = (int)position;
May 22, 2013
May 22, 2013
290
291
mad_timer_set(&target, int_part,
(int)((position - int_part) * 1000000), 1000000);
Jul 15, 2007
Jul 15, 2007
292
293
if (mad_timer_compare(mp3_mad->next_frame_start, target) > 0) {
May 22, 2013
May 22, 2013
294
295
296
297
298
299
300
301
302
303
304
305
/* 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. */
mp3_mad->frames_read = 0;
mad_timer_reset(&mp3_mad->next_frame_start);
mp3_mad->status &= ~MS_error_flags;
mp3_mad->output_begin = 0;
mp3_mad->output_end = 0;
Jun 4, 2013
Jun 4, 2013
306
SDL_RWseek(mp3_mad->src, 0, RW_SEEK_SET);
Jul 15, 2007
Jul 15, 2007
307
308
309
}
/* Now we have to skip frames until we come to the right one.
May 22, 2013
May 22, 2013
310
Again, only truly necessary if the file is VBR. */
Jul 15, 2007
Jul 15, 2007
311
while (mad_timer_compare(mp3_mad->next_frame_start, target) < 0) {
May 22, 2013
May 22, 2013
312
313
314
315
316
317
318
319
if (!read_next_frame(mp3_mad)) {
if ((mp3_mad->status & MS_error_flags) != 0) {
/* Couldn't read a frame; either an error condition or
end-of-file. Stop. */
mp3_mad->status &= ~MS_playing;
return;
}
}
Jul 15, 2007
Jul 15, 2007
320
321
322
}
/* Here we are, at the beginning of the frame that contains the
May 22, 2013
May 22, 2013
323
324
325
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. */
Jul 15, 2007
Jul 15, 2007
326
327
328
329
330
331
332
333
334
}
void
mad_setVolume(mad_data *mp3_mad, int volume) {
mp3_mad->volume = volume;
}
#endif /* MP3_MAD_MUSIC */