2005-02-16 01:38:22 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Dave Chapman
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
2005-02-18 16:28:52 +00:00
|
|
|
#if (CONFIG_HWCODEC == MASNONE)
|
|
|
|
/* software codec platforms */
|
2005-02-16 02:08:17 +00:00
|
|
|
|
2005-02-16 01:38:22 +00:00
|
|
|
#include <codecs/libmad/mad.h>
|
|
|
|
|
2005-02-16 18:51:21 +00:00
|
|
|
#include "lib/xxx2wav.h" /* Helper functions common to test decoders */
|
2005-02-16 01:38:22 +00:00
|
|
|
|
2005-02-16 18:51:21 +00:00
|
|
|
static struct plugin_api* rb;
|
2005-02-16 01:38:22 +00:00
|
|
|
|
2005-03-10 20:02:08 +00:00
|
|
|
struct mad_stream Stream IDATA_ATTR;
|
|
|
|
struct mad_frame Frame IDATA_ATTR;
|
|
|
|
struct mad_synth Synth IDATA_ATTR;
|
2005-02-16 01:38:22 +00:00
|
|
|
mad_timer_t Timer;
|
|
|
|
struct dither d0, d1;
|
|
|
|
|
2005-02-16 16:07:25 +00:00
|
|
|
/* The following function is used inside libmad - let's hope it's never
|
|
|
|
called.
|
|
|
|
*/
|
2005-02-16 01:38:22 +00:00
|
|
|
|
|
|
|
void abort(void) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The "dither" code to convert the 24-bit samples produced by libmad was
|
|
|
|
taken from the coolplayer project - coolplayer.sourceforge.net */
|
|
|
|
|
|
|
|
struct dither {
|
|
|
|
mad_fixed_t error[3];
|
|
|
|
mad_fixed_t random;
|
|
|
|
};
|
|
|
|
|
|
|
|
# define SAMPLE_DEPTH 16
|
|
|
|
# define scale(x, y) dither((x), (y))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NAME: prng()
|
|
|
|
* DESCRIPTION: 32-bit pseudo-random number generator
|
|
|
|
*/
|
|
|
|
static __inline
|
|
|
|
unsigned long prng(unsigned long state)
|
|
|
|
{
|
|
|
|
return (state * 0x0019660dL + 0x3c6ef35fL) & 0xffffffffL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NAME: dither()
|
|
|
|
* DESCRIPTION: dither and scale sample
|
|
|
|
*/
|
|
|
|
static __inline
|
|
|
|
signed int dither(mad_fixed_t sample, struct dither *dither)
|
|
|
|
{
|
|
|
|
unsigned int scalebits;
|
|
|
|
mad_fixed_t output, mask, random;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
MIN = -MAD_F_ONE,
|
|
|
|
MAX = MAD_F_ONE - 1
|
|
|
|
};
|
|
|
|
|
|
|
|
/* noise shape */
|
|
|
|
sample += dither->error[0] - dither->error[1] + dither->error[2];
|
|
|
|
|
|
|
|
dither->error[2] = dither->error[1];
|
|
|
|
dither->error[1] = dither->error[0] / 2;
|
|
|
|
|
|
|
|
/* bias */
|
|
|
|
output = sample + (1L << (MAD_F_FRACBITS + 1 - SAMPLE_DEPTH - 1));
|
|
|
|
|
|
|
|
scalebits = MAD_F_FRACBITS + 1 - SAMPLE_DEPTH;
|
|
|
|
mask = (1L << scalebits) - 1;
|
|
|
|
|
|
|
|
/* dither */
|
|
|
|
random = prng(dither->random);
|
|
|
|
output += (random & mask) - (dither->random & mask);
|
|
|
|
|
|
|
|
dither->random = random;
|
|
|
|
|
|
|
|
/* clip */
|
|
|
|
if (output > MAX) {
|
|
|
|
output = MAX;
|
|
|
|
|
|
|
|
if (sample > MAX)
|
|
|
|
sample = MAX;
|
|
|
|
}
|
|
|
|
else if (output < MIN) {
|
|
|
|
output = MIN;
|
|
|
|
|
|
|
|
if (sample < MIN)
|
|
|
|
sample = MIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* quantize */
|
|
|
|
output &= ~mask;
|
|
|
|
|
|
|
|
/* error feedback */
|
|
|
|
dither->error[0] = sample - output;
|
|
|
|
|
|
|
|
/* scale */
|
|
|
|
return output >> scalebits;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SHRT_MAX 32767
|
|
|
|
|
2005-03-01 14:39:01 +00:00
|
|
|
#define INPUT_BUFFER_SIZE (10*8192)
|
|
|
|
#define OUTPUT_BUFFER_SIZE 65536 /* Must be an integer multiple of 4. */
|
2005-02-16 01:38:22 +00:00
|
|
|
|
|
|
|
unsigned char InputBuffer[INPUT_BUFFER_SIZE+MAD_BUFFER_GUARD];
|
|
|
|
unsigned char OutputBuffer[OUTPUT_BUFFER_SIZE];
|
|
|
|
unsigned char *OutputPtr=OutputBuffer;
|
|
|
|
unsigned char *GuardPtr=NULL;
|
|
|
|
const unsigned char *OutputBufferEnd=OutputBuffer+OUTPUT_BUFFER_SIZE;
|
|
|
|
|
2005-04-19 10:50:04 +00:00
|
|
|
mad_fixed_t mad_frame_overlap[2][32][18] IDATA_ATTR;
|
|
|
|
unsigned char mad_main_data[MAD_BUFFER_MDLEN] IDATA_ATTR;
|
|
|
|
|
2005-03-10 20:02:08 +00:00
|
|
|
#ifdef USE_IRAM
|
2005-03-01 14:39:01 +00:00
|
|
|
extern char iramcopy[];
|
|
|
|
extern char iramstart[];
|
|
|
|
extern char iramend[];
|
|
|
|
#endif
|
|
|
|
|
2005-02-16 01:38:22 +00:00
|
|
|
/* this is the plugin entry point */
|
|
|
|
enum plugin_status plugin_start(struct plugin_api* api, void* file)
|
|
|
|
{
|
2005-02-16 16:07:25 +00:00
|
|
|
file_info_struct file_info;
|
2005-02-16 01:38:22 +00:00
|
|
|
int Status=0;
|
2005-02-16 16:07:25 +00:00
|
|
|
unsigned short Sample;
|
|
|
|
int i;
|
2005-02-16 01:38:22 +00:00
|
|
|
|
2005-02-16 16:07:25 +00:00
|
|
|
/* Generic plugin inititialisation */
|
2005-02-16 01:38:22 +00:00
|
|
|
|
2005-02-16 16:07:25 +00:00
|
|
|
TEST_PLUGIN_API(api);
|
2005-02-16 01:38:22 +00:00
|
|
|
rb = api;
|
|
|
|
|
2005-03-10 20:02:08 +00:00
|
|
|
#ifdef USE_IRAM
|
2005-03-01 14:39:01 +00:00
|
|
|
rb->memcpy(iramstart, iramcopy, iramend-iramstart);
|
|
|
|
#endif
|
|
|
|
|
2005-02-16 16:07:25 +00:00
|
|
|
/* This function sets up the buffers and reads the file into RAM */
|
2005-02-16 01:38:22 +00:00
|
|
|
|
2005-02-16 18:51:21 +00:00
|
|
|
if (local_init(file,"/libmadtest.wav",&file_info,api)) {
|
2005-02-16 16:07:25 +00:00
|
|
|
return PLUGIN_ERROR;
|
|
|
|
}
|
2005-02-16 01:38:22 +00:00
|
|
|
|
|
|
|
/* Create a decoder instance */
|
|
|
|
|
|
|
|
mad_stream_init(&Stream);
|
|
|
|
mad_frame_init(&Frame);
|
|
|
|
mad_synth_init(&Synth);
|
|
|
|
mad_timer_reset(&Timer);
|
|
|
|
|
2005-04-19 10:50:04 +00:00
|
|
|
/* We do this so libmad doesn't try to call codec_calloc() */
|
|
|
|
memset(mad_frame_overlap, 0, sizeof(mad_frame_overlap));
|
|
|
|
Frame.overlap = &mad_frame_overlap;
|
|
|
|
Stream.main_data = &mad_main_data;
|
|
|
|
|
|
|
|
GuardPtr = &filebuf[file_info.filesize];
|
|
|
|
memset(GuardPtr,0,MAD_BUFFER_GUARD);
|
|
|
|
mad_stream_buffer(&Stream, filebuf,file_info.filesize);
|
2005-02-16 01:38:22 +00:00
|
|
|
|
|
|
|
file_info.curpos=0;
|
2005-02-16 16:07:25 +00:00
|
|
|
file_info.start_tick=*(rb->current_tick);
|
2005-02-16 01:38:22 +00:00
|
|
|
|
|
|
|
rb->button_clear_queue();
|
|
|
|
|
|
|
|
/* This is the decoding loop. */
|
2005-04-19 10:50:04 +00:00
|
|
|
while (file_info.curpos < file_info.filesize &&
|
|
|
|
Stream.this_frame != GuardPtr &&
|
|
|
|
Stream.error != MAD_ERROR_BUFLEN) {
|
|
|
|
file_info.curpos += (int)Stream.next_frame - (int)Stream.this_frame;
|
2005-02-16 01:38:22 +00:00
|
|
|
|
|
|
|
if(mad_frame_decode(&Frame,&Stream))
|
|
|
|
{
|
|
|
|
if(MAD_RECOVERABLE(Stream.error))
|
|
|
|
{
|
|
|
|
if(Stream.error!=MAD_ERROR_LOSTSYNC || Stream.this_frame!=GuardPtr)
|
|
|
|
{
|
|
|
|
rb->splash(HZ*1, true, "Recoverable...!");
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if(Stream.error==MAD_ERROR_BUFLEN)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rb->splash(HZ*1, true, "Recoverable...!");
|
|
|
|
//fprintf(stderr,"%s: unrecoverable frame level error.\n",ProgName);
|
|
|
|
Status=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We assume all frames have same samplerate as the first */
|
2005-02-16 16:07:25 +00:00
|
|
|
if(file_info.frames_decoded==0) {
|
|
|
|
file_info.samplerate=Frame.header.samplerate;
|
2005-02-16 01:38:22 +00:00
|
|
|
}
|
|
|
|
|
2005-02-16 16:07:25 +00:00
|
|
|
file_info.frames_decoded++;
|
|
|
|
|
2005-02-16 01:38:22 +00:00
|
|
|
/* ?? Do we need the timer module? */
|
|
|
|
mad_timer_add(&Timer,Frame.header.duration);
|
|
|
|
|
|
|
|
/* DAVE: This can be used to attenuate the audio */
|
|
|
|
// if(DoFilter)
|
|
|
|
// ApplyFilter(&Frame);
|
|
|
|
|
|
|
|
mad_synth_frame(&Synth,&Frame);
|
|
|
|
|
|
|
|
/* Convert MAD's numbers to an array of 16-bit LE signed integers */
|
|
|
|
for(i=0;i<Synth.pcm.length;i++)
|
|
|
|
{
|
|
|
|
/* Left channel */
|
|
|
|
Sample=scale(Synth.pcm.samples[0][i],&d0);
|
|
|
|
*(OutputPtr++)=Sample&0xff;
|
|
|
|
*(OutputPtr++)=Sample>>8;
|
|
|
|
|
|
|
|
/* Right channel. If the decoded stream is monophonic then
|
|
|
|
* the right output channel is the same as the left one.
|
|
|
|
*/
|
|
|
|
if(MAD_NCHANNELS(&Frame.header)==2)
|
|
|
|
Sample=scale(Synth.pcm.samples[1][i],&d1);
|
|
|
|
*(OutputPtr++)=Sample&0xff;
|
|
|
|
*(OutputPtr++)=Sample>>8;
|
|
|
|
|
|
|
|
/* Flush the buffer if it is full. */
|
|
|
|
if(OutputPtr==OutputBufferEnd)
|
|
|
|
{
|
|
|
|
rb->write(file_info.outfile,OutputBuffer,OUTPUT_BUFFER_SIZE);
|
|
|
|
OutputPtr=OutputBuffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file_info.current_sample+=Synth.pcm.length;
|
|
|
|
|
2005-02-16 16:07:25 +00:00
|
|
|
display_status(&file_info);
|
2005-02-16 01:38:22 +00:00
|
|
|
|
|
|
|
if (rb->button_get(false)!=BUTTON_NONE) {
|
|
|
|
close_wav(&file_info);
|
|
|
|
return PLUGIN_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close_wav(&file_info);
|
|
|
|
rb->splash(HZ*2, true, "FINISHED!");
|
|
|
|
|
|
|
|
return PLUGIN_OK;
|
|
|
|
}
|
2005-02-16 02:08:17 +00:00
|
|
|
#endif /* CONFIG_HWCODEC == MASNONE */
|