2005-06-13 06:15:05 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 David Bryant
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
#include "codec.h"
|
2005-06-13 06:15:05 +00:00
|
|
|
|
|
|
|
#include <codecs/libwavpack/wavpack.h>
|
|
|
|
#include "playback.h"
|
|
|
|
#include "lib/codeclib.h"
|
2005-06-26 19:41:29 +00:00
|
|
|
#include "dsp.h"
|
2005-06-13 06:15:05 +00:00
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
static struct codec_api *rb;
|
2005-06-13 06:15:05 +00:00
|
|
|
static struct codec_api *ci;
|
|
|
|
|
|
|
|
#define BUFFER_SIZE 4096
|
|
|
|
|
|
|
|
static long temp_buffer [BUFFER_SIZE] IDATA_ATTR;
|
|
|
|
|
|
|
|
static long read_callback (void *buffer, long bytes)
|
|
|
|
{
|
2005-06-30 06:09:59 +00:00
|
|
|
long retval = ci->read_filebuf (buffer, bytes);
|
|
|
|
ci->id3->offset = ci->curpos;
|
|
|
|
return retval;
|
2005-06-13 06:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef SIMULATOR
|
|
|
|
extern char iramcopy[];
|
|
|
|
extern char iramstart[];
|
|
|
|
extern char iramend[];
|
|
|
|
#endif
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
/* this is the codec entry point */
|
2005-06-22 19:55:09 +00:00
|
|
|
enum codec_status codec_start(struct codec_api* api)
|
2005-06-13 06:15:05 +00:00
|
|
|
{
|
|
|
|
WavpackContext *wpc;
|
|
|
|
char error [80];
|
2005-06-27 00:12:40 +00:00
|
|
|
int bps, nchans, sr_100;
|
2005-06-13 06:15:05 +00:00
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
/* Generic codec initialisation */
|
|
|
|
TEST_CODEC_API(api);
|
2005-06-13 06:15:05 +00:00
|
|
|
|
|
|
|
rb = api;
|
2005-06-22 19:41:30 +00:00
|
|
|
ci = api;
|
2005-06-13 06:15:05 +00:00
|
|
|
|
|
|
|
#ifndef SIMULATOR
|
|
|
|
rb->memcpy(iramstart, iramcopy, iramend-iramstart);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ci->configure(CODEC_SET_FILEBUF_LIMIT, (int *)(1024*1024*10));
|
|
|
|
ci->configure(CODEC_SET_FILEBUF_WATERMARK, (int *)(1024*512));
|
|
|
|
ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*128));
|
2005-06-26 19:41:29 +00:00
|
|
|
|
|
|
|
ci->configure(DSP_DITHER, (bool *)false);
|
|
|
|
ci->configure(DSP_SET_STEREO_MODE, (int *)STEREO_INTERLEAVED);
|
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, (int *)(16));
|
2005-06-13 06:15:05 +00:00
|
|
|
|
|
|
|
next_track:
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
if (codec_init(api))
|
|
|
|
return CODEC_ERROR;
|
2005-06-13 06:15:05 +00:00
|
|
|
|
2005-07-10 20:37:36 +00:00
|
|
|
while (!*rb->taginfo_ready && !ci->stop_codec)
|
2005-07-11 18:47:47 +00:00
|
|
|
ci->sleep(1);
|
2005-06-26 19:41:29 +00:00
|
|
|
|
2005-07-25 03:34:25 +00:00
|
|
|
if (ci->id3->frequency != NATIVE_FREQUENCY ||
|
|
|
|
ci->global_settings->replaygain) {
|
|
|
|
ci->configure(CODEC_DSP_ENABLE, (bool *)true);
|
|
|
|
ci->configure(DSP_SET_FREQUENCY, (long *)(ci->id3->frequency));
|
|
|
|
codec_set_replaygain(rb->id3);
|
2005-06-26 19:41:29 +00:00
|
|
|
}
|
2005-07-25 03:34:25 +00:00
|
|
|
else
|
|
|
|
ci->configure(CODEC_DSP_ENABLE, (bool *)false);
|
2005-06-26 19:41:29 +00:00
|
|
|
|
2005-06-13 06:15:05 +00:00
|
|
|
/* Create a decoder instance */
|
|
|
|
wpc = WavpackOpenFileInput (read_callback, error);
|
|
|
|
|
|
|
|
if (!wpc)
|
2005-06-22 19:41:30 +00:00
|
|
|
return CODEC_ERROR;
|
2005-06-13 06:15:05 +00:00
|
|
|
|
|
|
|
bps = WavpackGetBytesPerSample (wpc);
|
|
|
|
nchans = WavpackGetReducedChannels (wpc);
|
2005-06-27 00:12:40 +00:00
|
|
|
sr_100 = ci->id3->frequency / 100;
|
2005-06-13 06:15:05 +00:00
|
|
|
|
|
|
|
ci->set_elapsed (0);
|
|
|
|
|
|
|
|
/* The main decoder loop */
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
long nsamples;
|
|
|
|
|
|
|
|
if (ci->seek_time && ci->taginfo_ready && ci->id3->length) {
|
2005-06-27 00:12:40 +00:00
|
|
|
int curpos_ms = WavpackGetSampleIndex (wpc) / sr_100 * 10;
|
2005-06-13 06:15:05 +00:00
|
|
|
int n, d, skip;
|
|
|
|
|
|
|
|
if (ci->seek_time > curpos_ms) {
|
|
|
|
n = ci->seek_time - curpos_ms;
|
|
|
|
d = ci->id3->length - curpos_ms;
|
|
|
|
skip = (int)((long long)(ci->filesize - ci->curpos) * n / d);
|
|
|
|
ci->seek_buffer (ci->curpos + skip);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
n = curpos_ms - ci->seek_time;
|
|
|
|
d = curpos_ms;
|
|
|
|
skip = (int)((long long) ci->curpos * n / d);
|
|
|
|
ci->seek_buffer (ci->curpos - skip);
|
|
|
|
}
|
|
|
|
|
|
|
|
wpc = WavpackOpenFileInput (read_callback, error);
|
|
|
|
ci->seek_time = 0;
|
|
|
|
|
|
|
|
if (!wpc)
|
|
|
|
break;
|
|
|
|
|
2005-06-27 00:12:40 +00:00
|
|
|
ci->set_elapsed (WavpackGetSampleIndex (wpc) / sr_100 * 10);
|
2005-06-13 06:15:05 +00:00
|
|
|
rb->yield ();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsamples = WavpackUnpackSamples (wpc, temp_buffer, BUFFER_SIZE / 2);
|
|
|
|
|
|
|
|
if (!nsamples || ci->stop_codec || ci->reload_codec)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* convert mono to stereo here, in place */
|
|
|
|
|
|
|
|
if (nchans == 1) {
|
|
|
|
long *dst = temp_buffer + (nsamples * 2);
|
|
|
|
long *src = temp_buffer + nsamples;
|
|
|
|
long count = nsamples;
|
|
|
|
|
|
|
|
while (count--) {
|
|
|
|
*--dst = *--src;
|
|
|
|
*--dst = *src;
|
|
|
|
if (!(count & 0x7f))
|
|
|
|
rb->yield ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bps == 1) {
|
|
|
|
short *dst = (short *) temp_buffer;
|
|
|
|
long *src = temp_buffer;
|
|
|
|
long count = nsamples;
|
|
|
|
|
|
|
|
while (count--) {
|
|
|
|
*dst++ = *src++ << 8;
|
|
|
|
*dst++ = *src++ << 8;
|
|
|
|
if (!(count & 0x7f))
|
|
|
|
rb->yield ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (bps == 2) {
|
|
|
|
short *dst = (short *) temp_buffer;
|
|
|
|
long *src = temp_buffer;
|
|
|
|
long count = nsamples;
|
|
|
|
|
|
|
|
while (count--) {
|
|
|
|
*dst++ = *src++;
|
|
|
|
*dst++ = *src++;
|
|
|
|
if (!(count & 0x7f))
|
|
|
|
rb->yield ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
short *dst = (short *) temp_buffer;
|
|
|
|
int shift = (bps - 2) * 8;
|
|
|
|
long *src = temp_buffer;
|
|
|
|
long count = nsamples;
|
|
|
|
|
|
|
|
while (count--) {
|
|
|
|
*dst++ = *src++ >> shift;
|
|
|
|
*dst++ = *src++ >> shift;
|
|
|
|
if (!(count & 0x7f))
|
|
|
|
rb->yield ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ci->stop_codec || ci->reload_codec)
|
|
|
|
break;
|
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
while (!ci->pcmbuf_insert ((char *) temp_buffer, nsamples * 4))
|
2005-07-11 18:47:47 +00:00
|
|
|
rb->sleep (1);
|
2005-06-13 06:15:05 +00:00
|
|
|
|
2005-06-27 00:12:40 +00:00
|
|
|
ci->set_elapsed (WavpackGetSampleIndex (wpc) / sr_100 * 10);
|
2005-06-13 06:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ci->request_next_track())
|
|
|
|
goto next_track;
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
return CODEC_OK;
|
2005-06-13 06:15:05 +00:00
|
|
|
}
|