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