GmeDecoderPlugin.cxx 6.91 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 "CheckAudioFormat.hxx"
24
#include "tag/TagHandler.hxx"
25
#include "fs/Path.hxx"
26
#include "fs/AllocatedPath.hxx"
27
#include "util/Alloc.hxx"
28
#include "util/FormatString.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "util/UriUtil.hxx"
30
#include "util/Error.hxx"
31 32
#include "util/Domain.hxx"
#include "Log.hxx"
33

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

38 39
#include <gme/gme.h>

40 41
#define SUBTUNE_PREFIX "tune_"

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

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

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

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
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;
}

72
/**
73 74
 * returns the file path stripped of any /tune_xxx.* subtune suffix
 * and the track number (or 0 if no "tune_xxx" suffix is present).
75
 */
76 77
static GmeContainerPath
ParseContainerPath(Path path_fs)
78
{
79 80 81 82 83
	const Path base = path_fs.GetBase();
	unsigned track;
	if (base.IsNull() ||
	    (track = ParseSubtuneName(base.c_str())) < 1)
		return { AllocatedPath(path_fs), 0 };
84

85
	return { path_fs.GetDirectoryName(), track - 1 };
86 87 88
}

static char *
89
gme_container_scan(Path path_fs, const unsigned int tnum)
90 91
{
	Music_Emu *emu;
92 93
	const char *gme_err = gme_open_file(path_fs.c_str(), &emu,
					    GME_SAMPLE_RATE);
94
	if (gme_err != nullptr) {
95
		LogWarning(gme_domain, gme_err);
96
		return nullptr;
97 98
	}

99
	const unsigned num_songs = gme_track_count(emu);
100
	gme_delete(emu);
101 102
	/* if it only contains a single tune, don't treat as container */
	if (num_songs < 2)
103
		return nullptr;
104

105
	const char *subtune_suffix = uri_get_suffix(path_fs.c_str());
106
	if (tnum <= num_songs){
107 108
		return FormatNew(SUBTUNE_PREFIX "%03u.%s",
				 tnum, subtune_suffix);
109
	} else
110
		return nullptr;
111 112
}

113
static void
114
gme_file_decode(Decoder &decoder, Path path_fs)
115
{
116
	const auto container = ParseContainerPath(path_fs);
117

118 119
	Music_Emu *emu;
	const char *gme_err =
120
		gme_open_file(container.path.c_str(), &emu, GME_SAMPLE_RATE);
121
	if (gme_err != nullptr) {
122
		LogWarning(gme_domain, gme_err);
123 124
		return;
	}
125

126
	gme_info_t *ti;
127
	gme_err = gme_track_info(emu, &ti, container.track);
128
	if (gme_err != nullptr) {
129
		LogWarning(gme_domain, gme_err);
130 131 132 133
		gme_delete(emu);
		return;
	}

134 135 136
	const SignedSongTime song_len = ti->length > 0
		? SignedSongTime::FromMS(ti->length)
		: SignedSongTime::Negative();
137 138 139

	/* initialize the MPD decoder */

140
	Error error;
141 142 143
	AudioFormat audio_format;
	if (!audio_format_init_checked(audio_format, GME_SAMPLE_RATE,
				       SampleFormat::S16, GME_CHANNELS,
144
				       error)) {
145
		LogError(error);
146 147 148 149 150
		gme_free_info(ti);
		gme_delete(emu);
		return;
	}

151
	decoder_initialized(decoder, audio_format, true, song_len);
152

153
	gme_err = gme_start_track(emu, container.track);
154
	if (gme_err != nullptr)
155
		LogWarning(gme_domain, gme_err);
156

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

160
	/* play */
161
	DecoderCommand cmd;
162
	do {
163
		short buf[GME_BUFFER_SAMPLES];
164
		gme_err = gme_play(emu, GME_BUFFER_SAMPLES, buf);
165
		if (gme_err != nullptr) {
166
			LogWarning(gme_domain, gme_err);
167 168 169
			return;
		}

170
		cmd = decoder_data(decoder, nullptr, buf, sizeof(buf), 0);
171
		if (cmd == DecoderCommand::SEEK) {
172
			unsigned where = decoder_seek_time(decoder).ToMS();
173
			gme_err = gme_seek(emu, where);
174
			if (gme_err != nullptr)
175
				LogWarning(gme_domain, gme_err);
176 177 178
			decoder_command_finished(decoder);
		}

179
		if (gme_track_ended(emu))
180
			break;
181
	} while (cmd != DecoderCommand::STOP);
182 183 184 185 186

	gme_free_info(ti);
	gme_delete(emu);
}

187
static void
188
ScanGmeInfo(const gme_info_t &info, unsigned song_num, int track_count,
189 190 191 192 193 194 195 196 197 198 199
	    const struct tag_handler *handler, void *handler_ctx)
{
	if (info.length > 0)
		tag_handler_invoke_duration(handler, handler_ctx,
					    SongTime::FromMS(info.length));

	if (info.song != nullptr) {
		if (track_count > 1) {
			/* start numbering subtunes from 1 */
			char tag_title[1024];
			snprintf(tag_title, sizeof(tag_title),
200
				 "%s (%u/%d)",
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
				 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);
}

227
static bool
228
ScanMusicEmu(Music_Emu *emu, unsigned song_num,
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
	     const struct tag_handler *handler, void *handler_ctx)
{
	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;
}

247
static bool
248
gme_scan_file(Path path_fs,
249
	      const struct tag_handler *handler, void *handler_ctx)
250
{
251
	const auto container = ParseContainerPath(path_fs);
252

253 254
	Music_Emu *emu;
	const char *gme_err =
255
		gme_open_file(container.path.c_str(), &emu, GME_SAMPLE_RATE);
256
	if (gme_err != nullptr) {
257
		LogWarning(gme_domain, gme_err);
258
		return false;
259
	}
260

261
	const bool result = ScanMusicEmu(emu, container.track, handler, handler_ctx);
262 263

	gme_delete(emu);
264

265
	return result;
266 267 268 269 270
}

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

274 275
extern const struct DecoderPlugin gme_decoder_plugin;
const struct DecoderPlugin gme_decoder_plugin = {
276 277 278 279 280 281 282 283 284 285
	"gme",
	nullptr,
	nullptr,
	nullptr,
	gme_file_decode,
	gme_scan_file,
	nullptr,
	gme_container_scan,
	gme_suffixes,
	nullptr,
286
};