summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/ay.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/ay.c')
-rw-r--r--lib/rbcodec/codecs/ay.c137
1 files changed, 137 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/ay.c b/lib/rbcodec/codecs/ay.c
new file mode 100644
index 0000000000..b11ad84294
--- /dev/null
+++ b/lib/rbcodec/codecs/ay.c
@@ -0,0 +1,137 @@
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/ay_emu.h"
6
7CODEC_HEADER
8
9/* Maximum number of bytes to process in one iteration */
10#define CHUNK_SIZE (1024*2)
11
12static int16_t samples[CHUNK_SIZE] IBSS_ATTR;
13static struct Ay_Emu ay_emu;
14
15/****************** rockbox interface ******************/
16
17static void set_codec_track(int t, int multitrack) {
18 Ay_start_track(&ay_emu, t);
19
20 /* for loop mode we disable track limits */
21 if (!ci->loop_track()) {
22 Track_set_fade(&ay_emu, Track_get_length( &ay_emu, t ) - 4000, 4000);
23 }
24 if (multitrack) ci->set_elapsed(t*1000); /* t is track no to display */
25 else ci->set_elapsed(0);
26}
27
28/* this is the codec entry point */
29enum codec_status codec_main(enum codec_entry_call_reason reason)
30{
31 if (reason == CODEC_LOAD) {
32 /* we only render 16 bits */
33 ci->configure(DSP_SET_SAMPLE_DEPTH, 16);
34
35 /* 44 Khz, Interleaved stereo */
36 ci->configure(DSP_SET_FREQUENCY, 44100);
37 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
38
39 Ay_init(&ay_emu);
40 Ay_set_sample_rate(&ay_emu, 44100);
41 }
42
43 return CODEC_OK;
44}
45
46/* this is called for each file to process */
47enum codec_status codec_run(void)
48{
49 blargg_err_t err;
50 uint8_t *buf;
51 size_t n;
52 int track, is_multitrack;
53 intptr_t param;
54 uint32_t elapsed_time;
55
56 /* reset values */
57 track = is_multitrack = 0;
58 elapsed_time = 0;
59
60 DEBUGF("AY: next_track\n");
61 if (codec_init()) {
62 return CODEC_ERROR;
63 }
64
65 codec_set_replaygain(ci->id3);
66
67 /* Read the entire file */
68 DEBUGF("AY: request file\n");
69 ci->seek_buffer(0);
70 buf = ci->request_buffer(&n, ci->filesize);
71 if (!buf || n < (size_t)ci->filesize) {
72 DEBUGF("AY: file load failed\n");
73 return CODEC_ERROR;
74 }
75
76 if ((err = Ay_load_mem(&ay_emu, buf, ci->filesize))) {
77 DEBUGF("AY: Ay_load_mem failed (%s)\n", err);
78 return CODEC_ERROR;
79 }
80
81 /* Update internal track count */
82 if (ay_emu.m3u.size > 0)
83 ay_emu.track_count = ay_emu.m3u.size;
84
85 /* Check if file has multiple tracks */
86 if (ay_emu.track_count > 1) {
87 is_multitrack = 1;
88 }
89
90next_track:
91 set_codec_track(track, is_multitrack);
92
93 /* The main decoder loop */
94 while (1) {
95 enum codec_command_action action = ci->get_command(&param);
96
97 if (action == CODEC_ACTION_HALT)
98 break;
99
100 if (action == CODEC_ACTION_SEEK_TIME) {
101 if (is_multitrack) {
102 track = param/1000;
103 ci->seek_complete();
104 if (track >= ay_emu.track_count) break;
105 goto next_track;
106 }
107
108 ci->set_elapsed(param);
109 elapsed_time = param;
110 Track_seek(&ay_emu, param);
111 ci->seek_complete();
112
113 /* Set fade again */
114 if (!ci->loop_track()) {
115 Track_set_fade(&ay_emu, Track_get_length( &ay_emu, track ) - 4000, 4000);
116 }
117 }
118
119 /* Generate audio buffer */
120 err = Ay_play(&ay_emu, CHUNK_SIZE, samples);
121 if (err || Track_ended(&ay_emu)) {
122 track++;
123 if (track >= ay_emu.track_count) break;
124 goto next_track;
125 }
126
127 ci->pcmbuf_insert(samples, NULL, CHUNK_SIZE >> 1);
128
129 /* Set elapsed time for one track files */
130 if (!is_multitrack) {
131 elapsed_time += (CHUNK_SIZE / 2) * 10 / 441;
132 ci->set_elapsed(elapsed_time);
133 }
134 }
135
136 return CODEC_OK;
137}