WildmidiDecoderPlugin.cxx 4.21 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "WildmidiDecoderPlugin.hxx"
21
#include "../DecoderAPI.hxx"
22
#include "tag/Handler.hxx"
23
#include "util/ScopeExit.hxx"
24
#include "util/StringFormat.hxx"
25
#include "fs/AllocatedPath.hxx"
26
#include "fs/FileSystem.hxx"
27
#include "fs/Path.hxx"
28
#include "fs/NarrowPath.hxx"
29
#include "PluginUnavailable.hxx"
30

31 32 33 34 35 36
#ifdef _WIN32
/* assume WildMidi is built as static library on Windows; without
   this, linking to the static library would fail */
#define WILDMIDI_STATIC
#endif

37
extern "C" {
38
#include <wildmidi_lib.h>
39
}
40

41
static constexpr AudioFormat wildmidi_audio_format{48000, SampleFormat::S16, 2};
42 43

static bool
44
wildmidi_init(const ConfigBlock &block)
45
{
46
	const AllocatedPath path =
47
		block.GetPath("config_file",
48
			      "/etc/timidity/timidity.cfg");
49 50 51

	if (!FileExists(path)) {
		const auto utf8 = path.ToUTF8();
52 53
		throw PluginUnavailable(StringFormat<1024>("configuration file does not exist: %s",
							   utf8.c_str()));
54
	}
55

56 57
#ifdef LIBWILDMIDI_VERSION
	/* WildMidi_ClearError() requires libwildmidi 0.4 */
58 59
	WildMidi_ClearError();
	AtScopeExit() { WildMidi_ClearError(); };
60
#endif
61

62 63
	if (WildMidi_Init(NarrowPath(path),
			  wildmidi_audio_format.sample_rate,
64 65 66
			  0) != 0) {
#ifdef LIBWILDMIDI_VERSION
		/* WildMidi_GetError() requires libwildmidi 0.4 */
67
		throw PluginUnavailable(WildMidi_GetError());
68 69 70
#else
		throw PluginUnavailable("WildMidi_Init() failed");
#endif
71
	}
72

73
	return true;
74 75 76
}

static void
77
wildmidi_finish() noexcept
78 79 80 81
{
	WildMidi_Shutdown();
}

82
static DecoderCommand
83
wildmidi_output(DecoderClient &client, midi *wm)
84
{
85 86 87 88 89
#ifdef LIBWILDMIDI_VER_MAJOR
	/* WildMidi 0.4 has switched from "char*" to "int8_t*" */
	int8_t buffer[4096];
#else
	/* pre 0.4 */
90
	char buffer[4096];
91 92
#endif

93 94 95 96
	int length = WildMidi_GetOutput(wm, buffer, sizeof(buffer));
	if (length <= 0)
		return DecoderCommand::STOP;

97
	return client.SubmitData(nullptr, buffer, length, 0);
98 99
}

100
static void
101
wildmidi_file_decode(DecoderClient &client, Path path_fs)
102 103
{
	midi *wm;
104
	const struct _WM_Info *info;
105

106
	wm = WildMidi_Open(NarrowPath(path_fs));
107
	if (wm == nullptr)
108 109
		return;

110
	info = WildMidi_GetInfo(wm);
111
	if (info == nullptr) {
112 113 114 115
		WildMidi_Close(wm);
		return;
	}

116 117
	const auto duration =
		SongTime::FromScale<uint64_t>(info->approx_total_samples,
118
					      wildmidi_audio_format.sample_rate);
119

120
	client.Ready(wildmidi_audio_format, true, duration);
121

122
	DecoderCommand cmd;
123
	do {
124
		info = WildMidi_GetInfo(wm);
125
		if (info == nullptr)
126 127
			break;

128
		cmd = wildmidi_output(client, wm);
129

130
		if (cmd == DecoderCommand::SEEK) {
131
			unsigned long seek_where = client.GetSeekFrame();
132

133
			WildMidi_FastSeek(wm, &seek_where);
134
			client.CommandFinished();
135
			cmd = DecoderCommand::NONE;
136 137
		}

138
	} while (cmd == DecoderCommand::NONE);
139 140 141 142

	WildMidi_Close(wm);
}

143
static bool
144
wildmidi_scan_file(Path path_fs, TagHandler &handler) noexcept
145
{
146
	midi *wm = WildMidi_Open(NarrowPath(path_fs));
147
	if (wm == nullptr)
148
		return false;
149

150
	const struct _WM_Info *info = WildMidi_GetInfo(wm);
151
	if (info == nullptr) {
152
		WildMidi_Close(wm);
153
		return false;
154 155
	}

156 157
	handler.OnAudioFormat(wildmidi_audio_format);

158 159
	const auto duration =
		SongTime::FromScale<uint64_t>(info->approx_total_samples,
160
					      wildmidi_audio_format.sample_rate);
161
	handler.OnDuration(duration);
162 163

	WildMidi_Close(wm);
164

165
	return true;
166 167 168 169
}

static const char *const wildmidi_suffixes[] = {
	"mid",
170
	nullptr
171 172
};

173 174 175 176
constexpr DecoderPlugin wildmidi_decoder_plugin =
	DecoderPlugin("wildmidi", wildmidi_file_decode, wildmidi_scan_file)
	.WithInit(wildmidi_init, wildmidi_finish)
	.WithSuffixes(wildmidi_suffixes);