1 /*****************************************************************************
2 * fluidsynth.c: Software MIDI synthesizer using libfluidsynth
3 *****************************************************************************
4 * Copyright © 2007 Rémi Denis-Courmont
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20 *****************************************************************************/
26 #include <vlc_common.h>
27 #include <vlc_plugin.h>
28 #include <vlc_codec.h>
29 #include <vlc_dialog.h>
36 /* On Win32, we link statically */
38 # define FLUIDSYNTH_NOT_A_DLL
41 #include <fluidsynth.h>
43 #define SOUNDFONT_TEXT N_("Sound fonts")
44 #define SOUNDFONT_LONGTEXT N_( \
45 "A sound fonts file is required for software synthesis." )
47 #define CHORUS_TEXT N_("Chorus")
49 #define GAIN_TEXT N_("Synthesis gain")
50 #define GAIN_LONGTEXT N_("This gain is applied to synthesis output. " \
51 "High values may cause saturation when many notes are played at a time." )
53 #define POLYPHONY_TEXT N_("Polyphony")
54 #define POLYPHONY_LONGTEXT N_( \
55 "The polyphony defines how many voices can be played at a time. " \
56 "Larger values require more processing power.")
58 #define REVERB_TEXT N_("Reverb")
60 #define SAMPLE_RATE_TEXT N_("Sample rate")
62 static int Open (vlc_object_t *);
63 static void Close (vlc_object_t *);
66 set_description (N_("FluidSynth MIDI synthesizer"))
67 set_capability ("decoder", 100)
68 set_shortname (N_("FluidSynth"))
69 set_category (CAT_INPUT)
70 set_subcategory (SUBCAT_INPUT_ACODEC)
71 set_callbacks (Open, Close)
72 add_loadfile ("soundfont", "",
73 SOUNDFONT_TEXT, SOUNDFONT_LONGTEXT, false)
74 add_bool ("synth-chorus", true, CHORUS_TEXT, CHORUS_TEXT, false)
75 add_float ("synth-gain", .5, GAIN_TEXT, GAIN_LONGTEXT, false)
76 change_float_range (0., 10.)
77 add_integer ("synth-polyphony", 256,
78 POLYPHONY_TEXT, POLYPHONY_LONGTEXT, false)
79 change_integer_range (1, 65535)
80 add_bool ("synth-reverb", true, REVERB_TEXT, REVERB_TEXT, true)
81 add_integer ("synth-sample-rate", 44100,
82 SAMPLE_RATE_TEXT, SAMPLE_RATE_TEXT, true)
83 change_integer_range (22050, 96000)
89 fluid_settings_t *settings;
96 static block_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block);
99 static int Open (vlc_object_t *p_this)
101 decoder_t *p_dec = (decoder_t *)p_this;
103 if (p_dec->fmt_in.i_codec != VLC_CODEC_MIDI)
106 decoder_sys_t *p_sys = malloc (sizeof (*p_sys));
107 if (unlikely(p_sys == NULL))
110 p_sys->settings = new_fluid_settings ();
111 p_sys->synth = new_fluid_synth (p_sys->settings);
112 p_sys->soundfont = -1;
114 char *font_path = var_InheritString (p_this, "soundfont");
115 if (font_path != NULL)
117 msg_Dbg (p_this, "loading sound fonts file %s", font_path);
118 p_sys->soundfont = fluid_synth_sfload (p_sys->synth, font_path, 1);
119 if (p_sys->soundfont == -1)
120 msg_Err (p_this, "cannot load sound fonts file %s", font_path);
123 #ifdef _POSIX_VERSION
128 glob ("/usr/share/sounds/sf2/*.sf2", GLOB_NOESCAPE, NULL, &gl);
129 for (size_t i = 0; i < gl.gl_pathc; i++)
131 const char *path = gl.gl_pathv[i];
133 msg_Dbg (p_this, "loading sound fonts file %s", path);
134 p_sys->soundfont = fluid_synth_sfload (p_sys->synth, path, 1);
135 if (p_sys->soundfont != -1)
136 break; /* it worked! */
137 msg_Err (p_this, "cannot load sound fonts file %s", path);
143 if (p_sys->soundfont == -1)
145 msg_Err (p_this, "sound font file required for synthesis");
146 dialog_Fatal (p_this, _("MIDI synthesis not set up"),
147 _("A sound font file (.SF2) is required for MIDI synthesis.\n"
148 "Please install a sound font and configure it "
149 "from the VLC preferences "
150 "(Input / Codecs > Audio codecs > FluidSynth).\n"));
151 delete_fluid_synth (p_sys->synth);
152 delete_fluid_settings (p_sys->settings);
157 fluid_synth_set_chorus_on (p_sys->synth,
158 var_InheritBool (p_this, "synth-chorus"));
159 fluid_synth_set_gain (p_sys->synth,
160 var_InheritFloat (p_this, "synth-gain"));
161 fluid_synth_set_polyphony (p_sys->synth,
162 var_InheritInteger (p_this, "synth-polyphony"));
163 fluid_synth_set_reverb_on (p_sys->synth,
164 var_InheritBool (p_this, "synth-reverb"));
166 p_dec->fmt_out.i_cat = AUDIO_ES;
167 p_dec->fmt_out.audio.i_rate =
168 var_InheritInteger (p_this, "synth-sample-rate");;
169 fluid_synth_set_sample_rate (p_sys->synth, p_dec->fmt_out.audio.i_rate);
170 p_dec->fmt_out.audio.i_channels = 2;
171 p_dec->fmt_out.audio.i_original_channels =
172 p_dec->fmt_out.audio.i_physical_channels =
173 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
174 p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
175 p_dec->fmt_out.audio.i_bitspersample = 32;
176 date_Init (&p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1);
177 date_Set (&p_sys->end_date, 0);
179 p_dec->p_sys = p_sys;
180 p_dec->pf_decode_audio = DecodeBlock;
185 static void Close (vlc_object_t *p_this)
187 decoder_sys_t *p_sys = ((decoder_t *)p_this)->p_sys;
189 fluid_synth_sfunload (p_sys->synth, p_sys->soundfont, 1);
190 delete_fluid_synth (p_sys->synth);
191 delete_fluid_settings (p_sys->settings);
196 static block_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block)
199 decoder_sys_t *p_sys = p_dec->p_sys;
200 block_t *p_out = NULL;
202 if (pp_block == NULL)
209 if (p_block->i_pts > VLC_TS_INVALID && !date_Get (&p_sys->end_date))
210 date_Set (&p_sys->end_date, p_block->i_pts);
212 if (p_block->i_pts < date_Get (&p_sys->end_date))
214 msg_Warn (p_dec, "MIDI message in the past?");
218 if (p_block->i_buffer < 1)
221 uint8_t event = p_block->p_buffer[0];
222 uint8_t channel = p_block->p_buffer[0] & 0xf;
229 if (p_block->p_buffer[p_block->i_buffer - 1] != 0xF7)
232 msg_Warn (p_dec, "fragmented SysEx not implemented");
235 fluid_synth_sysex (p_sys->synth, (char *)p_block->p_buffer + 1,
236 p_block->i_buffer - 2, NULL, NULL, NULL, 0);
239 fluid_synth_system_reset (p_sys->synth);
243 uint8_t p1 = (p_block->i_buffer > 1) ? (p_block->p_buffer[1] & 0x7f) : 0;
244 uint8_t p2 = (p_block->i_buffer > 2) ? (p_block->p_buffer[2] & 0x7f) : 0;
246 switch (event & 0xF0)
249 fluid_synth_noteoff (p_sys->synth, channel, p1);
252 fluid_synth_noteon (p_sys->synth, channel, p1, p2);
254 /*case 0xA0: note aftertouch not implemented */
256 fluid_synth_cc (p_sys->synth, channel, p1, p2);
259 fluid_synth_program_change (p_sys->synth, channel, p1);
262 fluid_synth_channel_pressure (p_sys->synth, channel, p1);
265 fluid_synth_pitch_bend (p_sys->synth, channel, (p2 << 7) | p1);
270 (p_block->i_pts - date_Get (&p_sys->end_date)) * 441 / 10000;
274 p_out = decoder_NewAudioBuffer (p_dec, samples);
278 p_out->i_pts = date_Get (&p_sys->end_date );
279 p_out->i_length = date_Increment (&p_sys->end_date, samples)
281 fluid_synth_write_float (p_sys->synth, samples, p_out->p_buffer, 0, 2,
282 p_out->p_buffer, 1, 2);
284 block_Release (p_block);