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