2 /* Ripped off from Game_Music_Emu 0.5.2. http://www.slack.net/~ant/ */
4 #include <codecs/lib/codeclib.h>
5 #include "libgme/kss_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 Kss_Emu kss_emu IDATA_ATTR CACHEALIGN_ATTR
;
15 /****************** rockbox interface ******************/
17 static void set_codec_track(int t
) {
18 Kss_start_track(&kss_emu
, t
);
20 /* for REPEAT_ONE we disable track limits */
21 if (ci
->global_settings
->repeat_mode
!= REPEAT_ONE
) {
22 Track_set_fade(&kss_emu
, Track_get_length( &kss_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 Kss_set_sample_rate(&kss_emu
, 44100);
45 /* this is called for each file to process */
46 enum codec_status
codec_run(void)
53 uint32_t elapsed_time
;
59 DEBUGF("KSS: next_track\n");
64 codec_set_replaygain(ci
->id3
);
66 /* Read the entire file */
67 DEBUGF("KSS: request file\n");
69 buf
= ci
->request_buffer(&n
, ci
->filesize
);
70 if (!buf
|| n
< (size_t)ci
->filesize
) {
71 DEBUGF("KSS: file load failed\n");
75 if ((err
= Kss_load_mem(&kss_emu
, buf
, ci
->filesize
))) {
76 DEBUGF("KSS: Kss_load failed (%s)\n", err
);
80 /* Update internal track count */
81 if (kss_emu
.m3u
.size
> 0)
82 kss_emu
.track_count
= kss_emu
.m3u
.size
;
85 set_codec_track(track
);
87 /* The main decoder loop */
89 enum codec_command_action action
= ci
->get_command(¶m
);
91 if (action
== CODEC_ACTION_HALT
)
94 if (action
== CODEC_ACTION_SEEK_TIME
) {
97 if (track
>= kss_emu
.track_count
) break;
101 /* Generate audio buffer */
102 err
= Kss_play(&kss_emu
, CHUNK_SIZE
, samples
);
103 if (err
|| kss_emu
.track_ended
) {
105 if (track
>= kss_emu
.track_count
) break;
109 ci
->pcmbuf_insert(samples
, NULL
, CHUNK_SIZE
>> 1);