diff options
Diffstat (limited to 'lib/rbcodec/codecs/sgc.c')
-rw-r--r-- | lib/rbcodec/codecs/sgc.c | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/sgc.c b/lib/rbcodec/codecs/sgc.c new file mode 100644 index 0000000000..348a54a2d3 --- /dev/null +++ b/lib/rbcodec/codecs/sgc.c | |||
@@ -0,0 +1,123 @@ | |||
1 | |||
2 | /* Ripped off from Game_Music_Emu 0.5.2. http://www.slack.net/~ant/ */ | ||
3 | |||
4 | #include <codecs/lib/codeclib.h> | ||
5 | #include "libgme/sgc_emu.h" | ||
6 | |||
7 | CODEC_HEADER | ||
8 | |||
9 | /* Maximum number of bytes to process in one iteration */ | ||
10 | #define CHUNK_SIZE (1024*2) | ||
11 | |||
12 | static int16_t samples[CHUNK_SIZE] IBSS_ATTR; | ||
13 | static struct Sgc_Emu sgc_emu; | ||
14 | |||
15 | /* Coleco Bios */ | ||
16 | /* Colecovision not supported yet | ||
17 | static char coleco_bios[0x2000]; | ||
18 | */ | ||
19 | |||
20 | /****************** rockbox interface ******************/ | ||
21 | |||
22 | static void set_codec_track(int t) { | ||
23 | Sgc_start_track(&sgc_emu, t); | ||
24 | |||
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); | ||
28 | } | ||
29 | ci->set_elapsed(t*1000); /* t is track no to display */ | ||
30 | } | ||
31 | |||
32 | /* this is the codec entry point */ | ||
33 | enum codec_status codec_main(enum codec_entry_call_reason reason) | ||
34 | { | ||
35 | if (reason == CODEC_LOAD) { | ||
36 | /* we only render 16 bits */ | ||
37 | ci->configure(DSP_SET_SAMPLE_DEPTH, 16); | ||
38 | |||
39 | /* 44 Khz, Interleaved stereo */ | ||
40 | ci->configure(DSP_SET_FREQUENCY, 44100); | ||
41 | ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED); | ||
42 | |||
43 | Sgc_init(&sgc_emu); | ||
44 | Sgc_set_sample_rate(&sgc_emu, 44100); | ||
45 | |||
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 ); | ||
53 | } | ||
54 | */ | ||
55 | } | ||
56 | |||
57 | return CODEC_OK; | ||
58 | } | ||
59 | |||
60 | /* this is called for each file to process */ | ||
61 | enum codec_status codec_run(void) | ||
62 | { | ||
63 | blargg_err_t err; | ||
64 | uint8_t *buf; | ||
65 | size_t n; | ||
66 | intptr_t param; | ||
67 | int track = 0; | ||
68 | |||
69 | DEBUGF("SGC: next_track\n"); | ||
70 | if (codec_init()) { | ||
71 | return CODEC_ERROR; | ||
72 | } | ||
73 | |||
74 | codec_set_replaygain(ci->id3); | ||
75 | |||
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; | ||
83 | } | ||
84 | |||
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; | ||
88 | } | ||
89 | |||
90 | /* Update internal track count */ | ||
91 | if (sgc_emu.m3u.size > 0) | ||
92 | sgc_emu.track_count = sgc_emu.m3u.size; | ||
93 | |||
94 | next_track: | ||
95 | set_codec_track(track); | ||
96 | |||
97 | /* The main decoder loop */ | ||
98 | while (1) { | ||
99 | enum codec_command_action action = ci->get_command(¶m); | ||
100 | |||
101 | if (action == CODEC_ACTION_HALT) | ||
102 | break; | ||
103 | |||
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; | ||
109 | } | ||
110 | |||
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; | ||
117 | } | ||
118 | |||
119 | ci->pcmbuf_insert(samples, NULL, CHUNK_SIZE >> 1); | ||
120 | } | ||
121 | |||
122 | return CODEC_OK; | ||
123 | } | ||