summaryrefslogtreecommitdiff
path: root/libs/ardour/fluid_synth.cc
blob: 45653a9e7046ca813c136c93428014edc4efe05f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include "pbd/failed_constructor.h"
#include "ardour/fluid_synth.h"

using namespace ARDOUR;

FluidSynth::FluidSynth (float samplerate, int polyphony)
	: _settings (0)
	, _synth (0)
	, _f_midi_event (0)
{
	_settings = new_fluid_settings ();

	if (!_settings) {
		throw failed_constructor ();
	}

	_f_midi_event = new_fluid_midi_event ();

	if (!_f_midi_event) {
		throw failed_constructor ();
	}

	fluid_settings_setnum (_settings, "synth.sample-rate", samplerate);
	fluid_settings_setint (_settings, "synth.parallel-render", 1);
	fluid_settings_setint (_settings, "synth.threadsafe-api", 0);

	_synth = new_fluid_synth (_settings);

	fluid_synth_set_gain (_synth, 1.0f);
	fluid_synth_set_polyphony (_synth, polyphony);
	fluid_synth_set_sample_rate (_synth, (float)samplerate);
}

FluidSynth::~FluidSynth ()
{
	delete_fluid_synth (_synth);
	delete_fluid_settings (_settings);
	delete_fluid_midi_event (_f_midi_event);
}

bool
FluidSynth::load_sf2 (const std::string& fn)
{
	_synth_id = fluid_synth_sfload (_synth, fn.c_str (), 1);
	if (_synth_id == FLUID_FAILED) {
		return false;
	}

	fluid_sfont_t* const sfont = fluid_synth_get_sfont_by_id (_synth, _synth_id);
	if (!sfont) {
		return false;
	}

	size_t count;
	fluid_preset_t preset;

	sfont->iteration_start (sfont);
	for (count = 0; sfont->iteration_next (sfont, &preset) != 0; ++count) {
		if (count < 16) {
			fluid_synth_program_select (_synth, count, _synth_id, preset.get_banknum (&preset), preset.get_num (&preset));
		}
		_presets.push_back (BankProgram (
					preset.get_name (&preset),
					preset.get_banknum (&preset),
					preset.get_num (&preset)));
	}

	if (count == 0) {
		return false;
	}

	/* boostrap synth engine. The first call re-initializes the chorus
	 * (fluid_rvoice_mixer_set_samplerate) which is not rt-safe.
	 */
	float l[1024];
	float r[1024];
	fluid_synth_all_notes_off (_synth, -1);
	fluid_synth_all_sounds_off (_synth, -1);
	fluid_synth_write_float (_synth, 1024, l, 0, 1, r, 0, 1);

	return true;
}

bool
FluidSynth::select_program (uint32_t pgm, uint8_t chan)
{
	if (pgm >= _presets.size ()) {
		return false;
	}
	const BankProgram& bp = _presets[pgm];
	return FLUID_OK == fluid_synth_program_select (_synth, chan, _synth_id, bp.bank, bp.program);
}

void
FluidSynth::panic ()
{
	fluid_synth_all_notes_off (_synth, -1);
	fluid_synth_all_sounds_off (_synth, -1);
}

bool
FluidSynth::synth (float* left, float* right, uint32_t n_samples)
{
	return FLUID_OK == fluid_synth_write_float (_synth, n_samples, left, 0, 1, right, 0, 1);
}

bool
FluidSynth::midi_event (uint8_t const* const data, size_t len)
{
	if (len > 3) {
		return false;
	}
	fluid_midi_event_set_type (_f_midi_event, data[0] & 0xf0);
	fluid_midi_event_set_channel (_f_midi_event, data[0] & 0x0f);
	if (len > 1) {
		fluid_midi_event_set_key (_f_midi_event, data[1]);
	}
	if (len > 2) {
		if (0xe0 /*PITCH_BEND*/ == fluid_midi_event_get_type (_f_midi_event)) {
			fluid_midi_event_set_value (_f_midi_event, 0);
			fluid_midi_event_set_pitch (_f_midi_event, ((data[2] & 0x7f) << 7) | (data[1] & 0x7f));
		} else {
			fluid_midi_event_set_value (_f_midi_event, data[2]);
		}
	}
	return FLUID_OK == fluid_synth_handle_midi_event (_synth, _f_midi_event);
}