WildmidiDecoderPlugin.cxx 3.67 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/Domain.hxx"
24
#include "fs/AllocatedPath.hxx"
25
#include "fs/FileSystem.hxx"
26
#include "fs/Path.hxx"
27
#include "Log.hxx"
28

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

33
static constexpr Domain wildmidi_domain("wildmidi");
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 48
		FormatDebug(wildmidi_domain,
			    "configuration file does not exist: %s",
			    utf8.c_str());
49 50
		return false;
	}
51

52 53
	return WildMidi_Init(path.c_str(), wildmidi_audio_format.sample_rate,
			     0) == 0;
54 55 56
}

static void
57
wildmidi_finish() noexcept
58 59 60 61
{
	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
{
	midi *wm;
84
	const struct _WM_Info *info;
85

86
	wm = WildMidi_Open(path_fs.c_str());
87
	if (wm == nullptr)
88 89
		return;

90
	info = WildMidi_GetInfo(wm);
91
	if (info == nullptr) {
92 93 94 95
		WildMidi_Close(wm);
		return;
	}

96 97
	const auto duration =
		SongTime::FromScale<uint64_t>(info->approx_total_samples,
98
					      wildmidi_audio_format.sample_rate);
99

100
	client.Ready(wildmidi_audio_format, true, duration);
101

102
	DecoderCommand cmd;
103
	do {
104
		info = WildMidi_GetInfo(wm);
105
		if (info == nullptr)
106 107
			break;

108
		cmd = wildmidi_output(client, wm);
109

110
		if (cmd == DecoderCommand::SEEK) {
111
			unsigned long seek_where = client.GetSeekFrame();
112

113
			WildMidi_FastSeek(wm, &seek_where);
114
			client.CommandFinished();
115
			cmd = DecoderCommand::NONE;
116 117
		}

118
	} while (cmd == DecoderCommand::NONE);
119 120 121 122

	WildMidi_Close(wm);
}

123
static bool
124
wildmidi_scan_file(Path path_fs, TagHandler &handler) noexcept
125
{
126
	midi *wm = WildMidi_Open(path_fs.c_str());
127
	if (wm == nullptr)
128
		return false;
129

130
	const struct _WM_Info *info = WildMidi_GetInfo(wm);
131
	if (info == nullptr) {
132
		WildMidi_Close(wm);
133
		return false;
134 135
	}

136 137
	handler.OnAudioFormat(wildmidi_audio_format);

138 139
	const auto duration =
		SongTime::FromScale<uint64_t>(info->approx_total_samples,
140
					      wildmidi_audio_format.sample_rate);
141
	handler.OnDuration(duration);
142 143

	WildMidi_Close(wm);
144

145
	return true;
146 147 148 149
}

static const char *const wildmidi_suffixes[] = {
	"mid",
150
	nullptr
151 152
};

153
const struct DecoderPlugin wildmidi_decoder_plugin = {
154 155 156 157 158 159 160 161 162 163
	"wildmidi",
	wildmidi_init,
	wildmidi_finish,
	nullptr,
	wildmidi_file_decode,
	wildmidi_scan_file,
	nullptr,
	nullptr,
	wildmidi_suffixes,
	nullptr,
164
};