diff options
author | Sean Bartell <wingedtachikoma@gmail.com> | 2011-06-25 21:32:25 -0400 |
---|---|---|
committer | Nils Wallménius <nils@rockbox.org> | 2012-04-25 22:13:20 +0200 |
commit | f40bfc9267b13b54e6379dfe7539447662879d24 (patch) | |
tree | 9b20069d5e62809ff434061ad730096836f916f2 /lib/rbcodec/codecs/vgm.c | |
parent | a0009907de7a0107d49040d8a180f140e2eff299 (diff) | |
download | rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.tar.gz rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.zip |
Add codecs to librbcodec.
Change-Id: Id7f4717d51ed02d67cb9f9cb3c0ada4a81843f97
Reviewed-on: http://gerrit.rockbox.org/137
Reviewed-by: Nils Wallménius <nils@rockbox.org>
Tested-by: Nils Wallménius <nils@rockbox.org>
Diffstat (limited to 'lib/rbcodec/codecs/vgm.c')
-rw-r--r-- | lib/rbcodec/codecs/vgm.c | 142 |
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 | |||
12 | CODEC_HEADER | ||
13 | |||
14 | /* Maximum number of bytes to process in one iteration */ | ||
15 | #define CHUNK_SIZE (1024*4) | ||
16 | #define MAINMEMBUF 0 | ||
17 | |||
18 | static int16_t samples[CHUNK_SIZE] IBSS_ATTR; | ||
19 | static struct Vgm_Emu vgm_emu; | ||
20 | |||
21 | static void *inflatebuf; /* heap for gunzip */ | ||
22 | static char *songbuf; /* destination for uncompressed song */ | ||
23 | static uint32_t songbuflen=0; /* size of the song buffer */ | ||
24 | static uint32_t songlen=0; /* used size of the song buffer */ | ||
25 | |||
26 | /****************** rockbox interface ******************/ | ||
27 | |||
28 | /* this is the codec entry point */ | ||
29 | enum 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 */ | ||
47 | enum 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(¶m); | ||
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 | } | ||