]> git.sesse.net Git - vlc/blobdiff - modules/codec/fluidsynth.c
Use _WIN32 rather than WIN32 (same for WIN64)
[vlc] / modules / codec / fluidsynth.c
index e8d82226236f4ae98c88aa4fd6fcb99c373d32f6..49d5ab146065198b3d1ab8fee992c5f4f1980076 100644 (file)
@@ -4,19 +4,19 @@
  * Copyright © 2007 Rémi Denis-Courmont
  * $Id$
  *
- * 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
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 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.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser 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.
  *****************************************************************************/
 
 #ifdef HAVE_CONFIG_H
 
 #include <vlc_common.h>
 #include <vlc_plugin.h>
-#include <vlc_aout.h>
 #include <vlc_codec.h>
-#include <vlc_cpu.h>
 #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
+#ifdef _WIN32
 # define FLUIDSYNTH_NOT_A_DLL
 #endif
 
 #include <fluidsynth.h>
 
-#if (FLUIDSYNTH_VERSION_MAJOR < 1) \
- || (FLUIDSYNTH_VERSION_MAJOR == 1 && FLUIDSYNTH_VERSION_MINOR < 1)
-# define FLUID_FAILED (-1)
-# define fluid_synth_sysex(synth, ptr, len, d, e, f, g) (FLUID_FAILED)
-# define fluid_synth_system_reset(synth) (FLUID_FAILED)
-# 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." )
 
+#define CHORUS_TEXT N_("Chorus")
+
+#define GAIN_TEXT N_("Synthesis gain")
+#define GAIN_LONGTEXT N_("This gain is applied to synthesis output. " \
+    "High values may cause saturation when many notes are played at a time." )
+
+#define POLYPHONY_TEXT N_("Polyphony")
+#define POLYPHONY_LONGTEXT N_( \
+    "The polyphony defines how many voices can be played at a time. " \
+    "Larger values require more processing power.")
+
+#define REVERB_TEXT N_("Reverb")
+
+#define SAMPLE_RATE_TEXT N_("Sample rate")
+
 static int  Open  (vlc_object_t *);
 static void Close (vlc_object_t *);
 
@@ -61,7 +72,17 @@ vlc_module_begin ()
     set_subcategory (SUBCAT_INPUT_ACODEC)
     set_callbacks (Open, Close)
     add_loadfile ("soundfont", "",
-                  SOUNDFONT_TEXT, SOUNDFONT_LONGTEXT, false);
+                  SOUNDFONT_TEXT, SOUNDFONT_LONGTEXT, false)
+    add_bool ("synth-chorus", true, CHORUS_TEXT, CHORUS_TEXT, false)
+    add_float ("synth-gain", .5, GAIN_TEXT, GAIN_LONGTEXT, false)
+        change_float_range (0., 10.)
+    add_integer ("synth-polyphony", 256,
+                 POLYPHONY_TEXT, POLYPHONY_LONGTEXT, false)
+        change_integer_range (1, 65535)
+    add_bool ("synth-reverb", true, REVERB_TEXT, REVERB_TEXT, true)
+    add_integer ("synth-sample-rate", 44100,
+                 SAMPLE_RATE_TEXT, SAMPLE_RATE_TEXT, true)
+        change_integer_range (22050, 96000)
 vlc_module_end ()
 
 
@@ -70,12 +91,11 @@ struct decoder_sys_t
     fluid_settings_t *settings;
     fluid_synth_t    *synth;
     int               soundfont;
-    bool              fixed;
     date_t            end_date;
 };
 
 
-static aout_buffer_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block);
+static block_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block);
 
 
 static int Open (vlc_object_t *p_this)
@@ -96,14 +116,31 @@ static int Open (vlc_object_t *p_this)
     char *font_path = var_InheritString (p_this, "soundfont");
     if (font_path != NULL)
     {
-        const char *lpath = ToLocale (font_path);
-
+        msg_Dbg (p_this, "loading sound fonts file %s", 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);
         free (font_path);
     }
+#ifdef _POSIX_VERSION
+    else
+    {
+        glob_t gl;
+
+        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];
+
+            msg_Dbg (p_this, "loading sound fonts file %s", path);
+            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
 
     if (p_sys->soundfont == -1)
     {
@@ -119,24 +156,25 @@ static int Open (vlc_object_t *p_this)
         return VLC_EGENERIC;
     }
 
+    fluid_synth_set_chorus_on (p_sys->synth,
+                               var_InheritBool (p_this, "synth-chorus"));
+    fluid_synth_set_gain (p_sys->synth,
+                          var_InheritFloat (p_this, "synth-gain"));
+    fluid_synth_set_polyphony (p_sys->synth,
+                               var_InheritInteger (p_this, "synth-polyphony"));
+    fluid_synth_set_reverb_on (p_sys->synth,
+                               var_InheritBool (p_this, "synth-reverb"));
+
     p_dec->fmt_out.i_cat = AUDIO_ES;
-    p_dec->fmt_out.audio.i_rate = 44100;
+    p_dec->fmt_out.audio.i_rate =
+        var_InheritInteger (p_this, "synth-sample-rate");;
+    fluid_synth_set_sample_rate (p_sys->synth, p_dec->fmt_out.audio.i_rate);
     p_dec->fmt_out.audio.i_channels = 2;
     p_dec->fmt_out.audio.i_original_channels =
     p_dec->fmt_out.audio.i_physical_channels =
         AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
-    if (HAVE_FPU)
-    {
-        p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
-        p_dec->fmt_out.audio.i_bitspersample = 32;
-        p_sys->fixed = false;
-    }
-    else
-    {
-        p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
-        p_dec->fmt_out.audio.i_bitspersample = 16;
-        p_sys->fixed = true;
-    }
+    p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
+    p_dec->fmt_out.audio.i_bitspersample = 32;
     date_Init (&p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1);
     date_Set (&p_sys->end_date, 0);
 
@@ -157,11 +195,11 @@ static void Close (vlc_object_t *p_this)
 }
 
 
-static aout_buffer_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block)
+static block_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block)
 {
     block_t *p_block;
     decoder_sys_t *p_sys = p_dec->p_sys;
-    aout_buffer_t *p_out = NULL;
+    block_t *p_out = NULL;
 
     if (pp_block == NULL)
         return NULL;
@@ -242,14 +280,8 @@ static aout_buffer_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block)
     p_out->i_pts = date_Get (&p_sys->end_date );
     p_out->i_length = date_Increment (&p_sys->end_date, samples)
                       - p_out->i_pts;
-    if (!p_sys->fixed)
-        fluid_synth_write_float (p_sys->synth, samples,
-                                 p_out->p_buffer, 0, 2,
-                                 p_out->p_buffer, 1, 2);
-    else
-        fluid_synth_write_s16 (p_sys->synth, samples,
-                               (int16_t *)p_out->p_buffer, 0, 2,
-                               (int16_t *)p_out->p_buffer, 1, 2);
+    fluid_synth_write_float (p_sys->synth, samples, p_out->p_buffer, 0, 2,
+                             p_out->p_buffer, 1, 2);
 drop:
     block_Release (p_block);
     return p_out;