2009-02-12 08:43:26 +01:00
|
|
|
/*
|
2015-01-01 19:48:13 +01:00
|
|
|
* Copyright (C) 2003-2015 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
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
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-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.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
|
2013-08-04 23:48:01 +02:00
|
|
|
fluidsynth_mpd_log_function(int level, char *message, gcc_unused void *data)
|
2009-02-12 08:43:26 +01:00
|
|
|
{
|
2013-09-27 22:31:24 +02:00
|
|
|
Log(fluidsynth_domain,
|
|
|
|
fluidsynth_level_to_mpd(fluid_log_level(level)),
|
|
|
|
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
|
|
|
{
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2012-08-15 00:57:32 +02:00
|
|
|
|
2015-01-21 22:13:44 +01:00
|
|
|
sample_rate = block.GetBlockValue("sample_rate", 48000u);
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!audio_check_sample_rate(sample_rate, error)) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(error);
|
2012-08-15 00:57:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
2014-02-07 18:52:19 +01:00
|
|
|
fluidsynth_file_decode(Decoder &decoder, 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);
|
2014-08-29 20:52:39 +02:00
|
|
|
decoder_initialized(decoder, 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;
|
|
|
|
|
2013-07-28 13:12:08 +02:00
|
|
|
cmd = decoder_data(decoder, nullptr, buffer, sizeof(buffer),
|
2010-01-03 22:44:23 +01:00
|
|
|
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,
|
2016-02-23 10:10:13 +01:00
|
|
|
gcc_unused const TagHandler &handler,
|
2013-08-04 23:48:01 +02:00
|
|
|
gcc_unused void *handler_ctx)
|
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
|
|
|
};
|
|
|
|
|
2013-10-21 20:31:34 +02:00
|
|
|
const struct DecoderPlugin fluidsynth_decoder_plugin = {
|
2013-07-28 13:12:08 +02:00
|
|
|
"fluidsynth",
|
|
|
|
fluidsynth_init,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
fluidsynth_file_decode,
|
|
|
|
fluidsynth_scan_file,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
fluidsynth_suffixes,
|
|
|
|
nullptr,
|
2009-02-12 08:43:26 +01:00
|
|
|
};
|