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