gitscraper: support Python3.
[maemo-rb.git] / apps / codecs / sgc.c
blob348a54a2d3bc72e1ba1fd9f1cd54835526ace7c0
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"
7 CODEC_HEADER
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;
15 /* Coleco Bios */
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);
43 Sgc_init(&sgc_emu);
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);
49 if ( fd >= 0 ) {
50 ci->read(fd, coleco_bios, 0x2000);
51 ci->close(fd);
52 set_coleco_bios( &sgc_emu, coleco_bios );
57 return CODEC_OK;
60 /* this is called for each file to process */
61 enum codec_status codec_run(void)
63 blargg_err_t err;
64 uint8_t *buf;
65 size_t n;
66 intptr_t param;
67 int track = 0;
69 DEBUGF("SGC: next_track\n");
70 if (codec_init()) {
71 return CODEC_ERROR;
74 codec_set_replaygain(ci->id3);
76 /* Read the entire file */
77 DEBUGF("SGC: request file\n");
78 ci->seek_buffer(0);
79 buf = ci->request_buffer(&n, ci->filesize);
80 if (!buf || n < (size_t)ci->filesize) {
81 DEBUGF("SGC: file load failed\n");
82 return CODEC_ERROR;
85 if ((err = Sgc_load_mem(&sgc_emu, buf, ci->filesize))) {
86 DEBUGF("SGC: Sgc_load_mem failed (%s)\n", err);
87 return CODEC_ERROR;
90 /* Update internal track count */
91 if (sgc_emu.m3u.size > 0)
92 sgc_emu.track_count = sgc_emu.m3u.size;
94 next_track:
95 set_codec_track(track);
97 /* The main decoder loop */
98 while (1) {
99 enum codec_command_action action = ci->get_command(&param);
101 if (action == CODEC_ACTION_HALT)
102 break;
104 if (action == CODEC_ACTION_SEEK_TIME) {
105 track = param/1000;
106 ci->seek_complete();
107 if (track >= sgc_emu.track_count) break;
108 goto next_track;
111 /* Generate audio buffer */
112 err = Sgc_play(&sgc_emu, CHUNK_SIZE, samples);
113 if (err || Track_ended(&sgc_emu)) {
114 track++;
115 if (track >= sgc_emu.track_count) break;
116 goto next_track;
119 ci->pcmbuf_insert(samples, NULL, CHUNK_SIZE >> 1);
122 return CODEC_OK;