| 1 |
/* Emacs style mode select -*- C -*- |
| 2 |
*----------------------------------------------------------------------------- |
| 3 |
* |
| 4 |
* |
| 5 |
* PrBoom: a Doom port merged with LxDoom and LSDLDoom |
| 6 |
* based on BOOM, a modified and improved DOOM engine |
| 7 |
* |
| 8 |
* Copyright (C) 2011 by |
| 9 |
* Nicholai Main |
| 10 |
* |
| 11 |
* This program is free software; you can redistribute it and/or |
| 12 |
* modify it under the terms of the GNU General Public License |
| 13 |
* as published by the Free Software Foundation; either version 2 |
| 14 |
* of the License, or (at your option) any later version. |
| 15 |
* |
| 16 |
* This program is distributed in the hope that it will be useful, |
| 17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
* GNU General Public License for more details. |
| 20 |
* |
| 21 |
* You should have received a copy of the GNU General Public License |
| 22 |
* along with this program; if not, write to the Free Software |
| 23 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 24 |
* 02111-1307, USA. |
| 25 |
* |
| 26 |
* DESCRIPTION: |
| 27 |
* |
| 28 |
*--------------------------------------------------------------------- |
| 29 |
*/ |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#ifdef HAVE_CONFIG_H |
| 34 |
#include "config.h" |
| 35 |
#endif |
| 36 |
|
| 37 |
#include "musicplayer.h" |
| 38 |
|
| 39 |
#ifndef HAVE_LIBMAD |
| 40 |
|
| 41 |
#include <stddef.h> |
| 42 |
|
| 43 |
static const char *mp_name (void) |
| 44 |
{ |
| 45 |
return "mad mp3 player (DISABLED)"; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
static int mp_init (int samplerate) |
| 50 |
{ |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
const music_player_t mp_player = |
| 55 |
{ |
| 56 |
mp_name, |
| 57 |
mp_init, |
| 58 |
NULL, |
| 59 |
NULL, |
| 60 |
NULL, |
| 61 |
NULL, |
| 62 |
NULL, |
| 63 |
NULL, |
| 64 |
NULL, |
| 65 |
NULL, |
| 66 |
NULL |
| 67 |
}; |
| 68 |
|
| 69 |
#else // HAVE_LIBMAD |
| 70 |
|
| 71 |
|
| 72 |
#include <stdlib.h> |
| 73 |
#include <string.h> |
| 74 |
#include "lprintf.h" |
| 75 |
|
| 76 |
#include <mad.h> |
| 77 |
|
| 78 |
#include "i_sound.h" |
| 79 |
|
| 80 |
static struct mad_stream Stream; |
| 81 |
static struct mad_frame Frame; |
| 82 |
static struct mad_synth Synth; |
| 83 |
static struct mad_header Header; |
| 84 |
|
| 85 |
|
| 86 |
static int mp_looping = 0; |
| 87 |
static int mp_volume = 0; // 0-15 |
| 88 |
static int mp_samplerate_target = 0; |
| 89 |
static int mp_paused = 0; |
| 90 |
static int mp_playing = 0; |
| 91 |
|
| 92 |
static const void *mp_data; |
| 93 |
static int mp_len; |
| 94 |
|
| 95 |
|
| 96 |
static int mp_leftoversamps = 0; // number of extra samples |
| 97 |
// left over in mad decoder |
| 98 |
static int mp_leftoversamppos = 0; |
| 99 |
|
| 100 |
|
| 101 |
static const char *mp_name (void) |
| 102 |
{ |
| 103 |
return "mad mp3 player"; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
static int mp_init (int samplerate) |
| 108 |
{ |
| 109 |
mad_stream_init (&Stream); |
| 110 |
mad_frame_init (&Frame); |
| 111 |
mad_synth_init (&Synth); |
| 112 |
mad_header_init (&Header); |
| 113 |
mp_samplerate_target = samplerate; |
| 114 |
return 1; |
| 115 |
} |
| 116 |
|
| 117 |
static void mp_shutdown (void) |
| 118 |
{ |
| 119 |
|
| 120 |
mad_synth_finish (&Synth); |
| 121 |
mad_frame_finish (&Frame); |
| 122 |
mad_stream_finish (&Stream); |
| 123 |
mad_header_finish (&Header); |
| 124 |
} |
| 125 |
|
| 126 |
static const void *mp_registersong (const void *data, unsigned len) |
| 127 |
{ |
| 128 |
int i; |
| 129 |
int maxtry; |
| 130 |
int success = 0; |
| 131 |
|
| 132 |
// the MP3 standard doesn't include any global file header. the only way to tell filetype |
| 133 |
// is to start decoding stuff. you can't be too strict however because MP3 is resilient to |
| 134 |
// crap in the stream. |
| 135 |
|
| 136 |
// this routine is a bit slower than it could be, but apparently there are lots of files out |
| 137 |
// there with some dodgy stuff at the beginning. |
| 138 |
|
| 139 |
// if the stream begins with an ID3v2 magic, search hard and long for our first valid header |
| 140 |
if (memcmp (data, "ID3", 3) == 0) |
| 141 |
maxtry = 100; |
| 142 |
// otherwise, search for not so long |
| 143 |
else |
| 144 |
maxtry = 20; |
| 145 |
|
| 146 |
mad_stream_buffer (&Stream, (const unsigned char*)data, len); |
| 147 |
|
| 148 |
for (i = 0; i < maxtry; i++) |
| 149 |
{ |
| 150 |
if (mad_header_decode (&Header, &Stream) != 0) |
| 151 |
{ |
| 152 |
if (!MAD_RECOVERABLE (Stream.error)) |
| 153 |
{ |
| 154 |
return NULL; |
| 155 |
} |
| 156 |
} |
| 157 |
else |
| 158 |
{ |
| 159 |
success++; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
// 80% to pass |
| 164 |
if (success < maxtry * 8 / 10) |
| 165 |
{ |
| 166 |
return NULL; |
| 167 |
} |
| 168 |
|
| 169 |
lprintf (LO_INFO, "mad_registersong succeed. bitrate %lu samplerate %d\n", Header.bitrate, Header.samplerate); |
| 170 |
|
| 171 |
mp_data = data; |
| 172 |
mp_len = len; |
| 173 |
// handle not used |
| 174 |
return data; |
| 175 |
} |
| 176 |
|
| 177 |
static void mp_setvolume (int v) |
| 178 |
{ |
| 179 |
mp_volume = v; |
| 180 |
} |
| 181 |
|
| 182 |
static void mp_pause (void) |
| 183 |
{ |
| 184 |
mp_paused = 1; |
| 185 |
} |
| 186 |
|
| 187 |
static void mp_resume (void) |
| 188 |
{ |
| 189 |
mp_paused = 0; |
| 190 |
} |
| 191 |
|
| 192 |
static void mp_unregistersong (const void *handle) |
| 193 |
{ // nothing to do |
| 194 |
mp_data = NULL; |
| 195 |
mp_playing = 0; |
| 196 |
} |
| 197 |
|
| 198 |
static void mp_play (const void *handle, int looping) |
| 199 |
{ |
| 200 |
mad_stream_buffer (&Stream, (const unsigned char *)mp_data, mp_len); |
| 201 |
|
| 202 |
mp_playing = 1; |
| 203 |
mp_looping = looping; |
| 204 |
mp_leftoversamps = 0; |
| 205 |
mp_leftoversamppos = 0; |
| 206 |
} |
| 207 |
|
| 208 |
static void mp_stop (void) |
| 209 |
{ |
| 210 |
mp_playing = 0; |
| 211 |
} |
| 212 |
|
| 213 |
// convert from mad's internal fixed point representation |
| 214 |
static inline short mp_fixtoshort (mad_fixed_t f) |
| 215 |
{ |
| 216 |
// clip |
| 217 |
if (f < -MAD_F_ONE) |
| 218 |
f = -MAD_F_ONE; |
| 219 |
if (f > MAD_F_ONE) |
| 220 |
f = MAD_F_ONE; |
| 221 |
// apply volume before conversion to 16bit |
| 222 |
f /= 15; |
| 223 |
f *= mp_volume; |
| 224 |
f >>= (MAD_F_FRACBITS - 15); |
| 225 |
|
| 226 |
return (short) f; |
| 227 |
} |
| 228 |
|
| 229 |
static void mp_render_ex (void *dest, unsigned nsamp) |
| 230 |
{ |
| 231 |
short *sout = (short *) dest; |
| 232 |
|
| 233 |
int localerrors = 0; |
| 234 |
|
| 235 |
if (!mp_playing || mp_paused) |
| 236 |
{ |
| 237 |
memset (dest, 0, nsamp * 4); |
| 238 |
return; |
| 239 |
} |
| 240 |
|
| 241 |
while (1) |
| 242 |
{ |
| 243 |
// write any leftover data from last MP3 frame |
| 244 |
while (mp_leftoversamps > 0 && nsamp > 0) |
| 245 |
{ |
| 246 |
short s = mp_fixtoshort (Synth.pcm.samples[0][mp_leftoversamppos]); |
| 247 |
*sout++ = s; |
| 248 |
if (Synth.pcm.channels == 2) |
| 249 |
s = mp_fixtoshort (Synth.pcm.samples[1][mp_leftoversamppos]); |
| 250 |
// if mono, just duplicate the first channel again |
| 251 |
*sout++ = s; |
| 252 |
|
| 253 |
mp_leftoversamps -= 1; |
| 254 |
mp_leftoversamppos += 1; |
| 255 |
nsamp -= 1; |
| 256 |
} |
| 257 |
if (nsamp == 0) |
| 258 |
return; // done |
| 259 |
|
| 260 |
// decode next valid MP3 frame |
| 261 |
while (mad_frame_decode (&Frame, &Stream) != 0) |
| 262 |
{ |
| 263 |
if (MAD_RECOVERABLE (Stream.error)) |
| 264 |
{ // unspecified problem with one frame. |
| 265 |
// try the next frame, but bail if we get a bunch of crap in a row; |
| 266 |
// likely indicates a larger problem (and if we don't bail, we could |
| 267 |
// spend arbitrarily long amounts of time looking for the next good |
| 268 |
// packet) |
| 269 |
localerrors++; |
| 270 |
if (localerrors == 10) |
| 271 |
{ |
| 272 |
lprintf (LO_WARN, "mad_frame_decode: Lots of errors. Most recent %s\n", mad_stream_errorstr (&Stream)); |
| 273 |
mp_playing = 0; |
| 274 |
memset (sout, 0, nsamp * 4); |
| 275 |
return; |
| 276 |
} |
| 277 |
} |
| 278 |
else if (Stream.error == MAD_ERROR_BUFLEN) |
| 279 |
{ // EOF |
| 280 |
// FIXME: in order to not drop the last frame, there must be at least MAD_BUFFER_GUARD |
| 281 |
// of extra bytes (with value 0) at the end of the file. current implementation |
| 282 |
// drops last frame |
| 283 |
if (mp_looping) |
| 284 |
{ // rewind, then go again |
| 285 |
mad_stream_buffer (&Stream, (const unsigned char *)mp_data, mp_len); |
| 286 |
continue; |
| 287 |
} |
| 288 |
else |
| 289 |
{ // stop |
| 290 |
mp_playing = 0; |
| 291 |
memset (sout, 0, nsamp * 4); |
| 292 |
return; |
| 293 |
} |
| 294 |
} |
| 295 |
else |
| 296 |
{ // oh well. |
| 297 |
lprintf (LO_WARN, "mad_frame_decode: Unrecoverable error %s\n", mad_stream_errorstr (&Stream)); |
| 298 |
mp_playing = 0; |
| 299 |
memset (sout, 0, nsamp * 4); |
| 300 |
return; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
// got a good frame, so synth it and dispatch it. |
| 305 |
mad_synth_frame (&Synth, &Frame); |
| 306 |
mp_leftoversamps = Synth.pcm.length; |
| 307 |
mp_leftoversamppos = 0; |
| 308 |
|
| 309 |
} |
| 310 |
// NOT REACHED |
| 311 |
} |
| 312 |
|
| 313 |
static void mp_render (void *dest, unsigned nsamp) |
| 314 |
{ |
| 315 |
I_ResampleStream (dest, nsamp, mp_render_ex, Header.samplerate, mp_samplerate_target); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
const music_player_t mp_player = |
| 320 |
{ |
| 321 |
mp_name, |
| 322 |
mp_init, |
| 323 |
mp_shutdown, |
| 324 |
mp_setvolume, |
| 325 |
mp_pause, |
| 326 |
mp_resume, |
| 327 |
mp_registersong, |
| 328 |
mp_unregistersong, |
| 329 |
mp_play, |
| 330 |
mp_stop, |
| 331 |
mp_render |
| 332 |
}; |
| 333 |
|
| 334 |
#endif // HAVE_LIBMAD |