From c73894213d7f1c53eac7ea3d08ce9dbe0187aa33 Mon Sep 17 00:00:00 2001 From: Michael Sevakis Date: Wed, 6 Mar 2013 19:36:24 -0500 Subject: [PATCH] VGM Codec: Improve time and fade behavior. Tweak minor misc. Prevents cutoff of tracks, especially short ones: * Extend looped tracks by fade length to fade at start of loop repeat. * No fade occurs for non-repeating track only having an intro. * Uses id3.tail_trim field to store fade duration. Use libGME built-in elapsed time reporting instead of custom calculation: * libGME already reports in milliseconds. * Don't advance time counter when Repeat == One. It just runs the progress over the length limit. Fix a comment about sample rate and set the reported bitrate to be accurate for 44.1 kHz stereo. Change-Id: I3ede22bda0f9a941a3fef751f4d678eb0027344c --- lib/rbcodec/codecs/vgm.c | 48 +++++++++++++++++++++++++------------- lib/rbcodec/metadata/vgm.c | 12 ++++++---- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/lib/rbcodec/codecs/vgm.c b/lib/rbcodec/codecs/vgm.c index 416f772f1d..9f2f1b9c5e 100644 --- a/lib/rbcodec/codecs/vgm.c +++ b/lib/rbcodec/codecs/vgm.c @@ -23,6 +23,31 @@ 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 */ +static void codec_vgz_update_length(void) +{ + ci->id3->length = Track_get_length( &vgm_emu ); + ci->id3->tail_trim = 4 * 1000; + + if (vgm_emu.info.loop_length <= 0) + ci->id3->tail_trim = 0; + + ci->id3->length += ci->id3->tail_trim; +} + +static void codec_update_fade(void) +{ + /* for REPEAT_ONE we disable track limits */ + Track_set_fade(&vgm_emu, + ci->loop_track() ? indefinite_count : + ci->id3->length - ci->id3->tail_trim, + ci->id3->tail_trim); +} + +static void codec_update_elapsed(void) +{ + ci->set_elapsed(ci->loop_track() ? 0 : Track_tell(&vgm_emu)); +} + /****************** rockbox interface ******************/ /* this is the codec entry point */ @@ -32,7 +57,7 @@ enum codec_status codec_main(enum codec_entry_call_reason reason) /* we only render 16 bits */ ci->configure(DSP_SET_SAMPLE_DEPTH, 16); - /* 32 Khz, Interleaved stereo */ + /* 44 Khz, Interleaved stereo */ ci->configure(DSP_SET_FREQUENCY, 44100); ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED); @@ -51,8 +76,6 @@ enum codec_status codec_run(void) size_t n; intptr_t param; - uint32_t elapsed_time = 0; - DEBUGF("VGM: next_track\n"); if (codec_init()) { return CODEC_ERROR; @@ -95,7 +118,7 @@ enum codec_status codec_run(void) /* Since metadata parser doesn't support VGZ will set song length here */ - ci->id3->length = Track_get_length( &vgm_emu ); + codec_vgz_update_length(); } else if ((err = Vgm_load_mem(&vgm_emu, buf, n, false))) { DEBUGF("VGM: Vgm_load failed_mem (%s)\n", err); @@ -104,12 +127,8 @@ enum codec_status codec_run(void) 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); + codec_update_fade(); /* The main decoder loop */ while (1) { @@ -119,23 +138,20 @@ enum codec_status codec_run(void) break; if (action == CODEC_ACTION_SEEK_TIME) { - ci->set_elapsed(param); - elapsed_time = param; Track_seek(&vgm_emu, param); + codec_update_elapsed(); ci->seek_complete(); /* Set fade again in case we seek to start of song */ - Track_set_fade(&vgm_emu, ci->id3->length - 4000, 4000); + codec_update_fade(); } /* 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); + ci->pcmbuf_insert(samples, NULL, CHUNK_SIZE / 2); + codec_update_elapsed(); } return CODEC_OK; diff --git a/lib/rbcodec/metadata/vgm.c b/lib/rbcodec/metadata/vgm.c index 7d3f45e204..03afb15a92 100644 --- a/lib/rbcodec/metadata/vgm.c +++ b/lib/rbcodec/metadata/vgm.c @@ -113,6 +113,8 @@ static long check_gd3_header( byte* h, long remain ) static void get_vgm_length( struct header_t* h, struct mp3entry* id3 ) { long length = get_long_le( h->track_duration ) * 10 / 441; + id3->tail_trim = 4 * 1000; /* assume fade */ + if ( length > 0 ) { long loop_length = 0, intro_length = 0; @@ -126,13 +128,15 @@ static void get_vgm_length( struct header_t* h, struct mp3entry* id3 ) { intro_length = length; /* make it clear that track is no longer than length */ loop_length = 0; + id3->tail_trim = 0; } - id3->length = intro_length + 2 * loop_length; /* intro + 2 loops */ - return; + /* intro + 2 loops + fade */ + id3->length = intro_length + 2 * loop_length + id3->tail_trim; + return; } - id3->length = 150 * 1000; /* 2.5 minutes */ + id3->length = 150 * 1000 + id3->tail_trim; /* 2.5 minutes + fade */ } bool get_vgm_metadata(int fd, struct mp3entry* id3) @@ -151,7 +155,7 @@ bool get_vgm_metadata(int fd, struct mp3entry* id3) id3->vbr = false; id3->filesize = filesize(fd); - id3->bitrate = 706; + id3->bitrate = 1411; id3->frequency = 44100; /* If file is gzipped, will get metadata later */