2
#include "../decoder_api.h"
3
#include "audio_check.h"
15
#define G_LOG_DOMAIN "gme"
17
#define SUBTUNE_PREFIX "tune_"
20
GME_SAMPLE_RATE = 44100,
22
GME_BUFFER_FRAMES = 2048,
23
GME_BUFFER_SAMPLES = GME_BUFFER_FRAMES * GME_CHANNELS,
27
* returns the file path stripped of any /tune_xxx.* subtune
31
get_container_name(const char *path_fs)
33
const char *subtune_suffix = uri_get_suffix(path_fs);
34
char *path_container = g_strdup(path_fs);
35
char *pat = g_strconcat("*/" SUBTUNE_PREFIX "???.", subtune_suffix, NULL);
36
GPatternSpec *path_with_subtune = g_pattern_spec_new(pat);
38
if (!g_pattern_match(path_with_subtune,
39
strlen(path_container), path_container, NULL)) {
40
g_pattern_spec_free(path_with_subtune);
41
return path_container;
44
char *ptr = g_strrstr(path_container, "/" SUBTUNE_PREFIX);
48
g_pattern_spec_free(path_with_subtune);
49
return path_container;
53
* returns tune number from file.nsf/tune_xxx.* style path or 0 if no subtune
57
get_song_num(const char *path_fs)
59
const char *subtune_suffix = uri_get_suffix(path_fs);
60
char *pat = g_strconcat("*/" SUBTUNE_PREFIX "???.", subtune_suffix, NULL);
61
GPatternSpec *path_with_subtune = g_pattern_spec_new(pat);
64
if (g_pattern_match(path_with_subtune,
65
strlen(path_fs), path_fs, NULL)) {
66
char *sub = g_strrstr(path_fs, "/" SUBTUNE_PREFIX);
67
g_pattern_spec_free(path_with_subtune);
71
sub += strlen("/" SUBTUNE_PREFIX);
72
int song_num = strtol(sub, NULL, 10);
76
g_pattern_spec_free(path_with_subtune);
82
gme_container_scan(const char *path_fs, const unsigned int tnum)
86
unsigned int num_songs;
88
gme_err = gme_open_file(path_fs, &emu, GME_SAMPLE_RATE);
89
if (gme_err != NULL) {
90
g_warning("%s", gme_err);
94
num_songs = gme_track_count(emu);
95
/* if it only contains a single tune, don't treat as container */
99
const char *subtune_suffix = uri_get_suffix(path_fs);
100
if (tnum <= num_songs){
101
char *subtune = g_strdup_printf(
102
SUBTUNE_PREFIX "%03u.%s", tnum, subtune_suffix);
109
gme_file_decode(struct decoder *decoder, const char *path_fs)
114
struct audio_format audio_format;
115
enum decoder_command cmd;
116
short buf[GME_BUFFER_SAMPLES];
118
char *path_container = get_container_name(path_fs);
119
int song_num = get_song_num(path_fs);
121
gme_err = gme_open_file(path_container, &emu, GME_SAMPLE_RATE);
122
g_free(path_container);
123
if (gme_err != NULL) {
124
g_warning("%s", gme_err);
128
if((gme_err = gme_track_info(emu, &ti, song_num)) != NULL){
129
g_warning("%s", gme_err);
135
song_len = ti->length / 1000.0;
138
/* initialize the MPD decoder */
140
GError *error = NULL;
141
if (!audio_format_init_checked(&audio_format, GME_SAMPLE_RATE,
142
SAMPLE_FORMAT_S16, GME_CHANNELS,
144
g_warning("%s", error->message);
151
decoder_initialized(decoder, &audio_format, true, song_len);
153
if((gme_err = gme_start_track(emu, song_num)) != NULL)
154
g_warning("%s", gme_err);
158
gme_err = gme_play(emu, GME_BUFFER_SAMPLES, buf);
159
if (gme_err != NULL) {
160
g_warning("%s", gme_err);
163
cmd = decoder_data(decoder, NULL, buf, sizeof(buf), 0);
165
if(cmd == DECODE_COMMAND_SEEK) {
166
float where = decoder_seek_where(decoder);
167
if((gme_err = gme_seek(emu, (int)where*1000)) != NULL)
168
g_warning("%s", gme_err);
169
decoder_command_finished(decoder);
172
if(gme_track_ended(emu))
174
} while(cmd != DECODE_COMMAND_STOP);
181
gme_tag_dup(const char *path_fs)
186
char *path_container=get_container_name(path_fs);
188
song_num=get_song_num(path_fs);
190
gme_err = gme_open_file(path_container, &emu, GME_SAMPLE_RATE);
191
g_free(path_container);
192
if (gme_err != NULL) {
193
g_warning("%s", gme_err);
196
if((gme_err = gme_track_info(emu, &ti, song_num)) != NULL){
197
g_warning("%s", gme_err);
202
struct tag *tag = tag_new();
205
tag->time = ti->length / 1000;
206
if(ti->song != NULL){
207
if(gme_track_count(emu) > 1){
208
/* start numbering subtunes from 1 */
209
char *tag_title=g_strdup_printf("%s (%d/%d)",
210
ti->song, song_num+1, gme_track_count(emu));
211
tag_add_item(tag, TAG_TITLE, tag_title);
214
tag_add_item(tag, TAG_TITLE, ti->song);
216
if(ti->author != NULL)
217
tag_add_item(tag, TAG_ARTIST, ti->author);
219
tag_add_item(tag, TAG_ALBUM, ti->game);
220
if(ti->comment != NULL)
221
tag_add_item(tag, TAG_COMMENT, ti->comment);
222
if(ti->copyright != NULL)
223
tag_add_item(tag, TAG_DATE, ti->copyright);
231
static const char *const gme_suffixes[] = {
232
"ay", "gbs", "gym", "hes", "kss", "nsf",
233
"nsfe", "sap", "spc", "vgm", "vgz",
237
extern const struct decoder_plugin gme_decoder_plugin;
238
const struct decoder_plugin gme_decoder_plugin = {
240
.file_decode = gme_file_decode,
241
.tag_dup = gme_tag_dup,
242
.suffixes = gme_suffixes,
243
.container_scan = gme_container_scan,