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