2005-06-13 06:15:05 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 David Bryant
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2005-06-13 06:15:05 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2005-10-13 11:32:52 +00:00
|
|
|
#include "codeclib.h"
|
|
|
|
#include "libwavpack/wavpack.h"
|
2005-06-13 06:15:05 +00:00
|
|
|
|
2006-01-18 00:05:14 +00:00
|
|
|
CODEC_HEADER
|
|
|
|
|
2005-06-13 06:15:05 +00:00
|
|
|
#define BUFFER_SIZE 4096
|
|
|
|
|
2006-03-26 22:54:15 +00:00
|
|
|
static int32_t temp_buffer [BUFFER_SIZE] IBSS_ATTR;
|
2005-06-13 06:15:05 +00:00
|
|
|
|
2006-03-26 22:54:15 +00:00
|
|
|
static int32_t read_callback (void *buffer, int32_t bytes)
|
2005-06-13 06:15:05 +00:00
|
|
|
{
|
2006-03-26 22:54:15 +00:00
|
|
|
int32_t retval = ci->read_filebuf (buffer, bytes);
|
2005-06-30 06:09:59 +00:00
|
|
|
ci->id3->offset = ci->curpos;
|
|
|
|
return retval;
|
2005-06-13 06:15:05 +00:00
|
|
|
}
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
/* this is the codec entry point */
|
2006-11-26 18:31:41 +00:00
|
|
|
enum codec_status codec_main(void)
|
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;
|
2006-01-18 20:22:03 +00:00
|
|
|
int retval;
|
2005-06-13 06:15:05 +00:00
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
/* Generic codec initialisation */
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(CODEC_SET_FILEBUF_WATERMARK, 1024*512);
|
2005-06-26 19:41:29 +00:00
|
|
|
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
|
2005-06-13 06:15:05 +00:00
|
|
|
|
|
|
|
next_track:
|
|
|
|
|
2006-11-26 18:31:41 +00:00
|
|
|
if (codec_init()) {
|
2006-01-18 20:22:03 +00:00
|
|
|
retval = CODEC_ERROR;
|
|
|
|
goto exit;
|
|
|
|
}
|
2005-06-13 06:15:05 +00:00
|
|
|
|
2005-09-23 00:12:02 +00:00
|
|
|
while (!*ci->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-06-13 06:15:05 +00:00
|
|
|
/* Create a decoder instance */
|
|
|
|
wpc = WavpackOpenFileInput (read_callback, error);
|
|
|
|
|
2006-01-18 20:22:03 +00:00
|
|
|
if (!wpc) {
|
|
|
|
retval = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2006-01-18 20:22:03 +00:00
|
|
|
}
|
2005-06-13 06:15:05 +00:00
|
|
|
|
2007-08-12 06:36:06 +00:00
|
|
|
ci->configure(DSP_SWITCH_FREQUENCY, WavpackGetSampleRate (wpc));
|
|
|
|
codec_set_replaygain(ci->id3);
|
2005-06-13 06:15:05 +00:00
|
|
|
bps = WavpackGetBytesPerSample (wpc);
|
|
|
|
nchans = WavpackGetReducedChannels (wpc);
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SET_STEREO_MODE, nchans == 2 ? STEREO_INTERLEAVED : STEREO_MONO);
|
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) {
|
2006-03-26 22:54:15 +00:00
|
|
|
int32_t nsamples;
|
2005-06-13 06:15:05 +00:00
|
|
|
|
|
|
|
if (ci->seek_time && ci->taginfo_ready && ci->id3->length) {
|
2005-11-06 19:18:04 +00:00
|
|
|
ci->seek_time--;
|
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;
|
2006-03-26 22:54:15 +00:00
|
|
|
skip = (int)((int64_t)(ci->filesize - ci->curpos) * n / d);
|
2005-06-13 06:15:05 +00:00
|
|
|
ci->seek_buffer (ci->curpos + skip);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
n = curpos_ms - ci->seek_time;
|
|
|
|
d = curpos_ms;
|
2006-03-26 22:54:15 +00:00
|
|
|
skip = (int)((int64_t) ci->curpos * n / d);
|
2005-06-13 06:15:05 +00:00
|
|
|
ci->seek_buffer (ci->curpos - skip);
|
|
|
|
}
|
|
|
|
|
|
|
|
wpc = WavpackOpenFileInput (read_callback, error);
|
2005-11-02 00:09:42 +00:00
|
|
|
ci->seek_complete();
|
2005-06-13 06:15:05 +00:00
|
|
|
|
|
|
|
if (!wpc)
|
|
|
|
break;
|
|
|
|
|
2005-06-27 00:12:40 +00:00
|
|
|
ci->set_elapsed (WavpackGetSampleIndex (wpc) / sr_100 * 10);
|
2005-09-23 00:12:02 +00:00
|
|
|
ci->yield ();
|
2005-06-13 06:15:05 +00:00
|
|
|
}
|
|
|
|
|
2006-02-06 07:40:35 +00:00
|
|
|
nsamples = WavpackUnpackSamples (wpc, temp_buffer, BUFFER_SIZE / nchans);
|
2005-06-13 06:15:05 +00:00
|
|
|
|
2006-04-15 02:03:11 +00:00
|
|
|
if (!nsamples || ci->stop_codec || ci->new_track)
|
2005-06-13 06:15:05 +00:00
|
|
|
break;
|
|
|
|
|
2006-02-06 07:40:35 +00:00
|
|
|
ci->yield ();
|
2005-06-13 06:15:05 +00:00
|
|
|
|
2006-04-15 02:03:11 +00:00
|
|
|
if (ci->stop_codec || ci->new_track)
|
2005-06-13 06:15:05 +00:00
|
|
|
break;
|
|
|
|
|
2007-02-07 00:51:50 +00:00
|
|
|
ci->pcmbuf_insert (temp_buffer, NULL, nsamples);
|
2005-06-13 06:15:05 +00:00
|
|
|
|
2005-06-27 00:12:40 +00:00
|
|
|
ci->set_elapsed (WavpackGetSampleIndex (wpc) / sr_100 * 10);
|
2006-02-06 07:40:35 +00:00
|
|
|
ci->yield ();
|
2005-06-13 06:15:05 +00:00
|
|
|
}
|
2006-04-22 14:40:13 +00:00
|
|
|
retval = CODEC_OK;
|
2005-06-13 06:15:05 +00:00
|
|
|
|
2006-04-22 14:40:13 +00:00
|
|
|
done:
|
2005-06-13 06:15:05 +00:00
|
|
|
if (ci->request_next_track())
|
|
|
|
goto next_track;
|
|
|
|
|
2006-01-18 20:22:03 +00:00
|
|
|
exit:
|
|
|
|
return retval;
|
2005-06-13 06:15:05 +00:00
|
|
|
}
|