1 /*****************************************************************************
2 * fluidsynth.c: Software MIDI synthetizer using libfluidsynth
3 *****************************************************************************
4 * Copyright © 2007 Rémi Denis-Courmont
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 *****************************************************************************/
26 #include <vlc_common.h>
27 #include <vlc_plugin.h>
29 #include <vlc_codec.h>
32 /* On Win32, we link statically */
34 # define FLUIDSYNTH_NOT_A_DLL
37 #include <fluidsynth.h>
39 #define SOUNDFONT_TEXT N_("Sound fonts (required)")
40 #define SOUNDFONT_LONGTEXT N_( \
41 "A sound fonts file is required for software synthesis." )
43 static int Open (vlc_object_t *);
44 static void Close (vlc_object_t *);
47 set_description (N_("FluidSynth MIDI synthetizer"))
48 set_capability ("decoder", 100)
49 set_shortname (N_("FluidSynth"))
50 set_category (CAT_INPUT)
51 set_subcategory (SUBCAT_INPUT_ACODEC)
52 set_callbacks (Open, Close)
53 add_file ("soundfont", "", NULL,
54 SOUNDFONT_TEXT, SOUNDFONT_LONGTEXT, false);
60 fluid_settings_t *settings;
68 static aout_buffer_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block);
71 static int Open (vlc_object_t *p_this)
73 decoder_t *p_dec = (decoder_t *)p_this;
76 if (p_dec->fmt_in.i_codec != VLC_CODEC_MIDI)
79 char *font_path = var_CreateGetNonEmptyString (p_this, "soundfont");
80 if (font_path == NULL)
82 msg_Err (p_this, "sound fonts file required for synthesis");
86 p_dec->pf_decode_audio = DecodeBlock;
87 p_sys = p_dec->p_sys = malloc (sizeof (*p_sys));
94 p_sys->settings = new_fluid_settings ();
95 p_sys->synth = new_fluid_synth (p_sys->settings);
96 /* FIXME: I bet this is not thread-safe */
97 p_sys->soundfont = fluid_synth_sfload (p_sys->synth, font_path, 1);
99 if (p_sys->soundfont == -1)
101 msg_Err (p_this, "cannot load sound fonts file");
106 p_dec->fmt_out.i_cat = AUDIO_ES;
107 p_dec->fmt_out.audio.i_rate = 44100;
108 p_dec->fmt_out.audio.i_channels = 2;
109 p_dec->fmt_out.audio.i_original_channels =
110 p_dec->fmt_out.audio.i_physical_channels =
111 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
112 if (vlc_CPU () & CPU_CAPABILITY_FPU)
114 p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
115 p_dec->fmt_out.audio.i_bitspersample = 32;
116 p_sys->fixed = false;
120 p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
121 p_dec->fmt_out.audio.i_bitspersample = 16;
124 date_Init (&p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1);
125 date_Set (&p_sys->end_date, 0);
131 static void Close (vlc_object_t *p_this)
133 decoder_sys_t *p_sys = ((decoder_t *)p_this)->p_sys;
135 if (p_sys->soundfont != -1)
136 fluid_synth_sfunload (p_sys->synth, p_sys->soundfont, 1);
137 delete_fluid_synth (p_sys->synth);
138 delete_fluid_settings (p_sys->settings);
143 static aout_buffer_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block)
146 decoder_sys_t *p_sys = p_dec->p_sys;
147 aout_buffer_t *p_out = NULL;
149 if (pp_block == NULL)
156 if (p_block->i_pts && !date_Get (&p_sys->end_date))
157 date_Set (&p_sys->end_date, p_block->i_pts);
159 if (p_block->i_pts < date_Get (&p_sys->end_date))
161 msg_Warn (p_dec, "MIDI message in the past?");
165 if (p_block->i_buffer < 1)
168 uint8_t channel = p_block->p_buffer[0] & 0xf;
169 uint8_t p1 = (p_block->i_buffer > 1) ? (p_block->p_buffer[1] & 0x7f) : 0;
170 uint8_t p2 = (p_block->i_buffer > 2) ? (p_block->p_buffer[2] & 0x7f) : 0;
172 switch (p_block->p_buffer[0] & 0xf0)
175 fluid_synth_noteoff (p_sys->synth, channel, p1);
178 fluid_synth_noteon (p_sys->synth, channel, p1, p2);
181 fluid_synth_cc (p_sys->synth, channel, p1, p2);
184 fluid_synth_program_change (p_sys->synth, channel, p1);
187 fluid_synth_pitch_bend (p_sys->synth, channel, (p1 << 7) | p2);
192 (p_block->i_pts - date_Get (&p_sys->end_date)) * 441 / 10000;
196 p_out = decoder_NewAudioBuffer (p_dec, samples);
200 p_out->start_date = date_Get (&p_sys->end_date );
201 p_out->end_date = date_Increment (&p_sys->end_date, samples);
203 fluid_synth_write_float (p_sys->synth, samples,
204 p_out->p_buffer, 0, 2,
205 p_out->p_buffer, 1, 2);
207 fluid_synth_write_s16 (p_sys->synth, samples,
208 (int16_t *)p_out->p_buffer, 0, 2,
209 (int16_t *)p_out->p_buffer, 1, 2);
211 block_Release (p_block);