From f40bfc9267b13b54e6379dfe7539447662879d24 Mon Sep 17 00:00:00 2001 From: Sean Bartell Date: Sat, 25 Jun 2011 21:32:25 -0400 Subject: Add codecs to librbcodec. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Id7f4717d51ed02d67cb9f9cb3c0ada4a81843f97 Reviewed-on: http://gerrit.rockbox.org/137 Reviewed-by: Nils Wallménius Tested-by: Nils Wallménius --- lib/rbcodec/codecs/vgm.c | 142 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 lib/rbcodec/codecs/vgm.c (limited to 'lib/rbcodec/codecs/vgm.c') 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 @@ + +/* Ripped off from Game_Music_Emu 0.5.2. http://www.slack.net/~ant/ */ +/* Inflate code taken from WikiViewer plugin by Adam Gashlin */ + +#include + +#include "libgme/blargg_endian.h" +#include "libgme/vgm_emu.h" +#include "libgme/inflate/mallocer.h" +#include "libgme/inflate/inflate.h" + +CODEC_HEADER + +/* Maximum number of bytes to process in one iteration */ +#define CHUNK_SIZE (1024*4) +#define MAINMEMBUF 0 + +static int16_t samples[CHUNK_SIZE] IBSS_ATTR; +static struct Vgm_Emu vgm_emu; + +static void *inflatebuf; /* heap for gunzip */ +static char *songbuf; /* destination for uncompressed song */ +static uint32_t songbuflen=0; /* size of the song buffer */ +static uint32_t songlen=0; /* used size of the song buffer */ + +/****************** rockbox interface ******************/ + +/* this is the codec entry point */ +enum codec_status codec_main(enum codec_entry_call_reason reason) +{ + if (reason == CODEC_LOAD) { + /* we only render 16 bits */ + ci->configure(DSP_SET_SAMPLE_DEPTH, 16); + + /* 32 Khz, Interleaved stereo */ + ci->configure(DSP_SET_FREQUENCY, 44100); + ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED); + + Vgm_init(&vgm_emu); + Vgm_set_sample_rate(&vgm_emu, 44100); + } + + return CODEC_OK; +} + +/* this is called for each file to process */ +enum codec_status codec_run(void) +{ + blargg_err_t err; + uint8_t *buf; + size_t n; + intptr_t param; + + uint32_t elapsed_time = 0; + + DEBUGF("VGM: next_track\n"); + if (codec_init()) { + return CODEC_ERROR; + } + + codec_set_replaygain(ci->id3); + + /* Read the entire file */ + DEBUGF("VGM: request file\n"); + ci->seek_buffer(0); + buf = ci->request_buffer(&n, ci->filesize); + if (!buf) { + DEBUGF("VGM: file load failed\n"); + return CODEC_ERROR; + } + + /* If couldn't get the whole buffer + will trim file and put and 'end_command' + at the end*/ + if (n < (size_t)ci->filesize) { + DEBUGF("VGM: file was trimmed\n"); + } + + /* If is gzipped decompress it */ + if ( get_le16( buf ) == 0x8b1f ) { + wpw_init_mempool(MAINMEMBUF); + inflatebuf=wpw_malloc(MAINMEMBUF,0x13500); + + /* Will use available remaining memory + as output buffer */ + songbuflen=wpw_available(MAINMEMBUF); + songbuf=wpw_malloc(MAINMEMBUF,songbuflen); + + songlen=decompress(buf,n,songbuf,songbuflen,0,inflatebuf); + + if ((err = Vgm_load_mem(&vgm_emu, songbuf, songlen, true))) { + DEBUGF("VGM: Vgm_load_mem failed (%s)\n", err); + return CODEC_ERROR; + } + + /* Since metadata parser doesn't support VGZ + will set song length here */ + ci->id3->length = Track_get_length( &vgm_emu ); + } + else if ((err = Vgm_load_mem(&vgm_emu, buf, n, false))) { + DEBUGF("VGM: Vgm_load failed_mem (%s)\n", err); + return CODEC_ERROR; + } + + Vgm_start_track(&vgm_emu); + + /* for REPEAT_ONE we disable track limits */ + if (!ci->loop_track()) { + Track_set_fade(&vgm_emu, ci->id3->length - 4000, 4000); + } + + ci->set_elapsed(0); + + /* The main decoder loop */ + while (1) { + enum codec_command_action action = ci->get_command(¶m); + + if (action == CODEC_ACTION_HALT) + break; + + if (action == CODEC_ACTION_SEEK_TIME) { + ci->set_elapsed(param); + elapsed_time = param; + Track_seek(&vgm_emu, param); + ci->seek_complete(); + + /* Set fade again in case we seek to start of song */ + Track_set_fade(&vgm_emu, ci->id3->length - 4000, 4000); + } + + /* Generate audio buffer */ + err = Vgm_play(&vgm_emu, CHUNK_SIZE, samples); + if (err || Track_ended(&vgm_emu)) break; + + ci->pcmbuf_insert(samples, NULL, CHUNK_SIZE >> 1); + + elapsed_time += (CHUNK_SIZE / 2) * 10 / 441; + ci->set_elapsed(elapsed_time); + } + + return CODEC_OK; +} -- cgit v1.2.3