GmeDecoderPlugin.cxx 7.49 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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 "config/Block.cxx"
24
#include "CheckAudioFormat.hxx"
25
#include "tag/TagHandler.hxx"
26
#include "fs/Path.hxx"
27
#include "fs/AllocatedPath.hxx"
28
#include "util/FormatString.hxx"
29
#include "util/AllocatedString.hxx"
Max Kellermann's avatar
Max Kellermann committed
30
#include "util/UriUtil.hxx"
31
#include "util/Error.hxx"
32 33
#include "util/Domain.hxx"
#include "Log.hxx"
34

35
#include <assert.h>
36 37 38
#include <stdlib.h>
#include <string.h>

39 40
#include <gme/gme.h>

41 42
#define SUBTUNE_PREFIX "tune_"

43 44
static constexpr Domain gme_domain("gme");

45 46 47 48 49
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;
50

51
struct GmeContainerPath {
52
	AllocatedPath path;
53 54 55
	unsigned track;
};

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#if GME_VERSION >= 0x000600
static int gme_accuracy;
#endif

static bool
gme_plugin_init(gcc_unused const ConfigBlock &block)
{
#if GME_VERSION >= 0x000600
	auto accuracy = block.GetBlockParam("accuracy");
	gme_accuracy = accuracy != nullptr
		? (int)accuracy->GetBoolValue()
		: -1;
#endif

	return true;
}

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
gcc_pure
static unsigned
ParseSubtuneName(const char *base)
{
	if (memcmp(base, SUBTUNE_PREFIX, sizeof(SUBTUNE_PREFIX) - 1) != 0)
		return 0;

	base += sizeof(SUBTUNE_PREFIX) - 1;

	char *endptr;
	auto track = strtoul(base, &endptr, 10);
	if (endptr == base || *endptr != '.')
		return 0;

	return track;
}

90
/**
91 92
 * returns the file path stripped of any /tune_xxx.* subtune suffix
 * and the track number (or 0 if no "tune_xxx" suffix is present).
93
 */
94 95
static GmeContainerPath
ParseContainerPath(Path path_fs)
96
{
97 98 99 100 101
	const Path base = path_fs.GetBase();
	unsigned track;
	if (base.IsNull() ||
	    (track = ParseSubtuneName(base.c_str())) < 1)
		return { AllocatedPath(path_fs), 0 };
102

103
	return { path_fs.GetDirectoryName(), track - 1 };
104 105
}

106
static AllocatedString<>
107
gme_container_scan(Path path_fs, const unsigned int tnum)
108 109
{
	Music_Emu *emu;
110 111
	const char *gme_err = gme_open_file(path_fs.c_str(), &emu,
					    GME_SAMPLE_RATE);
112
	if (gme_err != nullptr) {
113
		LogWarning(gme_domain, gme_err);
114
		return nullptr;
115 116
	}

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

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

131
static void
132
gme_file_decode(Decoder &decoder, Path path_fs)
133
{
134
	const auto container = ParseContainerPath(path_fs);
135

136 137
	Music_Emu *emu;
	const char *gme_err =
138
		gme_open_file(container.path.c_str(), &emu, GME_SAMPLE_RATE);
139
	if (gme_err != nullptr) {
140
		LogWarning(gme_domain, gme_err);
141 142
		return;
	}
143

144 145 146
	FormatDebug(gme_domain, "emulator type '%s'\n",
		    gme_type_system(gme_type(emu)));

147 148 149 150 151
#if GME_VERSION >= 0x000600
	if (gme_accuracy >= 0)
		gme_enable_accuracy(emu, gme_accuracy);
#endif

152
	gme_info_t *ti;
153
	gme_err = gme_track_info(emu, &ti, container.track);
154
	if (gme_err != nullptr) {
155
		LogWarning(gme_domain, gme_err);
156 157 158 159
		gme_delete(emu);
		return;
	}

160
	const int length = ti->play_length;
161 162 163 164
	gme_free_info(ti);

	const SignedSongTime song_len = length > 0
		? SignedSongTime::FromMS(length)
165
		: SignedSongTime::Negative();
166 167 168

	/* initialize the MPD decoder */

169
	Error error;
170 171 172
	AudioFormat audio_format;
	if (!audio_format_init_checked(audio_format, GME_SAMPLE_RATE,
				       SampleFormat::S16, GME_CHANNELS,
173
				       error)) {
174
		LogError(error);
175 176 177 178
		gme_delete(emu);
		return;
	}

179
	decoder_initialized(decoder, audio_format, true, song_len);
180

181
	gme_err = gme_start_track(emu, container.track);
182
	if (gme_err != nullptr)
183
		LogWarning(gme_domain, gme_err);
184

185 186
	if (length > 0)
		gme_set_fade(emu, length);
187

188
	/* play */
189
	DecoderCommand cmd;
190
	do {
191
		short buf[GME_BUFFER_SAMPLES];
192
		gme_err = gme_play(emu, GME_BUFFER_SAMPLES, buf);
193
		if (gme_err != nullptr) {
194
			LogWarning(gme_domain, gme_err);
195 196 197
			return;
		}

198
		cmd = decoder_data(decoder, nullptr, buf, sizeof(buf), 0);
199
		if (cmd == DecoderCommand::SEEK) {
200
			unsigned where = decoder_seek_time(decoder).ToMS();
201
			gme_err = gme_seek(emu, where);
202
			if (gme_err != nullptr) {
203
				LogWarning(gme_domain, gme_err);
204 205 206
				decoder_seek_error(decoder);
			} else
				decoder_command_finished(decoder);
207 208
		}

209
		if (gme_track_ended(emu))
210
			break;
211
	} while (cmd != DecoderCommand::STOP);
212 213 214 215

	gme_delete(emu);
}

216
static void
217
ScanGmeInfo(const gme_info_t &info, unsigned song_num, int track_count,
218
	    const TagHandler &handler, void *handler_ctx)
219
{
Max Kellermann's avatar
Max Kellermann committed
220
	if (info.play_length > 0)
221
		tag_handler_invoke_duration(handler, handler_ctx,
Max Kellermann's avatar
Max Kellermann committed
222
					    SongTime::FromMS(info.play_length));
223 224 225 226 227 228

	if (info.song != nullptr) {
		if (track_count > 1) {
			/* start numbering subtunes from 1 */
			char tag_title[1024];
			snprintf(tag_title, sizeof(tag_title),
229
				 "%s (%u/%d)",
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
				 info.song, song_num + 1,
				 track_count);
			tag_handler_invoke_tag(handler, handler_ctx,
					       TAG_TITLE, tag_title);
		} else
			tag_handler_invoke_tag(handler, handler_ctx,
					       TAG_TITLE, info.song);
	}

	if (info.author != nullptr)
		tag_handler_invoke_tag(handler, handler_ctx,
				       TAG_ARTIST, info.author);

	if (info.game != nullptr)
		tag_handler_invoke_tag(handler, handler_ctx,
				       TAG_ALBUM, info.game);

	if (info.comment != nullptr)
		tag_handler_invoke_tag(handler, handler_ctx,
				       TAG_COMMENT, info.comment);

	if (info.copyright != nullptr)
		tag_handler_invoke_tag(handler, handler_ctx,
				       TAG_DATE, info.copyright);
}

256
static bool
257
ScanMusicEmu(Music_Emu *emu, unsigned song_num,
258
	     const TagHandler &handler, void *handler_ctx)
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
{
	gme_info_t *ti;
	const char *gme_err = gme_track_info(emu, &ti, song_num);
	if (gme_err != nullptr) {
		LogWarning(gme_domain, gme_err);
		return false;
	}

	assert(ti != nullptr);

	ScanGmeInfo(*ti, song_num, gme_track_count(emu),
		    handler, handler_ctx);

	gme_free_info(ti);
	return true;
}

276
static bool
277
gme_scan_file(Path path_fs,
278
	      const TagHandler &handler, void *handler_ctx)
279
{
280
	const auto container = ParseContainerPath(path_fs);
281

282 283
	Music_Emu *emu;
	const char *gme_err =
284
		gme_open_file(container.path.c_str(), &emu, GME_SAMPLE_RATE);
285
	if (gme_err != nullptr) {
286
		LogWarning(gme_domain, gme_err);
287
		return false;
288
	}
289

290
	const bool result = ScanMusicEmu(emu, container.track, handler, handler_ctx);
291 292

	gme_delete(emu);
293

294
	return result;
295 296 297 298 299
}

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

303 304
extern const struct DecoderPlugin gme_decoder_plugin;
const struct DecoderPlugin gme_decoder_plugin = {
305
	"gme",
306
	gme_plugin_init,
307 308 309 310 311 312 313 314
	nullptr,
	nullptr,
	gme_file_decode,
	gme_scan_file,
	nullptr,
	gme_container_scan,
	gme_suffixes,
	nullptr,
315
};