]> git.sesse.net Git - vlc/blob - modules/codec/fluidsynth.c
5f605e674f21f01b8d7c8e3d1b275be8a42bc7d7
[vlc] / modules / codec / fluidsynth.c
1 /*****************************************************************************
2  * fluidsynth.c: Software MIDI synthesizer using libfluidsynth
3  *****************************************************************************
4  * Copyright © 2007 Rémi Denis-Courmont
5  * $Id$
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
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 General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20  *****************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include <vlc_common.h>
27 #include <vlc_plugin.h>
28 #include <vlc_codec.h>
29 #include <vlc_dialog.h>
30 #include <vlc_charset.h>
31
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 #ifdef _POSIX_VERSION
36 # include <glob.h>
37 #endif
38
39 /* On Win32, we link statically */
40 #ifdef WIN32
41 # define FLUIDSYNTH_NOT_A_DLL
42 #endif
43
44 #include <fluidsynth.h>
45
46 #define SOUNDFONT_TEXT N_("Sound fonts")
47 #define SOUNDFONT_LONGTEXT N_( \
48     "A sound fonts file is required for software synthesis." )
49
50 #define GAIN_TEXT N_("Synthesis gain")
51 #define GAIN_LONGTEXT N_("This gain is applied to synthesis output. " \
52     "High values may cause saturation when many notes are played at a time." )
53
54 #define POLYPHONY_TEXT N_("Polyphony")
55 #define POLYPHONY_LONGTEXT N_( \
56     "The polyphony defines how many voices can be played at a time. " \
57     "Larger values require more processing power.")
58
59 #define SAMPLE_RATE_TEXT N_("Sample rate")
60
61 static int  Open  (vlc_object_t *);
62 static void Close (vlc_object_t *);
63
64 vlc_module_begin ()
65     set_description (N_("FluidSynth MIDI synthesizer"))
66     set_capability ("decoder", 100)
67     set_shortname (N_("FluidSynth"))
68     set_category (CAT_INPUT)
69     set_subcategory (SUBCAT_INPUT_ACODEC)
70     set_callbacks (Open, Close)
71     add_loadfile ("soundfont", "",
72                   SOUNDFONT_TEXT, SOUNDFONT_LONGTEXT, false)
73     add_float ("synth-gain", 0.8, GAIN_TEXT, GAIN_LONGTEXT, false)
74         change_float_range (0., 10.)
75     add_integer ("synth-polyphony", 256,
76                  POLYPHONY_TEXT, POLYPHONY_LONGTEXT, false)
77         change_integer_range (1, 65535)
78     add_integer ("synth-sample-rate", 44100,
79                  SAMPLE_RATE_TEXT, SAMPLE_RATE_TEXT, true)
80         change_integer_range (22050, 96000)
81 vlc_module_end ()
82
83
84 struct decoder_sys_t
85 {
86     fluid_settings_t *settings;
87     fluid_synth_t    *synth;
88     int               soundfont;
89     date_t            end_date;
90 };
91
92
93 static aout_buffer_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block);
94
95
96 static int Open (vlc_object_t *p_this)
97 {
98     decoder_t *p_dec = (decoder_t *)p_this;
99
100     if (p_dec->fmt_in.i_codec != VLC_CODEC_MIDI)
101         return VLC_EGENERIC;
102
103     decoder_sys_t *p_sys = malloc (sizeof (*p_sys));
104     if (unlikely(p_sys == NULL))
105         return VLC_ENOMEM;
106
107     p_sys->settings = new_fluid_settings ();
108     p_sys->synth = new_fluid_synth (p_sys->settings);
109     p_sys->soundfont = -1;
110
111     char *font_path = var_InheritString (p_this, "soundfont");
112     if (font_path != NULL)
113     {
114         const char *lpath = ToLocale (font_path);
115
116         msg_Dbg (p_this, "loading sound fonts file %s", font_path);
117         p_sys->soundfont = fluid_synth_sfload (p_sys->synth, font_path, 1);
118         LocaleFree (lpath);
119         if (p_sys->soundfont == -1)
120             msg_Err (p_this, "cannot load sound fonts file %s", font_path);
121         free (font_path);
122     }
123 #ifdef _POSIX_VERSION
124     else
125     {
126         glob_t gl;
127
128         glob ("/usr/share/sounds/sf2/*.sf2", GLOB_NOESCAPE, NULL, &gl);
129         for (size_t i = 0; i < gl.gl_pathc; i++)
130         {
131             const char *path = gl.gl_pathv[i];
132
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);
138         }
139         globfree (&gl);
140     }
141 #endif
142
143     if (p_sys->soundfont == -1)
144     {
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);
153         free (p_sys);
154         return VLC_EGENERIC;
155     }
156
157     fluid_synth_set_gain (p_sys->synth,
158                           var_InheritFloat (p_this, "synth-gain"));
159     fluid_synth_set_polyphony (p_sys->synth,
160                                var_InheritInteger (p_this, "synth-polyphony"));
161
162     p_dec->fmt_out.i_cat = AUDIO_ES;
163     p_dec->fmt_out.audio.i_rate =
164         var_InheritInteger (p_this, "synth-sample-rate");;
165     fluid_synth_set_sample_rate (p_sys->synth, p_dec->fmt_out.audio.i_rate);
166     p_dec->fmt_out.audio.i_channels = 2;
167     p_dec->fmt_out.audio.i_original_channels =
168     p_dec->fmt_out.audio.i_physical_channels =
169         AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
170     p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
171     p_dec->fmt_out.audio.i_bitspersample = 32;
172     date_Init (&p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1);
173     date_Set (&p_sys->end_date, 0);
174
175     p_dec->p_sys = p_sys;
176     p_dec->pf_decode_audio = DecodeBlock;
177     return VLC_SUCCESS;
178 }
179
180
181 static void Close (vlc_object_t *p_this)
182 {
183     decoder_sys_t *p_sys = ((decoder_t *)p_this)->p_sys;
184
185     fluid_synth_sfunload (p_sys->synth, p_sys->soundfont, 1);
186     delete_fluid_synth (p_sys->synth);
187     delete_fluid_settings (p_sys->settings);
188     free (p_sys);
189 }
190
191
192 static aout_buffer_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block)
193 {
194     block_t *p_block;
195     decoder_sys_t *p_sys = p_dec->p_sys;
196     aout_buffer_t *p_out = NULL;
197
198     if (pp_block == NULL)
199         return NULL;
200     p_block = *pp_block;
201     if (p_block == NULL)
202         return NULL;
203     *pp_block = NULL;
204
205     if (p_block->i_pts > VLC_TS_INVALID && !date_Get (&p_sys->end_date))
206         date_Set (&p_sys->end_date, p_block->i_pts);
207     else
208     if (p_block->i_pts < date_Get (&p_sys->end_date))
209     {
210         msg_Warn (p_dec, "MIDI message in the past?");
211         goto drop;
212     }
213
214     if (p_block->i_buffer < 1)
215         goto drop;
216
217     uint8_t event = p_block->p_buffer[0];
218     uint8_t channel = p_block->p_buffer[0] & 0xf;
219     event &= 0xF0;
220
221     if (event == 0xF0)
222         switch (channel)
223         {
224             case 0:
225                 if (p_block->p_buffer[p_block->i_buffer - 1] != 0xF7)
226                 {
227             case 7:
228                     msg_Warn (p_dec, "fragmented SysEx not implemented");
229                     goto drop;
230                 }
231                 fluid_synth_sysex (p_sys->synth, (char *)p_block->p_buffer + 1,
232                                    p_block->i_buffer - 2, NULL, NULL, NULL, 0);
233                 break;
234             case 0xF:
235                 fluid_synth_system_reset (p_sys->synth);
236                 break;
237         }
238
239     uint8_t p1 = (p_block->i_buffer > 1) ? (p_block->p_buffer[1] & 0x7f) : 0;
240     uint8_t p2 = (p_block->i_buffer > 2) ? (p_block->p_buffer[2] & 0x7f) : 0;
241
242     switch (event & 0xF0)
243     {
244         case 0x80:
245             fluid_synth_noteoff (p_sys->synth, channel, p1);
246             break;
247         case 0x90:
248             fluid_synth_noteon (p_sys->synth, channel, p1, p2);
249             break;
250         /*case 0xA0: note aftertouch not implemented */
251         case 0xB0:
252             fluid_synth_cc (p_sys->synth, channel, p1, p2);
253             break;
254         case 0xC0:
255             fluid_synth_program_change (p_sys->synth, channel, p1);
256             break;
257         case 0xD0:
258             fluid_synth_channel_pressure (p_sys->synth, channel, p1);
259             break;
260         case 0xE0:
261             fluid_synth_pitch_bend (p_sys->synth, channel, (p2 << 7) | p1);
262             break;
263     }
264
265     unsigned samples =
266         (p_block->i_pts - date_Get (&p_sys->end_date)) * 441 / 10000;
267     if (samples == 0)
268         goto drop;
269
270     p_out = decoder_NewAudioBuffer (p_dec, samples);
271     if (p_out == NULL)
272         goto drop;
273
274     p_out->i_pts = date_Get (&p_sys->end_date );
275     p_out->i_length = date_Increment (&p_sys->end_date, samples)
276                       - p_out->i_pts;
277     fluid_synth_write_float (p_sys->synth, samples, p_out->p_buffer, 0, 2,
278                              p_out->p_buffer, 1, 2);
279 drop:
280     block_Release (p_block);
281     return p_out;
282 }