2 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "pbd/failed_constructor.h"
21 #include "ardour/fluid_synth.h"
23 using namespace ARDOUR;
25 FluidSynth::FluidSynth (float samplerate, int polyphony)
30 _settings = new_fluid_settings ();
33 throw failed_constructor ();
36 _f_midi_event = new_fluid_midi_event ();
39 throw failed_constructor ();
42 fluid_settings_setnum (_settings, "synth.sample-rate", samplerate);
43 fluid_settings_setint (_settings, "synth.parallel-render", 1);
44 fluid_settings_setint (_settings, "synth.threadsafe-api", 0);
46 _synth = new_fluid_synth (_settings);
48 fluid_synth_set_gain (_synth, 1.0f);
49 fluid_synth_set_polyphony (_synth, polyphony);
50 fluid_synth_set_sample_rate (_synth, (float)samplerate);
53 FluidSynth::~FluidSynth ()
55 delete_fluid_synth (_synth);
56 delete_fluid_settings (_settings);
57 delete_fluid_midi_event (_f_midi_event);
61 FluidSynth::load_sf2 (const std::string& fn)
63 _synth_id = fluid_synth_sfload (_synth, fn.c_str (), 1);
64 if (_synth_id == FLUID_FAILED) {
68 fluid_sfont_t* const sfont = fluid_synth_get_sfont_by_id (_synth, _synth_id);
74 fluid_preset_t* preset;
76 fluid_sfont_iteration_start (sfont);
77 for (count = 0; (preset = fluid_sfont_iteration_next (sfont)) != 0; ++count) {
79 fluid_synth_program_select (_synth, count, _synth_id, fluid_preset_get_banknum (preset), fluid_preset_get_num (preset));
81 _presets.push_back (BankProgram (
82 fluid_preset_get_name (preset),
83 fluid_preset_get_banknum (preset),
84 fluid_preset_get_num (preset)));
90 /* boostrap synth engine. The first call re-initializes the chorus
91 * (fluid_rvoice_mixer_set_samplerate) which is not rt-safe.
95 fluid_synth_all_notes_off (_synth, -1);
96 fluid_synth_all_sounds_off (_synth, -1);
97 fluid_synth_write_float (_synth, 1024, l, 0, 1, r, 0, 1);
103 FluidSynth::select_program (uint32_t pgm, uint8_t chan)
105 if (pgm >= _presets.size ()) {
108 const BankProgram& bp = _presets[pgm];
109 return FLUID_OK == fluid_synth_program_select (_synth, chan, _synth_id, bp.bank, bp.program);
115 fluid_synth_all_notes_off (_synth, -1);
116 fluid_synth_all_sounds_off (_synth, -1);
120 FluidSynth::synth (float* left, float* right, uint32_t n_samples)
122 return FLUID_OK == fluid_synth_write_float (_synth, n_samples, left, 0, 1, right, 0, 1);
126 FluidSynth::midi_event (uint8_t const* const data, size_t len)
131 fluid_midi_event_set_type (_f_midi_event, data[0] & 0xf0);
132 fluid_midi_event_set_channel (_f_midi_event, data[0] & 0x0f);
134 fluid_midi_event_set_key (_f_midi_event, data[1]);
137 if (0xe0 /*PITCH_BEND*/ == fluid_midi_event_get_type (_f_midi_event)) {
138 fluid_midi_event_set_value (_f_midi_event, 0);
139 fluid_midi_event_set_pitch (_f_midi_event, ((data[2] & 0x7f) << 7) | (data[1] & 0x7f));
141 fluid_midi_event_set_value (_f_midi_event, data[2]);
144 return FLUID_OK == fluid_synth_handle_midi_event (_synth, _f_midi_event);