Fixes a problem where the sim would try to start the WPS on HAVE_RTC_ALARM sims ...
[Rockbox.git] / apps / codecs / wavpack.c
blob1485eedf8b88b81cd24e3cce6ae8d4eef858abd8
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 David Bryant
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "codeclib.h"
21 #include "libwavpack/wavpack.h"
23 CODEC_HEADER
25 #define BUFFER_SIZE 4096
27 static int32_t temp_buffer [BUFFER_SIZE] IBSS_ATTR;
29 static int32_t read_callback (void *buffer, int32_t bytes)
31 int32_t retval = ci->read_filebuf (buffer, bytes);
32 ci->id3->offset = ci->curpos;
33 return retval;
36 /* this is the codec entry point */
37 enum codec_status codec_main(void)
39 WavpackContext *wpc;
40 char error [80];
41 int bps, nchans, sr_100;
42 int retval;
44 /* Generic codec initialisation */
45 ci->configure(CODEC_SET_FILEBUF_WATERMARK, 1024*512);
46 ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, 1024*128);
48 ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
50 next_track:
52 if (codec_init()) {
53 retval = CODEC_ERROR;
54 goto exit;
57 while (!*ci->taginfo_ready && !ci->stop_codec)
58 ci->sleep(1);
60 /* Create a decoder instance */
61 wpc = WavpackOpenFileInput (read_callback, error);
63 if (!wpc) {
64 retval = CODEC_ERROR;
65 goto done;
68 ci->configure(DSP_SWITCH_FREQUENCY, WavpackGetSampleRate (wpc));
69 codec_set_replaygain(ci->id3);
70 bps = WavpackGetBytesPerSample (wpc);
71 nchans = WavpackGetReducedChannels (wpc);
72 ci->configure(DSP_SET_STEREO_MODE, nchans == 2 ? STEREO_INTERLEAVED : STEREO_MONO);
73 sr_100 = ci->id3->frequency / 100;
75 ci->set_elapsed (0);
77 /* The main decoder loop */
79 while (1) {
80 int32_t nsamples;
82 if (ci->seek_time && ci->taginfo_ready && ci->id3->length) {
83 ci->seek_time--;
84 int curpos_ms = WavpackGetSampleIndex (wpc) / sr_100 * 10;
85 int n, d, skip;
87 if (ci->seek_time > curpos_ms) {
88 n = ci->seek_time - curpos_ms;
89 d = ci->id3->length - curpos_ms;
90 skip = (int)((int64_t)(ci->filesize - ci->curpos) * n / d);
91 ci->seek_buffer (ci->curpos + skip);
93 else {
94 n = curpos_ms - ci->seek_time;
95 d = curpos_ms;
96 skip = (int)((int64_t) ci->curpos * n / d);
97 ci->seek_buffer (ci->curpos - skip);
100 wpc = WavpackOpenFileInput (read_callback, error);
101 ci->seek_complete();
103 if (!wpc)
104 break;
106 ci->set_elapsed (WavpackGetSampleIndex (wpc) / sr_100 * 10);
107 ci->yield ();
110 nsamples = WavpackUnpackSamples (wpc, temp_buffer, BUFFER_SIZE / nchans);
112 if (!nsamples || ci->stop_codec || ci->new_track)
113 break;
115 ci->yield ();
117 if (ci->stop_codec || ci->new_track)
118 break;
120 ci->pcmbuf_insert (temp_buffer, NULL, nsamples);
122 ci->set_elapsed (WavpackGetSampleIndex (wpc) / sr_100 * 10);
123 ci->yield ();
125 retval = CODEC_OK;
127 done:
128 if (ci->request_next_track())
129 goto next_track;
131 exit:
132 return retval;