]> git.sesse.net Git - vlc/blob - modules/codec/fluidsynth.c
mediacodec: fix warning
[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 it
8  * under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * 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_codec.h>
29 #include <vlc_dialog.h>
30
31 #include <unistd.h>
32 #ifdef _POSIX_VERSION
33 # include <glob.h>
34 #endif
35
36 /* On Win32, we link statically */
37 #ifdef _WIN32
38 # define FLUIDSYNTH_NOT_A_DLL
39 #endif
40
41 #include <fluidsynth.h>
42
43 #define SOUNDFONT_TEXT N_("Sound fonts")
44 #define SOUNDFONT_LONGTEXT N_( \
45     "A sound fonts file is required for software synthesis." )
46
47 #define CHORUS_TEXT N_("Chorus")
48
49 #define GAIN_TEXT N_("Synthesis gain")
50 #define GAIN_LONGTEXT N_("This gain is applied to synthesis output. " \
51     "High values may cause saturation when many notes are played at a time." )
52
53 #define POLYPHONY_TEXT N_("Polyphony")
54 #define POLYPHONY_LONGTEXT N_( \
55     "The polyphony defines how many voices can be played at a time. " \
56     "Larger values require more processing power.")
57
58 #define REVERB_TEXT N_("Reverb")
59
60 #define SAMPLE_RATE_TEXT N_("Sample rate")
61
62 static int  Open  (vlc_object_t *);
63 static void Close (vlc_object_t *);
64
65 vlc_module_begin ()
66     set_description (N_("FluidSynth MIDI synthesizer"))
67     set_capability ("decoder", 100)
68     set_shortname (N_("FluidSynth"))
69     set_category (CAT_INPUT)
70     set_subcategory (SUBCAT_INPUT_ACODEC)
71     set_callbacks (Open, Close)
72     add_loadfile ("soundfont", "",
73                   SOUNDFONT_TEXT, SOUNDFONT_LONGTEXT, false)
74     add_bool ("synth-chorus", true, CHORUS_TEXT, CHORUS_TEXT, false)
75     add_float ("synth-gain", .5, GAIN_TEXT, GAIN_LONGTEXT, false)
76         change_float_range (0., 10.)
77     add_integer ("synth-polyphony", 256,
78                  POLYPHONY_TEXT, POLYPHONY_LONGTEXT, false)
79         change_integer_range (1, 65535)
80     add_bool ("synth-reverb", true, REVERB_TEXT, REVERB_TEXT, true)
81     add_integer ("synth-sample-rate", 44100,
82                  SAMPLE_RATE_TEXT, SAMPLE_RATE_TEXT, true)
83         change_integer_range (22050, 96000)
84 vlc_module_end ()
85
86
87 struct decoder_sys_t
88 {
89     fluid_settings_t *settings;
90     fluid_synth_t    *synth;
91     int               soundfont;
92     date_t            end_date;
93 };
94
95
96 static block_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block);
97
98
99 static int Open (vlc_object_t *p_this)
100 {
101     decoder_t *p_dec = (decoder_t *)p_this;
102
103     if (p_dec->fmt_in.i_codec != VLC_CODEC_MIDI)
104         return VLC_EGENERIC;
105
106     decoder_sys_t *p_sys = malloc (sizeof (*p_sys));
107     if (unlikely(p_sys == NULL))
108         return VLC_ENOMEM;
109
110     p_sys->settings = new_fluid_settings ();
111     p_sys->synth = new_fluid_synth (p_sys->settings);
112     p_sys->soundfont = -1;
113
114     char *font_path = var_InheritString (p_this, "soundfont");
115     if (font_path != NULL)
116     {
117         msg_Dbg (p_this, "loading sound fonts file %s", font_path);
118         p_sys->soundfont = fluid_synth_sfload (p_sys->synth, font_path, 1);
119         if (p_sys->soundfont == -1)
120             msg_Err (p_this, "cannot load sound fonts file %s", font_path);
121         free (font_path);
122     }
123 #ifdef _POSIX_VERSION
124     else
125     {
126         glob_t gl;
127
128         glob ("/usr/share/sounds/sf2/*.sf2", GLOB_NOESCAPE, NULL, &gl);
129         for (size_t i = 0; i < gl.gl_pathc; i++)
130         {
131             const char *path = gl.gl_pathv[i];
132
133             msg_Dbg (p_this, "loading sound fonts file %s", path);
134             p_sys->soundfont = fluid_synth_sfload (p_sys->synth, path, 1);
135             if (p_sys->soundfont != -1)
136                 break; /* it worked! */
137             msg_Err (p_this, "cannot load sound fonts file %s", path);
138         }
139         globfree (&gl);
140     }
141 #endif
142
143     if (p_sys->soundfont == -1)
144     {
145         msg_Err (p_this, "sound font file required for synthesis");
146         dialog_Fatal (p_this, _("MIDI synthesis not set up"),
147             _("A sound font file (.SF2) is required for MIDI synthesis.\n"
148               "Please install a sound font and configure it "
149               "from the VLC preferences "
150               "(Input / Codecs > Audio codecs > FluidSynth).\n"));
151         delete_fluid_synth (p_sys->synth);
152         delete_fluid_settings (p_sys->settings);
153         free (p_sys);
154         return VLC_EGENERIC;
155     }
156
157     fluid_synth_set_chorus_on (p_sys->synth,
158                                var_InheritBool (p_this, "synth-chorus"));
159     fluid_synth_set_gain (p_sys->synth,
160                           var_InheritFloat (p_this, "synth-gain"));
161     fluid_synth_set_polyphony (p_sys->synth,
162                                var_InheritInteger (p_this, "synth-polyphony"));
163     fluid_synth_set_reverb_on (p_sys->synth,
164                                var_InheritBool (p_this, "synth-reverb"));
165
166     p_dec->fmt_out.i_cat = AUDIO_ES;
167     p_dec->fmt_out.audio.i_rate =
168         var_InheritInteger (p_this, "synth-sample-rate");;
169     fluid_synth_set_sample_rate (p_sys->synth, p_dec->fmt_out.audio.i_rate);
170     p_dec->fmt_out.audio.i_channels = 2;
171     p_dec->fmt_out.audio.i_original_channels =
172     p_dec->fmt_out.audio.i_physical_channels =
173         AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
174     p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
175     p_dec->fmt_out.audio.i_bitspersample = 32;
176     date_Init (&p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1);
177     date_Set (&p_sys->end_date, 0);
178
179     p_dec->p_sys = p_sys;
180     p_dec->pf_decode_audio = DecodeBlock;
181     return VLC_SUCCESS;
182 }
183
184
185 static void Close (vlc_object_t *p_this)
186 {
187     decoder_sys_t *p_sys = ((decoder_t *)p_this)->p_sys;
188
189     fluid_synth_sfunload (p_sys->synth, p_sys->soundfont, 1);
190     delete_fluid_synth (p_sys->synth);
191     delete_fluid_settings (p_sys->settings);
192     free (p_sys);
193 }
194
195
196 static block_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block)
197 {
198     block_t *p_block;
199     decoder_sys_t *p_sys = p_dec->p_sys;
200     block_t *p_out = NULL;
201
202     if (pp_block == NULL)
203         return NULL;
204     p_block = *pp_block;
205     if (p_block == NULL)
206         return NULL;
207     *pp_block = NULL;
208
209     if (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED))
210     {
211         date_Set (&p_sys->end_date, 0);
212         //fluid_synth_system_reset (p_sys->synth);
213         fluid_synth_program_reset (p_sys->synth);
214         for (unsigned channel = 0; channel < 16; channel++)
215             for (unsigned note = 0; note < 128; note++)
216                 fluid_synth_noteoff (p_sys->synth, channel, note);
217     }
218
219     if (p_block->i_pts > VLC_TS_INVALID && !date_Get (&p_sys->end_date))
220         date_Set (&p_sys->end_date, p_block->i_pts);
221     else
222     if (p_block->i_pts < date_Get (&p_sys->end_date))
223     {
224         msg_Warn (p_dec, "MIDI message in the past?");
225         goto drop;
226     }
227
228     if (p_block->i_buffer < 1)
229         goto drop;
230
231     uint8_t event = p_block->p_buffer[0];
232     uint8_t channel = p_block->p_buffer[0] & 0xf;
233     event &= 0xF0;
234
235     if (event == 0xF0)
236         switch (channel)
237         {
238             case 0:
239                 if (p_block->p_buffer[p_block->i_buffer - 1] != 0xF7)
240                 {
241             case 7:
242                     msg_Warn (p_dec, "fragmented SysEx not implemented");
243                     goto drop;
244                 }
245                 fluid_synth_sysex (p_sys->synth, (char *)p_block->p_buffer + 1,
246                                    p_block->i_buffer - 2, NULL, NULL, NULL, 0);
247                 break;
248             case 0xF:
249                 fluid_synth_system_reset (p_sys->synth);
250                 break;
251         }
252
253     uint8_t p1 = (p_block->i_buffer > 1) ? (p_block->p_buffer[1] & 0x7f) : 0;
254     uint8_t p2 = (p_block->i_buffer > 2) ? (p_block->p_buffer[2] & 0x7f) : 0;
255
256     switch (event & 0xF0)
257     {
258         case 0x80:
259             fluid_synth_noteoff (p_sys->synth, channel, p1);
260             break;
261         case 0x90:
262             fluid_synth_noteon (p_sys->synth, channel, p1, p2);
263             break;
264         /*case 0xA0: note aftertouch not implemented */
265         case 0xB0:
266             fluid_synth_cc (p_sys->synth, channel, p1, p2);
267             break;
268         case 0xC0:
269             fluid_synth_program_change (p_sys->synth, channel, p1);
270             break;
271         case 0xD0:
272             fluid_synth_channel_pressure (p_sys->synth, channel, p1);
273             break;
274         case 0xE0:
275             fluid_synth_pitch_bend (p_sys->synth, channel, (p2 << 7) | p1);
276             break;
277     }
278
279     unsigned samples =
280         (p_block->i_pts - date_Get (&p_sys->end_date)) * 441 / 10000;
281     if (samples == 0)
282         goto drop;
283
284     p_out = decoder_NewAudioBuffer (p_dec, samples);
285     if (p_out == NULL)
286         goto drop;
287
288     p_out->i_pts = date_Get (&p_sys->end_date );
289     p_out->i_length = date_Increment (&p_sys->end_date, samples)
290                       - p_out->i_pts;
291     fluid_synth_write_float (p_sys->synth, samples, p_out->p_buffer, 0, 2,
292                              p_out->p_buffer, 1, 2);
293 drop:
294     block_Release (p_block);
295     return p_out;
296 }