GmeDecoderPlugin.cxx 7.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2003-2013 The Music Player Daemon Project
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "config.h"
21
#include "GmeDecoderPlugin.hxx"
22
#include "DecoderAPI.hxx"
23
#include "CheckAudioFormat.hxx"
24
#include "tag/TagHandler.hxx"
25
#include "util/FormatString.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "util/UriUtil.hxx"
27
#include "util/Error.hxx"
28 29
#include "util/Domain.hxx"
#include "Log.hxx"
30

31 32
#include <glib.h>
#include <assert.h>
33 34 35
#include <stdlib.h>
#include <string.h>

36 37
#include <gme/gme.h>

38 39
#define SUBTUNE_PREFIX "tune_"

40 41
static constexpr Domain gme_domain("gme");

42 43 44 45 46
static constexpr unsigned GME_SAMPLE_RATE = 44100;
static constexpr unsigned GME_CHANNELS = 2;
static constexpr unsigned GME_BUFFER_FRAMES = 2048;
static constexpr unsigned GME_BUFFER_SAMPLES =
	GME_BUFFER_FRAMES * GME_CHANNELS;
47

48 49 50 51 52 53 54 55 56
/**
 * 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);
57 58 59 60 61

	char pat[64];
	snprintf(pat, sizeof(pat), "%s%s",
		 "*/" SUBTUNE_PREFIX "???.",
		 subtune_suffix);
62 63
	GPatternSpec *path_with_subtune = g_pattern_spec_new(pat);
	if (!g_pattern_match(path_with_subtune,
64
			     strlen(path_container), path_container, nullptr)) {
65 66 67 68 69
		g_pattern_spec_free(path_with_subtune);
		return path_container;
	}

	char *ptr = g_strrstr(path_container, "/" SUBTUNE_PREFIX);
70
	if (ptr != nullptr)
71 72 73 74 75 76 77 78 79 80 81 82 83 84
		*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);
85 86 87 88 89

	char pat[64];
	snprintf(pat, sizeof(pat), "%s%s",
		 "*/" SUBTUNE_PREFIX "???.",
		 subtune_suffix);
90 91 92
	GPatternSpec *path_with_subtune = g_pattern_spec_new(pat);

	if (g_pattern_match(path_with_subtune,
93
			    strlen(path_fs), path_fs, nullptr)) {
94 95
		char *sub = g_strrstr(path_fs, "/" SUBTUNE_PREFIX);
		g_pattern_spec_free(path_with_subtune);
96
		if (!sub)
97 98 99
			return 0;

		sub += strlen("/" SUBTUNE_PREFIX);
100
		int song_num = strtol(sub, nullptr, 10);
101 102 103 104 105 106 107 108 109 110 111 112

		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;
113 114
	const char *gme_err = gme_open_file(path_fs, &emu, GME_SAMPLE_RATE);
	if (gme_err != nullptr) {
115
		LogWarning(gme_domain, gme_err);
116
		return nullptr;
117 118
	}

119
	const unsigned num_songs = gme_track_count(emu);
120 121
	/* if it only contains a single tune, don't treat as container */
	if (num_songs < 2)
122
		return nullptr;
123 124 125

	const char *subtune_suffix = uri_get_suffix(path_fs);
	if (tnum <= num_songs){
126 127
		return FormatNew(SUBTUNE_PREFIX "%03u.%s",
				 tnum, subtune_suffix);
128
	} else
129
		return nullptr;
130 131
}

132
static void
133
gme_file_decode(Decoder &decoder, const char *path_fs)
134
{
135
	char *path_container = get_container_name(path_fs);
136

137 138 139
	Music_Emu *emu;
	const char *gme_err =
		gme_open_file(path_container, &emu, GME_SAMPLE_RATE);
140
	g_free(path_container);
141
	if (gme_err != nullptr) {
142
		LogWarning(gme_domain, gme_err);
143 144
		return;
	}
145

146 147 148 149
	gme_info_t *ti;
	const int song_num = get_song_num(path_fs);
	gme_err = gme_track_info(emu, &ti, song_num);
	if (gme_err != nullptr) {
150
		LogWarning(gme_domain, gme_err);
151 152 153 154
		gme_delete(emu);
		return;
	}

155 156 157
	const float song_len = ti->length > 0
		? ti->length / 1000.0
		: -1.0;
158 159 160

	/* initialize the MPD decoder */

161
	Error error;
162 163 164
	AudioFormat audio_format;
	if (!audio_format_init_checked(audio_format, GME_SAMPLE_RATE,
				       SampleFormat::S16, GME_CHANNELS,
165
				       error)) {
166
		LogError(error);
167 168 169 170 171
		gme_free_info(ti);
		gme_delete(emu);
		return;
	}

172
	decoder_initialized(decoder, audio_format, true, song_len);
173

174 175
	gme_err = gme_start_track(emu, song_num);
	if (gme_err != nullptr)
176
		LogWarning(gme_domain, gme_err);
177

178
	if (ti->length > 0)
179 180
		gme_set_fade(emu, ti->length);

181
	/* play */
182
	DecoderCommand cmd;
183
	do {
184
		short buf[GME_BUFFER_SAMPLES];
185
		gme_err = gme_play(emu, GME_BUFFER_SAMPLES, buf);
186
		if (gme_err != nullptr) {
187
			LogWarning(gme_domain, gme_err);
188 189 190
			return;
		}

191
		cmd = decoder_data(decoder, nullptr, buf, sizeof(buf), 0);
192
		if (cmd == DecoderCommand::SEEK) {
193
			float where = decoder_seek_where(decoder);
194
			gme_err = gme_seek(emu, int(where * 1000));
195
			if (gme_err != nullptr)
196
				LogWarning(gme_domain, gme_err);
197 198 199
			decoder_command_finished(decoder);
		}

200
		if (gme_track_ended(emu))
201
			break;
202
	} while (cmd != DecoderCommand::STOP);
203 204 205 206 207

	gme_free_info(ti);
	gme_delete(emu);
}

208 209 210
static bool
gme_scan_file(const char *path_fs,
	      const struct tag_handler *handler, void *handler_ctx)
211
{
212
	char *path_container = get_container_name(path_fs);
213

214 215 216
	Music_Emu *emu;
	const char *gme_err =
		gme_open_file(path_container, &emu, GME_SAMPLE_RATE);
217
	g_free(path_container);
218
	if (gme_err != nullptr) {
219
		LogWarning(gme_domain, gme_err);
220
		return false;
221
	}
222 223 224 225 226 227

	const int song_num = get_song_num(path_fs);

	gme_info_t *ti;
	gme_err = gme_track_info(emu, &ti, song_num);
	if (gme_err != nullptr) {
228
		LogWarning(gme_domain, gme_err);
229
		gme_delete(emu);
230
		return false;
231 232
	}

233
	assert(ti != nullptr);
234

235
	if (ti->length > 0)
236 237 238
		tag_handler_invoke_duration(handler, handler_ctx,
					    ti->length / 100);

239 240
	if (ti->song != nullptr) {
		if (gme_track_count(emu) > 1) {
241
			/* start numbering subtunes from 1 */
242 243 244 245 246
			char tag_title[1024];
			snprintf(tag_title, sizeof(tag_title),
				 "%s (%d/%d)",
				 ti->song, song_num + 1,
				 gme_track_count(emu));
247 248
			tag_handler_invoke_tag(handler, handler_ctx,
					       TAG_TITLE, tag_title);
249
		} else
250 251
			tag_handler_invoke_tag(handler, handler_ctx,
					       TAG_TITLE, ti->song);
252
	}
253 254

	if (ti->author != nullptr)
255 256
		tag_handler_invoke_tag(handler, handler_ctx,
				       TAG_ARTIST, ti->author);
257 258

	if (ti->game != nullptr)
259 260
		tag_handler_invoke_tag(handler, handler_ctx,
				       TAG_ALBUM, ti->game);
261 262

	if (ti->comment != nullptr)
263 264
		tag_handler_invoke_tag(handler, handler_ctx,
				       TAG_COMMENT, ti->comment);
265 266

	if (ti->copyright != nullptr)
267 268
		tag_handler_invoke_tag(handler, handler_ctx,
				       TAG_DATE, ti->copyright);
269 270 271

	gme_free_info(ti);
	gme_delete(emu);
272 273

	return true;
274 275 276 277 278
}

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

282 283
extern const struct DecoderPlugin gme_decoder_plugin;
const struct DecoderPlugin gme_decoder_plugin = {
284 285 286 287 288 289 290 291 292 293
	"gme",
	nullptr,
	nullptr,
	nullptr,
	gme_file_decode,
	gme_scan_file,
	nullptr,
	gme_container_scan,
	gme_suffixes,
	nullptr,
294
};