Skip to content

Commit 72174b3

Browse files
committed
Improve parameter handling in clap plugin
1 parent f0cb88d commit 72174b3

File tree

4 files changed

+109
-61
lines changed

4 files changed

+109
-61
lines changed

NeuralRack/clap/NeuralRack.cc

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,17 @@ class NeuralRack
5959
}
6060

6161
void registerParameters() {
62+
// name group min, max, def, step value isStepped type
6263
param.registerParam("Buffered Mode", "Global", 0,2,0,1, (void*)&engine.buffered, true, Is_FLOAT);
6364
param.registerParam("Enable", "Global", 0,1,0,1, (void*)&engine.bypass, true, IS_UINT);
6465

65-
param.registerParam("Gate Enable", "NoiseGate",0,1,0,1, (void*)&engine.ngOnOff, true, IS_UINT);
66+
param.registerParam("Gate Enable", "NoiseGate", 0,1,0,1, (void*)&engine.ngOnOff, true, IS_UINT);
6667
param.registerParam("Gate Thresh", "NoiseGate", 0.01, 0.31, 0.017, 0.001, (void*)&engine.ngate->threshold, false, Is_FLOAT);
6768

6869
param.registerParam("Norm Slot A", "Pedal", 0,1,0,1, (void*)&engine.normSlotA, true, IS_INT);
6970
param.registerParam("Input Gain A", "Pedal", -20,20,0,0.1, (void*)&engine.inputGain, false, Is_FLOAT);
7071
param.registerParam("Output Gain A", "Pedal", -20,20,0,0.1, (void*)&engine.outputGain, false, Is_FLOAT);
7172

72-
param.registerParam("Norm Slot B", "Amp", 0,1,0,1, (void*)&engine.normSlotB, true, IS_INT);
73-
param.registerParam("Input Gain B", "Amp", -20,20,0,0.1, (void*)&engine.inputGain1, false, Is_FLOAT);
74-
param.registerParam("Output Gain B", "Amp", -20,20,0,0.1, (void*)&engine.outputGain1, false, Is_FLOAT);
75-
7673
param.registerParam("EQ Enable", "EQ", 0,1,0,1, (void*)&engine.eqOnOff, true, IS_UINT);
7774
param.registerParam("EQ Band 1", "EQ", -20,20,0,0.1, (void*)&engine.peq->fVslider1, false, Is_FLOAT);
7875
param.registerParam("EQ Band 2", "EQ", -20,20,0,0.1, (void*)&engine.peq->fVslider0, false, Is_FLOAT);
@@ -81,6 +78,10 @@ class NeuralRack
8178
param.registerParam("EQ Band 5", "EQ", -20,20,0,0.1, (void*)&engine.peq->fVslider4, false, Is_FLOAT);
8279
param.registerParam("EQ Band 6", "EQ", -20,20,0,0.1, (void*)&engine.peq->fVslider5, false, Is_FLOAT);
8380

81+
param.registerParam("Norm Slot B", "Amp", 0,1,0,1, (void*)&engine.normSlotB, true, IS_INT);
82+
param.registerParam("Input Gain B", "Amp", -20,20,0,0.1, (void*)&engine.inputGain1, false, Is_FLOAT);
83+
param.registerParam("Output Gain B", "Amp", -20,20,0,0.1, (void*)&engine.outputGain1, false, Is_FLOAT);
84+
8485
param.registerParam("IR Out Gain L", "IR", -20,20,0,0.1, (void*)&engine.IRoutputGain, false, Is_FLOAT);
8586
param.registerParam("IR Out Gain R", "IR", -20,20,0,0.1, (void*)&engine.IRoutputGain1, false, Is_FLOAT);
8687
}
@@ -168,13 +169,11 @@ class NeuralRack
168169
widget_hide(TopWin);
169170
firstLoop = false;
170171
}
171-
172-
void quitMain() {
173-
main_quit(&ui->main);
174-
}
175172

176173
void quitGui() {
177174
fetch.stop();
175+
cleanup();
176+
main_quit(&ui->main);
178177
}
179178

180179
void runGui() {
@@ -237,6 +236,7 @@ class NeuralRack
237236
void initEngine(uint32_t rate, int32_t prio, int32_t policy) {
238237
engine.init(rate, prio, policy);
239238
initEQ();
239+
engine.bypass = 1;
240240
s_time = (1.0 / (double)rate) * 1000;
241241
}
242242

@@ -282,19 +282,24 @@ class NeuralRack
282282
// 0 + 1 audio ports
283283
case 2:
284284
engine.inputGain = value;
285+
param.setParamDirty(5 , true);
285286
break;
286287
case 3:
287288
engine.outputGain = value;
289+
param.setParamDirty(6 , true);
288290
break;
289291
case 4:
290292
engine.outputGain1 = value;
293+
param.setParamDirty(16 , true);
291294
break;
292295
// 5 + 6 atom ports
293296
case 7:
294297
engine.IRoutputGain = value;
298+
param.setParamDirty(17 , true);
295299
break;
296300
case 8:
297301
engine.IRoutputGain1 = value;
302+
param.setParamDirty(18 , true);
298303
break;
299304
case 9:
300305
{
@@ -316,15 +321,19 @@ class NeuralRack
316321
break;
317322
case 11:
318323
engine.inputGain1 = value;
324+
param.setParamDirty(15 , true);
319325
break;
320326
case 12:
321327
engine.normSlotA = static_cast<int32_t>(value);
328+
param.setParamDirty(4 , true);
322329
break;
323330
case 13:
324331
engine.normSlotB = static_cast<int32_t>(value);
332+
param.setParamDirty(14 , true);
325333
break;
326334
case 14:
327335
engine.bypass = static_cast<uint32_t>(value);
336+
param.setParamDirty(1 , true);
328337
break;
329338
case 15:
330339
{
@@ -359,34 +368,44 @@ class NeuralRack
359368
{
360369
engine.buffered = value;
361370
engine._notify_ui.store(true, std::memory_order_release);
371+
param.setParamDirty(0 , true);
362372
}
363373
break;
364374
case 24:
365375
engine.peq->fVslider1 = value;
376+
param.setParamDirty(8 , true);
366377
break;
367378
case 25:
368379
engine.peq->fVslider0 = value;
380+
param.setParamDirty(9 , true);
369381
break;
370382
case 26:
371383
engine.peq->fVslider2 = value;
384+
param.setParamDirty(10 , true);
372385
break;
373386
case 27:
374387
engine.peq->fVslider3 = value;
388+
param.setParamDirty(11 , true);
375389
break;
376390
case 28:
377391
engine.peq->fVslider4 = value;
392+
param.setParamDirty(12 , true);
378393
break;
379394
case 29:
380395
engine.peq->fVslider5 = value;
396+
param.setParamDirty(13 , true);
381397
break;
382398
case 30:
383399
engine.eqOnOff = static_cast<uint32_t>(value);
400+
param.setParamDirty(7 , true);
384401
break;
385402
case 31:
386403
engine.ngate->threshold = value;
404+
param.setParamDirty(3 , true);
387405
break;
388406
case 32:
389407
engine.ngOnOff = static_cast<uint32_t>(value);
408+
param.setParamDirty(2 , true);
390409
break;
391410
default:
392411
break;

NeuralRack/clap/NeuralRackClap.cpp

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ struct neuralrack_plugin_t {
3636
clap_plugin_t plugin;
3737
const clap_host_t *host;
3838
NeuralRack *r;
39+
std::string state;
40+
bool isInited;
3941
bool guiIsCreated;
4042
uint32_t latency;
4143
uint32_t width;
@@ -48,13 +50,13 @@ struct neuralrack_plugin_t {
4850

4951
static uint32_t params_count(const clap_plugin_t* plugin) {
5052
neuralrack_plugin_t *plug = (neuralrack_plugin_t *)plugin->plugin_data;
51-
return (uint32_t)plug->r->param.parameter.size();
53+
return (uint32_t)plug->r->param.getParamCount();
5254
}
5355

5456
static bool params_get_info(const clap_plugin_t* plugin, uint32_t param_index, clap_param_info_t* param_info) {
5557
neuralrack_plugin_t *plug = (neuralrack_plugin_t *)plugin->plugin_data;
56-
if (param_index >= plug->r->param.parameter.size()) return false;
57-
const auto& def = plug->r->param.parameter[param_index];
58+
if (param_index >= plug->r->param.getParamCount()) return false;
59+
const auto& def = plug->r->param.getParameter(param_index);
5860
memset(param_info, 0, sizeof(*param_info));
5961
param_info->id = def.id;
6062
strncpy(param_info->name, def.name.c_str(), CLAP_NAME_SIZE-1);
@@ -71,21 +73,21 @@ static bool params_get_info(const clap_plugin_t* plugin, uint32_t param_index, c
7173

7274
static bool params_get_value(const clap_plugin_t* plugin, clap_id param_id, double* value) {
7375
neuralrack_plugin_t *plug = (neuralrack_plugin_t *)plugin->plugin_data;
74-
if (param_id < 0 || param_id >= plug->r->param.parameter.size()) return false;
76+
if (param_id < 0 || param_id >= plug->r->param.getParamCount()) return false;
7577
*value = plug->r->param.getParam(param_id);
7678
return true;
7779
}
7880

7981
static bool params_value_to_text(const clap_plugin_t* plugin, clap_id param_id, double value, char* out, uint32_t size) {
8082
neuralrack_plugin_t *plug = (neuralrack_plugin_t *)plugin->plugin_data;
81-
if (param_id < 0 || param_id >= plug->r->param.parameter.size()) return false;
83+
if (param_id < 0 || param_id >= plug->r->param.getParamCount()) return false;
8284
snprintf(out, size, "%.2f", value);
8385
return true;
8486
}
8587

8688
static bool params_text_to_value(const clap_plugin_t* plugin, clap_id param_id, const char* text, double* out_value) {
8789
neuralrack_plugin_t *plug = (neuralrack_plugin_t *)plugin->plugin_data;
88-
if (param_id < 0 || param_id >= plug->r->param.parameter.size()) return false;
90+
if (param_id < 0 || param_id >= plug->r->param.getParamCount()) return false;
8991
*out_value = atof(text);
9092
return true;
9193
}
@@ -105,21 +107,24 @@ static void sync_params_to_plug(const clap_plugin_t *plugin, const clap_event_he
105107

106108
static void sync_params_to_host(const clap_plugin_t *plugin, const clap_output_events_t *out) {
107109
neuralrack_plugin_t *plug = (neuralrack_plugin_t *)plugin->plugin_data;
108-
for (uint32_t i = 0; i < plug->r->param.parameter.size(); i++) {
109-
clap_event_param_value_t event = {};
110-
event.header.size = sizeof(event);
111-
event.header.time = 0;
112-
event.header.space_id = CLAP_CORE_EVENT_SPACE_ID;
113-
event.header.type = CLAP_EVENT_PARAM_VALUE;
114-
event.header.flags = 0;
115-
event.param_id = i;
116-
event.cookie = NULL;
117-
event.note_id = -1;
118-
event.port_index = -1;
119-
event.channel = -1;
120-
event.key = -1;
121-
event.value = plug->r->param.getParam(i);
122-
out->try_push(out, &event.header);
110+
for (int i = 0; i < plug->r->param.getParamCount(); i++) {
111+
if (plug->r->param.isParamDirty(i)) {
112+
clap_event_param_value_t event = {};
113+
event.header.size = sizeof(event);
114+
event.header.time = 0;
115+
event.header.space_id = CLAP_CORE_EVENT_SPACE_ID;
116+
event.header.type = CLAP_EVENT_PARAM_VALUE;
117+
event.header.flags = 0;
118+
event.param_id = i;
119+
event.cookie = NULL;
120+
event.note_id = -1;
121+
event.port_index = -1;
122+
event.channel = -1;
123+
event.key = -1;
124+
event.value = plug->r->param.getParam(i);
125+
out->try_push(out, &event.header);
126+
plug->r->param.setParamDirty(i, false);
127+
}
123128
}
124129
}
125130

@@ -131,7 +136,7 @@ static void params_flush(const clap_plugin_t *plugin,
131136
const clap_event_header_t *ev = in->get(in, i);
132137
if (ev->type == CLAP_EVENT_PARAM_VALUE) {
133138
auto *p = (const clap_event_param_value_t *)ev;
134-
if (p->param_id >= 0 && p->param_id < plug->r->param.parameter.size()) {
139+
if (p->param_id >= 0 && p->param_id < plug->r->param.getParamCount()) {
135140
plug->r->param.setParam(p->param_id, p->value);
136141
}
137142
}
@@ -197,20 +202,19 @@ static const clap_plugin_latency_t latency_extension = {
197202
// State Management
198203
static bool neuralrack_state_save(const clap_plugin_t *plugin, const clap_ostream_t *stream) {
199204
neuralrack_plugin_t *plug = (neuralrack_plugin_t *)plugin->plugin_data;
200-
std::string state;
201-
plug->r->saveState(&state);
202-
stream->write(stream, state.c_str(), strlen(state.c_str()));
205+
plug->r->saveState(&plug->state);
206+
stream->write(stream, plug->state.c_str(), strlen(plug->state.c_str()));
203207
return true;
204208
}
205209

206210
static bool neuralrack_state_load(const clap_plugin_t *plugin, const clap_istream_t *stream) {
207211
neuralrack_plugin_t *plug = (neuralrack_plugin_t *)plugin->plugin_data;
208-
char state[2048];
209-
char *curr = state;
212+
char _state[2048];
213+
char *curr = _state;
210214
int thisread = stream->read(stream, curr, 2048);
211215
if (thisread < 0) return false;
212-
std::string stream_ = state ;
213-
plug->r->readState(stream_);
216+
plug->state = _state ;
217+
if(plug->isInited) plug->r->readState(plug->state);
214218
return true;
215219
}
216220

@@ -274,7 +278,6 @@ static bool neuralrack_gui_create(const clap_plugin *plugin, const char *api, bo
274278
if (strcmp(api, GUIAPI) == 0) {
275279
if (!plug->guiIsCreated) {
276280
plug->r->startGui();
277-
plug->r->enableEngine(1);
278281
}
279282
plug->guiIsCreated = true;
280283
return true;
@@ -285,10 +288,8 @@ static bool neuralrack_gui_create(const clap_plugin *plugin, const char *api, bo
285288
static void neuralrack_gui_destroy(const clap_plugin *plugin) {
286289
neuralrack_plugin_t *plug = (neuralrack_plugin_t *)plugin->plugin_data;
287290
if (plug->guiIsCreated) {
288-
plug->r->cleanup();
289-
plug->r->quitMain();
290-
}
291-
plug->r->quitGui();
291+
plug->r->quitGui();
292+
}
292293
plug->guiIsCreated = false;
293294
}
294295

@@ -313,15 +314,13 @@ static bool neuralrack_gui_set_parent(const clap_plugin_t *plugin, const clap_wi
313314
#else
314315
plug->r->startGui(window->x11);
315316
#endif
316-
plug->r->enableEngine(1);
317317
}
318318
plug->guiIsCreated = true;
319319
#if defined(_WIN32)
320320
plug->r->setParent((Window)window->win32);
321321
#else
322322
plug->r->setParent(window->x11);
323323
#endif
324-
//plug->r->showGui();
325324
return true;
326325
}
327326

@@ -362,8 +361,8 @@ static const clap_plugin_gui_t extensionGUI = {
362361

363362
// Initialize the plugin
364363
static bool neuralrack_init(const clap_plugin_t *plugin) {
365-
neuralrack_plugin_t *plug = (neuralrack_plugin_t *)plugin->plugin_data;
366-
plug->r->initEngine(48000, 25, 1);
364+
//neuralrack_plugin_t *plug = (neuralrack_plugin_t *)plugin->plugin_data;
365+
//plug->r->initEngine(48000, 25, 1);
367366
return true;
368367
}
369368

@@ -424,18 +423,23 @@ static clap_process_status neuralrack_process(const clap_plugin_t *plugin, const
424423
return CLAP_PROCESS_CONTINUE;
425424
}
426425

427-
// Finally get the sample rate and re-init the engine
426+
// Finally get the sample rate and init the engine
428427
static bool neuralrack_activate(const struct clap_plugin *plugin,
429428
double sample_rate,
430429
uint32_t min_frames_count,
431430
uint32_t max_frames_count) {
432431
neuralrack_plugin_t *plug = (neuralrack_plugin_t *)plugin->plugin_data;
433-
//if (sample_rate != 48000)
434432
plug->r->initEngine(sample_rate, 25, 1);
433+
plug->isInited = true;
434+
if(!plug->state.empty()) plug->r->readState(plug->state);
435435
return true;
436436
}
437437

438-
static void neuralrack_deactivate(const struct clap_plugin *plugin) {}
438+
// clear the state string when we get deactivated
439+
static void neuralrack_deactivate(const struct clap_plugin *plugin) {
440+
neuralrack_plugin_t *plug = (neuralrack_plugin_t *)plugin->plugin_data;
441+
if(!plug->state.empty()) plug->state.clear();
442+
}
439443

440444
static bool neuralrack_start_processing(const struct clap_plugin *plugin) { return true; }
441445

@@ -473,6 +477,7 @@ static const clap_plugin_t *neuralrack_create(const clap_host_t *host) {
473477
if (!plug) return NULL;
474478
plug->r = new NeuralRack();
475479
plug->guiIsCreated = false;
480+
plug->isInited = false;
476481
plug->width = WINDOW_WIDTH;
477482
plug->height = WINDOW_HEIGHT;
478483
plug->plugin.desc = &neuralrack_descriptor;

0 commit comments

Comments
 (0)