2007-01-25 18:06:17 +00:00
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
/* Ripped off from Game_Music_Emu 0.5.2. http://www.slack.net/~ant/ */
|
2007-01-25 18:06:17 +00:00
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
#define GME_NSF_TYPE
|
2007-01-25 18:06:17 +00:00
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
#include <codecs/lib/codeclib.h>
|
|
|
|
#include "libgme/nsf_emu.h"
|
2007-01-25 18:06:17 +00:00
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
CODEC_HEADER
|
2007-01-25 18:06:17 +00:00
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
/* Maximum number of bytes to process in one iteration */
|
|
|
|
#define CHUNK_SIZE (1024*2)
|
2007-01-25 18:06:17 +00:00
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
static int16_t samples[CHUNK_SIZE] IBSS_ATTR;
|
2011-08-13 16:41:14 +00:00
|
|
|
static struct Nsf_Emu nsf_emu;
|
2007-01-25 18:06:17 +00:00
|
|
|
|
|
|
|
/****************** rockbox interface ******************/
|
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
static void set_codec_track(int t, int multitrack) {
|
|
|
|
Nsf_start_track(&nsf_emu, t);
|
2007-01-25 18:06:17 +00:00
|
|
|
|
|
|
|
/* for REPEAT_ONE we disable track limits */
|
2011-08-30 19:40:09 +00:00
|
|
|
if (!ci->loop_track()) {
|
2011-08-07 20:01:04 +00:00
|
|
|
Track_set_fade(&nsf_emu, Track_length( &nsf_emu, t ) - 4000, 4000);
|
2007-01-25 18:06:17 +00:00
|
|
|
}
|
2011-08-07 20:01:04 +00:00
|
|
|
if (multitrack) ci->set_elapsed(t*1000); /* t is track no to display */
|
|
|
|
else ci->set_elapsed(0);
|
2007-01-25 18:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* this is the codec entry point */
|
2011-04-27 03:08:23 +00:00
|
|
|
enum codec_status codec_main(enum codec_entry_call_reason reason)
|
|
|
|
{
|
|
|
|
if (reason == CODEC_LOAD) {
|
2011-08-07 20:01:04 +00:00
|
|
|
/* we only render 16 bits */
|
2011-04-27 03:08:23 +00:00
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, 16);
|
2011-08-07 20:01:04 +00:00
|
|
|
|
|
|
|
/* 44 Khz, Interleaved stereo */
|
2011-04-27 03:08:23 +00:00
|
|
|
ci->configure(DSP_SET_FREQUENCY, 44100);
|
2011-08-07 20:01:04 +00:00
|
|
|
ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
|
2011-04-27 03:08:23 +00:00
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
Nsf_init(&nsf_emu);
|
|
|
|
Nsf_set_sample_rate(&nsf_emu, 44100);
|
2011-04-27 03:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return CODEC_OK;
|
|
|
|
}
|
2011-08-07 20:01:04 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* this is called for each file to process */
|
|
|
|
enum codec_status codec_run(void)
|
2007-01-25 18:06:17 +00:00
|
|
|
{
|
2011-08-07 20:01:04 +00:00
|
|
|
blargg_err_t err;
|
2007-01-25 18:06:17 +00:00
|
|
|
uint8_t *buf;
|
|
|
|
size_t n;
|
2011-08-07 20:01:04 +00:00
|
|
|
int track, is_multitrack;
|
|
|
|
uint32_t elapsed_time;
|
2011-04-27 16:46:27 +00:00
|
|
|
intptr_t param;
|
2011-08-07 20:01:04 +00:00
|
|
|
|
|
|
|
track = is_multitrack = 0;
|
|
|
|
elapsed_time = 0;
|
2013-07-14 11:59:39 +00:00
|
|
|
param = ci->id3->elapsed;
|
2011-08-07 20:01:04 +00:00
|
|
|
|
2007-01-25 18:06:17 +00:00
|
|
|
DEBUGF("NSF: next_track\n");
|
|
|
|
if (codec_init()) {
|
|
|
|
return CODEC_ERROR;
|
2011-08-07 20:01:04 +00:00
|
|
|
}
|
|
|
|
|
2007-02-26 17:15:04 +00:00
|
|
|
codec_set_replaygain(ci->id3);
|
2007-01-25 18:06:17 +00:00
|
|
|
|
|
|
|
/* Read the entire file */
|
|
|
|
DEBUGF("NSF: request file\n");
|
|
|
|
ci->seek_buffer(0);
|
|
|
|
buf = ci->request_buffer(&n, ci->filesize);
|
|
|
|
if (!buf || n < (size_t)ci->filesize) {
|
|
|
|
DEBUGF("NSF: file load failed\n");
|
|
|
|
return CODEC_ERROR;
|
|
|
|
}
|
2011-08-07 20:01:04 +00:00
|
|
|
|
2011-08-31 19:19:49 +00:00
|
|
|
if ((err = Nsf_load_mem(&nsf_emu, buf, ci->filesize))) {
|
|
|
|
DEBUGF("NSF: Nsf_load_mem failed (%s)\n", err);
|
2011-08-07 20:01:04 +00:00
|
|
|
return CODEC_ERROR;
|
|
|
|
}
|
2011-04-28 02:56:28 +00:00
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
/* Update internal track count */
|
|
|
|
if (nsf_emu.m3u.size > 0)
|
|
|
|
nsf_emu.track_count = nsf_emu.m3u.size;
|
2007-01-25 18:06:17 +00:00
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
if (nsf_emu.track_count > 1) is_multitrack = 1;
|
2007-01-25 18:06:17 +00:00
|
|
|
|
2013-07-14 11:59:39 +00:00
|
|
|
if (param) {
|
|
|
|
goto resume_start;
|
|
|
|
}
|
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
next_track:
|
|
|
|
set_codec_track(track, is_multitrack);
|
2007-01-25 18:06:17 +00:00
|
|
|
|
|
|
|
/* The main decoder loop */
|
2011-08-07 20:01:04 +00:00
|
|
|
while (1) {
|
2011-04-27 03:08:23 +00:00
|
|
|
enum codec_command_action action = ci->get_command(¶m);
|
2007-01-25 18:06:17 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (action == CODEC_ACTION_HALT)
|
2007-01-25 18:06:17 +00:00
|
|
|
break;
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (action == CODEC_ACTION_SEEK_TIME) {
|
2013-07-14 11:59:39 +00:00
|
|
|
resume_start:
|
2011-08-07 20:01:04 +00:00
|
|
|
if (is_multitrack) {
|
|
|
|
track = param/1000;
|
|
|
|
ci->seek_complete();
|
|
|
|
if (track >= nsf_emu.track_count) break;
|
|
|
|
goto next_track;
|
2007-01-25 18:06:17 +00:00
|
|
|
}
|
2011-08-07 20:01:04 +00:00
|
|
|
|
|
|
|
ci->set_elapsed(param);
|
|
|
|
elapsed_time = param;
|
|
|
|
Track_seek(&nsf_emu, param);
|
2011-04-27 16:46:27 +00:00
|
|
|
ci->seek_complete();
|
2011-08-07 20:01:04 +00:00
|
|
|
|
|
|
|
/* Set fade again */
|
2011-08-30 19:40:09 +00:00
|
|
|
if (!ci->loop_track()) {
|
2011-08-07 20:01:04 +00:00
|
|
|
Track_set_fade(&nsf_emu, Track_length( &nsf_emu, track ), 4000);
|
|
|
|
}
|
2007-01-25 18:06:17 +00:00
|
|
|
}
|
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
/* Generate audio buffer */
|
|
|
|
err = Nsf_play(&nsf_emu, CHUNK_SIZE, samples);
|
2011-08-31 19:19:49 +00:00
|
|
|
if (err || Track_ended(&nsf_emu)) {
|
2007-01-25 18:06:17 +00:00
|
|
|
track++;
|
2011-08-07 20:01:04 +00:00
|
|
|
if (track >= nsf_emu.track_count) break;
|
|
|
|
goto next_track;
|
2007-01-25 18:06:17 +00:00
|
|
|
}
|
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
ci->pcmbuf_insert(samples, NULL, CHUNK_SIZE >> 1);
|
2011-02-20 15:27:10 +00:00
|
|
|
|
2011-08-07 20:01:04 +00:00
|
|
|
/* Set elapsed time for one track files */
|
|
|
|
if (is_multitrack == 0) {
|
2011-09-10 02:52:12 +00:00
|
|
|
elapsed_time += (CHUNK_SIZE / 2) * 10 / 441;
|
2011-08-07 20:01:04 +00:00
|
|
|
ci->set_elapsed(elapsed_time);
|
|
|
|
}
|
2007-01-25 18:06:17 +00:00
|
|
|
}
|
2011-08-07 20:01:04 +00:00
|
|
|
|
2007-01-25 18:06:17 +00:00
|
|
|
return CODEC_OK;
|
|
|
|
}
|