-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportal.c
406 lines (328 loc) · 10.6 KB
/
portal.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
//
#include <stdatomic.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#include <lv2/lv2plug.in/ns/lv2core/lv2.h>
#include <lv2/lv2plug.in/ns/ext/atom/atom.h>
#include <lv2/lv2plug.in/ns/ext/buf-size/buf-size.h>
#include <lv2/lv2plug.in/ns/ext/log/logger.h>
#include <lv2/lv2plug.in/ns/ext/options/options.h>
#include <lv2/lv2plug.in/ns/ext/urid/urid.h>
#ifdef __MOD_DEVICES__
#define MAX_BUFFER_SIZE 256
#define SEMAPHORE_PSHARED 0
#else
#define MAX_BUFFER_SIZE 8192
#define SEMAPHORE_PSHARED 1
#endif
//=====================================================================================================================
typedef struct {
const float* left;
const float* right;
const float* enabled;
float* status;
} Sink;
typedef struct {
float* left;
float* right;
const float* enabled;
float* status;
} Source;
typedef struct {
float* buffer_left;
float* buffer_right;
atomic_uint counter_sink;
atomic_uint counter_source;
pthread_mutex_t mutex;
sem_t sem;
} Portal;
//=====================================================================================================================
static Portal* portal_init()
{
Portal* const portal = (Portal*)malloc(sizeof(Portal));
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
if (pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT) != 0)
goto error1;
if (pthread_mutex_init(&portal->mutex, &attr) != 0)
goto error2;
pthread_mutexattr_destroy(&attr);
atomic_init(&portal->counter_sink, 0);
atomic_init(&portal->counter_source, 0);
portal->buffer_left = calloc(MAX_BUFFER_SIZE, sizeof(float));
portal->buffer_right = calloc(MAX_BUFFER_SIZE, sizeof(float));
// NOTE initial value 1 (lock by default)
sem_init(&portal->sem, SEMAPHORE_PSHARED, 1);
return portal;
error1:
pthread_mutexattr_destroy(&attr);
error2:
free(portal);
return NULL;
}
static void portal_destroy(Portal* const portal)
{
sem_destroy(&portal->sem);
pthread_mutex_destroy(&portal->mutex);
free(portal->buffer_left);
free(portal->buffer_right);
free(portal);
}
//=====================================================================================================================
static Portal* g_portal = NULL;
static bool g_sink_loaded = false;
static bool g_source_loaded = false;
static LV2_Handle instantiate_sink(const LV2_Descriptor* const descriptor,
const double samplerate,
const char* const bundle_path,
const LV2_Feature* const* const features)
{
bool fixedBlockLength = false;
LV2_Log_Log* log;
LV2_URID_Map* uridMap;
for (int i=0; features[i] != NULL; ++i)
{
if (!strcmp(features[i]->URI, LV2_BUF_SIZE__fixedBlockLength))
fixedBlockLength = true;
else if (!strcmp(features[i]->URI, LV2_LOG__log))
log = features[i]->data;
else if (!strcmp(features[i]->URI, LV2_URID__map))
uridMap = features[i]->data;
}
LV2_Log_Logger logger;
lv2_log_logger_init(&logger, uridMap, log);
if (!fixedBlockLength)
{
lv2_log_error(&logger, "Host does not support lv2plug.in/ns/ext/buf-size#fixedBlockLength, cannot continue");
return NULL;
}
if (g_sink_loaded)
{
lv2_log_error(&logger, "A portal sink is already loaded");
return NULL;
}
if (!g_source_loaded)
{
lv2_log_note(&logger, "Creating global portal instance");
g_portal = portal_init();
}
if (g_portal == NULL)
{
lv2_log_error(&logger, "Failed to fetch global portal instance");
return NULL;
}
g_sink_loaded = true;
return malloc(sizeof(Sink));
}
static LV2_Handle instantiate_source(const LV2_Descriptor* const descriptor,
const double samplerate,
const char* const bundle_path,
const LV2_Feature* const* const features)
{
bool fixedBlockLength = false;
LV2_Log_Log* log;
LV2_URID_Map* uridMap;
for (int i=0; features[i] != NULL; ++i)
{
if (!strcmp(features[i]->URI, LV2_BUF_SIZE__fixedBlockLength))
fixedBlockLength = true;
else if (!strcmp(features[i]->URI, LV2_LOG__log))
log = features[i]->data;
else if (!strcmp(features[i]->URI, LV2_URID__map))
uridMap = features[i]->data;
}
LV2_Log_Logger logger;
lv2_log_logger_init(&logger, uridMap, log);
if (!fixedBlockLength)
{
lv2_log_error(&logger, "Host does not support lv2plug.in/ns/ext/buf-size#fixedBlockLength, cannot continue");
return NULL;
}
if (g_source_loaded)
{
lv2_log_error(&logger, "A portal source is already loaded");
return NULL;
}
if (!g_sink_loaded)
{
lv2_log_note(&logger, "Creating global portal instance");
g_portal = portal_init();
}
if (g_portal == NULL)
{
lv2_log_error(&logger, "Failed to fetch global portal instance");
return NULL;
}
g_source_loaded = true;
return malloc(sizeof(Source));
}
//=====================================================================================================================
static void cleanup_sink(const LV2_Handle instance)
{
free(instance);
g_sink_loaded = false;
if (!g_source_loaded)
{
portal_destroy(g_portal);
g_portal = NULL;
}
}
static void cleanup_source(const LV2_Handle instance)
{
free(instance);
g_source_loaded = false;
if (!g_sink_loaded)
{
portal_destroy(g_portal);
g_portal = NULL;
}
}
//=====================================================================================================================
static void run_sink(const LV2_Handle instance, const uint32_t samples)
{
Sink* const sink = instance;
if (samples > MAX_BUFFER_SIZE)
{
*sink->status = 2.f;
return;
}
Portal* const portal = g_portal;
const uint counter_sink = atomic_load(&portal->counter_sink);
const uint counter_source = atomic_load(&portal->counter_source);
// if there is no source active yet, do nothing
if (counter_source == 0)
{
*sink->status = 0.f;
return;
}
const bool processing = counter_sink != 1 && counter_source != 1;
*sink->status = processing ? 1.f : 0.f;
if (samples == 0)
return;
// if sink and source are processing, make sure source side always goes before us
if (processing)
sem_wait(&portal->sem);
pthread_mutex_lock(&portal->mutex);
memcpy(portal->buffer_left, sink->left, sizeof(float)*samples);
memcpy(portal->buffer_right, sink->right, sizeof(float)*samples);
pthread_mutex_unlock(&portal->mutex);
// increment sink counter
atomic_fetch_add(&portal->counter_sink, 1);
}
static void run_source(const LV2_Handle instance, const uint32_t samples)
{
Source* const source = instance;
if (samples > MAX_BUFFER_SIZE)
{
*source->status = 2.f;
goto clear;
}
Portal* const portal = g_portal;
// if there is no sink processing yet, do nothing
if (atomic_load(&portal->counter_sink) <= 1)
{
*source->status = 0.f;
goto clear;
}
*source->status = 1.f;
if (samples == 0)
return;
pthread_mutex_lock(&portal->mutex);
memcpy(source->left, portal->buffer_left, sizeof(float)*samples);
memcpy(source->right, portal->buffer_right, sizeof(float)*samples);
pthread_mutex_unlock(&portal->mutex);
// increment source counter
atomic_fetch_add(&portal->counter_source, 1);
// notify sink so it goes after us
sem_post(&portal->sem);
return;
clear:
memset(source->left, 0, sizeof(float)*samples);
memset(source->right, 0, sizeof(float)*samples);
}
//=====================================================================================================================
static void activate_sink(const LV2_Handle instance)
{
Portal* const portal = g_portal;
atomic_store(&portal->counter_sink, 1);
}
static void activate_source(const LV2_Handle instance)
{
Portal* const portal = g_portal;
atomic_store(&portal->counter_source, 1);
}
//=====================================================================================================================
static void deactivate_sink(const LV2_Handle instance)
{
Portal* const portal = g_portal;
atomic_store(&portal->counter_sink, 0);
}
static void deactivate_source(const LV2_Handle instance)
{
Portal* const portal = g_portal;
const int old_counter_source = atomic_exchange(&portal->counter_source, 0);
// handle case of sink waiting for source
if (atomic_load(&portal->counter_sink) != 1 && old_counter_source != 1)
sem_post(&portal->sem);
}
//=====================================================================================================================
static void connect_port(const LV2_Handle instance, const uint32_t port, void* const data)
{
Sink* const shared = instance;
switch (port)
{
case 0:
shared->left = data;
break;
case 1:
shared->right = data;
break;
case 2:
shared->enabled = data;
break;
case 3:
shared->status = data;
break;
}
}
//=====================================================================================================================
static const void* extension_data(const char* const uri)
{
return NULL;
}
//=====================================================================================================================
LV2_SYMBOL_EXPORT
const LV2_Descriptor* lv2_descriptor(const uint32_t index)
{
static const LV2_Descriptor descriptor_sink = {
"https://falktx.com/plugins/portal#sink",
instantiate_sink,
connect_port,
activate_sink,
run_sink,
deactivate_sink,
cleanup_sink,
extension_data
};
static const LV2_Descriptor descriptor_source = {
"https://falktx.com/plugins/portal#source",
instantiate_source,
connect_port,
activate_source,
run_source,
deactivate_source,
cleanup_source,
extension_data
};
switch (index)
{
case 0: return &descriptor_sink;
case 1: return &descriptor_source;
default: return NULL;
}
}
//=====================================================================================================================