2 /* Ripped off from Game_Music_Emu 0.5.2. http://www.slack.net/~ant/ */
4 #include <codecs/lib/codeclib.h>
5 #include "libgme/sgc_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 Sgc_Emu sgc_emu
;
16 /* Colecovision not supported yet
17 static char coleco_bios[0x2000];
20 /****************** rockbox interface ******************/
22 static void set_codec_track(int t
) {
23 Sgc_start_track(&sgc_emu
, t
);
25 /* for REPEAT_ONE we disable track limits */
26 if (!ci
->loop_track()) {
27 Track_set_fade(&sgc_emu
, Track_get_length( &sgc_emu
, t
), 4000);
29 ci
->set_elapsed(t
*1000); /* t is track no to display */
32 /* this is the codec entry point */
33 enum codec_status
codec_main(enum codec_entry_call_reason reason
)
35 if (reason
== CODEC_LOAD
) {
36 /* we only render 16 bits */
37 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 16);
39 /* 44 Khz, Interleaved stereo */
40 ci
->configure(DSP_SET_FREQUENCY
, 44100);
41 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_INTERLEAVED
);
44 Sgc_set_sample_rate(&sgc_emu
, 44100);
46 /* set coleco bios, should be named coleco_bios.rom */
47 /* Colecovision not supported yet
48 int fd = ci->open("/coleco_bios.rom", O_RDONLY);
50 ci->read(fd, coleco_bios, 0x2000);
52 set_coleco_bios( &sgc_emu, coleco_bios );
60 /* this is called for each file to process */
61 enum codec_status
codec_run(void)
69 DEBUGF("SGC: next_track\n");
74 codec_set_replaygain(ci
->id3
);
76 /* Read the entire file */
77 DEBUGF("SGC: request file\n");
79 buf
= ci
->request_buffer(&n
, ci
->filesize
);
80 if (!buf
|| n
< (size_t)ci
->filesize
) {
81 DEBUGF("SGC: file load failed\n");
85 if ((err
= Sgc_load_mem(&sgc_emu
, buf
, ci
->filesize
))) {
86 DEBUGF("SGC: Sgc_load_mem failed (%s)\n", err
);
90 /* Update internal track count */
91 if (sgc_emu
.m3u
.size
> 0)
92 sgc_emu
.track_count
= sgc_emu
.m3u
.size
;
95 set_codec_track(track
);
97 /* The main decoder loop */
99 enum codec_command_action action
= ci
->get_command(¶m
);
101 if (action
== CODEC_ACTION_HALT
)
104 if (action
== CODEC_ACTION_SEEK_TIME
) {
107 if (track
>= sgc_emu
.track_count
) break;
111 /* Generate audio buffer */
112 err
= Sgc_play(&sgc_emu
, CHUNK_SIZE
, samples
);
113 if (err
|| Track_ended(&sgc_emu
)) {
115 if (track
>= sgc_emu
.track_count
) break;
119 ci
->pcmbuf_insert(samples
, NULL
, CHUNK_SIZE
>> 1);