From e56a90f3b343217671327aba2ecdca8709463331 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 12 Feb 2009 08:43:26 +0100 Subject: fluidsynth: new decoder plugin for MIDI files There are a few problems left in this plugin: - fluidsynth decodes in real time, while MPD prefers to buffer as quickly as possible; as a workaround, this plugin uses a timer object to synchronize with real-time playback - I don't know yet how fluidsynth tells me when the song has ended - the "soundfont" configuration setting is not yet documented, and it will likely change soon (in favor of a per-decoder configuration block) --- INSTALL | 3 + NEWS | 1 + configure.ac | 22 +++- src/Makefile.am | 6 ++ src/decoder/fluidsynth_plugin.c | 233 ++++++++++++++++++++++++++++++++++++++++ src/decoder_list.c | 4 + 6 files changed, 268 insertions(+), 1 deletion(-) create mode 100644 src/decoder/fluidsynth_plugin.c diff --git a/INSTALL b/INSTALL index e18ea515..994479cd 100644 --- a/INSTALL +++ b/INSTALL @@ -98,6 +98,9 @@ Multi-codec library. libsidplay2 - http://sidplay2.sourceforge.net/ For C64 SID support. +libfluidsynth - http://fluidsynth.resonance.org/ +For MIDI support. + Optional Miscellaneous Dependencies ----------------------------------- diff --git a/NEWS b/NEWS index 5d48ee85..8e6f6af9 100644 --- a/NEWS +++ b/NEWS @@ -11,6 +11,7 @@ ver 0.15 - (200?/??/??) - modplug: another MOD plugin, based on libmodplug - mikmod disabled by default, due to severe security issues in libmikmod - sidplay: new decoder plugin for C64 SID (using libsidplay2) + - fluidsynth: new decoder plugin for MIDI files (using libfluidsynth) * audio outputs: - shout: enlarged buffer size to 32 kB - null: allow disabling synchronization diff --git a/configure.ac b/configure.ac index 8fd2977f..84ffce57 100644 --- a/configure.ac +++ b/configure.ac @@ -417,6 +417,11 @@ AC_ARG_ENABLE(sidplay, [enable C64 SID support via libsidplay2 (default: disable)]),, enable_sidplay=no) +AC_ARG_ENABLE(fluidsynth, + AS_HELP_STRING([--enable-fluidsynth], + [enable MIDI support via fluidsynth (default: disable)]),, + enable_fluidsynth=no) + AC_ARG_ENABLE(wavpack, AS_HELP_STRING([--disable-wavpack], [disable WavPack support (default: enable)]), @@ -1018,13 +1023,21 @@ if test x$enable_sidplay = xyes; then AC_SUBST(SIDPLAY_LIBS,"-lsidplay2 -lresid-builder") AC_SUBST(SIDPLAY_CFLAGS,) - AC_DEFINE(ENABLE_SIDPLAY, 1, [Define for libsidplay2 support]), + AC_DEFINE(ENABLE_SIDPLAY, 1, [Define for libsidplay2 support]) else AM_CONDITIONAL(am__fastdepCXX, false) fi AM_CONDITIONAL(ENABLE_SIDPLAY, test x$enable_sidplay = xyes) +if test x$enable_fluidsynth = xyes; then + PKG_CHECK_MODULES(FLUIDSYNTH, [fluidsynth], + AC_DEFINE(ENABLE_FLUIDSYNTH, 1, [Define for fluidsynth support]), + enable_fluidsynth=no) +fi + +AM_CONDITIONAL(ENABLE_FLUIDSYNTH, test x$enable_fluidsynth = xyes) + dnl dnl Documentation @@ -1310,6 +1323,12 @@ else echo " C64 SID support ...............disabled" fi +if test x$enable_fluidsynth = xyes; then + echo " fluidsynth MIDI support .......enabled" +else + echo " fluidsynth MIDI support .......disabled" +fi + if test x$enable_ffmpeg = xyes; then echo " FFMPEG support ................enabled" else @@ -1328,6 +1347,7 @@ if test x$enable_ffmpeg = xno && test x$enable_modplug = xno && test x$enable_sidplay = xno && + test x$enable_fluidsynth = xno && test x$enable_mod = xno; then AC_MSG_ERROR([No input plugins supported!]) fi diff --git a/src/Makefile.am b/src/Makefile.am index 8170badb..935f8b3d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -12,6 +12,7 @@ mpd_CPPFLAGS = \ $(AUDIOFILE_CFLAGS) $(LIBMIKMOD_CFLAGS) \ $(MODPLUG_CFLAGS) \ $(SIDPLAY_CFLAGS) \ + $(FLUIDSYNTH_CFLAGS) \ $(ID3TAG_CFLAGS) \ $(MAD_CFLAGS) \ $(FFMPEG_CFLAGS) \ @@ -26,6 +27,7 @@ mpd_LDADD = $(MPD_LIBS) \ $(AUDIOFILE_LIBS) $(LIBMIKMOD_LIBS) \ $(MODPLUG_LIBS) \ $(SIDPLAY_LIBS) \ + $(FLUIDSYNTH_LIBS) \ $(ID3TAG_LIBS) \ $(MAD_LIBS) \ $(MP4FF_LIBS) \ @@ -317,6 +319,10 @@ if ENABLE_SIDPLAY mpd_SOURCES += decoder/sidplay_plugin.cxx endif +if ENABLE_FLUIDSYNTH +mpd_SOURCES += decoder/fluidsynth_plugin.c +endif + if HAVE_FFMPEG mpd_SOURCES += decoder/ffmpeg_plugin.c endif diff --git a/src/decoder/fluidsynth_plugin.c b/src/decoder/fluidsynth_plugin.c new file mode 100644 index 00000000..3a744d2f --- /dev/null +++ b/src/decoder/fluidsynth_plugin.c @@ -0,0 +1,233 @@ +/* + * Copyright (C) 2003-2009 The Music Player Daemon Project + * 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. + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "../decoder_api.h" +#include "../timer.h" +#include "../conf.h" + +#include + +#include + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "fluidsynth" + +/** + * Convert a fluidsynth log level to a GLib log level. + */ +static GLogLevelFlags +fluidsynth_level_to_glib(enum fluid_log_level level) +{ + switch (level) { + case FLUID_PANIC: + case FLUID_ERR: + return G_LOG_LEVEL_CRITICAL; + + case FLUID_WARN: + return G_LOG_LEVEL_WARNING; + + case FLUID_INFO: + return G_LOG_LEVEL_INFO; + + case FLUID_DBG: + case LAST_LOG_LEVEL: + return G_LOG_LEVEL_DEBUG; + } + + /* invalid fluidsynth log level */ + return G_LOG_LEVEL_MESSAGE; +} + +/** + * The fluidsynth logging callback. It forwards messages to the GLib + * logging library. + */ +static void +fluidsynth_mpd_log_function(int level, char *message, G_GNUC_UNUSED void *data) +{ + g_log(G_LOG_DOMAIN, fluidsynth_level_to_glib(level), "%s", message); +} + +static bool +fluidsynth_init(void) +{ + fluid_set_log_function(LAST_LOG_LEVEL, + fluidsynth_mpd_log_function, NULL); + + return true; +} + +static void +fluidsynth_file_decode(struct decoder *decoder, const char *path_fs) +{ + static const struct audio_format audio_format = { + .sample_rate = 48000, + .bits = 16, + .channels = 2, + }; + char setting_sample_rate[] = "synth.sample-rate"; + /* + char setting_verbose[] = "synth.verbose"; + char setting_yes[] = "yes"; + */ + const char *soundfont_path; + fluid_settings_t *settings; + fluid_synth_t *synth; + fluid_player_t *player; + char *path_dup; + int ret; + Timer *timer; + enum decoder_command cmd; + + soundfont_path = + config_get_string("soundfont", + "/usr/share/sounds/sf2/FluidR3_GM.sf2"); + + /* set up fluid settings */ + + settings = new_fluid_settings(); + if (settings == NULL) + return; + + fluid_settings_setnum(settings, setting_sample_rate, 48000); + + /* + fluid_settings_setstr(settings, setting_verbose, setting_yes); + */ + + /* create the fluid synth */ + + synth = new_fluid_synth(settings); + if (synth == NULL) { + delete_fluid_settings(settings); + return; + } + + ret = fluid_synth_sfload(synth, soundfont_path, true); + if (ret < 0) { + g_warning("fluid_synth_sfload() failed"); + delete_fluid_synth(synth); + delete_fluid_settings(settings); + return; + } + + /* create the fluid player */ + + player = new_fluid_player(synth); + if (player == NULL) { + delete_fluid_synth(synth); + delete_fluid_settings(settings); + return; + } + + /* temporarily duplicate the path_fs string, because + fluidsynth wants a writable string */ + path_dup = g_strdup(path_fs); + ret = fluid_player_add(player, path_dup); + g_free(path_dup); + if (ret != 0) { + g_warning("fluid_player_add() failed"); + delete_fluid_player(player); + delete_fluid_synth(synth); + delete_fluid_settings(settings); + return; + } + + /* start the player */ + + ret = fluid_player_play(player); + if (ret != 0) { + g_warning("fluid_player_play() failed"); + delete_fluid_player(player); + delete_fluid_synth(synth); + delete_fluid_settings(settings); + return; + } + + /* set up a timer for synchronization; fluidsynth always + decodes in real time, which forces us to synchronize */ + /* XXX is there any way to switch off real-time decoding? */ + + timer = timer_new(&audio_format); + timer_start(timer); + + /* initialization complete - announce the audio format to the + MPD core */ + + decoder_initialized(decoder, &audio_format, false, -1); + + do { + int16_t buffer[2048]; + const unsigned max_frames = G_N_ELEMENTS(buffer) / 2; + + /* synchronize with the fluid player */ + + timer_add(timer, sizeof(buffer)); + timer_sync(timer); + + /* 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); + /* XXX how do we see whether the player is done? We + can't access the private attribute + player->status */ + if (ret != 0) + break; + + cmd = decoder_data(decoder, NULL, buffer, sizeof(buffer), + 0, 0, NULL); + } while (cmd == DECODE_COMMAND_NONE); + + /* clean up */ + + timer_free(timer); + + fluid_player_stop(player); + fluid_player_join(player); + + delete_fluid_player(player); + delete_fluid_synth(synth); + delete_fluid_settings(settings); +} + +static struct tag * +fluidsynth_tag_dup(const char *file) +{ + struct tag *tag = tag_new(); + + /* to be implemented */ + (void)file; + + return tag; +} + +static const char *const fluidsynth_suffixes[] = { + "mid", + NULL +}; + +const struct decoder_plugin fluidsynth_decoder_plugin = { + .name = "fluidsynth", + .init = fluidsynth_init, + .file_decode = fluidsynth_file_decode, + .tag_dup = fluidsynth_tag_dup, + .suffixes = fluidsynth_suffixes, +}; diff --git a/src/decoder_list.c b/src/decoder_list.c index 96429067..e7f757e7 100644 --- a/src/decoder_list.c +++ b/src/decoder_list.c @@ -35,6 +35,7 @@ extern const struct decoder_plugin wavpack_plugin; extern const struct decoder_plugin modplug_plugin; extern const struct decoder_plugin mikmod_decoder_plugin; extern const struct decoder_plugin sidplay_decoder_plugin; +extern const struct decoder_plugin fluidsynth_decoder_plugin; extern const struct decoder_plugin ffmpeg_plugin; static const struct decoder_plugin *const decoder_plugins[] = { @@ -74,6 +75,9 @@ static const struct decoder_plugin *const decoder_plugins[] = { #ifdef ENABLE_SIDPLAY &sidplay_decoder_plugin, #endif +#ifdef ENABLE_FLUIDSYNTH + &fluidsynth_decoder_plugin, +#endif #ifdef HAVE_FFMPEG &ffmpeg_plugin, #endif -- cgit v1.2.3