FluidsynthDecoderPlugin.cxx 5.14 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 "FluidsynthDecoderPlugin.hxx"
22
#include "../DecoderAPI.hxx"
23
#include "CheckAudioFormat.hxx"
24
#include "fs/Path.hxx"
25
#include "util/Error.hxx"
26
#include "util/Domain.hxx"
27
#include "util/Macros.hxx"
28
#include "Log.hxx"
29 30 31

#include <fluidsynth.h>

32
static constexpr Domain fluidsynth_domain("fluidsynth");
33

34
static unsigned sample_rate;
35 36
static const char *soundfont_path;

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

	case FLUID_WARN:
49
		return LogLevel::WARNING;
50 51

	case FLUID_INFO:
52
		return LogLevel::INFO;
53 54 55

	case FLUID_DBG:
	case LAST_LOG_LEVEL:
56
		return LogLevel::DEBUG;
57 58 59
	}

	/* invalid fluidsynth log level */
60
	return LogLevel::INFO;
61 62 63
}

/**
64
 * The fluidsynth logging callback.  It forwards messages to the MPD
65 66 67
 * logging library.
 */
static void
68
fluidsynth_mpd_log_function(int level, char *message, gcc_unused void *data)
69
{
70 71 72
	Log(fluidsynth_domain,
	    fluidsynth_level_to_mpd(fluid_log_level(level)),
	    message);
73 74 75
}

static bool
76
fluidsynth_init(const ConfigBlock &block)
77
{
78
	Error error;
79

80
	sample_rate = block.GetBlockValue("sample_rate", 48000u);
81
	if (!audio_check_sample_rate(sample_rate, error)) {
82
		LogError(error);
83 84 85
		return false;
	}

86
	soundfont_path = block.GetBlockValue("soundfont",
87
					     "/usr/share/sounds/sf2/FluidR3_GM.sf2");
88

89
	fluid_set_log_function(LAST_LOG_LEVEL,
90
			       fluidsynth_mpd_log_function, nullptr);
91 92 93 94 95

	return true;
}

static void
96
fluidsynth_file_decode(Decoder &decoder, Path path_fs)
97 98 99 100 101 102 103 104 105 106 107 108 109 110
{
	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();
111
	if (settings == nullptr)
112 113
		return;

114
	fluid_settings_setnum(settings, setting_sample_rate, sample_rate);
115 116 117 118 119 120 121 122

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

	/* create the fluid synth */

	synth = new_fluid_synth(settings);
123
	if (synth == nullptr) {
124 125 126 127 128 129
		delete_fluid_settings(settings);
		return;
	}

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

	/* create the fluid player */

	player = new_fluid_player(synth);
139
	if (player == nullptr) {
140 141 142 143 144
		delete_fluid_synth(synth);
		delete_fluid_settings(settings);
		return;
	}

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

	/* start the player */

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

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

168
	const AudioFormat audio_format(sample_rate, SampleFormat::S16, 2);
169 170
	decoder_initialized(decoder, audio_format, false,
			    SignedSongTime::Negative());
171

172
	DecoderCommand cmd;
173
	while (fluid_player_get_status(player) == FLUID_PLAYER_PLAYING) {
174
		int16_t buffer[2048];
175
		const unsigned max_frames = ARRAY_SIZE(buffer) / 2;
176 177 178 179 180 181 182 183 184 185

		/* 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;

186
		cmd = decoder_data(decoder, nullptr, buffer, sizeof(buffer),
187
				   0);
188
		if (cmd != DecoderCommand::NONE)
189 190
			break;
	}
191 192 193 194 195 196 197 198 199 200 201

	/* clean up */

	fluid_player_stop(player);
	fluid_player_join(player);

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

202
static bool
203
fluidsynth_scan_file(Path path_fs,
204 205
		     gcc_unused const struct tag_handler *handler,
		     gcc_unused void *handler_ctx)
206
{
207
	return fluid_is_midifile(path_fs.c_str());
208 209 210 211
}

static const char *const fluidsynth_suffixes[] = {
	"mid",
212
	nullptr
213 214
};

215
const struct DecoderPlugin fluidsynth_decoder_plugin = {
216 217 218 219 220 221 222 223 224 225
	"fluidsynth",
	fluidsynth_init,
	nullptr,
	nullptr,
	fluidsynth_file_decode,
	fluidsynth_scan_file,
	nullptr,
	nullptr,
	fluidsynth_suffixes,
	nullptr,
226
};