diff options
Diffstat (limited to 'apps/codecs/kss.c')
-rw-r--r-- | apps/codecs/kss.c | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/apps/codecs/kss.c b/apps/codecs/kss.c new file mode 100644 index 0000000000..9db16521b4 --- /dev/null +++ b/apps/codecs/kss.c | |||
@@ -0,0 +1,113 @@ | |||
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/kss_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 Kss_Emu kss_emu IDATA_ATTR CACHEALIGN_ATTR; | ||
14 | |||
15 | /****************** rockbox interface ******************/ | ||
16 | |||
17 | static void set_codec_track(int t) { | ||
18 | Kss_start_track(&kss_emu, t); | ||
19 | |||
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); | ||
23 | } | ||
24 | ci->set_elapsed(t*1000); /* t is track no to display */ | ||
25 | } | ||
26 | |||
27 | /* this is the codec entry point */ | ||
28 | enum codec_status codec_main(enum codec_entry_call_reason reason) | ||
29 | { | ||
30 | if (reason == CODEC_LOAD) { | ||
31 | /* we only render 16 bits */ | ||
32 | ci->configure(DSP_SET_SAMPLE_DEPTH, 16); | ||
33 | |||
34 | /* 44 Khz, Interleaved stereo */ | ||
35 | ci->configure(DSP_SET_FREQUENCY, 44100); | ||
36 | ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED); | ||
37 | |||
38 | Kss_init(&kss_emu); | ||
39 | Kss_set_sample_rate(&kss_emu, 44100); | ||
40 | } | ||
41 | |||
42 | return CODEC_OK; | ||
43 | } | ||
44 | |||
45 | /* this is called for each file to process */ | ||
46 | enum codec_status codec_run(void) | ||
47 | { | ||
48 | blargg_err_t err; | ||
49 | uint8_t *buf; | ||
50 | size_t n; | ||
51 | int track; | ||
52 | intptr_t param; | ||
53 | uint32_t elapsed_time; | ||
54 | |||
55 | /* reset values */ | ||
56 | track = 0; | ||
57 | elapsed_time = 0; | ||
58 | |||
59 | DEBUGF("KSS: next_track\n"); | ||
60 | if (codec_init()) { | ||
61 | return CODEC_ERROR; | ||
62 | } | ||
63 | |||
64 | codec_set_replaygain(ci->id3); | ||
65 | |||
66 | /* Read the entire file */ | ||
67 | DEBUGF("KSS: request file\n"); | ||
68 | ci->seek_buffer(0); | ||
69 | buf = ci->request_buffer(&n, ci->filesize); | ||
70 | if (!buf || n < (size_t)ci->filesize) { | ||
71 | DEBUGF("KSS: file load failed\n"); | ||
72 | return CODEC_ERROR; | ||
73 | } | ||
74 | |||
75 | if ((err = Kss_load_mem(&kss_emu, buf, ci->filesize))) { | ||
76 | DEBUGF("KSS: Kss_load failed (%s)\n", err); | ||
77 | return CODEC_ERROR; | ||
78 | } | ||
79 | |||
80 | /* Update internal track count */ | ||
81 | if (kss_emu.m3u.size > 0) | ||
82 | kss_emu.track_count = kss_emu.m3u.size; | ||
83 | |||
84 | next_track: | ||
85 | set_codec_track(track); | ||
86 | |||
87 | /* The main decoder loop */ | ||
88 | while (1) { | ||
89 | enum codec_command_action action = ci->get_command(¶m); | ||
90 | |||
91 | if (action == CODEC_ACTION_HALT) | ||
92 | break; | ||
93 | |||
94 | if (action == CODEC_ACTION_SEEK_TIME) { | ||
95 | track = param/1000; | ||
96 | ci->seek_complete(); | ||
97 | if (track >= kss_emu.track_count) break; | ||
98 | goto next_track; | ||
99 | } | ||
100 | |||
101 | /* Generate audio buffer */ | ||
102 | err = Kss_play(&kss_emu, CHUNK_SIZE, samples); | ||
103 | if (err || kss_emu.track_ended) { | ||
104 | track++; | ||
105 | if (track >= kss_emu.track_count) break; | ||
106 | goto next_track; | ||
107 | } | ||
108 | |||
109 | ci->pcmbuf_insert(samples, NULL, CHUNK_SIZE >> 1); | ||
110 | } | ||
111 | |||
112 | return CODEC_OK; | ||
113 | } | ||