-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
effectchain.cpp
441 lines (374 loc) · 16.1 KB
/
effectchain.cpp
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include "effects/effectchain.h"
#include "control/controlencoder.h"
#include "control/controlpotmeter.h"
#include "control/controlpushbutton.h"
#include "effects/effectslot.h"
#include "effects/effectsmanager.h"
#include "effects/effectsmessenger.h"
#include "effects/presets/effectchainpresetmanager.h"
#include "engine/effects/engineeffectchain.h"
#include "engine/engine.h"
#include "mixer/playermanager.h"
#include "moc_effectchain.cpp"
#include "util/defs.h"
#include "util/math.h"
#include "util/sample.h"
#include "util/xml.h"
EffectChain::EffectChain(const QString& group,
EffectsManager* pEffectsManager,
EffectsMessengerPointer pEffectsMessenger,
SignalProcessingStage stage)
: // The control group names are 1-indexed while internally everything
// is 0-indexed.
m_presetName(""),
m_pEffectsManager(pEffectsManager),
m_pChainPresetManager(pEffectsManager->getChainPresetManager()),
m_pMessenger(pEffectsMessenger),
m_group(group),
m_signalProcessingStage(stage),
m_pEngineEffectChain(nullptr) {
// qDebug() << "EffectChain::EffectChain " << group << ' ' << iChainNumber;
m_pControlClear = std::make_unique<ControlPushButton>(ConfigKey(m_group, "clear"));
connect(m_pControlClear.get(),
&ControlObject::valueChanged,
this,
&EffectChain::slotControlClear);
m_pControlNumEffectSlots = std::make_unique<ControlObject>(
ConfigKey(m_group, "num_effectslots"));
m_pControlNumEffectSlots->setReadOnly();
m_pControlNumChainPresets = std::make_unique<ControlObject>(
ConfigKey(m_group, "num_chain_presets"));
m_pControlNumChainPresets->set(m_pChainPresetManager->numPresets());
m_pControlNumChainPresets->setReadOnly();
connect(m_pChainPresetManager.data(),
&EffectChainPresetManager::effectChainPresetRenamed,
this,
&EffectChain::slotEffectChainPresetRenamed);
connect(m_pChainPresetManager.data(),
&EffectChainPresetManager::effectChainPresetListUpdated,
this,
&EffectChain::slotPresetListUpdated);
m_pControlChainEnabled =
std::make_unique<ControlPushButton>(ConfigKey(m_group, "enabled"));
m_pControlChainEnabled->setButtonMode(ControlPushButton::POWERWINDOW);
// Default to enabled. The skin might not show these buttons.
m_pControlChainEnabled->setDefaultValue(true);
m_pControlChainEnabled->set(true);
connect(m_pControlChainEnabled.get(),
&ControlObject::valueChanged,
this,
&EffectChain::sendParameterUpdate);
m_pControlChainMix = std::make_unique<ControlPotmeter>(
ConfigKey(m_group, "mix"), 0.0, 1.0, false, true, false, true, 1.0);
m_pControlChainMix->setDefaultValue(0.0);
m_pControlChainMix->set(0.0);
connect(m_pControlChainMix.get(),
&ControlObject::valueChanged,
this,
&EffectChain::sendParameterUpdate);
m_pControlChainSuperParameter = std::make_unique<ControlPotmeter>(
ConfigKey(m_group, "super1"), 0.0, 1.0);
// QObject::connect cannot connect to slots with optional parameters using function
// pointer syntax if the slot has more parameters than the signal, so use a lambda
// to hack around this limitation.
connect(m_pControlChainSuperParameter.get(),
&ControlObject::valueChanged,
this,
[=, this](double value) { slotControlChainSuperParameter(value, false); });
m_pControlChainSuperParameter->set(0.0);
m_pControlChainSuperParameter->setDefaultValue(0.0);
m_pControlChainMixMode =
std::make_unique<ControlPushButton>(ConfigKey(m_group, "mix_mode"));
m_pControlChainMixMode->setButtonMode(ControlPushButton::TOGGLE);
m_pControlChainMixMode->setStates(EffectChainMixMode::kNumModes);
double mixModeCODefault = static_cast<double>(EffectChainMixMode::DrySlashWet);
m_pControlChainMixMode->setDefaultValue(mixModeCODefault);
m_pControlChainMixMode->set(mixModeCODefault);
connect(m_pControlChainMixMode.get(),
&ControlObject::valueChanged,
this,
&EffectChain::sendParameterUpdate);
m_pControlLoadedChainPreset = std::make_unique<ControlObject>(
ConfigKey(m_group, "loaded_chain_preset"), false);
m_pControlLoadedChainPreset->connectValueChangeRequest(
this,
&EffectChain::slotControlLoadedChainPresetRequest);
m_pControlNextChainPreset = std::make_unique<ControlPushButton>(
ConfigKey(m_group, "next_chain_preset"));
connect(m_pControlNextChainPreset.get(),
&ControlObject::valueChanged,
this,
&EffectChain::slotControlNextChainPreset);
ControlDoublePrivate::insertAlias(ConfigKey(m_group, "next_chain"),
ConfigKey(m_group, "next_chain_preset"));
m_pControlPrevChainPreset = std::make_unique<ControlPushButton>(
ConfigKey(m_group, "prev_chain_preset"));
connect(m_pControlPrevChainPreset.get(),
&ControlObject::valueChanged,
this,
&EffectChain::slotControlPrevChainPreset);
ControlDoublePrivate::insertAlias(ConfigKey(m_group, "prev_chain"),
ConfigKey(m_group, "prev_chain_preset"));
// Ignoring no-ops is important since this is for +/- tickers.
m_pControlChainPresetSelector = std::make_unique<ControlEncoder>(
ConfigKey(m_group, "chain_preset_selector"), false);
connect(m_pControlChainPresetSelector.get(),
&ControlObject::valueChanged,
this,
&EffectChain::slotControlChainPresetSelector);
ControlDoublePrivate::insertAlias(ConfigKey(m_group, "chain_selector"),
ConfigKey(m_group, "chain_preset_selector"));
// ControlObjects for skin <-> controller mapping interaction.
// Refer to comment in header for full explanation.
m_pControlChainShowFocus = std::make_unique<ControlPushButton>(
ConfigKey(m_group, "show_focus"));
m_pControlChainShowFocus->setButtonMode(ControlPushButton::TOGGLE);
m_pControlChainHasControllerFocus = std::make_unique<ControlPushButton>(
ConfigKey(m_group, "controller_input_active"));
m_pControlChainHasControllerFocus->setButtonMode(ControlPushButton::TOGGLE);
m_pControlChainShowParameters = std::make_unique<ControlPushButton>(
ConfigKey(m_group, "show_parameters"),
true);
m_pControlChainShowParameters->setButtonMode(ControlPushButton::TOGGLE);
m_pControlChainFocusedEffect = std::make_unique<ControlPushButton>(
ConfigKey(m_group, "focused_effect"),
true);
m_pControlChainFocusedEffect->setButtonMode(ControlPushButton::TOGGLE);
addToEngine();
}
EffectChain::~EffectChain() {
m_effectSlots.clear();
removeFromEngine();
}
void EffectChain::addToEngine() {
VERIFY_OR_DEBUG_ASSERT(!m_pEngineEffectChain) {
return;
}
m_pEngineEffectChain = new EngineEffectChain(
m_group,
m_pEffectsManager->registeredInputChannels(),
m_pEffectsManager->registeredOutputChannels());
EffectsRequest* pRequest = new EffectsRequest();
pRequest->type = EffectsRequest::ADD_EFFECT_CHAIN;
pRequest->AddEffectChain.signalProcessingStage = m_signalProcessingStage;
pRequest->AddEffectChain.pChain = m_pEngineEffectChain;
m_pMessenger->writeRequest(pRequest);
sendParameterUpdate();
}
void EffectChain::removeFromEngine() {
VERIFY_OR_DEBUG_ASSERT(m_effectSlots.isEmpty()) {
m_effectSlots.clear();
}
VERIFY_OR_DEBUG_ASSERT(m_pEngineEffectChain) {
return;
}
EffectsRequest* pRequest = new EffectsRequest();
pRequest->type = EffectsRequest::REMOVE_EFFECT_CHAIN;
pRequest->RemoveEffectChain.signalProcessingStage = m_signalProcessingStage;
pRequest->RemoveEffectChain.pChain = m_pEngineEffectChain;
m_pMessenger->writeRequest(pRequest);
m_pEngineEffectChain = nullptr;
}
const QString& EffectChain::presetName() const {
return m_presetName;
}
void EffectChain::loadChainPreset(EffectChainPresetPointer pPreset) {
slotControlClear(1);
VERIFY_OR_DEBUG_ASSERT(pPreset) {
return;
}
int effectSlotIndex = 0;
for (const auto& pEffectPreset : pPreset->effectPresets()) {
EffectSlotPointer pEffectSlot = m_effectSlots.at(effectSlotIndex);
pEffectSlot->loadEffectFromPreset(pEffectPreset);
effectSlotIndex++;
}
setMixMode(pPreset->mixMode());
m_pControlChainSuperParameter->setDefaultValue(pPreset->superKnob());
m_presetName = pPreset->name();
emit chainPresetChanged(m_presetName);
setControlLoadedPresetIndex(presetIndex());
}
void EffectChain::sendParameterUpdate() {
EffectsRequest* pRequest = new EffectsRequest();
pRequest->type = EffectsRequest::SET_EFFECT_CHAIN_PARAMETERS;
pRequest->pTargetChain = m_pEngineEffectChain;
pRequest->SetEffectChainParameters.enabled = m_pControlChainEnabled->toBool();
pRequest->SetEffectChainParameters.mix_mode = mixMode();
pRequest->SetEffectChainParameters.mix = m_pControlChainMix->get();
m_pMessenger->writeRequest(pRequest);
}
QString EffectChain::group() const {
return m_group;
}
double EffectChain::getSuperParameter() const {
return m_pControlChainSuperParameter->get();
}
void EffectChain::setSuperParameter(double value, bool force) {
m_pControlChainSuperParameter->set(value);
slotControlChainSuperParameter(value, force);
}
EffectChainMixMode::Type EffectChain::mixMode() const {
return static_cast<EffectChainMixMode::Type>(
static_cast<int>(
m_pControlChainMixMode->get()));
}
void EffectChain::setMixMode(EffectChainMixMode::Type mixMode) {
m_pControlChainMixMode->set(static_cast<double>(mixMode));
sendParameterUpdate();
}
EffectSlotPointer EffectChain::addEffectSlot(const QString& group) {
if (kEffectDebugOutput) {
qDebug() << debugString() << "addEffectSlot" << group;
}
EffectSlotPointer pEffectSlot = EffectSlotPointer(new EffectSlot(group,
m_pEffectsManager,
m_pMessenger,
m_effectSlots.size(),
this,
m_pEngineEffectChain));
m_effectSlots.append(pEffectSlot);
int numEffectSlots = static_cast<int>(m_pControlNumEffectSlots->get()) + 1;
m_pControlNumEffectSlots->forceSet(numEffectSlots);
m_pControlChainFocusedEffect->setStates(numEffectSlots);
return pEffectSlot;
}
int EffectChain::numPresets() const {
VERIFY_OR_DEBUG_ASSERT(m_pChainPresetManager) {
return 0;
}
return m_pChainPresetManager->numPresets();
}
void EffectChain::registerInputChannel(const ChannelHandleAndGroup& handleGroup,
const double initialValue) {
VERIFY_OR_DEBUG_ASSERT(!m_channelEnableButtons.contains(handleGroup)) {
return;
}
auto pEnableControl = std::make_shared<ControlPushButton>(
ConfigKey(m_group, QString("group_%1_enable").arg(handleGroup.name())),
true,
initialValue);
m_channelEnableButtons.insert(handleGroup, pEnableControl);
pEnableControl->setButtonMode(ControlPushButton::POWERWINDOW);
if (pEnableControl->toBool()) {
enableForInputChannel(handleGroup);
}
connect(pEnableControl.get(),
&ControlObject::valueChanged,
this,
[this, handleGroup](double value) { slotChannelStatusChanged(value, handleGroup); });
}
EffectSlotPointer EffectChain::getEffectSlot(unsigned int slotNumber) {
//qDebug() << debugString() << "getEffectSlot" << slotNumber;
VERIFY_OR_DEBUG_ASSERT(slotNumber <= static_cast<unsigned int>(m_effectSlots.size())) {
return EffectSlotPointer();
}
return m_effectSlots[slotNumber];
}
void EffectChain::slotControlClear(double v) {
for (EffectSlotPointer pEffectSlot : std::as_const(m_effectSlots)) {
pEffectSlot->slotClear(v);
}
}
void EffectChain::slotControlChainSuperParameter(double v, bool force) {
// qDebug() << debugString() << "slotControlChainSuperParameter" << v;
m_pControlChainSuperParameter->set(v);
for (const auto& pEffectSlot : std::as_const(m_effectSlots)) {
pEffectSlot->setMetaParameter(v, force);
}
}
void EffectChain::slotControlChainPresetSelector(double value) {
int index = presetIndex();
if (value > 0) {
index++;
} else if (value < 0) {
index--;
} else {
// Do not reload the current preset when set to 0.
return;
}
loadChainPreset(presetAtIndex(index));
}
void EffectChain::slotControlLoadedChainPresetRequest(double value) {
// subtract 1 to make the ControlObject 1-indexed like other ControlObjects
int index = static_cast<int>(value) - 1;
if (index < 0 || index >= numPresets()) {
return;
}
// loadChainPreset calls setAndConfirm
loadChainPreset(presetAtIndex(index));
}
void EffectChain::setControlLoadedPresetIndex(uint index) {
// add 1 to make the ControlObject 1-indexed like other ControlObjects
m_pControlLoadedChainPreset->setAndConfirm(index + 1);
}
void EffectChain::slotControlNextChainPreset(double value) {
if (value > 0) {
loadChainPreset(presetAtIndex(presetIndex() + 1));
}
}
void EffectChain::slotControlPrevChainPreset(double value) {
if (value > 0) {
loadChainPreset(presetAtIndex(presetIndex() - 1));
}
}
void EffectChain::slotChannelStatusChanged(
double value, const ChannelHandleAndGroup& handleGroup) {
if (value > 0) {
enableForInputChannel(handleGroup);
} else {
disableForInputChannel(handleGroup);
}
}
void EffectChain::slotEffectChainPresetRenamed(const QString& oldName, const QString& newName) {
if (m_presetName == oldName) {
m_presetName = newName;
}
}
void EffectChain::slotPresetListUpdated() {
setControlLoadedPresetIndex(presetIndex());
m_pControlNumChainPresets->forceSet(numPresets());
}
void EffectChain::enableForInputChannel(const ChannelHandleAndGroup& handleGroup) {
if (m_enabledInputChannels.contains(handleGroup)) {
return;
}
EffectsRequest* request = new EffectsRequest();
request->type = EffectsRequest::ENABLE_EFFECT_CHAIN_FOR_INPUT_CHANNEL;
request->pTargetChain = m_pEngineEffectChain;
request->EnableInputChannelForChain.channelHandle = handleGroup.handle();
// Allocate EffectStates here in the main thread to avoid allocating
// memory in the realtime audio callback thread. Pointers to the
// EffectStates are passed to the EffectRequest and the EffectProcessorImpls
// store the pointers. The containers of EffectState* pointers get deleted
// by ~EffectsRequest, but the EffectStates are managed by EffectProcessorImpl.
// The EffectStates for one EngineEffectChain must be sent all together in
// the same message using an EffectStatesMapArray. If they were separated
// into a message for each effect, there would be a chance that the
// EngineEffectChain could get activated in one cycle of the audio callback
// thread but the EffectStates for an EngineEffect would not be received by
// EngineEffectsManager until the next audio callback cycle.
auto* pEffectStatesMapArray = new EffectStatesMapArray;
for (int i = 0; i < m_effectSlots.size(); ++i) {
m_effectSlots[i]->fillEffectStatesMap(&(*pEffectStatesMapArray)[i]);
}
request->EnableInputChannelForChain.pEffectStatesMapArray = pEffectStatesMapArray;
m_pMessenger->writeRequest(request);
m_enabledInputChannels.insert(handleGroup);
}
void EffectChain::disableForInputChannel(const ChannelHandleAndGroup& handleGroup) {
if (!m_enabledInputChannels.remove(handleGroup)) {
return;
}
EffectsRequest* request = new EffectsRequest();
request->type = EffectsRequest::DISABLE_EFFECT_CHAIN_FOR_INPUT_CHANNEL;
request->pTargetChain = m_pEngineEffectChain;
request->DisableInputChannelForChain.channelHandle = handleGroup.handle();
m_pMessenger->writeRequest(request);
}
int EffectChain::presetIndex() const {
return m_pChainPresetManager->presetIndex(m_presetName);
}
EffectChainPresetPointer EffectChain::presetAtIndex(int index) const {
return m_pChainPresetManager->presetAtIndex(index);
}