gme_decoder_plugin.c 5.73 KB
Newer Older
1 2 3
#include "config.h"
#include "../decoder_api.h"
#include "audio_check.h"
4 5
#include "uri.h"

6 7
#include <glib.h>
#include <assert.h>
8 9 10 11
#include <errno.h>
#include <stdlib.h>
#include <string.h>

12 13 14 15 16
#include <gme/gme.h>

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "gme"

17 18
#define SUBTUNE_PREFIX "tune_"

19 20 21
enum {
	GME_SAMPLE_RATE = 44100,
	GME_CHANNELS = 2,
22 23
	GME_BUFFER_FRAMES = 2048,
	GME_BUFFER_SAMPLES = GME_BUFFER_FRAMES * GME_CHANNELS,
24 25
};

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
/**
 * returns the file path stripped of any /tune_xxx.* subtune
 * suffix
 */
static char *
get_container_name(const char *path_fs)
{
	const char *subtune_suffix = uri_get_suffix(path_fs);
	char *path_container = g_strdup(path_fs);
	char *pat = g_strconcat("*/" SUBTUNE_PREFIX "???.", subtune_suffix, NULL);
	GPatternSpec *path_with_subtune = g_pattern_spec_new(pat);
	g_free(pat);
	if (!g_pattern_match(path_with_subtune,
			     strlen(path_container), path_container, NULL)) {
		g_pattern_spec_free(path_with_subtune);
		return path_container;
	}

	char *ptr = g_strrstr(path_container, "/" SUBTUNE_PREFIX);
	if (ptr != NULL)
		*ptr='\0';

	g_pattern_spec_free(path_with_subtune);
	return path_container;
}

/**
 * returns tune number from file.nsf/tune_xxx.* style path or 0 if no subtune
 * is appended.
 */
static int
get_song_num(const char *path_fs)
{
	const char *subtune_suffix = uri_get_suffix(path_fs);
	char *pat = g_strconcat("*/" SUBTUNE_PREFIX "???.", subtune_suffix, NULL);
	GPatternSpec *path_with_subtune = g_pattern_spec_new(pat);
	g_free(pat);

	if (g_pattern_match(path_with_subtune,
			    strlen(path_fs), path_fs, NULL)) {
		char *sub = g_strrstr(path_fs, "/" SUBTUNE_PREFIX);
		g_pattern_spec_free(path_with_subtune);
		if(!sub)
			return 0;

		sub += strlen("/" SUBTUNE_PREFIX);
		int song_num = strtol(sub, NULL, 10);

		return song_num - 1;
	} else {
		g_pattern_spec_free(path_with_subtune);
		return 0;
	}
}

static char *
gme_container_scan(const char *path_fs, const unsigned int tnum)
{
	Music_Emu *emu;
	const char* gme_err;
	unsigned int num_songs;

	gme_err = gme_open_file(path_fs, &emu, GME_SAMPLE_RATE);
	if (gme_err != NULL) {
		g_warning("%s", gme_err);
		return NULL;
	}

	num_songs = gme_track_count(emu);
	/* if it only contains a single tune, don't treat as container */
	if (num_songs < 2)
		return NULL;

	const char *subtune_suffix = uri_get_suffix(path_fs);
	if (tnum <= num_songs){
		char *subtune = g_strdup_printf(
			SUBTUNE_PREFIX "%03u.%s", tnum, subtune_suffix);
		return subtune;
	} else
		return NULL;
}

108 109 110 111 112 113 114 115
static void
gme_file_decode(struct decoder *decoder, const char *path_fs)
{
	float song_len;
	Music_Emu *emu;
	gme_info_t *ti;
	struct audio_format audio_format;
	enum decoder_command cmd;
116
	short buf[GME_BUFFER_SAMPLES];
117
	const char* gme_err;
118 119
	char *path_container = get_container_name(path_fs);
	int song_num = get_song_num(path_fs);
120

121 122
	gme_err = gme_open_file(path_container, &emu, GME_SAMPLE_RATE);
	g_free(path_container);
123
	if (gme_err != NULL) {
124 125 126
		g_warning("%s", gme_err);
		return;
	}
127 128

	if((gme_err = gme_track_info(emu, &ti, song_num)) != NULL){
129 130 131 132 133 134 135 136 137 138 139 140
		g_warning("%s", gme_err);
		gme_delete(emu);
		return;
	}

	if(ti->length > 0)
		song_len = ti->length / 1000.0;
	else song_len = -1;

	/* initialize the MPD decoder */

	GError *error = NULL;
141 142 143
	if (!audio_format_init_checked(&audio_format, GME_SAMPLE_RATE,
				       SAMPLE_FORMAT_S16, GME_CHANNELS,
				       &error)) {
144 145 146 147 148 149 150 151 152
		g_warning("%s", error->message);
		g_error_free(error);
		gme_free_info(ti);
		gme_delete(emu);
		return;
	}

	decoder_initialized(decoder, &audio_format, true, song_len);

153
	if((gme_err = gme_start_track(emu, song_num)) != NULL)
154 155
		g_warning("%s", gme_err);

156 157 158
	if(ti->length > 0)
		gme_set_fade(emu, ti->length);

159 160
	/* play */
	do {
161 162
		gme_err = gme_play(emu, GME_BUFFER_SAMPLES, buf);
		if (gme_err != NULL) {
163 164 165
			g_warning("%s", gme_err);
			return;
		}
166
		cmd = decoder_data(decoder, NULL, buf, sizeof(buf), 0);
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

		if(cmd == DECODE_COMMAND_SEEK) {
			float where = decoder_seek_where(decoder);
			if((gme_err = gme_seek(emu, (int)where*1000)) != NULL)
				g_warning("%s", gme_err);
			decoder_command_finished(decoder);
		}

		if(gme_track_ended(emu))
			break;
	} while(cmd != DECODE_COMMAND_STOP);

	gme_free_info(ti);
	gme_delete(emu);
}

static struct tag *
gme_tag_dup(const char *path_fs)
{
	Music_Emu *emu;
	gme_info_t *ti;
	const char* gme_err;
189 190 191
	char *path_container=get_container_name(path_fs);
	int song_num;
	song_num=get_song_num(path_fs);
192

193 194
	gme_err = gme_open_file(path_container, &emu, GME_SAMPLE_RATE);
	g_free(path_container);
195
	if (gme_err != NULL) {
196 197 198
		g_warning("%s", gme_err);
		return NULL;
	}
199
	if((gme_err = gme_track_info(emu, &ti, song_num)) != NULL){
200 201 202 203 204
		g_warning("%s", gme_err);
		gme_delete(emu);
		return NULL;
	}

205
	struct tag *tag = tag_new();
206 207 208
	if(ti != NULL){
		if(ti->length > 0)
			tag->time = ti->length / 1000;
209 210 211 212 213 214 215 216 217 218
		if(ti->song != NULL){
			if(gme_track_count(emu) > 1){
				/* start numbering subtunes from 1 */
				char *tag_title=g_strdup_printf("%s (%d/%d)",
					ti->song, song_num+1, gme_track_count(emu));
				tag_add_item(tag, TAG_TITLE, tag_title);
				g_free(tag_title);
			}else
				tag_add_item(tag, TAG_TITLE, ti->song);
		}
219 220
		if(ti->author != NULL)
			tag_add_item(tag, TAG_ARTIST, ti->author);
221 222
		if(ti->game != NULL)
			tag_add_item(tag, TAG_ALBUM, ti->game);
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
		if(ti->comment != NULL)
			tag_add_item(tag, TAG_COMMENT, ti->comment);
		if(ti->copyright != NULL)
			tag_add_item(tag, TAG_DATE, ti->copyright);
	}

	gme_free_info(ti);
	gme_delete(emu);
	return tag;
}

static const char *const gme_suffixes[] = {
	"ay", "gbs", "gym", "hes", "kss", "nsf",
	"nsfe", "sap", "spc", "vgm", "vgz",
	NULL
};

extern const struct decoder_plugin gme_decoder_plugin;
const struct decoder_plugin gme_decoder_plugin = {
	.name = "gme",
	.file_decode = gme_file_decode,
	.tag_dup = gme_tag_dup,
	.suffixes = gme_suffixes,
246
	.container_scan = gme_container_scan,
247
};