-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathliquidsfz.cc
More file actions
260 lines (242 loc) · 7.28 KB
/
Copy pathliquidsfz.cc
File metadata and controls
260 lines (242 loc) · 7.28 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
#include "ase/processor.hh"
#include "ase/midievent.hh"
#include "ase/internal.hh"
#include <liquidsfz.hh>
namespace {
using namespace Ase;
using LiquidSFZ::Synth;
class LiquidSFZLoader
{
enum { STATE_IDLE, STATE_LOAD };
std::atomic<int> state_ { STATE_IDLE };
std::atomic<int> quit_ { 0 };
Ase::ScopedSemaphore sem_;
std::thread thread_;
Synth &synth_;
String have_sfz_;
String want_sfz_;
uint want_sample_rate_ = 0;
uint have_sample_rate_ = 0;
void
run()
{
while (!quit_.load())
{
sem_.wait();
if (state_.load() == STATE_LOAD)
{
if (want_sfz_ != have_sfz_)
{
printerr ("LiquidSFZ: loading %s...", want_sfz_.c_str());
bool result = synth_.load (want_sfz_);
printerr ("%s\n", result ? "OK" : "FAIL");
// TODO: handle load error
have_sfz_ = want_sfz_;
}
if (want_sample_rate_ != have_sample_rate_)
{
synth_.set_sample_rate (want_sample_rate_);
have_sample_rate_ = want_sample_rate_;
}
state_.store (STATE_IDLE);
}
}
printerr ("LiquidSFZ: run() done\n");
}
public:
LiquidSFZLoader (Synth &synth) :
synth_ (synth)
{
thread_ = std::thread (&LiquidSFZLoader::run, this);
want_sfz_.reserve (4096); // avoid allocations in audio thread
printerr ("LiquidSFZLoader()\n");
}
~LiquidSFZLoader()
{
quit_.store (1);
sem_.post();
thread_.join();
printerr ("~LiquidSFZLoader()\n");
}
// called from audio thread
bool
idle()
{
if (state_.load() == STATE_IDLE)
{
if (want_sfz_ == have_sfz_ && (want_sample_rate_ == have_sample_rate_ || want_sfz_ == ""))
return true;
}
state_.store (STATE_LOAD);
sem_.post();
return false;
}
// called from audio thread
void
load (const String &sfz)
{
want_sfz_ = sfz;
}
// called from audio thread
void
set_sample_rate (uint sample_rate)
{
want_sample_rate_ = sample_rate;
}
};
// == LiquidSFZ ==
// SFZ sampler using liquidsfz library
class LiquidSFZ : public AudioProcessor {
OBusId stereo_out_;
Synth synth_;
bool synth_need_reset_ = false;
bool instrument_changed_ = false;
LiquidSFZLoader loader_;
enum Params {
INSTRUMENT = 1,
};
static constexpr const int PID_CC_OFFSET = 1000;
void
initialize (SpeakerArrangement busses) override
{
install_params (build_parameter_map());
loader_.set_sample_rate (sample_rate());
prepare_event_input();
stereo_out_ = add_output_bus ("Stereo Out", SpeakerArrangement::STEREO);
assert_return (bus_info (stereo_out_).ident == "stereo_out");
}
/* TODO: the parameter map generated by this function depends on the .sfz file that is
* currently loaded in synth_ -> so we should rerun this after loading a new .sfz
*/
ParameterMap
build_parameter_map()
{
ParameterMap pmap;
pmap[INSTRUMENT] = Param { "instrument", _("Instrument"), _("Inst"), "", "", {}, "", {
String ("blurb=") + _("Instrument File Name"),
"extensions=sfz",
} };
auto ccs = synth_.list_ccs();
for (const auto& cc_info : ccs)
{
uint32_t pid = cc_info.cc() + PID_CC_OFFSET;
pmap[pid] = Param { string_format ("cc_%d", cc_info.cc()), cc_info.label(), cc_info.label(),
cc_info.default_value() / 127. * 100, "%", { 0, 100 } };
}
return pmap;
}
void
reset (uint64 target_stamp) override
{
synth_need_reset_ = true;
adjust_all_params();
}
void
adjust_param (uint32_t tag) override
{
switch (tag)
{
case INSTRUMENT:
instrument_changed_ = true;
break;
}
}
void
generate_cc_event (const MidiEvent& event)
{
const int cc = event.param - PID_CC_OFFSET;
const int cc_value = std::clamp (irintf (event.pvalue * 0.01 * 127), 0, 127);
/* TODO: make a decision here how UI parameters (which ought to be automatable) map to CC values
*
* - should the liquidsfz synth maintain different CC state for different midi channels?
* - should automating a parameter affect all notes (or only these on specific channels)?
* - should CC events affect all notes (or only those on specific channels)?
* - should we support per note modulation?
*
* Right now, we map parameter changes to CC events on channel 0 only.
*/
synth_.add_event_cc (event.frame, 0, cc, cc_value);
}
void
render (uint n_frames) override
{
bool loader_idle = loader_.idle();
if (instrument_changed_ && loader_idle)
{
loader_.load (text_param_from_quark (INSTRUMENT, irintf (get_param (INSTRUMENT))));
instrument_changed_ = false;
loader_idle = loader_.idle(); /* calling load can change idle state */
}
if (loader_idle)
{
if (synth_need_reset_)
{
synth_.system_reset();
synth_need_reset_ = false;
}
MidiEventInput evinput = midi_event_input();
for (const auto &ev : evinput)
{
switch (ev.message())
{
case MidiMessage::NOTE_OFF:
synth_.add_event_note_off (ev.frame, ev.channel, ev.key);
break;
case MidiMessage::NOTE_ON:
synth_.add_event_note_on (ev.frame, ev.channel, ev.key, std::clamp (irintf (ev.velocity * 127), 0, 127));
break;
case MidiMessage::ALL_NOTES_OFF:
case MidiMessage::ALL_SOUND_OFF:
synth_.all_sound_off(); // NOTE: there is no extra "all notes off" in liquidsfz
break;
case MidiMessage::PARAM_VALUE:
apply_event (ev);
adjust_param (ev.param);
if (ev.param >= PID_CC_OFFSET)
generate_cc_event (ev);
break;
default: ;
}
}
float *output[2] = {
oblock (stereo_out_, 0),
oblock (stereo_out_, 1)
};
synth_.process (output, n_frames);
}
else
{
// loader thread is busy, so we can't access synth_ - ignore all events that affect synth_
MidiEventInput evinput = midi_event_input();
for (const auto &ev : evinput)
{
if (ev.message() == MidiMessage::PARAM_VALUE) // still track instrument changes, though
{
apply_event (ev);
adjust_param (ev.param);
}
}
float *left_out = oblock (stereo_out_, 0);
float *right_out = oblock (stereo_out_, 1);
floatfill (left_out, 0.f, n_frames);
floatfill (right_out, 0.f, n_frames);
}
}
public:
LiquidSFZ (const ProcessorSetup &psetup) :
AudioProcessor (psetup),
loader_ (synth_)
{}
static void
static_info (AudioProcessorInfo &info)
{
info.version = "1";
info.label = "LiquidSFZ";
info.category = "Synth";
info.creator_name = "Stefan Westerfeld";
info.website_url = "https://anklang.testbit.eu";
}
};
static auto liquidsfz = register_audio_processor<LiquidSFZ> ("Ase::Devices::LiquidSFZ");
} // Bse