A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1
2/* Ripped off from Game_Music_Emu 0.5.2. http://www.slack.net/~ant/ */
3
4#include <codecs/lib/codeclib.h>
5#include "libgme/gbs_emu.h"
6
7CODEC_HEADER
8
9/* Maximum number of bytes to process in one iteration */
10#define CHUNK_SIZE (1024*2)
11
12static int16_t samples[CHUNK_SIZE] IBSS_ATTR;
13static struct Gbs_Emu gbs_emu;
14
15/****************** rockbox interface ******************/
16
17static void set_codec_track(int t) {
18 Gbs_start_track(&gbs_emu, t);
19
20 /* for loop mode we disable track limits */
21 if (!ci->loop_track()) {
22 Track_set_fade(&gbs_emu, Track_get_length( &gbs_emu, t ), 4000);
23 }
24 ci->set_elapsed(t*1000); /* t is track no to display */
25}
26
27/* this is the codec entry point */
28enum codec_status codec_main(enum codec_entry_call_reason reason)
29{
30 if (reason == CODEC_LOAD) {
31 /* we only render 16 bits */
32 ci->configure(DSP_SET_SAMPLE_DEPTH, 16);
33
34 /* 44 Khz, Interleaved stereo */
35 ci->configure(DSP_SET_FREQUENCY, 44100);
36 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
37
38 Gbs_init(&gbs_emu);
39 Gbs_set_sample_rate(&gbs_emu, 44100);
40 }
41
42 return CODEC_OK;
43}
44
45/* this is called for each file to process */
46enum codec_status codec_run(void)
47{
48 blargg_err_t err;
49 uint8_t *buf;
50 size_t n;
51 intptr_t param;
52 int track = 0;
53
54 DEBUGF("GBS: next_track\n");
55 if (codec_init()) {
56 return CODEC_ERROR;
57 }
58
59 codec_set_replaygain(ci->id3);
60
61 /* Read the entire file */
62 DEBUGF("GBS: request file\n");
63 ci->seek_buffer(0);
64 buf = ci->request_buffer(&n, ci->filesize);
65 if (!buf || n < (size_t)ci->filesize) {
66 DEBUGF("GBS: file load failed\n");
67 return CODEC_ERROR;
68 }
69
70 if ((err = Gbs_load_mem(&gbs_emu, buf, ci->filesize))) {
71 DEBUGF("GBS: Gbs_load_mem failed (%s)\n", err);
72 return CODEC_ERROR;
73 }
74
75 /* Update internal track count */
76 if (gbs_emu.m3u.size > 0)
77 gbs_emu.track_count = gbs_emu.m3u.size;
78
79 if (ci->id3->elapsed) {
80 track = ci->id3->elapsed/1000;
81 if (track >= gbs_emu.track_count) return CODEC_OK;
82 }
83
84next_track:
85 set_codec_track(track);
86
87 /* The main decoder loop */
88 while (1) {
89 long action = ci->get_command(¶m);
90
91 if (action == CODEC_ACTION_HALT)
92 break;
93
94 if (action == CODEC_ACTION_SEEK_TIME) {
95 track = param/1000;
96 ci->seek_complete();
97 if (track >= gbs_emu.track_count) break;
98 goto next_track;
99 }
100
101 /* Generate audio buffer */
102 err = Gbs_play(&gbs_emu, CHUNK_SIZE, samples);
103 if (err || Track_ended(&gbs_emu)) {
104 track++;
105 if (track >= gbs_emu.track_count) break;
106 goto next_track;
107 }
108
109 ci->pcmbuf_insert(samples, NULL, CHUNK_SIZE >> 1);
110 }
111
112 return CODEC_OK;
113}