FluidsynthDecoderPlugin.cxx 5.08 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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 "util/Error.hxx"
25
#include "util/Domain.hxx"
26
#include "util/Macros.hxx"
27
#include "Log.hxx"
28 29 30

#include <fluidsynth.h>

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

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

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

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

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

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

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

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

static bool
75
fluidsynth_init(const config_param &param)
76
{
77
	Error error;
78

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

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

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

	return true;
}

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

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

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

	/* create the fluid synth */

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

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

	/* create the fluid player */

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

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

	/* start the player */

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

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

167 168
	const AudioFormat audio_format(sample_rate, SampleFormat::S16, 2);
	decoder_initialized(decoder, audio_format, false, -1);
169

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

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

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

	/* clean up */

	fluid_player_stop(player);
	fluid_player_join(player);

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

200 201
static bool
fluidsynth_scan_file(const char *file,
202 203
		     gcc_unused const struct tag_handler *handler,
		     gcc_unused void *handler_ctx)
204
{
205
	return fluid_is_midifile(file);
206 207 208 209
}

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

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