WildmidiDecoderPlugin.cxx 4.24 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "Log.hxx"
30
#include "PluginUnavailable.hxx"
31

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

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

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

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

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

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

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

	return true;
75 76 77
}

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

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

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

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

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

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

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

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

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

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

129
		cmd = wildmidi_output(client, wm);
130

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

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

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

	WildMidi_Close(wm);
}

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

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

157 158
	handler.OnAudioFormat(wildmidi_audio_format);

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

	WildMidi_Close(wm);
165

166
	return true;
167 168 169 170
}

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

174
const struct DecoderPlugin wildmidi_decoder_plugin = {
175 176 177 178 179 180 181 182 183 184
	"wildmidi",
	wildmidi_init,
	wildmidi_finish,
	nullptr,
	wildmidi_file_decode,
	wildmidi_scan_file,
	nullptr,
	nullptr,
	wildmidi_suffixes,
	nullptr,
185
};