WildmidiDecoderPlugin.cxx 3.53 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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/Error.hxx"
25
#include "util/Domain.hxx"
26
#include "fs/AllocatedPath.hxx"
27
#include "fs/FileSystem.hxx"
28
#include "fs/Path.hxx"
29
#include "system/FatalError.hxx"
30
#include "Log.hxx"
31

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

36
static constexpr Domain wildmidi_domain("wildmidi");
37

38
static constexpr unsigned WILDMIDI_SAMPLE_RATE = 48000;
39 40

static bool
41
wildmidi_init(const config_param &param)
42
{
43
	Error error;
44 45 46 47
	const AllocatedPath path =
		param.GetBlockPath("config_file",
				   "/etc/timidity/timidity.cfg",
				   error);
48 49 50 51 52
	if (path.IsNull())
		FatalError(error);

	if (!FileExists(path)) {
		const auto utf8 = path.ToUTF8();
53 54 55
		FormatDebug(wildmidi_domain,
			    "configuration file does not exist: %s",
			    utf8.c_str());
56 57
		return false;
	}
58

59
	return WildMidi_Init(path.c_str(), WILDMIDI_SAMPLE_RATE, 0) == 0;
60 61 62 63 64 65 66 67 68
}

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

static void
69
wildmidi_file_decode(Decoder &decoder, Path path_fs)
70
{
71
	static constexpr AudioFormat audio_format = {
72
		WILDMIDI_SAMPLE_RATE,
73
		SampleFormat::S16,
74
		2,
75 76
	};
	midi *wm;
77
	const struct _WM_Info *info;
78

79
	wm = WildMidi_Open(path_fs.c_str());
80
	if (wm == nullptr)
81 82
		return;

83
	info = WildMidi_GetInfo(wm);
84
	if (info == nullptr) {
85 86 87 88
		WildMidi_Close(wm);
		return;
	}

89
	decoder_initialized(decoder, audio_format, true,
90
			    info->approx_total_samples / WILDMIDI_SAMPLE_RATE);
91

92
	DecoderCommand cmd;
93 94 95 96
	do {
		char buffer[4096];
		int len;

97
		info = WildMidi_GetInfo(wm);
98
		if (info == nullptr)
99 100
			break;

101 102 103 104
		len = WildMidi_GetOutput(wm, buffer, sizeof(buffer));
		if (len <= 0)
			break;

105
		cmd = decoder_data(decoder, nullptr, buffer, len, 0);
106

107
		if (cmd == DecoderCommand::SEEK) {
108 109 110
			unsigned long seek_where = WILDMIDI_SAMPLE_RATE *
				decoder_seek_where(decoder);

111
			WildMidi_FastSeek(wm, &seek_where);
112
			decoder_command_finished(decoder);
113
			cmd = DecoderCommand::NONE;
114 115
		}

116
	} while (cmd == DecoderCommand::NONE);
117 118 119 120

	WildMidi_Close(wm);
}

121
static bool
122
wildmidi_scan_file(Path path_fs,
123
		   const struct tag_handler *handler, void *handler_ctx)
124
{
125
	midi *wm = WildMidi_Open(path_fs.c_str());
126
	if (wm == nullptr)
127
		return false;
128

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

135 136
	int duration = info->approx_total_samples / WILDMIDI_SAMPLE_RATE;
	tag_handler_invoke_duration(handler, handler_ctx, duration);
137 138

	WildMidi_Close(wm);
139

140
	return true;
141 142 143 144
}

static const char *const wildmidi_suffixes[] = {
	"mid",
145
	nullptr
146 147
};

148
const struct DecoderPlugin wildmidi_decoder_plugin = {
149 150 151 152 153 154 155 156 157 158
	"wildmidi",
	wildmidi_init,
	wildmidi_finish,
	nullptr,
	wildmidi_file_decode,
	wildmidi_scan_file,
	nullptr,
	nullptr,
	wildmidi_suffixes,
	nullptr,
159
};