Optimize automation-event process splitting
[ardour.git] / libs / ardour / fluid_synth.cc
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  *
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.
8  *
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.
13  *
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.
17  *
18  */
19
20 #include "pbd/failed_constructor.h"
21 #include "ardour/fluid_synth.h"
22
23 using namespace ARDOUR;
24
25 FluidSynth::FluidSynth (float samplerate, int polyphony)
26         : _settings (0)
27         , _synth (0)
28         , _f_midi_event (0)
29 {
30         _settings = new_fluid_settings ();
31
32         if (!_settings) {
33                 throw failed_constructor ();
34         }
35
36         _f_midi_event = new_fluid_midi_event ();
37
38         if (!_f_midi_event) {
39                 throw failed_constructor ();
40         }
41
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);
45
46         _synth = new_fluid_synth (_settings);
47
48         fluid_synth_set_gain (_synth, 1.0f);
49         fluid_synth_set_polyphony (_synth, polyphony);
50         fluid_synth_set_sample_rate (_synth, (float)samplerate);
51 }
52
53 FluidSynth::~FluidSynth ()
54 {
55         delete_fluid_synth (_synth);
56         delete_fluid_settings (_settings);
57         delete_fluid_midi_event (_f_midi_event);
58 }
59
60 bool
61 FluidSynth::load_sf2 (const std::string& fn)
62 {
63         _synth_id = fluid_synth_sfload (_synth, fn.c_str (), 1);
64         if (_synth_id == FLUID_FAILED) {
65                 return false;
66         }
67
68         fluid_sfont_t* const sfont = fluid_synth_get_sfont_by_id (_synth, _synth_id);
69         if (!sfont) {
70                 return false;
71         }
72
73         size_t count;
74         fluid_preset_t* preset;
75
76         fluid_sfont_iteration_start (sfont);
77         for (count = 0; (preset = fluid_sfont_iteration_next (sfont)) != 0; ++count) {
78                 if (count < 16) {
79                         fluid_synth_program_select (_synth, count, _synth_id, fluid_preset_get_banknum (preset), fluid_preset_get_num (preset));
80                 }
81                 _presets.push_back (BankProgram (
82                                         fluid_preset_get_name (preset),
83                                         fluid_preset_get_banknum (preset),
84                                         fluid_preset_get_num (preset)));
85         }
86         if (count == 0) {
87                 return false;
88         }
89
90         /* boostrap synth engine. The first call re-initializes the chorus
91          * (fluid_rvoice_mixer_set_samplerate) which is not rt-safe.
92          */
93         float l[1024];
94         float r[1024];
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);
98
99         return true;
100 }
101
102 bool
103 FluidSynth::select_program (uint32_t pgm, uint8_t chan)
104 {
105         if (pgm >= _presets.size ()) {
106                 return false;
107         }
108         const BankProgram& bp = _presets[pgm];
109         return FLUID_OK == fluid_synth_program_select (_synth, chan, _synth_id, bp.bank, bp.program);
110 }
111
112 void
113 FluidSynth::panic ()
114 {
115         fluid_synth_all_notes_off (_synth, -1);
116         fluid_synth_all_sounds_off (_synth, -1);
117 }
118
119 bool
120 FluidSynth::synth (float* left, float* right, uint32_t n_samples)
121 {
122         return FLUID_OK == fluid_synth_write_float (_synth, n_samples, left, 0, 1, right, 0, 1);
123 }
124
125 bool
126 FluidSynth::midi_event (uint8_t const* const data, size_t len)
127 {
128         if (len > 3) {
129                 return false;
130         }
131         fluid_midi_event_set_type (_f_midi_event, data[0] & 0xf0);
132         fluid_midi_event_set_channel (_f_midi_event, data[0] & 0x0f);
133         if (len > 1) {
134                 fluid_midi_event_set_key (_f_midi_event, data[1]);
135         }
136         if (len > 2) {
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));
140                 } else {
141                         fluid_midi_event_set_value (_f_midi_event, data[2]);
142                 }
143         }
144         return FLUID_OK == fluid_synth_handle_midi_event (_synth, _f_midi_event);
145 }