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