]> git.sesse.net Git - vlc/blobdiff - modules/codec/fluidsynth.c
live555 : enable/disable tracks on the fly
[vlc] / modules / codec / fluidsynth.c
index 6d1e3562f68f9fe0778eb58e07eaefa53a0cb939..7c3ee9b8c23c81675cf173c0f40931ef95d4b04e 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * fluidsynth.c: Software MIDI synthetizer using libfluidsynth
+ * fluidsynth.c: Software MIDI synthesizer using libfluidsynth
  *****************************************************************************
  * Copyright © 2007 Rémi Denis-Courmont
  * $Id$
 #include <vlc_dialog.h>
 #include <vlc_charset.h>
 
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+#ifdef _POSIX_VERSION
+# include <glob.h>
+#endif
+
 /* On Win32, we link statically */
 #ifdef WIN32
 # define FLUIDSYNTH_NOT_A_DLL
@@ -46,7 +53,7 @@
 # define fluid_synth_channel_pressure(synth, channel, p) (FLUID_FAILED)
 #endif
 
-#define SOUNDFONT_TEXT N_("Sound fonts (required)")
+#define SOUNDFONT_TEXT N_("Sound fonts")
 #define SOUNDFONT_LONGTEXT N_( \
     "A sound fonts file is required for software synthesis." )
 
@@ -54,7 +61,7 @@ static int  Open  (vlc_object_t *);
 static void Close (vlc_object_t *);
 
 vlc_module_begin ()
-    set_description (N_("FluidSynth MIDI synthetizer"))
+    set_description (N_("FluidSynth MIDI synthesizer"))
     set_capability ("decoder", 100)
     set_shortname (N_("FluidSynth"))
     set_category (CAT_INPUT)
@@ -81,49 +88,63 @@ static aout_buffer_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block);
 static int Open (vlc_object_t *p_this)
 {
     decoder_t *p_dec = (decoder_t *)p_this;
-    decoder_sys_t *p_sys;
 
     if (p_dec->fmt_in.i_codec != VLC_CODEC_MIDI)
         return VLC_EGENERIC;
 
+    decoder_sys_t *p_sys = malloc (sizeof (*p_sys));
+    if (unlikely(p_sys == NULL))
+        return VLC_ENOMEM;
+
+    p_sys->settings = new_fluid_settings ();
+    p_sys->synth = new_fluid_synth (p_sys->settings);
+    p_sys->soundfont = -1;
+
     char *font_path = var_InheritString (p_this, "soundfont");
-    if (font_path == NULL)
+    if (font_path != NULL)
     {
-        msg_Err (p_this, "sound font file required for synthesis");
-        dialog_Fatal (p_this, _("MIDI synthesis not set up"),
-            _("A sound font file (.SF2) is required for MIDI synthesis.\n"
-              "Please install a sound font and configure it "
-              "from the VLC preferences (Codecs / Audio / FluidSynth).\n"));
-        return VLC_EGENERIC;
-    }
+        const char *lpath = ToLocale (font_path);
 
-    p_dec->pf_decode_audio = DecodeBlock;
-    p_sys = p_dec->p_sys = malloc (sizeof (*p_sys));
-    if (p_sys == NULL)
-    {
+        p_sys->soundfont = fluid_synth_sfload (p_sys->synth, font_path, 1);
+        LocaleFree (lpath);
+        if (p_sys->soundfont == -1)
+            msg_Err (p_this, "cannot load sound fonts file %s", font_path);
         free (font_path);
-        return VLC_ENOMEM;
     }
+#ifdef _POSIX_VERSION
+    else
+    {
+        glob_t gl;
+
+        if (!glob ("/usr/share/sounds/sf2/*.sf2", GLOB_NOESCAPE, NULL, &gl))
+        {
+            for (size_t i = 0; i < gl.gl_pathc; i++)
+            {
+                const char *path = gl.gl_pathv[i];
+
+                p_sys->soundfont = fluid_synth_sfload (p_sys->synth, path, 1);
+                if (p_sys->soundfont != -1)
+                    break; /* it worked! */
+                msg_Err (p_this, "cannot load sound fonts file %s", path);
+            }
+            globfree (&gl);
+        }
+    }
+#endif
 
-    p_sys->settings = new_fluid_settings ();
-    p_sys->synth = new_fluid_synth (p_sys->settings);
-    /* FIXME: I bet this is not thread-safe */
-    const char *lpath = ToLocale (font_path);
-    p_sys->soundfont = fluid_synth_sfload (p_sys->synth, font_path, 1);
-    LocaleFree (lpath);
     if (p_sys->soundfont == -1)
     {
-        msg_Err (p_this, "cannot load sound fonts file %s", font_path);
-        Close (p_this);
+        msg_Err (p_this, "sound font file required for synthesis");
         dialog_Fatal (p_this, _("MIDI synthesis not set up"),
-            _("The specified sound font file (%s) is incorrect.\n"
-              "Please install a valid sound font and reconfigure it "
-              "from the VLC preferences (Codecs / Audio / FluidSynth).\n"),
-              font_path);
-        free (font_path);
+            _("A sound font file (.SF2) is required for MIDI synthesis.\n"
+              "Please install a sound font and configure it "
+              "from the VLC preferences "
+              "(Input / Codecs > Audio codecs > FluidSynth).\n"));
+        delete_fluid_synth (p_sys->synth);
+        delete_fluid_settings (p_sys->settings);
+        free (p_sys);
         return VLC_EGENERIC;
     }
-    free (font_path);
 
     p_dec->fmt_out.i_cat = AUDIO_ES;
     p_dec->fmt_out.audio.i_rate = 44100;
@@ -146,6 +167,8 @@ static int Open (vlc_object_t *p_this)
     date_Init (&p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1);
     date_Set (&p_sys->end_date, 0);
 
+    p_dec->p_sys = p_sys;
+    p_dec->pf_decode_audio = DecodeBlock;
     return VLC_SUCCESS;
 }
 
@@ -154,8 +177,7 @@ static void Close (vlc_object_t *p_this)
 {
     decoder_sys_t *p_sys = ((decoder_t *)p_this)->p_sys;
 
-    if (p_sys->soundfont != -1)
-        fluid_synth_sfunload (p_sys->synth, p_sys->soundfont, 1);
+    fluid_synth_sfunload (p_sys->synth, p_sys->soundfont, 1);
     delete_fluid_synth (p_sys->synth);
     delete_fluid_settings (p_sys->settings);
     free (p_sys);