Qt: sprefs_interface add developers hints
[vlc/solaris.git] / modules / codec / fluidsynth.c
blob4cf309870a12d496fdf000dd7df4d00e6d9a8038
1 /*****************************************************************************
2 * fluidsynth.c: Software MIDI synthetizer using libfluidsynth
3 *****************************************************************************
4 * Copyright © 2007 Rémi Denis-Courmont
5 * $Id$
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.
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.
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 *****************************************************************************/
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
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>
34 /* On Win32, we link statically */
35 #ifdef WIN32
36 # define FLUIDSYNTH_NOT_A_DLL
37 #endif
39 #include <fluidsynth.h>
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
49 #define SOUNDFONT_TEXT N_("Sound fonts (required)")
50 #define SOUNDFONT_LONGTEXT N_( \
51 "A sound fonts file is required for software synthesis." )
53 static int Open (vlc_object_t *);
54 static void Close (vlc_object_t *);
56 vlc_module_begin ()
57 set_description (N_("FluidSynth MIDI synthetizer"))
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_file ("soundfont", "", NULL,
64 SOUNDFONT_TEXT, SOUNDFONT_LONGTEXT, false);
65 vlc_module_end ()
68 struct decoder_sys_t
70 fluid_settings_t *settings;
71 fluid_synth_t *synth;
72 int soundfont;
73 bool fixed;
74 date_t end_date;
78 static aout_buffer_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block);
81 static int Open (vlc_object_t *p_this)
83 decoder_t *p_dec = (decoder_t *)p_this;
84 decoder_sys_t *p_sys;
86 if (p_dec->fmt_in.i_codec != VLC_CODEC_MIDI)
87 return VLC_EGENERIC;
89 char *font_path = var_InheritString (p_this, "soundfont");
90 if (font_path == NULL)
92 msg_Err (p_this, "sound font file required for synthesis");
93 dialog_Fatal (p_this, _("MIDI synthesis not set up"),
94 _("A sound font file (.SF2) is required for MIDI synthesis.\n"
95 "Please install a sound font and configure it "
96 "from the VLC preferences (Codecs / Audio / FluidSynth).\n"));
97 return VLC_EGENERIC;
100 p_dec->pf_decode_audio = DecodeBlock;
101 p_sys = p_dec->p_sys = malloc (sizeof (*p_sys));
102 if (p_sys == NULL)
104 free (font_path);
105 return VLC_ENOMEM;
108 p_sys->settings = new_fluid_settings ();
109 p_sys->synth = new_fluid_synth (p_sys->settings);
110 /* FIXME: I bet this is not thread-safe */
111 const char *lpath = ToLocale (font_path);
112 p_sys->soundfont = fluid_synth_sfload (p_sys->synth, font_path, 1);
113 LocaleFree (lpath);
114 if (p_sys->soundfont == -1)
116 msg_Err (p_this, "cannot load sound fonts file %s", font_path);
117 Close (p_this);
118 dialog_Fatal (p_this, _("MIDI synthesis not set up"),
119 _("The specified sound font file (%s) is incorrect.\n"
120 "Please install a valid sound font and reconfigure it "
121 "from the VLC preferences (Codecs / Audio / FluidSynth).\n"),
122 font_path);
123 free (font_path);
124 return VLC_EGENERIC;
126 free (font_path);
128 p_dec->fmt_out.i_cat = AUDIO_ES;
129 p_dec->fmt_out.audio.i_rate = 44100;
130 p_dec->fmt_out.audio.i_channels = 2;
131 p_dec->fmt_out.audio.i_original_channels =
132 p_dec->fmt_out.audio.i_physical_channels =
133 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
134 if (HAVE_FPU)
136 p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
137 p_dec->fmt_out.audio.i_bitspersample = 32;
138 p_sys->fixed = false;
140 else
142 p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
143 p_dec->fmt_out.audio.i_bitspersample = 16;
144 p_sys->fixed = true;
146 date_Init (&p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1);
147 date_Set (&p_sys->end_date, 0);
149 return VLC_SUCCESS;
153 static void Close (vlc_object_t *p_this)
155 decoder_sys_t *p_sys = ((decoder_t *)p_this)->p_sys;
157 if (p_sys->soundfont != -1)
158 fluid_synth_sfunload (p_sys->synth, p_sys->soundfont, 1);
159 delete_fluid_synth (p_sys->synth);
160 delete_fluid_settings (p_sys->settings);
161 free (p_sys);
165 static aout_buffer_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block)
167 block_t *p_block;
168 decoder_sys_t *p_sys = p_dec->p_sys;
169 aout_buffer_t *p_out = NULL;
171 if (pp_block == NULL)
172 return NULL;
173 p_block = *pp_block;
174 if (p_block == NULL)
175 return NULL;
176 *pp_block = NULL;
178 if (p_block->i_pts > VLC_TS_INVALID && !date_Get (&p_sys->end_date))
179 date_Set (&p_sys->end_date, p_block->i_pts);
180 else
181 if (p_block->i_pts < date_Get (&p_sys->end_date))
183 msg_Warn (p_dec, "MIDI message in the past?");
184 goto drop;
187 if (p_block->i_buffer < 1)
188 goto drop;
190 uint8_t event = p_block->p_buffer[0];
191 uint8_t channel = p_block->p_buffer[0] & 0xf;
192 event &= 0xF0;
194 if (event == 0xF0)
195 switch (channel)
197 case 0:
198 if (p_block->p_buffer[p_block->i_buffer - 1] != 0xF7)
200 case 7:
201 msg_Warn (p_dec, "fragmented SysEx not implemented");
202 goto drop;
204 fluid_synth_sysex (p_sys->synth, (char *)p_block->p_buffer + 1,
205 p_block->i_buffer - 2, NULL, NULL, NULL, 0);
206 break;
207 case 0xF:
208 fluid_synth_system_reset (p_sys->synth);
209 break;
212 uint8_t p1 = (p_block->i_buffer > 1) ? (p_block->p_buffer[1] & 0x7f) : 0;
213 uint8_t p2 = (p_block->i_buffer > 2) ? (p_block->p_buffer[2] & 0x7f) : 0;
215 switch (event & 0xF0)
217 case 0x80:
218 fluid_synth_noteoff (p_sys->synth, channel, p1);
219 break;
220 case 0x90:
221 fluid_synth_noteon (p_sys->synth, channel, p1, p2);
222 break;
223 /*case 0xA0: note aftertouch not implemented */
224 case 0xB0:
225 fluid_synth_cc (p_sys->synth, channel, p1, p2);
226 break;
227 case 0xC0:
228 fluid_synth_program_change (p_sys->synth, channel, p1);
229 break;
230 case 0xD0:
231 fluid_synth_channel_pressure (p_sys->synth, channel, p1);
232 break;
233 case 0xE0:
234 fluid_synth_pitch_bend (p_sys->synth, channel, (p2 << 7) | p1);
235 break;
238 unsigned samples =
239 (p_block->i_pts - date_Get (&p_sys->end_date)) * 441 / 10000;
240 if (samples == 0)
241 return NULL;
243 p_out = decoder_NewAudioBuffer (p_dec, samples);
244 if (p_out == NULL)
245 goto drop;
247 p_out->i_pts = date_Get (&p_sys->end_date );
248 p_out->i_length = date_Increment (&p_sys->end_date, samples)
249 - p_out->i_pts;
250 if (!p_sys->fixed)
251 fluid_synth_write_float (p_sys->synth, samples,
252 p_out->p_buffer, 0, 2,
253 p_out->p_buffer, 1, 2);
254 else
255 fluid_synth_write_s16 (p_sys->synth, samples,
256 (int16_t *)p_out->p_buffer, 0, 2,
257 (int16_t *)p_out->p_buffer, 1, 2);
258 drop:
259 block_Release (p_block);
260 return p_out;