1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
21 #include "libwavpack/wavpack.h"
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
;
36 /* this is the codec entry point */
37 enum codec_status
codec_main(void)
41 int bps
, nchans
, sr_100
;
44 /* Generic codec initialisation */
45 ci
->configure(CODEC_SET_FILEBUF_WATERMARK
, 1024*512);
47 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 28);
56 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
59 /* Create a decoder instance */
60 wpc
= WavpackOpenFileInput (read_callback
, error
);
67 ci
->configure(DSP_SWITCH_FREQUENCY
, WavpackGetSampleRate (wpc
));
68 codec_set_replaygain(ci
->id3
);
69 bps
= WavpackGetBytesPerSample (wpc
);
70 nchans
= WavpackGetReducedChannels (wpc
);
71 ci
->configure(DSP_SET_STEREO_MODE
, nchans
== 2 ? STEREO_INTERLEAVED
: STEREO_MONO
);
72 sr_100
= ci
->id3
->frequency
/ 100;
76 /* The main decoder loop */
81 if (ci
->seek_time
&& ci
->taginfo_ready
&& ci
->id3
->length
) {
83 int curpos_ms
= WavpackGetSampleIndex (wpc
) / sr_100
* 10;
86 if (ci
->seek_time
> curpos_ms
) {
87 n
= ci
->seek_time
- curpos_ms
;
88 d
= ci
->id3
->length
- curpos_ms
;
89 skip
= (int)((int64_t)(ci
->filesize
- ci
->curpos
) * n
/ d
);
90 ci
->seek_buffer (ci
->curpos
+ skip
);
93 n
= curpos_ms
- ci
->seek_time
;
95 skip
= (int)((int64_t) ci
->curpos
* n
/ d
);
96 ci
->seek_buffer (ci
->curpos
- skip
);
99 wpc
= WavpackOpenFileInput (read_callback
, error
);
105 ci
->set_elapsed (WavpackGetSampleIndex (wpc
) / sr_100
* 10);
109 nsamples
= WavpackUnpackSamples (wpc
, temp_buffer
, BUFFER_SIZE
/ nchans
);
111 if (!nsamples
|| ci
->stop_codec
|| ci
->new_track
)
116 if (ci
->stop_codec
|| ci
->new_track
)
119 ci
->pcmbuf_insert (temp_buffer
, NULL
, nsamples
);
121 ci
->set_elapsed (WavpackGetSampleIndex (wpc
) / sr_100
* 10);
127 if (ci
->request_next_track())