fix crash caused by trying to connect monitor section before it has ports.
[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         sfont->iteration_start (sfont);
77         for (count = 0; sfont->iteration_next (sfont, &preset) != 0; ++count) {
78                 if (count < 16) {
79                         fluid_synth_program_select (_synth, count, _synth_id, preset.get_banknum (&preset), preset.get_num (&preset));
80                 }
81                 _presets.push_back (BankProgram (
82                                         preset.get_name (&preset),
83                                         preset.get_banknum (&preset),
84                                         preset.get_num (&preset)));
85         }
86
87         if (count == 0) {
88                 return false;
89         }
90
91         /* boostrap synth engine. The first call re-initializes the chorus
92          * (fluid_rvoice_mixer_set_samplerate) which is not rt-safe.
93          */
94         float l[1024];
95         float r[1024];
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);
99
100         return true;
101 }
102
103 bool
104 FluidSynth::select_program (uint32_t pgm, uint8_t chan)
105 {
106         if (pgm >= _presets.size ()) {
107                 return false;
108         }
109         const BankProgram& bp = _presets[pgm];
110         return FLUID_OK == fluid_synth_program_select (_synth, chan, _synth_id, bp.bank, bp.program);
111 }
112
113 void
114 FluidSynth::panic ()
115 {
116         fluid_synth_all_notes_off (_synth, -1);
117         fluid_synth_all_sounds_off (_synth, -1);
118 }
119
120 bool
121 FluidSynth::synth (float* left, float* right, uint32_t n_samples)
122 {
123         return FLUID_OK == fluid_synth_write_float (_synth, n_samples, left, 0, 1, right, 0, 1);
124 }
125
126 bool
127 FluidSynth::midi_event (uint8_t const* const data, size_t len)
128 {
129         if (len > 3) {
130                 return false;
131         }
132         fluid_midi_event_set_type (_f_midi_event, data[0] & 0xf0);
133         fluid_midi_event_set_channel (_f_midi_event, data[0] & 0x0f);
134         if (len > 1) {
135                 fluid_midi_event_set_key (_f_midi_event, data[1]);
136         }
137         if (len > 2) {
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));
141                 } else {
142                         fluid_midi_event_set_value (_f_midi_event, data[2]);
143                 }
144         }
145         return FLUID_OK == fluid_synth_handle_midi_event (_synth, _f_midi_event);
146 }