diff options
Diffstat (limited to 'apps/codecs/hes.c')
-rw-r--r-- | apps/codecs/hes.c | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/apps/codecs/hes.c b/apps/codecs/hes.c new file mode 100644 index 0000000000..598d787594 --- /dev/null +++ b/apps/codecs/hes.c | |||
@@ -0,0 +1,108 @@ | |||
1 | /* Ripped off from Game_Music_Emu 0.5.2. http://www.slack.net/~ant/ */ | ||
2 | |||
3 | #include <string.h> | ||
4 | #include "codeclib.h" | ||
5 | #include "libgme/hes_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 Hes_Emu hes_emu IDATA_ATTR CACHEALIGN_ATTR; | ||
14 | |||
15 | /****************** rockbox interface ******************/ | ||
16 | |||
17 | static void set_codec_track(int t) { | ||
18 | Hes_start_track(&hes_emu, t); | ||
19 | |||
20 | /* for REPEAT_ONE we disable track limits */ | ||
21 | if (ci->global_settings->repeat_mode != REPEAT_ONE) { | ||
22 | Track_set_fade(&hes_emu, Track_get_length( &hes_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 | Hes_init(&hes_emu); | ||
39 | Hes_set_sample_rate(&hes_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 | intptr_t param; | ||
52 | int track = 0; | ||
53 | |||
54 | DEBUGF("HES: next_track\n"); | ||
55 | if (codec_init()) { | ||
56 | return CODEC_ERROR; | ||
57 | } | ||
58 | |||
59 | codec_set_replaygain(ci->id3); | ||
60 | |||
61 | /* Read the entire file */ | ||
62 | DEBUGF("HES: request file\n"); | ||
63 | ci->seek_buffer(0); | ||
64 | buf = ci->request_buffer(&n, ci->filesize); | ||
65 | if (!buf || n < (size_t)ci->filesize) { | ||
66 | DEBUGF("HES: file load failed\n"); | ||
67 | return CODEC_ERROR; | ||
68 | } | ||
69 | |||
70 | if ((err = Hes_load(&hes_emu, buf, ci->filesize))) { | ||
71 | DEBUGF("HES: Hes_load failed (%s)\n", err); | ||
72 | return CODEC_ERROR; | ||
73 | } | ||
74 | |||
75 | /* Update internal track count */ | ||
76 | if (hes_emu.m3u.size > 0) | ||
77 | hes_emu.track_count = hes_emu.m3u.size; | ||
78 | |||
79 | next_track: | ||
80 | set_codec_track(track); | ||
81 | |||
82 | /* The main decoder loop */ | ||
83 | while ( 1 ) { | ||
84 | enum codec_command_action action = ci->get_command(¶m); | ||
85 | |||
86 | if (action == CODEC_ACTION_HALT) | ||
87 | break; | ||
88 | |||
89 | if (action == CODEC_ACTION_SEEK_TIME) { | ||
90 | track = param/1000; | ||
91 | ci->seek_complete(); | ||
92 | if (track >= hes_emu.track_count) break; | ||
93 | goto next_track; | ||
94 | } | ||
95 | |||
96 | /* Generate audio buffer */ | ||
97 | err = Hes_play(&hes_emu, CHUNK_SIZE, samples); | ||
98 | if (err || hes_emu.track_ended) { | ||
99 | track++; | ||
100 | if (track >= hes_emu.track_count) break; | ||
101 | goto next_track; | ||
102 | } | ||
103 | |||
104 | ci->pcmbuf_insert(samples, NULL, CHUNK_SIZE >> 1); | ||
105 | } | ||
106 | |||
107 | return CODEC_OK; | ||
108 | } | ||