WildmidiDecoderPlugin.cxx 3.69 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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/Handler.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 AudioFormat wildmidi_audio_format{48000, SampleFormat::S16, 2};
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 54
	return WildMidi_Init(path.c_str(), wildmidi_audio_format.sample_rate,
			     0) == 0;
55 56 57
}

static void
58
wildmidi_finish() noexcept
59 60 61 62
{
	WildMidi_Shutdown();
}

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

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

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

81
static void
82
wildmidi_file_decode(DecoderClient &client, Path path_fs)
83 84
{
	midi *wm;
85
	const struct _WM_Info *info;
86

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

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

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

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

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

109
		cmd = wildmidi_output(client, wm);
110

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

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

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

	WildMidi_Close(wm);
}

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

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

137 138
	handler.OnAudioFormat(wildmidi_audio_format);

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

	WildMidi_Close(wm);
145

146
	return true;
147 148 149 150
}

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

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