FluidsynthDecoderPlugin.cxx 4.96 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 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 "FluidsynthDecoderPlugin.hxx"
21
#include "../DecoderAPI.hxx"
22
#include "CheckAudioFormat.hxx"
23
#include "fs/Path.hxx"
24 25
#include "util/Domain.hxx"
#include "Log.hxx"
26 27 28

#include <fluidsynth.h>

29
static constexpr Domain fluidsynth_domain("fluidsynth");
30

31
static unsigned sample_rate;
32 33
static const char *soundfont_path;

34
/**
35
 * Convert a fluidsynth log level to a MPD log level.
36
 */
37 38
static LogLevel
fluidsynth_level_to_mpd(enum fluid_log_level level)
39 40 41 42
{
	switch (level) {
	case FLUID_PANIC:
	case FLUID_ERR:
43
		return LogLevel::ERROR;
44 45

	case FLUID_WARN:
46
		return LogLevel::WARNING;
47 48

	case FLUID_INFO:
49
		return LogLevel::INFO;
50 51 52

	case FLUID_DBG:
	case LAST_LOG_LEVEL:
53
		return LogLevel::DEBUG;
54 55 56
	}

	/* invalid fluidsynth log level */
57
	return LogLevel::INFO;
58 59 60
}

/**
61
 * The fluidsynth logging callback.  It forwards messages to the MPD
62 63 64
 * logging library.
 */
static void
65 66 67 68 69 70
fluidsynth_mpd_log_function(int level,
#if FLUIDSYNTH_VERSION_MAJOR >= 2
			    const
#endif
			    char *message,
			    void *)
71
{
72 73
	Log(fluidsynth_level_to_mpd(fluid_log_level(level)),
	    fluidsynth_domain,
74
	    message);
75 76 77
}

static bool
78
fluidsynth_init(const ConfigBlock &block)
79
{
80
	sample_rate = block.GetPositiveValue("sample_rate", 48000u);
81
	CheckSampleRate(sample_rate);
82

83
	soundfont_path = block.GetBlockValue("soundfont",
84
					     "/usr/share/sounds/sf2/FluidR3_GM.sf2");
85

86
	fluid_set_log_function(LAST_LOG_LEVEL,
87
			       fluidsynth_mpd_log_function, nullptr);
88 89 90 91 92

	return true;
}

static void
93
fluidsynth_file_decode(DecoderClient &client, Path path_fs)
94 95 96 97 98 99 100 101 102 103 104 105 106 107
{
	char setting_sample_rate[] = "synth.sample-rate";
	/*
	char setting_verbose[] = "synth.verbose";
	char setting_yes[] = "yes";
	*/
	fluid_settings_t *settings;
	fluid_synth_t *synth;
	fluid_player_t *player;
	int ret;

	/* set up fluid settings */

	settings = new_fluid_settings();
108
	if (settings == nullptr)
109 110
		return;

111
	fluid_settings_setnum(settings, setting_sample_rate, sample_rate);
112 113 114 115 116 117 118 119

	/*
	fluid_settings_setstr(settings, setting_verbose, setting_yes);
	*/

	/* create the fluid synth */

	synth = new_fluid_synth(settings);
120
	if (synth == nullptr) {
121 122 123 124 125 126
		delete_fluid_settings(settings);
		return;
	}

	ret = fluid_synth_sfload(synth, soundfont_path, true);
	if (ret < 0) {
127
		LogWarning(fluidsynth_domain, "fluid_synth_sfload() failed");
128 129 130 131 132 133 134 135
		delete_fluid_synth(synth);
		delete_fluid_settings(settings);
		return;
	}

	/* create the fluid player */

	player = new_fluid_player(synth);
136
	if (player == nullptr) {
137 138 139 140 141
		delete_fluid_synth(synth);
		delete_fluid_settings(settings);
		return;
	}

142
	ret = fluid_player_add(player, path_fs.c_str());
143
	if (ret != 0) {
144
		LogWarning(fluidsynth_domain, "fluid_player_add() failed");
145 146 147 148 149 150 151 152 153 154
		delete_fluid_player(player);
		delete_fluid_synth(synth);
		delete_fluid_settings(settings);
		return;
	}

	/* start the player */

	ret = fluid_player_play(player);
	if (ret != 0) {
155
		LogWarning(fluidsynth_domain, "fluid_player_play() failed");
156 157 158 159 160 161 162 163 164
		delete_fluid_player(player);
		delete_fluid_synth(synth);
		delete_fluid_settings(settings);
		return;
	}

	/* initialization complete - announce the audio format to the
	   MPD core */

165
	const AudioFormat audio_format(sample_rate, SampleFormat::S16, 2);
166
	client.Ready(audio_format, false, SignedSongTime::Negative());
167

168
	DecoderCommand cmd;
169
	while (fluid_player_get_status(player) == FLUID_PLAYER_PLAYING) {
170
		int16_t buffer[2048];
171
		const unsigned max_frames = std::size(buffer) / 2;
172 173 174 175 176 177 178 179 180 181

		/* read samples from fluidsynth and send them to the
		   MPD core */

		ret = fluid_synth_write_s16(synth, max_frames,
					    buffer, 0, 2,
					    buffer, 1, 2);
		if (ret != 0)
			break;

182
		cmd = client.SubmitData(nullptr, buffer, sizeof(buffer), 0);
183
		if (cmd != DecoderCommand::NONE)
184 185
			break;
	}
186 187 188 189 190 191 192 193 194 195 196

	/* clean up */

	fluid_player_stop(player);
	fluid_player_join(player);

	delete_fluid_player(player);
	delete_fluid_synth(synth);
	delete_fluid_settings(settings);
}

197
static bool
198
fluidsynth_scan_file(Path path_fs,
199
		     gcc_unused TagHandler &handler) noexcept
200
{
201
	return fluid_is_midifile(path_fs.c_str());
202 203 204 205
}

static const char *const fluidsynth_suffixes[] = {
	"mid",
206
	nullptr
207 208
};

209 210 211 212 213
constexpr DecoderPlugin fluidsynth_decoder_plugin =
	DecoderPlugin("fluidsynth",
		      fluidsynth_file_decode, fluidsynth_scan_file)
	.WithInit(fluidsynth_init)
	.WithSuffixes(fluidsynth_suffixes);