WildmidiDecoderPlugin.cxx 3.71 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 "config.h"
21
#include "WildmidiDecoderPlugin.hxx"
22
#include "../DecoderAPI.hxx"
23
#include "tag/TagHandler.hxx"
24
#include "util/Domain.hxx"
25
#include "fs/AllocatedPath.hxx"
26
#include "fs/FileSystem.hxx"
27
#include "fs/Path.hxx"
28
#include "Log.hxx"
29

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

34
static constexpr Domain wildmidi_domain("wildmidi");
35

36
static constexpr unsigned WILDMIDI_SAMPLE_RATE = 48000;
37 38

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

	if (!FileExists(path)) {
		const auto utf8 = path.ToUTF8();
47 48 49
		FormatDebug(wildmidi_domain,
			    "configuration file does not exist: %s",
			    utf8.c_str());
50 51
		return false;
	}
52

53
	return WildMidi_Init(path.c_str(), WILDMIDI_SAMPLE_RATE, 0) == 0;
54 55 56 57 58 59 60 61
}

static void
wildmidi_finish(void)
{
	WildMidi_Shutdown();
}

62
static DecoderCommand
63
wildmidi_output(DecoderClient &client, midi *wm)
64
{
65 66 67 68 69
#ifdef LIBWILDMIDI_VER_MAJOR
	/* WildMidi 0.4 has switched from "char*" to "int8_t*" */
	int8_t buffer[4096];
#else
	/* pre 0.4 */
70
	char buffer[4096];
71 72
#endif

73 74 75 76
	int length = WildMidi_GetOutput(wm, buffer, sizeof(buffer));
	if (length <= 0)
		return DecoderCommand::STOP;

77
	return client.SubmitData(nullptr, buffer, length, 0);
78 79
}

80
static void
81
wildmidi_file_decode(DecoderClient &client, Path path_fs)
82
{
83
	static constexpr AudioFormat audio_format = {
84
		WILDMIDI_SAMPLE_RATE,
85
		SampleFormat::S16,
86
		2,
87 88
	};
	midi *wm;
89
	const struct _WM_Info *info;
90

91
	wm = WildMidi_Open(path_fs.c_str());
92
	if (wm == nullptr)
93 94
		return;

95
	info = WildMidi_GetInfo(wm);
96
	if (info == nullptr) {
97 98 99 100
		WildMidi_Close(wm);
		return;
	}

101 102 103 104
	const auto duration =
		SongTime::FromScale<uint64_t>(info->approx_total_samples,
					      WILDMIDI_SAMPLE_RATE);

105
	client.Ready(audio_format, true, duration);
106

107
	DecoderCommand cmd;
108
	do {
109
		info = WildMidi_GetInfo(wm);
110
		if (info == nullptr)
111 112
			break;

113
		cmd = wildmidi_output(client, wm);
114

115
		if (cmd == DecoderCommand::SEEK) {
116
			unsigned long seek_where = client.GetSeekFrame();
117

118
			WildMidi_FastSeek(wm, &seek_where);
119
			client.CommandFinished();
120
			cmd = DecoderCommand::NONE;
121 122
		}

123
	} while (cmd == DecoderCommand::NONE);
124 125 126 127

	WildMidi_Close(wm);
}

128
static bool
129
wildmidi_scan_file(Path path_fs,
130
		   const TagHandler &handler, void *handler_ctx)
131
{
132
	midi *wm = WildMidi_Open(path_fs.c_str());
133
	if (wm == nullptr)
134
		return false;
135

136
	const struct _WM_Info *info = WildMidi_GetInfo(wm);
137
	if (info == nullptr) {
138
		WildMidi_Close(wm);
139
		return false;
140 141
	}

142 143 144
	const auto duration =
		SongTime::FromScale<uint64_t>(info->approx_total_samples,
					      WILDMIDI_SAMPLE_RATE);
145
	tag_handler_invoke_duration(handler, handler_ctx, duration);
146 147

	WildMidi_Close(wm);
148

149
	return true;
150 151 152 153
}

static const char *const wildmidi_suffixes[] = {
	"mid",
154
	nullptr
155 156
};

157
const struct DecoderPlugin wildmidi_decoder_plugin = {
158 159 160 161 162 163 164 165 166 167
	"wildmidi",
	wildmidi_init,
	wildmidi_finish,
	nullptr,
	wildmidi_file_decode,
	wildmidi_scan_file,
	nullptr,
	nullptr,
	wildmidi_suffixes,
	nullptr,
168
};