]> git.sesse.net Git - vlc/blob - modules/codec/fluidsynth.c
Revert the so-called whitelisting commits that are actually blacklisting
[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 #include <vlc/vlc.h>
23 #include <vlc_aout.h>
24 #include <vlc_codec.h>
25
26 #include <fluidsynth.h>
27
28 #define SOUNDFONT_TEXT N_("Sound fonts (required)")
29 #define SOUNDFONT_LONGTEXT N_( \
30     "A sound fonts file is required for software synthesis." )
31
32 static int  Open  (vlc_object_t *);
33 static void Close (vlc_object_t *);
34
35 vlc_module_begin();
36     set_description (_("FluidSynth MIDI synthetizer"));
37     set_capability ("decoder", 100);
38     set_category (CAT_INPUT);
39     set_subcategory (SUBCAT_INPUT_ACODEC);
40     set_callbacks (Open, Close);
41     add_file ("soundfont", "", NULL,
42               SOUNDFONT_TEXT, SOUNDFONT_LONGTEXT, VLC_FALSE);
43 vlc_module_end();
44
45
46 struct decoder_sys_t
47 {
48     fluid_settings_t *settings;
49     fluid_synth_t    *synth;
50     int               soundfont;
51     audio_date_t      end_date;
52 };
53
54
55 static aout_buffer_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block);
56
57
58 static int Open (vlc_object_t *p_this)
59 {
60     decoder_t *p_dec = (decoder_t *)p_this;
61     decoder_sys_t *p_sys;
62
63     if (p_dec->fmt_in.i_codec != VLC_FOURCC ('M', 'I', 'D', 'I'))
64         return VLC_EGENERIC;
65
66     char *font_path = var_CreateGetNonEmptyString (p_this, "soundfont");
67     if (font_path == NULL)
68     {
69         msg_Err (p_this, "sound fonts file required for synthesis");
70         return VLC_EGENERIC;
71     }
72
73     p_dec->fmt_out.i_cat = AUDIO_ES;
74     p_dec->fmt_out.audio.i_rate = 44100;
75     p_dec->fmt_out.audio.i_channels = 2;
76     p_dec->fmt_out.audio.i_original_channels =
77     p_dec->fmt_out.audio.i_physical_channels =
78         AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
79     p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
80     p_dec->fmt_out.audio.i_bitspersample = 16;
81
82     p_dec->pf_decode_audio = DecodeBlock;
83     p_sys = p_dec->p_sys = malloc (sizeof (*p_sys));
84     if (p_sys == NULL)
85     {
86         free (font_path);
87         return VLC_ENOMEM;
88     }
89
90     p_sys->settings = new_fluid_settings ();
91     p_sys->synth = new_fluid_synth (p_sys->settings);
92     /* FIXME: I bet this is not thread-safe */
93     p_sys->soundfont = fluid_synth_sfload (p_sys->synth, font_path, 1);
94     free (font_path);
95     if (p_sys->soundfont == -1)
96     {
97         msg_Err (p_this, "cannot load sound fonts file");
98         Close (p_this);
99         return VLC_EGENERIC;
100     }
101
102     aout_DateInit (&p_sys->end_date, p_dec->fmt_out.audio.i_rate);
103     aout_DateSet (&p_sys->end_date, 0);
104
105     return VLC_SUCCESS;
106 }
107
108
109 static void Close (vlc_object_t *p_this)
110 {
111     decoder_sys_t *p_sys = ((decoder_t *)p_this)->p_sys;
112
113     if (p_sys->soundfont != -1)
114         fluid_synth_sfunload (p_sys->synth, p_sys->soundfont, 1);
115     delete_fluid_synth (p_sys->synth);
116     delete_fluid_settings (p_sys->settings);
117     free (p_sys);
118 }
119
120
121 static aout_buffer_t *DecodeBlock (decoder_t *p_dec, block_t **pp_block)
122 {
123     block_t *p_block;
124     decoder_sys_t *p_sys = p_dec->p_sys;
125
126     if (pp_block == NULL)
127         return NULL;
128     p_block = *pp_block;
129     if (p_block == NULL)
130         return NULL;
131
132     if (p_block->i_pts && !aout_DateGet (&p_sys->end_date))
133         aout_DateSet (&p_sys->end_date, p_block->i_pts);
134     else
135     if (p_block->i_pts < aout_DateGet (&p_sys->end_date))
136     {
137         msg_Warn (p_dec, "MIDI message in the past?");
138         block_Release (p_block);
139         return NULL;
140     }
141
142     if (p_block->i_buffer < 1)
143         return NULL;
144
145     uint8_t channel = p_block->p_buffer[0] & 0xf;
146     uint8_t p1 = (p_block->i_buffer > 1) ? (p_block->p_buffer[1] & 0x7f) : 0;
147     uint8_t p2 = (p_block->i_buffer > 2) ? (p_block->p_buffer[2] & 0x7f) : 0;
148
149     switch (p_block->p_buffer[0] & 0xf0)
150     {
151         case 0x80:
152             fluid_synth_noteoff (p_sys->synth, channel, p1);
153             break;
154         case 0x90:
155             fluid_synth_noteon (p_sys->synth, channel, p1, p2);
156             break;
157         case 0xB0:
158             fluid_synth_cc (p_sys->synth, channel, p1, p2);
159             break;
160         case 0xC0:
161             fluid_synth_program_change (p_sys->synth, channel, p1);
162             break;
163         case 0xE0:
164             fluid_synth_pitch_bend (p_sys->synth, channel, (p1 << 7) | p2);
165             break;
166     }
167     p_block->p_buffer += p_block->i_buffer;
168     p_block->i_buffer = 0;
169
170     unsigned samples =
171         (p_block->i_pts - aout_DateGet (&p_sys->end_date)) * 441 / 10000;
172     if (samples == 0)
173         return NULL;
174
175     aout_buffer_t *p_out = p_dec->pf_aout_buffer_new (p_dec, samples);
176     if (p_out == NULL)
177     {
178         block_Release (p_block);
179         return NULL;
180     }
181
182     p_out->start_date = aout_DateGet (&p_sys->end_date );
183     p_out->end_date   = aout_DateIncrement (&p_sys->end_date, samples);
184     fluid_synth_write_s16 (p_sys->synth, samples,
185                            (int16_t *)p_out->p_buffer, 0, 2,
186                            (int16_t *)p_out->p_buffer, 1, 2);
187     return p_out;
188 }