1 /* Ripped off from Game_Music_Emu 0.5.2. http://www.slack.net/~ant/ */
5 #include "libgme/hes_emu.h"
9 /* Maximum number of bytes to process in one iteration */
10 #define CHUNK_SIZE (1024*2)
12 static int16_t samples
[CHUNK_SIZE
] IBSS_ATTR
;
13 static struct Hes_Emu hes_emu
;
15 /****************** rockbox interface ******************/
17 static void set_codec_track(int t
) {
18 Hes_start_track(&hes_emu
, t
);
20 /* for loop mode we disable track limits */
21 if (!ci
->loop_track()) {
22 Track_set_fade(&hes_emu
, Track_get_length( &hes_emu
, t
), 4000);
24 ci
->set_elapsed(t
*1000); /* t is track no to display */
27 /* this is the codec entry point */
28 enum codec_status
codec_main(enum codec_entry_call_reason reason
)
30 if (reason
== CODEC_LOAD
) {
31 /* we only render 16 bits */
32 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 16);
34 /* 44 Khz, Interleaved stereo */
35 ci
->configure(DSP_SET_FREQUENCY
, 44100);
36 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_INTERLEAVED
);
39 Hes_set_sample_rate(&hes_emu
, 44100);
45 /* this is called for each file to process */
46 enum codec_status
codec_run(void)
54 DEBUGF("HES: next_track\n");
59 codec_set_replaygain(ci
->id3
);
61 /* Read the entire file */
62 DEBUGF("HES: request file\n");
64 buf
= ci
->request_buffer(&n
, ci
->filesize
);
65 if (!buf
|| n
< (size_t)ci
->filesize
) {
66 DEBUGF("HES: file load failed\n");
70 if ((err
= Hes_load_mem(&hes_emu
, buf
, ci
->filesize
))) {
71 DEBUGF("HES: Hes_load_mem failed (%s)\n", err
);
75 /* Update internal track count */
76 if (hes_emu
.m3u
.size
> 0)
77 hes_emu
.track_count
= hes_emu
.m3u
.size
;
80 set_codec_track(track
);
82 /* The main decoder loop */
84 enum codec_command_action action
= ci
->get_command(¶m
);
86 if (action
== CODEC_ACTION_HALT
)
89 if (action
== CODEC_ACTION_SEEK_TIME
) {
92 if (track
>= hes_emu
.track_count
) break;
96 /* Generate audio buffer */
97 err
= Hes_play(&hes_emu
, CHUNK_SIZE
, samples
);
98 if (err
|| Track_ended(&hes_emu
)) {
100 if (track
>= hes_emu
.track_count
) break;
104 ci
->pcmbuf_insert(samples
, NULL
, CHUNK_SIZE
>> 1);