This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
carla-patchbay-wrapper.c
542 lines (439 loc) · 13.8 KB
/
carla-patchbay-wrapper.c
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
* Carla plugin for OBS
* Copyright (C) 2023 Filipe Coelho <falktx@falktx.com>
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "carla-wrapper.h"
#include "common.h"
#include "qtutils.h"
#include <util/platform.h>
// IDE helpers, must match cmake config
// #define CARLA_PLUGIN_BUILD 1
// #define HAVE_X11 1
#define REAL_BUILD 1
#define STATIC_PLUGIN_TARGET 1
#include "CarlaNativePlugin.h"
// generates a warning if this is defined as anything else
#define CARLA_API
// ----------------------------------------------------------------------------
// helper methods
struct carla_main_thread_param_change {
const NativePluginDescriptor *descriptor;
NativePluginHandle handle;
uint32_t index;
float value;
};
static void carla_main_thread_param_change(void *data)
{
struct carla_main_thread_param_change *priv = data;
priv->descriptor->ui_set_parameter_value(priv->handle, priv->index,
priv->value);
bfree(data);
}
// ----------------------------------------------------------------------------
// private data methods
struct carla_param_data {
uint32_t hints;
float min, max;
};
struct carla_priv {
obs_source_t *source;
uint32_t bufferSize;
double sampleRate;
const NativePluginDescriptor *descriptor;
NativePluginHandle handle;
NativeHostDescriptor host;
NativeTimeInfo timeInfo;
// cached parameter info
uint32_t paramCount;
struct carla_param_data *paramDetails;
// update properties when timeout is reached, 0 means do nothing
uint64_t update_request;
// keep track of active state
volatile bool activated;
};
// ----------------------------------------------------------------------------
// carla host methods
static uint32_t host_get_buffer_size(NativeHostHandle handle)
{
const struct carla_priv *priv = handle;
return priv->bufferSize;
}
static double host_get_sample_rate(NativeHostHandle handle)
{
const struct carla_priv *priv = handle;
return priv->sampleRate;
}
static bool host_is_offline(NativeHostHandle handle)
{
UNUSED_PARAMETER(handle);
return false;
}
static const NativeTimeInfo *host_get_time_info(NativeHostHandle handle)
{
const struct carla_priv *priv = handle;
return &priv->timeInfo;
}
static bool host_write_midi_event(NativeHostHandle handle,
const NativeMidiEvent *event)
{
UNUSED_PARAMETER(handle);
UNUSED_PARAMETER(event);
return false;
}
static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index,
float value)
{
struct carla_priv *priv = handle;
if (index >= priv->paramCount)
return;
// skip parameters that we do not show
const uint32_t hints = priv->paramDetails[index].hints;
if ((hints & NATIVE_PARAMETER_IS_ENABLED) == 0)
return;
if (hints & NATIVE_PARAMETER_IS_OUTPUT)
return;
char pname[PARAM_NAME_SIZE] = PARAM_NAME_INIT;
param_index_to_name(index, pname);
obs_source_t *source = priv->source;
obs_data_t *settings = obs_source_get_settings(source);
/**/ if (hints & NATIVE_PARAMETER_IS_BOOLEAN)
obs_data_set_bool(settings, pname, value > 0.5f ? 1.f : 0.f);
else if (hints & NATIVE_PARAMETER_IS_INTEGER)
obs_data_set_int(settings, pname, (int)value);
else
obs_data_set_double(settings, pname, value);
obs_data_release(settings);
postpone_update_request(&priv->update_request);
}
static void host_ui_midi_program_changed(NativeHostHandle handle,
uint8_t channel, uint32_t bank,
uint32_t program)
{
UNUSED_PARAMETER(handle);
UNUSED_PARAMETER(channel);
UNUSED_PARAMETER(bank);
UNUSED_PARAMETER(program);
}
static void host_ui_custom_data_changed(NativeHostHandle handle,
const char *key, const char *value)
{
UNUSED_PARAMETER(handle);
UNUSED_PARAMETER(key);
UNUSED_PARAMETER(value);
}
static void host_ui_closed(NativeHostHandle handle)
{
UNUSED_PARAMETER(handle);
}
static const char *host_ui_open_file(NativeHostHandle handle, bool isDir,
const char *title, const char *filter)
{
UNUSED_PARAMETER(handle);
return carla_qt_file_dialog(false, isDir, title, filter);
}
static const char *host_ui_save_file(NativeHostHandle handle, bool isDir,
const char *title, const char *filter)
{
UNUSED_PARAMETER(handle);
return carla_qt_file_dialog(true, isDir, title, filter);
}
static intptr_t host_dispatcher(NativeHostHandle handle,
NativeHostDispatcherOpcode opcode,
int32_t index, intptr_t value, void *ptr,
float opt)
{
UNUSED_PARAMETER(index);
UNUSED_PARAMETER(value);
UNUSED_PARAMETER(ptr);
UNUSED_PARAMETER(opt);
struct carla_priv *priv = handle;
switch (opcode) {
case NATIVE_HOST_OPCODE_NULL:
case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
break;
case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
case NATIVE_HOST_OPCODE_RELOAD_ALL:
postpone_update_request(&priv->update_request);
break;
case NATIVE_HOST_OPCODE_GET_FILE_PATH:
case NATIVE_HOST_OPCODE_HOST_IDLE:
case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
case NATIVE_HOST_OPCODE_PREVIEW_BUFFER_DATA:
case NATIVE_HOST_OPCODE_QUEUE_INLINE_DISPLAY:
case NATIVE_HOST_OPCODE_REQUEST_IDLE:
case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
case NATIVE_HOST_OPCODE_UI_RESIZE:
case NATIVE_HOST_OPCODE_UI_TOUCH_PARAMETER:
break;
}
return 0;
}
// ----------------------------------------------------------------------------
// carla + obs integration methods
struct carla_priv *carla_priv_create(obs_source_t *source,
enum buffer_size_mode bufsize,
uint32_t srate)
{
_Static_assert(MAX_AV_PLANES == 8, "expected 8 IO");
const NativePluginDescriptor *descriptor =
carla_get_native_patchbay_obs_plugin();
if (descriptor == NULL)
return NULL;
struct carla_priv *priv = bzalloc(sizeof(struct carla_priv));
if (priv == NULL)
return NULL;
priv->source = source;
priv->bufferSize = bufsize_mode_to_frames(bufsize);
priv->sampleRate = srate;
priv->descriptor = descriptor;
assert(priv->bufferSize != 0);
if (priv->bufferSize == 0)
goto fail;
{
NativeHostDescriptor host = {
.handle = priv,
.resourceDir = get_carla_resource_path(),
.uiName = "Carla-OBS",
.uiParentId = 0,
.get_buffer_size = host_get_buffer_size,
.get_sample_rate = host_get_sample_rate,
.is_offline = host_is_offline,
.get_time_info = host_get_time_info,
.write_midi_event = host_write_midi_event,
.ui_parameter_changed = host_ui_parameter_changed,
.ui_midi_program_changed = host_ui_midi_program_changed,
.ui_custom_data_changed = host_ui_custom_data_changed,
.ui_closed = host_ui_closed,
.ui_open_file = host_ui_open_file,
.ui_save_file = host_ui_save_file,
.dispatcher = host_dispatcher};
priv->host = host;
}
{
NativeTimeInfo timeInfo = {
.usecs = os_gettime_ns() / 1000,
};
priv->timeInfo = timeInfo;
}
priv->handle = descriptor->instantiate(&priv->host);
if (priv->handle == NULL)
goto fail;
descriptor->dispatcher(priv->handle, NATIVE_PLUGIN_OPCODE_HOST_OPTION,
ENGINE_OPTION_PATH_BINARIES, 0,
(void *)get_carla_bin_path(), 0.f);
return priv;
fail:
bfree(priv);
return NULL;
}
void carla_priv_destroy(struct carla_priv *priv)
{
if (priv->activated)
carla_priv_deactivate(priv);
priv->descriptor->cleanup(priv->handle);
bfree(priv->paramDetails);
bfree(priv);
}
// ----------------------------------------------------------------------------
void carla_priv_activate(struct carla_priv *priv)
{
assert(!priv->activated);
priv->descriptor->activate(priv->handle);
priv->activated = true;
}
void carla_priv_deactivate(struct carla_priv *priv)
{
assert(priv->activated);
priv->activated = false;
priv->descriptor->deactivate(priv->handle);
}
void carla_priv_process_audio(struct carla_priv *priv,
float *buffers[MAX_AV_PLANES], uint32_t frames)
{
priv->timeInfo.usecs = os_gettime_ns() / 1000;
priv->descriptor->process(priv->handle, buffers, buffers, frames, NULL,
0);
}
void carla_priv_idle(struct carla_priv *priv)
{
priv->descriptor->ui_idle(priv->handle);
handle_update_request(priv->source, &priv->update_request);
}
void carla_priv_save(struct carla_priv *priv, obs_data_t *settings)
{
char *state = priv->descriptor->get_state(priv->handle);
if (state) {
obs_data_set_string(settings, "state", state);
free(state);
}
}
void carla_priv_load(struct carla_priv *priv, obs_data_t *settings)
{
const char *state = obs_data_get_string(settings, "state");
if (state)
priv->descriptor->set_state(priv->handle, state);
}
// ----------------------------------------------------------------------------
void carla_priv_set_buffer_size(struct carla_priv *priv,
enum buffer_size_mode bufsize)
{
const uint32_t new_buffer_size = bufsize_mode_to_frames(bufsize);
assert(new_buffer_size != 0);
if (new_buffer_size == 0)
return;
const bool activated = priv->activated;
if (activated)
carla_priv_deactivate(priv);
priv->bufferSize = new_buffer_size;
priv->descriptor->dispatcher(priv->handle,
NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED,
new_buffer_size, 0, NULL, 0.f);
if (activated)
carla_priv_activate(priv);
}
// ----------------------------------------------------------------------------
static bool carla_priv_param_changed(void *data, obs_properties_t *props,
obs_property_t *property,
obs_data_t *settings)
{
UNUSED_PARAMETER(props);
struct carla_priv *priv = data;
const char *const pname = obs_property_name(property);
if (pname == NULL)
return false;
const char *pname2 = pname + 1;
while (*pname2 == '0')
++pname2;
const int pindex = atoi(pname2);
if (pindex < 0 || pindex >= (int)priv->paramCount)
return false;
const float min = priv->paramDetails[pindex].min;
const float max = priv->paramDetails[pindex].max;
float value;
switch (obs_property_get_type(property)) {
case OBS_PROPERTY_BOOL:
value = obs_data_get_bool(settings, pname) ? max : min;
break;
case OBS_PROPERTY_INT:
value = (float)obs_data_get_int(settings, pname);
if (value < min)
value = min;
else if (value > max)
value = max;
break;
case OBS_PROPERTY_FLOAT:
value = (float)obs_data_get_double(settings, pname);
if (value < min)
value = min;
else if (value > max)
value = max;
break;
default:
return false;
}
priv->descriptor->set_parameter_value(priv->handle, pindex, value);
// UI param change notification needs to happen on main thread
struct carla_main_thread_param_change mchange = {
.descriptor = priv->descriptor,
.handle = priv->handle,
.index = pindex,
.value = value};
struct carla_main_thread_param_change *mchangeptr =
bmalloc(sizeof(mchange));
*mchangeptr = mchange;
carla_qt_callback_on_main_thread(carla_main_thread_param_change,
mchangeptr);
return false;
}
static bool carla_priv_show_gui_callback(obs_properties_t *props,
obs_property_t *property, void *data)
{
UNUSED_PARAMETER(props);
UNUSED_PARAMETER(property);
struct carla_priv *priv = data;
// FIXME check if needed, frontend already sets these?
char winIdStr[24];
snprintf(winIdStr, sizeof(winIdStr), "%llx",
(ulonglong)carla_qt_get_main_window_id());
priv->descriptor->dispatcher(priv->handle,
NATIVE_PLUGIN_OPCODE_HOST_OPTION,
ENGINE_OPTION_FRONTEND_WIN_ID, 0, winIdStr,
0.f);
const double scaleFactor = carla_qt_get_scale_factor();
priv->descriptor->dispatcher(priv->handle,
NATIVE_PLUGIN_OPCODE_HOST_OPTION,
ENGINE_OPTION_FRONTEND_UI_SCALE,
(intptr_t)(scaleFactor * 1000), NULL, 0.f);
priv->descriptor->ui_show(priv->handle, true);
return false;
}
void carla_priv_readd_properties(struct carla_priv *priv,
obs_properties_t *props, bool reset)
{
obs_data_t *settings = obs_source_get_settings(priv->source);
if (priv->descriptor->hints & NATIVE_PLUGIN_HAS_UI) {
obs_properties_add_button2(props, PROP_SHOW_GUI,
obs_module_text("Show custom GUI"),
carla_priv_show_gui_callback, priv);
}
uint32_t params = priv->descriptor->get_parameter_count(priv->handle);
if (params > MAX_PARAMS)
params = MAX_PARAMS;
bfree(priv->paramDetails);
priv->paramCount = params;
priv->paramDetails = bzalloc(sizeof(struct carla_param_data) * params);
char pname[PARAM_NAME_SIZE] = PARAM_NAME_INIT;
for (uint32_t i = 0; i < params; ++i) {
const NativeParameter *const info =
priv->descriptor->get_parameter_info(priv->handle, i);
if ((info->hints & NATIVE_PARAMETER_IS_ENABLED) == 0)
continue;
if (info->hints & NATIVE_PARAMETER_IS_OUTPUT)
continue;
param_index_to_name(i, pname);
priv->paramDetails[i].hints = info->hints;
priv->paramDetails[i].min = info->ranges.min;
priv->paramDetails[i].max = info->ranges.max;
obs_property_t *prop;
if (info->hints & NATIVE_PARAMETER_IS_BOOLEAN) {
prop = obs_properties_add_bool(props, pname,
info->name);
obs_data_set_default_bool(settings, pname,
info->ranges.def ==
info->ranges.max);
if (reset)
obs_data_set_bool(settings, pname,
info->ranges.def ==
info->ranges.max);
} else if (info->hints & NATIVE_PARAMETER_IS_INTEGER) {
prop = obs_properties_add_int_slider(
props, pname, info->name, (int)info->ranges.min,
(int)info->ranges.max, (int)info->ranges.step);
obs_data_set_default_int(settings, pname,
(int)info->ranges.def);
if (info->unit && *info->unit)
obs_property_int_set_suffix(prop, info->unit);
if (reset)
obs_data_set_int(settings, pname,
(int)info->ranges.def);
} else {
prop = obs_properties_add_float_slider(
props, pname, info->name, info->ranges.min,
info->ranges.max, info->ranges.step);
obs_data_set_default_double(settings, pname,
info->ranges.def);
if (info->unit && *info->unit)
obs_property_float_set_suffix(prop, info->unit);
if (reset)
obs_data_set_double(settings, pname,
info->ranges.def);
}
obs_property_set_modified_callback2(
prop, carla_priv_param_changed, priv);
}
obs_data_release(settings);
}
// ----------------------------------------------------------------------------