summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/nsf.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/nsf.c')
-rw-r--r--lib/rbcodec/codecs/nsf.c135
1 files changed, 135 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/nsf.c b/lib/rbcodec/codecs/nsf.c
new file mode 100644
index 0000000000..4c5b37c3fa
--- /dev/null
+++ b/lib/rbcodec/codecs/nsf.c
@@ -0,0 +1,135 @@
1
2/* Ripped off from Game_Music_Emu 0.5.2. http://www.slack.net/~ant/ */
3
4#define GME_NSF_TYPE
5
6#include <codecs/lib/codeclib.h>
7#include "libgme/nsf_emu.h"
8
9CODEC_HEADER
10
11/* Maximum number of bytes to process in one iteration */
12#define CHUNK_SIZE (1024*2)
13
14static int16_t samples[CHUNK_SIZE] IBSS_ATTR;
15static struct Nsf_Emu nsf_emu;
16
17/****************** rockbox interface ******************/
18
19static void set_codec_track(int t, int multitrack) {
20 Nsf_start_track(&nsf_emu, t);
21
22 /* for REPEAT_ONE we disable track limits */
23 if (!ci->loop_track()) {
24 Track_set_fade(&nsf_emu, Track_length( &nsf_emu, t ) - 4000, 4000);
25 }
26 if (multitrack) ci->set_elapsed(t*1000); /* t is track no to display */
27 else ci->set_elapsed(0);
28}
29
30/* this is the codec entry point */
31enum codec_status codec_main(enum codec_entry_call_reason reason)
32{
33 if (reason == CODEC_LOAD) {
34 /* we only render 16 bits */
35 ci->configure(DSP_SET_SAMPLE_DEPTH, 16);
36
37 /* 44 Khz, Interleaved stereo */
38 ci->configure(DSP_SET_FREQUENCY, 44100);
39 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
40
41 Nsf_init(&nsf_emu);
42 Nsf_set_sample_rate(&nsf_emu, 44100);
43 }
44
45 return CODEC_OK;
46}
47
48/* this is called for each file to process */
49enum codec_status codec_run(void)
50{
51 blargg_err_t err;
52 uint8_t *buf;
53 size_t n;
54 int track, is_multitrack;
55 uint32_t elapsed_time;
56 intptr_t param;
57
58 track = is_multitrack = 0;
59 elapsed_time = 0;
60
61 DEBUGF("NSF: next_track\n");
62 if (codec_init()) {
63 return CODEC_ERROR;
64 }
65
66 codec_set_replaygain(ci->id3);
67
68 /* Read the entire file */
69 DEBUGF("NSF: request file\n");
70 ci->seek_buffer(0);
71 buf = ci->request_buffer(&n, ci->filesize);
72 if (!buf || n < (size_t)ci->filesize) {
73 DEBUGF("NSF: file load failed\n");
74 return CODEC_ERROR;
75 }
76
77 if ((err = Nsf_load_mem(&nsf_emu, buf, ci->filesize))) {
78 DEBUGF("NSF: Nsf_load_mem failed (%s)\n", err);
79 return CODEC_ERROR;
80 }
81
82 /* Update internal track count */
83 if (nsf_emu.m3u.size > 0)
84 nsf_emu.track_count = nsf_emu.m3u.size;
85
86 if (nsf_emu.track_count > 1) is_multitrack = 1;
87
88next_track:
89 set_codec_track(track, is_multitrack);
90
91 /* The main decoder loop */
92 while (1) {
93 enum codec_command_action action = ci->get_command(&param);
94
95 if (action == CODEC_ACTION_HALT)
96 break;
97
98 if (action == CODEC_ACTION_SEEK_TIME) {
99 if (is_multitrack) {
100 track = param/1000;
101 ci->seek_complete();
102 if (track >= nsf_emu.track_count) break;
103 goto next_track;
104 }
105
106 ci->set_elapsed(param);
107 elapsed_time = param;
108 Track_seek(&nsf_emu, param);
109 ci->seek_complete();
110
111 /* Set fade again */
112 if (!ci->loop_track()) {
113 Track_set_fade(&nsf_emu, Track_length( &nsf_emu, track ), 4000);
114 }
115 }
116
117 /* Generate audio buffer */
118 err = Nsf_play(&nsf_emu, CHUNK_SIZE, samples);
119 if (err || Track_ended(&nsf_emu)) {
120 track++;
121 if (track >= nsf_emu.track_count) break;
122 goto next_track;
123 }
124
125 ci->pcmbuf_insert(samples, NULL, CHUNK_SIZE >> 1);
126
127 /* Set elapsed time for one track files */
128 if (is_multitrack == 0) {
129 elapsed_time += (CHUNK_SIZE / 2) * 10 / 441;
130 ci->set_elapsed(elapsed_time);
131 }
132 }
133
134 return CODEC_OK;
135}