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