summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/vgm.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/vgm.c')
-rw-r--r--lib/rbcodec/codecs/vgm.c142
1 files changed, 142 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/vgm.c b/lib/rbcodec/codecs/vgm.c
new file mode 100644
index 0000000000..416f772f1d
--- /dev/null
+++ b/lib/rbcodec/codecs/vgm.c
@@ -0,0 +1,142 @@
1
2/* Ripped off from Game_Music_Emu 0.5.2. http://www.slack.net/~ant/ */
3/* Inflate code taken from WikiViewer plugin by Adam Gashlin */
4
5#include <codecs/lib/codeclib.h>
6
7#include "libgme/blargg_endian.h"
8#include "libgme/vgm_emu.h"
9#include "libgme/inflate/mallocer.h"
10#include "libgme/inflate/inflate.h"
11
12CODEC_HEADER
13
14/* Maximum number of bytes to process in one iteration */
15#define CHUNK_SIZE (1024*4)
16#define MAINMEMBUF 0
17
18static int16_t samples[CHUNK_SIZE] IBSS_ATTR;
19static struct Vgm_Emu vgm_emu;
20
21static void *inflatebuf; /* heap for gunzip */
22static char *songbuf; /* destination for uncompressed song */
23static uint32_t songbuflen=0; /* size of the song buffer */
24static uint32_t songlen=0; /* used size of the song buffer */
25
26/****************** rockbox interface ******************/
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 /* 32 Khz, Interleaved stereo */
36 ci->configure(DSP_SET_FREQUENCY, 44100);
37 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
38
39 Vgm_init(&vgm_emu);
40 Vgm_set_sample_rate(&vgm_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 intptr_t param;
53
54 uint32_t elapsed_time = 0;
55
56 DEBUGF("VGM: next_track\n");
57 if (codec_init()) {
58 return CODEC_ERROR;
59 }
60
61 codec_set_replaygain(ci->id3);
62
63 /* Read the entire file */
64 DEBUGF("VGM: request file\n");
65 ci->seek_buffer(0);
66 buf = ci->request_buffer(&n, ci->filesize);
67 if (!buf) {
68 DEBUGF("VGM: file load failed\n");
69 return CODEC_ERROR;
70 }
71
72 /* If couldn't get the whole buffer
73 will trim file and put and 'end_command'
74 at the end*/
75 if (n < (size_t)ci->filesize) {
76 DEBUGF("VGM: file was trimmed\n");
77 }
78
79 /* If is gzipped decompress it */
80 if ( get_le16( buf ) == 0x8b1f ) {
81 wpw_init_mempool(MAINMEMBUF);
82 inflatebuf=wpw_malloc(MAINMEMBUF,0x13500);
83
84 /* Will use available remaining memory
85 as output buffer */
86 songbuflen=wpw_available(MAINMEMBUF);
87 songbuf=wpw_malloc(MAINMEMBUF,songbuflen);
88
89 songlen=decompress(buf,n,songbuf,songbuflen,0,inflatebuf);
90
91 if ((err = Vgm_load_mem(&vgm_emu, songbuf, songlen, true))) {
92 DEBUGF("VGM: Vgm_load_mem failed (%s)\n", err);
93 return CODEC_ERROR;
94 }
95
96 /* Since metadata parser doesn't support VGZ
97 will set song length here */
98 ci->id3->length = Track_get_length( &vgm_emu );
99 }
100 else if ((err = Vgm_load_mem(&vgm_emu, buf, n, false))) {
101 DEBUGF("VGM: Vgm_load failed_mem (%s)\n", err);
102 return CODEC_ERROR;
103 }
104
105 Vgm_start_track(&vgm_emu);
106
107 /* for REPEAT_ONE we disable track limits */
108 if (!ci->loop_track()) {
109 Track_set_fade(&vgm_emu, ci->id3->length - 4000, 4000);
110 }
111
112 ci->set_elapsed(0);
113
114 /* The main decoder loop */
115 while (1) {
116 enum codec_command_action action = ci->get_command(&param);
117
118 if (action == CODEC_ACTION_HALT)
119 break;
120
121 if (action == CODEC_ACTION_SEEK_TIME) {
122 ci->set_elapsed(param);
123 elapsed_time = param;
124 Track_seek(&vgm_emu, param);
125 ci->seek_complete();
126
127 /* Set fade again in case we seek to start of song */
128 Track_set_fade(&vgm_emu, ci->id3->length - 4000, 4000);
129 }
130
131 /* Generate audio buffer */
132 err = Vgm_play(&vgm_emu, CHUNK_SIZE, samples);
133 if (err || Track_ended(&vgm_emu)) break;
134
135 ci->pcmbuf_insert(samples, NULL, CHUNK_SIZE >> 1);
136
137 elapsed_time += (CHUNK_SIZE / 2) * 10 / 441;
138 ci->set_elapsed(elapsed_time);
139 }
140
141 return CODEC_OK;
142}