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