WildmidiDecoderPlugin.cxx 4.04 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 "Log.hxx"
29
#include "PluginUnavailable.hxx"
30

31
extern "C" {
32
#include <wildmidi_lib.h>
33
}
34

35
static constexpr AudioFormat wildmidi_audio_format{48000, SampleFormat::S16, 2};
36 37

static bool
38
wildmidi_init(const ConfigBlock &block)
39
{
40
	const AllocatedPath path =
41
		block.GetPath("config_file",
42
			      "/etc/timidity/timidity.cfg");
43 44 45

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

50 51
#ifdef LIBWILDMIDI_VERSION
	/* WildMidi_ClearError() requires libwildmidi 0.4 */
52 53
	WildMidi_ClearError();
	AtScopeExit() { WildMidi_ClearError(); };
54
#endif
55 56

	if (WildMidi_Init(path.c_str(), wildmidi_audio_format.sample_rate,
57 58 59
			  0) != 0) {
#ifdef LIBWILDMIDI_VERSION
		/* WildMidi_GetError() requires libwildmidi 0.4 */
60
		throw PluginUnavailable(WildMidi_GetError());
61 62 63 64
#else
		throw PluginUnavailable("WildMidi_Init() failed");
#endif
	}
65 66

	return true;
67 68 69
}

static void
70
wildmidi_finish() noexcept
71 72 73 74
{
	WildMidi_Shutdown();
}

75
static DecoderCommand
76
wildmidi_output(DecoderClient &client, midi *wm)
77
{
78 79 80 81 82
#ifdef LIBWILDMIDI_VER_MAJOR
	/* WildMidi 0.4 has switched from "char*" to "int8_t*" */
	int8_t buffer[4096];
#else
	/* pre 0.4 */
83
	char buffer[4096];
84 85
#endif

86 87 88 89
	int length = WildMidi_GetOutput(wm, buffer, sizeof(buffer));
	if (length <= 0)
		return DecoderCommand::STOP;

90
	return client.SubmitData(nullptr, buffer, length, 0);
91 92
}

93
static void
94
wildmidi_file_decode(DecoderClient &client, Path path_fs)
95 96
{
	midi *wm;
97
	const struct _WM_Info *info;
98

99
	wm = WildMidi_Open(path_fs.c_str());
100
	if (wm == nullptr)
101 102
		return;

103
	info = WildMidi_GetInfo(wm);
104
	if (info == nullptr) {
105 106 107 108
		WildMidi_Close(wm);
		return;
	}

109 110
	const auto duration =
		SongTime::FromScale<uint64_t>(info->approx_total_samples,
111
					      wildmidi_audio_format.sample_rate);
112

113
	client.Ready(wildmidi_audio_format, true, duration);
114

115
	DecoderCommand cmd;
116
	do {
117
		info = WildMidi_GetInfo(wm);
118
		if (info == nullptr)
119 120
			break;

121
		cmd = wildmidi_output(client, wm);
122

123
		if (cmd == DecoderCommand::SEEK) {
124
			unsigned long seek_where = client.GetSeekFrame();
125

126
			WildMidi_FastSeek(wm, &seek_where);
127
			client.CommandFinished();
128
			cmd = DecoderCommand::NONE;
129 130
		}

131
	} while (cmd == DecoderCommand::NONE);
132 133 134 135

	WildMidi_Close(wm);
}

136
static bool
137
wildmidi_scan_file(Path path_fs, TagHandler &handler) noexcept
138
{
139
	midi *wm = WildMidi_Open(path_fs.c_str());
140
	if (wm == nullptr)
141
		return false;
142

143
	const struct _WM_Info *info = WildMidi_GetInfo(wm);
144
	if (info == nullptr) {
145
		WildMidi_Close(wm);
146
		return false;
147 148
	}

149 150
	handler.OnAudioFormat(wildmidi_audio_format);

151 152
	const auto duration =
		SongTime::FromScale<uint64_t>(info->approx_total_samples,
153
					      wildmidi_audio_format.sample_rate);
154
	handler.OnDuration(duration);
155 156

	WildMidi_Close(wm);
157

158
	return true;
159 160 161 162
}

static const char *const wildmidi_suffixes[] = {
	"mid",
163
	nullptr
164 165
};

166
const struct DecoderPlugin wildmidi_decoder_plugin = {
167 168 169 170 171 172 173 174 175 176
	"wildmidi",
	wildmidi_init,
	wildmidi_finish,
	nullptr,
	wildmidi_file_decode,
	wildmidi_scan_file,
	nullptr,
	nullptr,
	wildmidi_suffixes,
	nullptr,
177
};