]> git.sesse.net Git - vlc/blob - modules/codec/fluidsynth.c
macosx: save 19 l10n strings by replacing them with already present strings from...
[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 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.
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 Lesser General Public License for more details.
16  *
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  *****************************************************************************/
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
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
34 #ifdef _POSIX_VERSION
35 # include <glob.h>
36 #endif
37
38 /* On Win32, we link statically */
39 #ifdef WIN32
40 # define FLUIDSYNTH_NOT_A_DLL
41 #endif
42
43 #include <fluidsynth.h>
44
45 #define SOUNDFONT_TEXT N_("Sound fonts")
46 #define SOUNDFONT_LONGTEXT N_( \
47     "A sound fonts file is required for software synthesis." )
48
49 #define CHORUS_TEXT N_("Chorus")
50
51 #define GAIN_TEXT N_("Synthesis gain")
52 #define GAIN_LONGTEXT N_("This gain is applied to synthesis output. " \
53     "High values may cause saturation when many notes are played at a time." )
54
55 #define POLYPHONY_TEXT N_("Polyphony")
56 #define POLYPHONY_LONGTEXT N_( \
57     "The polyphony defines how many voices can be played at a time. " \
58     "Larger values require more processing power.")
59
60 #define REVERB_TEXT N_("Reverb")
61
62 #define SAMPLE_RATE_TEXT N_("Sample rate")
63
64 static int  Open  (vlc_object_t *);
65 static void Close (vlc_object_t *);
66
67 vlc_module_begin ()
68     set_description (N_("FluidSynth MIDI synthesizer"))
69     set_capability ("decoder", 100)
70     set_shortname (N_("FluidSynth"))
71     set_category (CAT_INPUT)
72     set_subcategory (SUBCAT_INPUT_ACODEC)
73     set_callbacks (Open, Close)
74     add_loadfile ("soundfont", "",
75                   SOUNDFONT_TEXT, SOUNDFONT_LONGTEXT, false)
76     add_bool ("synth-chorus", true, CHORUS_TEXT, CHORUS_TEXT, false)
77     add_float ("synth-gain", .5, GAIN_TEXT, GAIN_LONGTEXT, false)
78         change_float_range (0., 10.)
79     add_integer ("synth-polyphony", 256,
80                  POLYPHONY_TEXT, POLYPHONY_LONGTEXT, false)
81         change_integer_range (1, 65535)
82     add_bool ("synth-reverb", true, REVERB_TEXT, REVERB_TEXT, true)
83     add_integer ("synth-sample-rate", 44100,
84                  SAMPLE_RATE_TEXT, SAMPLE_RATE_TEXT, true)
85         change_integer_range (22050, 96000)
86 vlc_module_end ()
87
88
89 struct decoder_sys_t
90 {
91     fluid_settings_t *settings;
92     fluid_synth_t    *synth;
93     int               soundfont;
94     date_t            end_date;
95 };
96
97
98 static block_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block);
99
100
101 static int Open (vlc_object_t *p_this)
102 {
103     decoder_t *p_dec = (decoder_t *)p_this;
104
105     if (p_dec->fmt_in.i_codec != VLC_CODEC_MIDI)
106         return VLC_EGENERIC;
107
108     decoder_sys_t *p_sys = malloc (sizeof (*p_sys));
109     if (unlikely(p_sys == NULL))
110         return VLC_ENOMEM;
111
112     p_sys->settings = new_fluid_settings ();
113     p_sys->synth = new_fluid_synth (p_sys->settings);
114     p_sys->soundfont = -1;
115
116     char *font_path = var_InheritString (p_this, "soundfont");
117     if (font_path != NULL)
118     {
119         msg_Dbg (p_this, "loading sound fonts file %s", font_path);
120         p_sys->soundfont = fluid_synth_sfload (p_sys->synth, font_path, 1);
121         if (p_sys->soundfont == -1)
122             msg_Err (p_this, "cannot load sound fonts file %s", font_path);
123         free (font_path);
124     }
125 #ifdef _POSIX_VERSION
126     else
127     {
128         glob_t gl;
129
130         glob ("/usr/share/sounds/sf2/*.sf2", GLOB_NOESCAPE, NULL, &gl);
131         for (size_t i = 0; i < gl.gl_pathc; i++)
132         {
133             const char *path = gl.gl_pathv[i];
134
135             msg_Dbg (p_this, "loading sound fonts file %s", path);
136             p_sys->soundfont = fluid_synth_sfload (p_sys->synth, path, 1);
137             if (p_sys->soundfont != -1)
138                 break; /* it worked! */
139             msg_Err (p_this, "cannot load sound fonts file %s", path);
140         }
141         globfree (&gl);
142     }
143 #endif
144
145     if (p_sys->soundfont == -1)
146     {
147         msg_Err (p_this, "sound font file required for synthesis");
148         dialog_Fatal (p_this, _("MIDI synthesis not set up"),
149             _("A sound font file (.SF2) is required for MIDI synthesis.\n"
150               "Please install a sound font and configure it "
151               "from the VLC preferences "
152               "(Input / Codecs > Audio codecs > FluidSynth).\n"));
153         delete_fluid_synth (p_sys->synth);
154         delete_fluid_settings (p_sys->settings);
155         free (p_sys);
156         return VLC_EGENERIC;
157     }
158
159     fluid_synth_set_chorus_on (p_sys->synth,
160                                var_InheritBool (p_this, "synth-chorus"));
161     fluid_synth_set_gain (p_sys->synth,
162                           var_InheritFloat (p_this, "synth-gain"));
163     fluid_synth_set_polyphony (p_sys->synth,
164                                var_InheritInteger (p_this, "synth-polyphony"));
165     fluid_synth_set_reverb_on (p_sys->synth,
166                                var_InheritBool (p_this, "synth-reverb"));
167
168     p_dec->fmt_out.i_cat = AUDIO_ES;
169     p_dec->fmt_out.audio.i_rate =
170         var_InheritInteger (p_this, "synth-sample-rate");;
171     fluid_synth_set_sample_rate (p_sys->synth, p_dec->fmt_out.audio.i_rate);
172     p_dec->fmt_out.audio.i_channels = 2;
173     p_dec->fmt_out.audio.i_original_channels =
174     p_dec->fmt_out.audio.i_physical_channels =
175         AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
176     p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
177     p_dec->fmt_out.audio.i_bitspersample = 32;
178     date_Init (&p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1);
179     date_Set (&p_sys->end_date, 0);
180
181     p_dec->p_sys = p_sys;
182     p_dec->pf_decode_audio = DecodeBlock;
183     return VLC_SUCCESS;
184 }
185
186
187 static void Close (vlc_object_t *p_this)
188 {
189     decoder_sys_t *p_sys = ((decoder_t *)p_this)->p_sys;
190
191     fluid_synth_sfunload (p_sys->synth, p_sys->soundfont, 1);
192     delete_fluid_synth (p_sys->synth);
193     delete_fluid_settings (p_sys->settings);
194     free (p_sys);
195 }
196
197
198 static block_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block)
199 {
200     block_t *p_block;
201     decoder_sys_t *p_sys = p_dec->p_sys;
202     block_t *p_out = NULL;
203
204     if (pp_block == NULL)
205         return NULL;
206     p_block = *pp_block;
207     if (p_block == NULL)
208         return NULL;
209     *pp_block = NULL;
210
211     if (p_block->i_pts > VLC_TS_INVALID && !date_Get (&p_sys->end_date))
212         date_Set (&p_sys->end_date, p_block->i_pts);
213     else
214     if (p_block->i_pts < date_Get (&p_sys->end_date))
215     {
216         msg_Warn (p_dec, "MIDI message in the past?");
217         goto drop;
218     }
219
220     if (p_block->i_buffer < 1)
221         goto drop;
222
223     uint8_t event = p_block->p_buffer[0];
224     uint8_t channel = p_block->p_buffer[0] & 0xf;
225     event &= 0xF0;
226
227     if (event == 0xF0)
228         switch (channel)
229         {
230             case 0:
231                 if (p_block->p_buffer[p_block->i_buffer - 1] != 0xF7)
232                 {
233             case 7:
234                     msg_Warn (p_dec, "fragmented SysEx not implemented");
235                     goto drop;
236                 }
237                 fluid_synth_sysex (p_sys->synth, (char *)p_block->p_buffer + 1,
238                                    p_block->i_buffer - 2, NULL, NULL, NULL, 0);
239                 break;
240             case 0xF:
241                 fluid_synth_system_reset (p_sys->synth);
242                 break;
243         }
244
245     uint8_t p1 = (p_block->i_buffer > 1) ? (p_block->p_buffer[1] & 0x7f) : 0;
246     uint8_t p2 = (p_block->i_buffer > 2) ? (p_block->p_buffer[2] & 0x7f) : 0;
247
248     switch (event & 0xF0)
249     {
250         case 0x80:
251             fluid_synth_noteoff (p_sys->synth, channel, p1);
252             break;
253         case 0x90:
254             fluid_synth_noteon (p_sys->synth, channel, p1, p2);
255             break;
256         /*case 0xA0: note aftertouch not implemented */
257         case 0xB0:
258             fluid_synth_cc (p_sys->synth, channel, p1, p2);
259             break;
260         case 0xC0:
261             fluid_synth_program_change (p_sys->synth, channel, p1);
262             break;
263         case 0xD0:
264             fluid_synth_channel_pressure (p_sys->synth, channel, p1);
265             break;
266         case 0xE0:
267             fluid_synth_pitch_bend (p_sys->synth, channel, (p2 << 7) | p1);
268             break;
269     }
270
271     unsigned samples =
272         (p_block->i_pts - date_Get (&p_sys->end_date)) * 441 / 10000;
273     if (samples == 0)
274         goto drop;
275
276     p_out = decoder_NewAudioBuffer (p_dec, samples);
277     if (p_out == NULL)
278         goto drop;
279
280     p_out->i_pts = date_Get (&p_sys->end_date );
281     p_out->i_length = date_Increment (&p_sys->end_date, samples)
282                       - p_out->i_pts;
283     fluid_synth_write_float (p_sys->synth, samples, p_out->p_buffer, 0, 2,
284                              p_out->p_buffer, 1, 2);
285 drop:
286     block_Release (p_block);
287     return p_out;
288 }