2009-02-12 08:43:26 +01:00
|
|
|
/*
|
2018-10-31 17:54:59 +01:00
|
|
|
* Copyright 2003-2018 The Music Player Daemon Project
|
2009-02-12 08:43:26 +01:00
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-02-12 08:43:26 +01:00
|
|
|
*/
|
|
|
|
|
2013-07-28 13:12:08 +02:00
|
|
|
#include "FluidsynthDecoderPlugin.hxx"
|
2014-01-24 00:02:24 +01:00
|
|
|
#include "../DecoderAPI.hxx"
|
2013-07-29 07:50:08 +02:00
|
|
|
#include "CheckAudioFormat.hxx"
|
2014-02-07 18:52:19 +01:00
|
|
|
#include "fs/Path.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "util/Domain.hxx"
|
2013-10-15 22:04:17 +02:00
|
|
|
#include "util/Macros.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2009-02-12 08:43:26 +01:00
|
|
|
|
|
|
|
#include <fluidsynth.h>
|
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
static constexpr Domain fluidsynth_domain("fluidsynth");
|
2009-02-12 08:43:26 +01:00
|
|
|
|
2012-08-15 00:57:32 +02:00
|
|
|
static unsigned sample_rate;
|
2012-08-15 00:51:45 +02:00
|
|
|
static const char *soundfont_path;
|
|
|
|
|
2009-02-12 08:43:26 +01:00
|
|
|
/**
|
2014-12-04 23:33:08 +01:00
|
|
|
* Convert a fluidsynth log level to a MPD log level.
|
2009-02-12 08:43:26 +01:00
|
|
|
*/
|
2013-09-27 22:31:24 +02:00
|
|
|
static LogLevel
|
|
|
|
fluidsynth_level_to_mpd(enum fluid_log_level level)
|
2009-02-12 08:43:26 +01:00
|
|
|
{
|
|
|
|
switch (level) {
|
|
|
|
case FLUID_PANIC:
|
|
|
|
case FLUID_ERR:
|
2013-09-27 22:31:24 +02:00
|
|
|
return LogLevel::ERROR;
|
2009-02-12 08:43:26 +01:00
|
|
|
|
|
|
|
case FLUID_WARN:
|
2013-09-27 22:31:24 +02:00
|
|
|
return LogLevel::WARNING;
|
2009-02-12 08:43:26 +01:00
|
|
|
|
|
|
|
case FLUID_INFO:
|
2013-09-27 22:31:24 +02:00
|
|
|
return LogLevel::INFO;
|
2009-02-12 08:43:26 +01:00
|
|
|
|
|
|
|
case FLUID_DBG:
|
|
|
|
case LAST_LOG_LEVEL:
|
2013-09-27 22:31:24 +02:00
|
|
|
return LogLevel::DEBUG;
|
2009-02-12 08:43:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* invalid fluidsynth log level */
|
2013-09-27 22:31:24 +02:00
|
|
|
return LogLevel::INFO;
|
2009-02-12 08:43:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-04 23:33:08 +01:00
|
|
|
* The fluidsynth logging callback. It forwards messages to the MPD
|
2009-02-12 08:43:26 +01:00
|
|
|
* logging library.
|
|
|
|
*/
|
|
|
|
static void
|
2018-10-23 18:44:28 +02:00
|
|
|
fluidsynth_mpd_log_function(int level,
|
|
|
|
#if FLUIDSYNTH_VERSION_MAJOR >= 2
|
|
|
|
const
|
|
|
|
#endif
|
|
|
|
char *message,
|
|
|
|
void *)
|
2009-02-12 08:43:26 +01:00
|
|
|
{
|
2019-05-22 18:38:25 +02:00
|
|
|
Log(fluidsynth_level_to_mpd(fluid_log_level(level)),
|
|
|
|
fluidsynth_domain,
|
2013-09-27 22:31:24 +02:00
|
|
|
message);
|
2009-02-12 08:43:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2015-01-21 22:13:44 +01:00
|
|
|
fluidsynth_init(const ConfigBlock &block)
|
2009-02-12 08:43:26 +01:00
|
|
|
{
|
2018-01-02 17:22:25 +01:00
|
|
|
sample_rate = block.GetPositiveValue("sample_rate", 48000u);
|
2016-11-10 11:45:17 +01:00
|
|
|
CheckSampleRate(sample_rate);
|
2012-08-15 00:57:32 +02:00
|
|
|
|
2015-01-21 22:13:44 +01:00
|
|
|
soundfont_path = block.GetBlockValue("soundfont",
|
2013-08-04 11:30:26 +02:00
|
|
|
"/usr/share/sounds/sf2/FluidR3_GM.sf2");
|
2012-08-15 00:51:45 +02:00
|
|
|
|
2009-02-12 08:43:26 +01:00
|
|
|
fluid_set_log_function(LAST_LOG_LEVEL,
|
2013-07-28 13:12:08 +02:00
|
|
|
fluidsynth_mpd_log_function, nullptr);
|
2009-02-12 08:43:26 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-11-18 07:13:35 +01:00
|
|
|
fluidsynth_file_decode(DecoderClient &client, Path path_fs)
|
2009-02-12 08:43:26 +01:00
|
|
|
{
|
|
|
|
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();
|
2013-07-28 13:12:08 +02:00
|
|
|
if (settings == nullptr)
|
2009-02-12 08:43:26 +01:00
|
|
|
return;
|
|
|
|
|
2012-08-15 00:57:32 +02:00
|
|
|
fluid_settings_setnum(settings, setting_sample_rate, sample_rate);
|
2009-02-12 08:43:26 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
fluid_settings_setstr(settings, setting_verbose, setting_yes);
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* create the fluid synth */
|
|
|
|
|
|
|
|
synth = new_fluid_synth(settings);
|
2013-07-28 13:12:08 +02:00
|
|
|
if (synth == nullptr) {
|
2009-02-12 08:43:26 +01:00
|
|
|
delete_fluid_settings(settings);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = fluid_synth_sfload(synth, soundfont_path, true);
|
|
|
|
if (ret < 0) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogWarning(fluidsynth_domain, "fluid_synth_sfload() failed");
|
2009-02-12 08:43:26 +01:00
|
|
|
delete_fluid_synth(synth);
|
|
|
|
delete_fluid_settings(settings);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create the fluid player */
|
|
|
|
|
|
|
|
player = new_fluid_player(synth);
|
2013-07-28 13:12:08 +02:00
|
|
|
if (player == nullptr) {
|
2009-02-12 08:43:26 +01:00
|
|
|
delete_fluid_synth(synth);
|
|
|
|
delete_fluid_settings(settings);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-07 18:52:19 +01:00
|
|
|
ret = fluid_player_add(player, path_fs.c_str());
|
2009-02-12 08:43:26 +01:00
|
|
|
if (ret != 0) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogWarning(fluidsynth_domain, "fluid_player_add() failed");
|
2009-02-12 08:43:26 +01:00
|
|
|
delete_fluid_player(player);
|
|
|
|
delete_fluid_synth(synth);
|
|
|
|
delete_fluid_settings(settings);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start the player */
|
|
|
|
|
|
|
|
ret = fluid_player_play(player);
|
|
|
|
if (ret != 0) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogWarning(fluidsynth_domain, "fluid_player_play() failed");
|
2009-02-12 08:43:26 +01:00
|
|
|
delete_fluid_player(player);
|
|
|
|
delete_fluid_synth(synth);
|
|
|
|
delete_fluid_settings(settings);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialization complete - announce the audio format to the
|
|
|
|
MPD core */
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
const AudioFormat audio_format(sample_rate, SampleFormat::S16, 2);
|
2016-11-18 07:59:01 +01:00
|
|
|
client.Ready(audio_format, false, SignedSongTime::Negative());
|
2009-02-12 08:43:26 +01:00
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand cmd;
|
2012-08-15 00:47:10 +02:00
|
|
|
while (fluid_player_get_status(player) == FLUID_PLAYER_PLAYING) {
|
2009-02-12 08:43:26 +01:00
|
|
|
int16_t buffer[2048];
|
2013-10-15 22:04:17 +02:00
|
|
|
const unsigned max_frames = ARRAY_SIZE(buffer) / 2;
|
2009-02-12 08:43:26 +01:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2016-11-18 08:27:30 +01:00
|
|
|
cmd = client.SubmitData(nullptr, buffer, sizeof(buffer), 0);
|
2013-09-27 12:11:37 +02:00
|
|
|
if (cmd != DecoderCommand::NONE)
|
2012-08-15 00:47:10 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-02-12 08:43:26 +01:00
|
|
|
|
|
|
|
/* clean up */
|
|
|
|
|
|
|
|
fluid_player_stop(player);
|
|
|
|
fluid_player_join(player);
|
|
|
|
|
|
|
|
delete_fluid_player(player);
|
|
|
|
delete_fluid_synth(synth);
|
|
|
|
delete_fluid_settings(settings);
|
|
|
|
}
|
|
|
|
|
2012-02-11 19:26:41 +01:00
|
|
|
static bool
|
2014-02-07 18:52:19 +01:00
|
|
|
fluidsynth_scan_file(Path path_fs,
|
2018-07-05 19:07:05 +02:00
|
|
|
gcc_unused TagHandler &handler) noexcept
|
2009-02-12 08:43:26 +01:00
|
|
|
{
|
2014-02-07 18:52:19 +01:00
|
|
|
return fluid_is_midifile(path_fs.c_str());
|
2009-02-12 08:43:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *const fluidsynth_suffixes[] = {
|
|
|
|
"mid",
|
2013-07-28 13:12:08 +02:00
|
|
|
nullptr
|
2009-02-12 08:43:26 +01:00
|
|
|
};
|
|
|
|
|
2019-06-15 14:44:37 +02:00
|
|
|
constexpr DecoderPlugin fluidsynth_decoder_plugin =
|
|
|
|
DecoderPlugin("fluidsynth",
|
|
|
|
fluidsynth_file_decode, fluidsynth_scan_file)
|
|
|
|
.WithInit(fluidsynth_init)
|
|
|
|
.WithSuffixes(fluidsynth_suffixes);
|