Expose virtual-keyboard port as async-port
[ardour.git] / libs / ardour / fluid_synth.cc
1 /*
2  * Copyright (C) 2016-2019 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include "pbd/failed_constructor.h"
20 #include "ardour/fluid_synth.h"
21
22 using namespace ARDOUR;
23
24 FluidSynth::FluidSynth (float samplerate, int polyphony)
25         : _settings (0)
26         , _synth (0)
27         , _f_midi_event (0)
28 {
29         _settings = new_fluid_settings ();
30
31         if (!_settings) {
32                 throw failed_constructor ();
33         }
34
35         _f_midi_event = new_fluid_midi_event ();
36
37         if (!_f_midi_event) {
38                 throw failed_constructor ();
39         }
40
41         fluid_settings_setnum (_settings, "synth.sample-rate", samplerate);
42         fluid_settings_setint (_settings, "synth.threadsafe-api", 0);
43
44         _synth = new_fluid_synth (_settings);
45
46         fluid_synth_set_gain (_synth, 1.0f);
47         fluid_synth_set_polyphony (_synth, polyphony);
48         fluid_synth_set_sample_rate (_synth, (float)samplerate);
49 }
50
51 FluidSynth::~FluidSynth ()
52 {
53         delete_fluid_synth (_synth);
54         delete_fluid_settings (_settings);
55         delete_fluid_midi_event (_f_midi_event);
56 }
57
58 bool
59 FluidSynth::load_sf2 (const std::string& fn)
60 {
61         _synth_id = fluid_synth_sfload (_synth, fn.c_str (), 1);
62         if (_synth_id == FLUID_FAILED) {
63                 return false;
64         }
65
66         fluid_sfont_t* const sfont = fluid_synth_get_sfont_by_id (_synth, _synth_id);
67         if (!sfont) {
68                 return false;
69         }
70
71         size_t count;
72         fluid_preset_t* preset;
73
74         fluid_sfont_iteration_start (sfont);
75         for (count = 0; (preset = fluid_sfont_iteration_next (sfont)) != 0; ++count) {
76                 if (count < 16) {
77                         fluid_synth_program_select (_synth, count, _synth_id, fluid_preset_get_banknum (preset), fluid_preset_get_num (preset));
78                 }
79                 _presets.push_back (BankProgram (
80                                         fluid_preset_get_name (preset),
81                                         fluid_preset_get_banknum (preset),
82                                         fluid_preset_get_num (preset)));
83         }
84         if (count == 0) {
85                 return false;
86         }
87
88         /* boostrap synth engine. The first call re-initializes the chorus
89          * (fluid_rvoice_mixer_set_samplerate) which is not rt-safe.
90          */
91         float l[1024];
92         float r[1024];
93         fluid_synth_all_notes_off (_synth, -1);
94         fluid_synth_all_sounds_off (_synth, -1);
95         fluid_synth_write_float (_synth, 1024, l, 0, 1, r, 0, 1);
96
97         return true;
98 }
99
100 bool
101 FluidSynth::select_program (uint32_t pgm, uint8_t chan)
102 {
103         if (pgm >= _presets.size ()) {
104                 return false;
105         }
106         const BankProgram& bp = _presets[pgm];
107         return FLUID_OK == fluid_synth_program_select (_synth, chan, _synth_id, bp.bank, bp.program);
108 }
109
110 void
111 FluidSynth::panic ()
112 {
113         fluid_synth_all_notes_off (_synth, -1);
114         fluid_synth_all_sounds_off (_synth, -1);
115 }
116
117 bool
118 FluidSynth::synth (float* left, float* right, uint32_t n_samples)
119 {
120         return FLUID_OK == fluid_synth_write_float (_synth, n_samples, left, 0, 1, right, 0, 1);
121 }
122
123 bool
124 FluidSynth::midi_event (uint8_t const* const data, size_t len)
125 {
126         if (len > 3) {
127                 return false;
128         }
129         fluid_midi_event_set_type (_f_midi_event, data[0] & 0xf0);
130         fluid_midi_event_set_channel (_f_midi_event, data[0] & 0x0f);
131         if (len > 1) {
132                 fluid_midi_event_set_key (_f_midi_event, data[1]);
133         }
134         if (len > 2) {
135                 if (0xe0 /*PITCH_BEND*/ == fluid_midi_event_get_type (_f_midi_event)) {
136                         fluid_midi_event_set_value (_f_midi_event, 0);
137                         fluid_midi_event_set_pitch (_f_midi_event, ((data[2] & 0x7f) << 7) | (data[1] & 0x7f));
138                 } else {
139                         fluid_midi_event_set_value (_f_midi_event, data[2]);
140                 }
141         }
142         return FLUID_OK == fluid_synth_handle_midi_event (_synth, _f_midi_event);
143 }