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