WildmidiDecoderPlugin.cxx 3.63 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 90 91 92 93
	const auto duration =
		SongTime::FromScale<uint64_t>(info->approx_total_samples,
					      WILDMIDI_SAMPLE_RATE);

	decoder_initialized(decoder, audio_format, true, duration);
94

95
	DecoderCommand cmd;
96 97 98 99
	do {
		char buffer[4096];
		int len;

100
		info = WildMidi_GetInfo(wm);
101
		if (info == nullptr)
102 103
			break;

104 105 106 107
		len = WildMidi_GetOutput(wm, buffer, sizeof(buffer));
		if (len <= 0)
			break;

108
		cmd = decoder_data(decoder, nullptr, buffer, len, 0);
109

110
		if (cmd == DecoderCommand::SEEK) {
111 112
			unsigned long seek_where =
				decoder_seek_where_frame(decoder);
113

114
			WildMidi_FastSeek(wm, &seek_where);
115
			decoder_command_finished(decoder);
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,
126
		   const struct tag_handler *handler, void *handler_ctx)
127
{
128
	midi *wm = WildMidi_Open(path_fs.c_str());
129
	if (wm == nullptr)
130
		return false;
131

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

138 139 140
	const auto duration =
		SongTime::FromScale<uint64_t>(info->approx_total_samples,
					      WILDMIDI_SAMPLE_RATE);
141
	tag_handler_invoke_duration(handler, handler_ctx, 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
};