| 1 |
/* |
| 2 |
SDL_mixer: An audio mixer library based on the SDL library |
| 3 |
Copyright (C) 1997-2012 Sam Lantinga <[email protected]> |
| 4 |
|
| 5 |
This software is provided 'as-is', without any express or implied |
| 6 |
warranty. In no event will the authors be held liable for any damages |
| 7 |
arising from the use of this software. |
| 8 |
|
| 9 |
Permission is granted to anyone to use this software for any purpose, |
| 10 |
including commercial applications, and to alter it and redistribute it |
| 11 |
freely, subject to the following restrictions: |
| 12 |
|
| 13 |
1. The origin of this software must not be misrepresented; you must not |
| 14 |
claim that you wrote the original software. If you use this software |
| 15 |
in a product, an acknowledgment in the product documentation would be |
| 16 |
appreciated but is not required. |
| 17 |
2. Altered source versions must be plainly marked as such, and must not be |
| 18 |
misrepresented as being the original software. |
| 19 |
3. This notice may not be removed or altered from any source distribution. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifdef MP3_MAD_MUSIC |
| 23 |
|
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include "music_mad.h" |
| 27 |
|
| 28 |
mad_data * |
| 29 |
mad_openFileRW(SDL_RWops *rw, SDL_AudioSpec *mixer, int freerw) |
| 30 |
{ |
| 31 |
mad_data *mp3_mad; |
| 32 |
|
| 33 |
mp3_mad = (mad_data *)SDL_malloc(sizeof(mad_data)); |
| 34 |
if (mp3_mad) { |
| 35 |
mp3_mad->rw = rw; |
| 36 |
mp3_mad->freerw = freerw; |
| 37 |
mad_stream_init(&mp3_mad->stream); |
| 38 |
mad_frame_init(&mp3_mad->frame); |
| 39 |
mad_synth_init(&mp3_mad->synth); |
| 40 |
mp3_mad->frames_read = 0; |
| 41 |
mad_timer_reset(&mp3_mad->next_frame_start); |
| 42 |
mp3_mad->volume = MIX_MAX_VOLUME; |
| 43 |
mp3_mad->status = 0; |
| 44 |
mp3_mad->output_begin = 0; |
| 45 |
mp3_mad->output_end = 0; |
| 46 |
mp3_mad->mixer = *mixer; |
| 47 |
} |
| 48 |
return mp3_mad; |
| 49 |
} |
| 50 |
|
| 51 |
void |
| 52 |
mad_closeFile(mad_data *mp3_mad) |
| 53 |
{ |
| 54 |
mad_stream_finish(&mp3_mad->stream); |
| 55 |
mad_frame_finish(&mp3_mad->frame); |
| 56 |
mad_synth_finish(&mp3_mad->synth); |
| 57 |
|
| 58 |
if (mp3_mad->freerw) { |
| 59 |
SDL_RWclose(mp3_mad->rw); |
| 60 |
} |
| 61 |
SDL_free(mp3_mad); |
| 62 |
} |
| 63 |
|
| 64 |
/* Starts the playback. */ |
| 65 |
void |
| 66 |
mad_start(mad_data *mp3_mad) { |
| 67 |
mp3_mad->status |= MS_playing; |
| 68 |
} |
| 69 |
|
| 70 |
/* Stops the playback. */ |
| 71 |
void |
| 72 |
mad_stop(mad_data *mp3_mad) { |
| 73 |
mp3_mad->status &= ~MS_playing; |
| 74 |
} |
| 75 |
|
| 76 |
/* Returns true if the playing is engaged, false otherwise. */ |
| 77 |
int |
| 78 |
mad_isPlaying(mad_data *mp3_mad) { |
| 79 |
return ((mp3_mad->status & MS_playing) != 0); |
| 80 |
} |
| 81 |
|
| 82 |
/* Reads the next frame from the file. Returns true on success or |
| 83 |
false on failure. */ |
| 84 |
static int |
| 85 |
read_next_frame(mad_data *mp3_mad) { |
| 86 |
if (mp3_mad->stream.buffer == NULL || |
| 87 |
mp3_mad->stream.error == MAD_ERROR_BUFLEN) { |
| 88 |
size_t read_size; |
| 89 |
size_t remaining; |
| 90 |
unsigned char *read_start; |
| 91 |
|
| 92 |
/* There might be some bytes in the buffer left over from last |
| 93 |
time. If so, move them down and read more bytes following |
| 94 |
them. */ |
| 95 |
if (mp3_mad->stream.next_frame != NULL) { |
| 96 |
remaining = mp3_mad->stream.bufend - mp3_mad->stream.next_frame; |
| 97 |
memmove(mp3_mad->input_buffer, mp3_mad->stream.next_frame, remaining); |
| 98 |
read_start = mp3_mad->input_buffer + remaining; |
| 99 |
read_size = MAD_INPUT_BUFFER_SIZE - remaining; |
| 100 |
|
| 101 |
} else { |
| 102 |
read_size = MAD_INPUT_BUFFER_SIZE; |
| 103 |
read_start = mp3_mad->input_buffer; |
| 104 |
remaining = 0; |
| 105 |
} |
| 106 |
|
| 107 |
/* Now read additional bytes from the input file. */ |
| 108 |
read_size = SDL_RWread(mp3_mad->rw, read_start, 1, read_size); |
| 109 |
|
| 110 |
if (read_size <= 0) { |
| 111 |
if ((mp3_mad->status & (MS_input_eof | MS_input_error)) == 0) { |
| 112 |
if (read_size == 0) { |
| 113 |
mp3_mad->status |= MS_input_eof; |
| 114 |
} else { |
| 115 |
mp3_mad->status |= MS_input_error; |
| 116 |
} |
| 117 |
|
| 118 |
/* At the end of the file, we must stuff MAD_BUFFER_GUARD |
| 119 |
number of 0 bytes. */ |
| 120 |
memset(read_start + read_size, 0, MAD_BUFFER_GUARD); |
| 121 |
read_size += MAD_BUFFER_GUARD; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/* Now feed those bytes into the libmad stream. */ |
| 126 |
mad_stream_buffer(&mp3_mad->stream, mp3_mad->input_buffer, |
| 127 |
read_size + remaining); |
| 128 |
mp3_mad->stream.error = MAD_ERROR_NONE; |
| 129 |
} |
| 130 |
|
| 131 |
/* Now ask libmad to extract a frame from the data we just put in |
| 132 |
its buffer. */ |
| 133 |
if (mad_frame_decode(&mp3_mad->frame, &mp3_mad->stream)) { |
| 134 |
if (MAD_RECOVERABLE(mp3_mad->stream.error)) { |
| 135 |
return 0; |
| 136 |
|
| 137 |
} else if (mp3_mad->stream.error == MAD_ERROR_BUFLEN) { |
| 138 |
return 0; |
| 139 |
|
| 140 |
} else { |
| 141 |
mp3_mad->status |= MS_decode_error; |
| 142 |
return 0; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
mp3_mad->frames_read++; |
| 147 |
mad_timer_add(&mp3_mad->next_frame_start, mp3_mad->frame.header.duration); |
| 148 |
|
| 149 |
return 1; |
| 150 |
} |
| 151 |
|
| 152 |
/* Scale a MAD sample to 16 bits for output. */ |
| 153 |
static signed int |
| 154 |
scale(mad_fixed_t sample) { |
| 155 |
/* round */ |
| 156 |
sample += (1L << (MAD_F_FRACBITS - 16)); |
| 157 |
|
| 158 |
/* clip */ |
| 159 |
if (sample >= MAD_F_ONE) |
| 160 |
sample = MAD_F_ONE - 1; |
| 161 |
else if (sample < -MAD_F_ONE) |
| 162 |
sample = -MAD_F_ONE; |
| 163 |
|
| 164 |
/* quantize */ |
| 165 |
return sample >> (MAD_F_FRACBITS + 1 - 16); |
| 166 |
} |
| 167 |
|
| 168 |
/* Once the frame has been read, copies its samples into the output |
| 169 |
buffer. */ |
| 170 |
static void |
| 171 |
decode_frame(mad_data *mp3_mad) { |
| 172 |
struct mad_pcm *pcm; |
| 173 |
unsigned int nchannels, nsamples; |
| 174 |
mad_fixed_t const *left_ch, *right_ch; |
| 175 |
unsigned char *out; |
| 176 |
int ret; |
| 177 |
|
| 178 |
mad_synth_frame(&mp3_mad->synth, &mp3_mad->frame); |
| 179 |
pcm = &mp3_mad->synth.pcm; |
| 180 |
out = mp3_mad->output_buffer + mp3_mad->output_end; |
| 181 |
|
| 182 |
if ((mp3_mad->status & MS_cvt_decoded) == 0) { |
| 183 |
mp3_mad->status |= MS_cvt_decoded; |
| 184 |
|
| 185 |
/* The first frame determines some key properties of the stream. |
| 186 |
In particular, it tells us enough to set up the convert |
| 187 |
structure now. */ |
| 188 |
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); |
| 189 |
} |
| 190 |
|
| 191 |
/* pcm->samplerate contains the sampling frequency */ |
| 192 |
|
| 193 |
nchannels = pcm->channels; |
| 194 |
nsamples = pcm->length; |
| 195 |
left_ch = pcm->samples[0]; |
| 196 |
right_ch = pcm->samples[1]; |
| 197 |
|
| 198 |
while (nsamples--) { |
| 199 |
signed int sample; |
| 200 |
|
| 201 |
/* output sample(s) in 16-bit signed little-endian PCM */ |
| 202 |
|
| 203 |
sample = scale(*left_ch++); |
| 204 |
*out++ = ((sample >> 0) & 0xff); |
| 205 |
*out++ = ((sample >> 8) & 0xff); |
| 206 |
|
| 207 |
if (nchannels == 2) { |
| 208 |
sample = scale(*right_ch++); |
| 209 |
*out++ = ((sample >> 0) & 0xff); |
| 210 |
*out++ = ((sample >> 8) & 0xff); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
mp3_mad->output_end = out - mp3_mad->output_buffer; |
| 215 |
/*assert(mp3_mad->output_end <= MAD_OUTPUT_BUFFER_SIZE);*/ |
| 216 |
} |
| 217 |
|
| 218 |
int |
| 219 |
mad_getSamples(mad_data *mp3_mad, Uint8 *stream, int len) { |
| 220 |
int bytes_remaining; |
| 221 |
int num_bytes; |
| 222 |
Uint8 *out; |
| 223 |
|
| 224 |
if ((mp3_mad->status & MS_playing) == 0) { |
| 225 |
/* We're not supposed to be playing, so send silence instead. */ |
| 226 |
memset(stream, 0, len); |
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
out = stream; |
| 231 |
bytes_remaining = len; |
| 232 |
while (bytes_remaining > 0) { |
| 233 |
if (mp3_mad->output_end == mp3_mad->output_begin) { |
| 234 |
/* We need to get a new frame. */ |
| 235 |
mp3_mad->output_begin = 0; |
| 236 |
mp3_mad->output_end = 0; |
| 237 |
if (!read_next_frame(mp3_mad)) { |
| 238 |
if ((mp3_mad->status & MS_error_flags) != 0) { |
| 239 |
/* Couldn't read a frame; either an error condition or |
| 240 |
end-of-file. Stop. */ |
| 241 |
memset(out, 0, bytes_remaining); |
| 242 |
mp3_mad->status &= ~MS_playing; |
| 243 |
return bytes_remaining; |
| 244 |
} |
| 245 |
} else { |
| 246 |
decode_frame(mp3_mad); |
| 247 |
|
| 248 |
/* Now convert the frame data to the appropriate format for |
| 249 |
output. */ |
| 250 |
mp3_mad->cvt.buf = mp3_mad->output_buffer; |
| 251 |
mp3_mad->cvt.len = mp3_mad->output_end; |
| 252 |
|
| 253 |
mp3_mad->output_end = (int)(mp3_mad->output_end * mp3_mad->cvt.len_ratio); |
| 254 |
/*assert(mp3_mad->output_end <= MAD_OUTPUT_BUFFER_SIZE);*/ |
| 255 |
SDL_ConvertAudio(&mp3_mad->cvt); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
num_bytes = mp3_mad->output_end - mp3_mad->output_begin; |
| 260 |
if (bytes_remaining < num_bytes) { |
| 261 |
num_bytes = bytes_remaining; |
| 262 |
} |
| 263 |
|
| 264 |
if (mp3_mad->volume == MIX_MAX_VOLUME) { |
| 265 |
memcpy(out, mp3_mad->output_buffer + mp3_mad->output_begin, num_bytes); |
| 266 |
} else { |
| 267 |
SDL_MixAudio(out, mp3_mad->output_buffer + mp3_mad->output_begin, |
| 268 |
num_bytes, mp3_mad->volume); |
| 269 |
} |
| 270 |
out += num_bytes; |
| 271 |
mp3_mad->output_begin += num_bytes; |
| 272 |
bytes_remaining -= num_bytes; |
| 273 |
} |
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
void |
| 278 |
mad_seek(mad_data *mp3_mad, double position) { |
| 279 |
mad_timer_t target; |
| 280 |
int int_part; |
| 281 |
|
| 282 |
int_part = (int)position; |
| 283 |
mad_timer_set(&target, int_part, |
| 284 |
(int)((position - int_part) * 1000000), 1000000); |
| 285 |
|
| 286 |
if (mad_timer_compare(mp3_mad->next_frame_start, target) > 0) { |
| 287 |
/* In order to seek backwards in a VBR file, we have to rewind and |
| 288 |
start again from the beginning. This isn't necessary if the |
| 289 |
file happens to be CBR, of course; in that case we could seek |
| 290 |
directly to the frame we want. But I leave that little |
| 291 |
optimization for the future developer who discovers she really |
| 292 |
needs it. */ |
| 293 |
mp3_mad->frames_read = 0; |
| 294 |
mad_timer_reset(&mp3_mad->next_frame_start); |
| 295 |
mp3_mad->status &= ~MS_error_flags; |
| 296 |
mp3_mad->output_begin = 0; |
| 297 |
mp3_mad->output_end = 0; |
| 298 |
|
| 299 |
SDL_RWseek(mp3_mad->rw, 0, RW_SEEK_SET); |
| 300 |
} |
| 301 |
|
| 302 |
/* Now we have to skip frames until we come to the right one. |
| 303 |
Again, only truly necessary if the file is VBR. */ |
| 304 |
while (mad_timer_compare(mp3_mad->next_frame_start, target) < 0) { |
| 305 |
if (!read_next_frame(mp3_mad)) { |
| 306 |
if ((mp3_mad->status & MS_error_flags) != 0) { |
| 307 |
/* Couldn't read a frame; either an error condition or |
| 308 |
end-of-file. Stop. */ |
| 309 |
mp3_mad->status &= ~MS_playing; |
| 310 |
return; |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
/* Here we are, at the beginning of the frame that contains the |
| 316 |
target time. Ehh, I say that's close enough. If we wanted to, |
| 317 |
we could get more precise by decoding the frame now and counting |
| 318 |
the appropriate number of samples out of it. */ |
| 319 |
} |
| 320 |
|
| 321 |
void |
| 322 |
mad_setVolume(mad_data *mp3_mad, int volume) { |
| 323 |
mp3_mad->volume = volume; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
#endif /* MP3_MAD_MUSIC */ |