// // MP3 decoding using libMAD // // (c) 1999-2004 Jim Peters . All Rights Reserved. // For latest version see http://sbagen.sf.net/ or // http://uazu.net/sbagen/. Released under the GNU GPL version 2. // #include "libs/mad.h" extern FILE *mix_in; extern void *Alloc(size_t); extern void error(char *fmt, ...); int mp3_read(int *dst, int dlen); static struct mad_stream stream; static struct mad_frame frame; static struct mad_synth synth; static char *mp3_buf; static int mp3_len; static int synth_extra; // Extra samples carried over from previous frame void mp3_init() { mp3_len= 32768; mp3_buf= Alloc(mp3_len); // Setup MAD decoder mad_stream_init(&stream); mad_frame_init(&frame); mad_synth_init(&synth); mad_stream_options(&stream, 0); // Force first data read stream.error= MAD_ERROR_BUFLEN; synth_extra= 0; inbuf_start(mp3_read, 256*1024); // 1024K buffer: 3s@44.1kHz } void mp3_term() { mad_synth_finish(&synth); mad_frame_finish(&frame); mad_stream_finish(&stream); free(mp3_buf); } // Limiting and truncation to 20 bits #define ROUND(xx) ((((xx)<-MAD_F_ONE) ? -MAD_F_ONE : \ ((xx)>=MAD_F_ONE) ? MAD_F_ONE-1 : \ (xx)) >> (MAD_F_FRACBITS-15-4)) int mp3_read(int *dst, int dlen) { int *dst0= dst; int cnt, a, val; //debug("mp3_read %d", dlen); while (dlen > 0) { // First use up any samples from previous synth frame if (synth_extra > 0) { cnt= dlen/2; if (cnt > synth_extra) cnt= synth_extra; for (a= 0; a