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 sfont->iteration_start (sfont);
77 for (count = 0; sfont->iteration_next (sfont, &preset) != 0; ++count) {
79 fluid_synth_program_select (_synth, count, _synth_id, preset.get_banknum (&preset), preset.get_num (&preset));
81 _presets.push_back (BankProgram (
82 preset.get_name (&preset),
83 preset.get_banknum (&preset),
84 preset.get_num (&preset)));
91 /* boostrap synth engine. The first call re-initializes the chorus
92 * (fluid_rvoice_mixer_set_samplerate) which is not rt-safe.
96 fluid_synth_all_notes_off (_synth, -1);
97 fluid_synth_all_sounds_off (_synth, -1);
98 fluid_synth_write_float (_synth, 1024, l, 0, 1, r, 0, 1);
104 FluidSynth::select_program (uint32_t pgm, uint8_t chan)
106 if (pgm >= _presets.size ()) {
109 const BankProgram& bp = _presets[pgm];
110 return FLUID_OK == fluid_synth_program_select (_synth, chan, _synth_id, bp.bank, bp.program);
116 fluid_synth_all_notes_off (_synth, -1);
117 fluid_synth_all_sounds_off (_synth, -1);
121 FluidSynth::synth (float* left, float* right, uint32_t n_samples)
123 return FLUID_OK == fluid_synth_write_float (_synth, n_samples, left, 0, 1, right, 0, 1);
127 FluidSynth::midi_event (uint8_t const* const data, size_t len)
132 fluid_midi_event_set_type (_f_midi_event, data[0] & 0xf0);
133 fluid_midi_event_set_channel (_f_midi_event, data[0] & 0x0f);
135 fluid_midi_event_set_key (_f_midi_event, data[1]);
138 if (0xe0 /*PITCH_BEND*/ == fluid_midi_event_get_type (_f_midi_event)) {
139 fluid_midi_event_set_value (_f_midi_event, 0);
140 fluid_midi_event_set_pitch (_f_midi_event, ((data[2] & 0x7f) << 7) | (data[1] & 0x7f));
142 fluid_midi_event_set_value (_f_midi_event, data[2]);
145 return FLUID_OK == fluid_synth_handle_midi_event (_synth, _f_midi_event);