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
/
Copy pathcarla.c
527 lines (421 loc) · 13.7 KB
/
carla.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
/*
* Carla plugin for OBS
* Copyright (C) 2023 Filipe Coelho <[email protected]>
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <obs-module.h>
#include <util/platform.h>
#ifndef CARLA_MODULE_ID
#error CARLA_MODULE_ID undefined
#endif
#ifndef CARLA_MODULE_NAME
#error CARLA_MODULE_NAME undefined
#endif
#include "carla-wrapper.h"
#include "common.h"
// for audio generator thread
#include <pthread.h>
// default mode, defined as macro for easy change
#define DEFAULT_BUFFER_SIZE_MODE buffer_size_direct
// --------------------------------------------------------------------------------------------------------------------
struct carla_data {
// carla host details, intentionally kept private so we can easily swap internals
struct carla_priv *priv;
// current OBS config
bool activated;
size_t channels;
uint32_t sample_rate;
obs_source_t *source;
// audio generator thread
bool audiogen_enabled;
volatile bool audiogen_running;
pthread_t audiogen_thread;
// internal buffering
float *buffers[MAX_AV_PLANES];
uint16_t buffer_head;
uint16_t buffer_tail;
enum buffer_size_mode buffer_size_mode;
// dummy buffer for unused audio channels
float *dummybuffer;
};
// --------------------------------------------------------------------------------------------------------------------
// private methods
static void *carla_obs_audio_gen_thread(void *data)
{
struct carla_data *carla = data;
struct obs_source_audio out = {
.speakers = SPEAKERS_STEREO,
.format = AUDIO_FORMAT_FLOAT_PLANAR,
.samples_per_sec = carla->sample_rate,
};
for (uint8_t c = 0; c < MAX_AV_PLANES; ++c)
out.data[c] = (const uint8_t *)carla->buffers[c];
const uint32_t sample_rate = carla->sample_rate;
const uint64_t start_time = out.timestamp = os_gettime_ns();
uint64_t total_samples = 0;
while (carla->audiogen_running) {
const uint32_t buffer_size =
bufsize_mode_to_frames(carla->buffer_size_mode);
out.frames = buffer_size;
carla_priv_process_audio(carla->priv, carla->buffers,
buffer_size);
obs_source_output_audio(carla->source, &out);
if (!carla->audiogen_running)
break;
total_samples += buffer_size;
out.timestamp = start_time +
audio_frames_to_ns(sample_rate, total_samples);
os_sleepto_ns_fast(out.timestamp);
}
return NULL;
}
static void carla_obs_idle_callback(void *data, float unused)
{
UNUSED_PARAMETER(unused);
struct carla_data *carla = data;
carla_priv_idle(carla->priv);
}
// --------------------------------------------------------------------------------------------------------------------
// obs plugin methods
static void carla_obs_activate(void *data);
static void carla_obs_deactivate(void *data);
static const char *carla_obs_get_name(void *data)
{
return !strcmp(data, "filter")
? obs_module_text(CARLA_MODULE_NAME " (Filter)")
: obs_module_text(CARLA_MODULE_NAME " (Input)");
}
static void *carla_obs_create(obs_data_t *settings, obs_source_t *source,
bool isFilter)
{
UNUSED_PARAMETER(settings);
const audio_t *audio = obs_get_audio();
const size_t channels = audio_output_get_channels(audio);
const uint32_t sample_rate = audio_output_get_sample_rate(audio);
if (channels == 0 || sample_rate == 0)
return NULL;
struct carla_data *carla = bzalloc(sizeof(*carla));
if (carla == NULL)
return NULL;
for (uint8_t c = 0; c < MAX_AV_PLANES; ++c) {
carla->buffers[c] =
bzalloc(sizeof(float) * MAX_AUDIO_BUFFER_SIZE);
if (carla->buffers[c] == NULL)
goto fail1;
}
carla->dummybuffer = bzalloc(sizeof(float) * MAX_AUDIO_BUFFER_SIZE);
if (carla->dummybuffer == NULL)
goto fail2;
struct carla_priv *priv = carla_priv_create(
source, DEFAULT_BUFFER_SIZE_MODE, sample_rate);
if (carla == NULL)
goto fail3;
carla->priv = priv;
carla->source = source;
carla->channels = channels;
carla->sample_rate = sample_rate;
carla->buffer_head = 0;
carla->buffer_tail = UINT16_MAX;
carla->buffer_size_mode = DEFAULT_BUFFER_SIZE_MODE;
// audio generator, aka input source
carla->audiogen_enabled = !isFilter;
obs_add_tick_callback(carla_obs_idle_callback, carla);
return carla;
fail3:
bfree(carla->dummybuffer);
fail2:
for (uint8_t c = 0; c < MAX_AV_PLANES; ++c)
bfree(carla->buffers[c]);
fail1:
bfree(carla);
return NULL;
}
static void *carla_obs_create_filter(obs_data_t *settings, obs_source_t *source)
{
return carla_obs_create(settings, source, true);
}
static void *carla_obs_create_input(obs_data_t *settings, obs_source_t *source)
{
return carla_obs_create(settings, source, false);
}
static void carla_obs_destroy(void *data)
{
struct carla_data *carla = data;
if (carla->activated)
carla_obs_deactivate(carla);
obs_remove_tick_callback(carla_obs_idle_callback, carla);
carla_priv_destroy(carla->priv);
bfree(carla->dummybuffer);
for (uint8_t c = 0; c < MAX_AV_PLANES; ++c)
bfree(carla->buffers[c]);
bfree(carla);
}
static bool carla_obs_bufsize_callback(void *data, obs_properties_t *props,
obs_property_t *list,
obs_data_t *settings)
{
UNUSED_PARAMETER(props);
UNUSED_PARAMETER(list);
struct carla_data *carla = data;
enum buffer_size_mode bufsize;
const char *const value =
obs_data_get_string(settings, PROP_BUFFER_SIZE);
/**/ if (!strcmp(value, "direct"))
bufsize = buffer_size_direct;
else if (!strcmp(value, "128"))
bufsize = buffer_size_buffered_128;
else if (!strcmp(value, "256"))
bufsize = buffer_size_buffered_256;
else if (!strcmp(value, "512"))
bufsize = buffer_size_buffered_512;
else
return false;
if (carla->buffer_size_mode == bufsize)
return false;
blog(LOG_INFO, "[" CARLA_MODULE_ID "] changing buffer size to %s",
value);
// deactivate first, to stop audio from processing
carla_priv_deactivate(carla->priv);
// safely change to new buffer size
carla->buffer_size_mode = bufsize;
carla_priv_set_buffer_size(carla->priv, bufsize);
// activate again
carla_priv_activate(carla->priv);
return false;
}
static obs_properties_t *carla_obs_get_properties(void *data)
{
struct carla_data *carla = data;
obs_properties_t *props = obs_properties_create();
obs_property_t *list = obs_properties_add_list(
props, PROP_BUFFER_SIZE, obs_module_text("Buffer Size"),
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
obs_property_list_add_string(
list, obs_module_text("Direct (variable buffer)"), "direct");
obs_property_list_add_string(
list,
obs_module_text("128 samples (fixed buffer with latency)"),
"128");
obs_property_list_add_string(
list,
obs_module_text("256 samples (fixed buffer with latency)"),
"256");
obs_property_list_add_string(
list,
obs_module_text("512 samples (fixed buffer with latency)"),
"512");
obs_property_set_modified_callback2(list, carla_obs_bufsize_callback,
carla);
carla_priv_readd_properties(carla->priv, props, false);
return props;
}
static void carla_obs_activate(void *data)
{
struct carla_data *carla = data;
assert(!carla->activated);
if (carla->activated)
return;
carla->activated = true;
carla_priv_activate(carla->priv);
if (carla->audiogen_enabled) {
assert(!carla->audiogen_running);
carla->audiogen_running = true;
pthread_create(&carla->audiogen_thread, NULL,
carla_obs_audio_gen_thread, carla);
}
}
static void carla_obs_deactivate(void *data)
{
struct carla_data *carla = data;
assert(carla->activated);
if (!carla->activated)
return;
carla->activated = false;
if (carla->audiogen_running) {
carla->audiogen_running = false;
pthread_join(carla->audiogen_thread, NULL);
}
carla_priv_deactivate(carla->priv);
}
static void carla_obs_filter_audio_direct(struct carla_data *carla,
struct obs_audio_data *audio)
{
uint32_t frames = audio->frames;
float *obsbuffers[MAX_AV_PLANES];
for (uint32_t i = 0; i < frames;) {
const uint32_t stepframes = frames >= MAX_AUDIO_BUFFER_SIZE
? MAX_AUDIO_BUFFER_SIZE
: frames;
for (uint8_t c = 0; c < MAX_AV_PLANES; ++c)
obsbuffers[c] = audio->data[c]
? ((float *)audio->data[c] + i)
: carla->dummybuffer;
carla_priv_process_audio(carla->priv, obsbuffers, stepframes);
memset(carla->dummybuffer, 0, sizeof(float) * stepframes);
frames -= stepframes;
}
}
static void carla_obs_filter_audio_buffered(struct carla_data *carla,
struct obs_audio_data *audio)
{
const uint32_t buffer_size =
bufsize_mode_to_frames(carla->buffer_size_mode);
const size_t channels = carla->channels;
const uint32_t frames = audio->frames;
// cast audio buffers to correct type
float *obsbuffers[MAX_AV_PLANES];
for (uint8_t c = 0; c < MAX_AV_PLANES; ++c)
obsbuffers[c] = audio->data[c] ? (float *)audio->data[c]
: carla->dummybuffer;
// preload some variables before looping section
uint16_t buffer_head = carla->buffer_head;
uint16_t buffer_tail = carla->buffer_tail;
for (uint32_t i = 0, h, t; i < frames; ++i) {
// OBS -> plugin internal buffering
h = buffer_head++;
for (uint8_t c = 0; c < channels; ++c)
carla->buffers[c][h] = obsbuffers[c][i];
// when we reach the target buffer size, do audio processing
if (buffer_head == buffer_size) {
buffer_head = 0;
carla_priv_process_audio(carla->priv, carla->buffers,
buffer_size);
memset(carla->dummybuffer, 0,
sizeof(float) * buffer_size);
// we can now begin to copy back the buffer into OBS
if (buffer_tail == UINT16_MAX)
buffer_tail = 0;
}
if (buffer_tail == UINT16_MAX) {
// buffering still taking place, skip until first audio cycle
for (uint8_t c = 0; c < channels; ++c)
obsbuffers[c][i] = 0.f;
} else {
// plugin -> OBS buffer copy
t = buffer_tail++;
for (uint8_t c = 0; c < channels; ++c)
obsbuffers[c][i] = carla->buffers[c][t];
if (buffer_tail == buffer_size)
buffer_tail = 0;
}
}
carla->buffer_head = buffer_head;
carla->buffer_tail = buffer_tail;
}
static struct obs_audio_data *
carla_obs_filter_audio(void *data, struct obs_audio_data *audio)
{
struct carla_data *carla = data;
switch (carla->buffer_size_mode) {
case buffer_size_direct:
carla_obs_filter_audio_direct(carla, audio);
break;
case buffer_size_buffered_128:
case buffer_size_buffered_256:
case buffer_size_buffered_512:
carla_obs_filter_audio_buffered(carla, audio);
break;
}
return audio;
}
static void carla_obs_save(void *data, obs_data_t *settings)
{
struct carla_data *carla = data;
carla_priv_save(carla->priv, settings);
}
static void carla_obs_load(void *data, obs_data_t *settings)
{
struct carla_data *carla = data;
carla_priv_load(carla->priv, settings);
}
// --------------------------------------------------------------------------------------------------------------------
OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("carla", "en-US")
OBS_MODULE_AUTHOR("Filipe Coelho")
const char *obs_module_name(void)
{
return CARLA_MODULE_NAME;
}
/*
const char *obs_module_description(void)
{
return CARLA_MODULE_NAME;
}
*/
bool obs_module_load(void)
{
const char *carla_bin_path = get_carla_bin_path();
if (!carla_bin_path) {
blog(LOG_WARNING,
"[" CARLA_MODULE_ID
"] failed to find binaries, will not load module");
return false;
}
blog(LOG_INFO, "[" CARLA_MODULE_ID "] using binary path %s",
carla_bin_path);
#ifndef BUILDING_CARLA_OBS
const char *carla_res_path = get_carla_resource_path();
if (!carla_res_path) {
blog(LOG_WARNING,
"[" CARLA_MODULE_ID
"] failed to find resources, will not load module");
return false;
}
blog(LOG_INFO, "[" CARLA_MODULE_ID "] using resource path %s",
carla_res_path);
#endif
static const struct obs_source_info filter = {
.id = CARLA_MODULE_ID "-filter",
.type = OBS_SOURCE_TYPE_FILTER,
.output_flags = OBS_SOURCE_AUDIO,
.get_name = carla_obs_get_name,
.create = carla_obs_create_filter,
.destroy = carla_obs_destroy,
// get_width, get_height, get_defaults
.get_properties = carla_obs_get_properties,
// update
.activate = carla_obs_activate,
.deactivate = carla_obs_deactivate,
// show, hide, video_tick, video_render, filter_video
.filter_audio = carla_obs_filter_audio,
// enum_active_sources
.save = carla_obs_save,
.load = carla_obs_load,
// mouse_click, mouse_move, mouse_wheel, focus, key_click, filter_remove,
.type_data = "filter",
// free_type_data
// audio_render, enum_all_sources, transition_start, transition_stop, get_defaults2, audio_mix
.icon_type = OBS_ICON_TYPE_PROCESS_AUDIO_OUTPUT,
// media_play_pause, media_restart, media_stop, media_next, media_previous
// media_get_duration, media_get_time, media_set_time, media_get_state
// version, unversioned_id, missing_files, video_get_color_space
};
obs_register_source(&filter);
static const struct obs_source_info input = {
.id = CARLA_MODULE_ID "-input",
.type = OBS_SOURCE_TYPE_INPUT,
.output_flags = OBS_SOURCE_AUDIO,
.get_name = carla_obs_get_name,
.create = carla_obs_create_input,
.destroy = carla_obs_destroy,
// get_width, get_height, get_defaults
.get_properties = carla_obs_get_properties,
// update
.activate = carla_obs_activate,
.deactivate = carla_obs_deactivate,
// show, hide, video_tick, video_render, filter_video, filter_audio, enum_active_sources
.save = carla_obs_save,
.load = carla_obs_load,
// mouse_click, mouse_move, mouse_wheel, focus, key_click, filter_remove,
.type_data = "input",
// free_type_data
// audio_render, enum_all_sources, transition_start, transition_stop, get_defaults2, audio_mix
.icon_type = OBS_ICON_TYPE_AUDIO_OUTPUT,
// media_play_pause, media_restart, media_stop, media_next, media_previous
// media_get_duration, media_get_time, media_set_time, media_get_state
// version, unversioned_id, missing_files, video_get_color_space
};
obs_register_source(&input);
return true;
}
// --------------------------------------------------------------------------------------------------------------------