File: lv2.c

package info (click to toggle)
x42-plugins 20180803-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 14,752 kB
  • sloc: ansic: 94,316; cpp: 6,670; makefile: 3,388; objc: 951; sh: 75
file content (661 lines) | stat: -rw-r--r-- 18,281 bytes parent folder | download | duplicates (2)
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/* convoLV2 -- LV2 convolution plugin
 *
 * Copyright (C) 2012-2016 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "convolution.h"

#include "lv2/lv2plug.in/ns/ext/buf-size/buf-size.h"
#include "lv2/lv2plug.in/ns/ext/log/log.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/state/state.h"
#include "lv2/lv2plug.in/ns/ext/worker/worker.h"
#include "lv2/lv2plug.in/ns/lv2core/lv2.h"

#include "./uris.h"

#ifndef MAX
#define MAX(a,b) ( ((a)<(b))?(b):(a) )
#endif

/* note: when whaning this, update MAX_CHANNEL_MAPS in convolution.h to (MAX_CHN * MAX_CHN) */
#define MAX_CHN (2)
/* loop MACRO from 0 .. MAX_CHN */
#define LOOP_DEFINE_PORTS(def) \
  def(0) \
  def(1)

#define ENUMPORT(n) \
  P_OUTPUT ## n = (n*2+3), \
  P_INPUT ## n  = (n*2+4),

typedef enum {
  P_CONTROL    = 0,
  P_NOTIFY     = 1,
  P_OUTGAIN    = 2,

  LOOP_DEFINE_PORTS(ENUMPORT)
} PortIndex;

enum {
  CMD_APPLY    = 0,
  CMD_FREE     = 1,
};

typedef struct {
  LV2_URID_Map*        map;
  LV2_Worker_Schedule* schedule;
  LV2_Log_Log*         log;

  LV2_Atom_Forge forge;
  LV2_Log_Logger logger;

  float* input[MAX_CHN];
  float* output[MAX_CHN];
  const LV2_Atom_Sequence* control_port;
  LV2_Atom_Sequence*       notify_port;

  float * p_output_gain;
  float output_gain_db, output_gain_target, output_gain;

  LV2_Atom_Forge_Frame notify_frame;

  ConvoLV2URIs uris;

  LV2convolv *clv_online; ///< currently active engine
  LV2convolv *clv_offline; ///< inactive engine being configured

  int rate; ///< sample-rate -- constant per instance
  int chn_in; ///< input channel count -- constant per instance
  int chn_out; ///< output channel count --constant per instance

  unsigned int bufsize;

  short flag_reinit_in_progress;
  short flag_notify_ui; ///< notify UI about setting on next run()

} convoLV2;

static LV2_Handle
instantiate(const LV2_Descriptor*     descriptor,
            double                    rate,
            const char*               bundle_path,
            const LV2_Feature* const* features)
{
  const LV2_Options_Option* options  = NULL;
  LV2_URID_Map*             map      = NULL;
  LV2_Worker_Schedule*      schedule = NULL;
  LV2_Log_Log*              log      = NULL;
  for (int i = 0; features[i]; ++i) {
    if (!strcmp(features[i]->URI, LV2_URID__map)) {
      map = (LV2_URID_Map*)features[i]->data;
    } else if (!strcmp(features[i]->URI, LV2_WORKER__schedule)) {
      schedule = (LV2_Worker_Schedule*)features[i]->data;
    } else if (!strcmp(features[i]->URI, LV2_OPTIONS__options)) {
      options = (const LV2_Options_Option*)features[i]->data;
    } else if (!strcmp(features[i]->URI, LV2_LOG__log)) {
      log = (LV2_Log_Log*)features[i]->data;
    }
  }

  // Initialise logger (if map is unavailable, will fallback to printf)
  LV2_Log_Logger logger;
  lv2_log_logger_init(&logger, map, log);

  if (!map) {
    lv2_log_error(&logger, "Missing feature uri:map\n");
    return NULL;
  } else if (!schedule) {
    lv2_log_error(&logger, "Missing feature work:schedule\n");
    return NULL;
  } else if (!options) {
    lv2_log_error(&logger, "Missing options\n");
    return NULL;
  }

  LV2_URID bufsz_max = map->map(map->handle, LV2_BUF_SIZE__maxBlockLength);
  LV2_URID atom_Int  = map->map(map->handle, LV2_ATOM__Int);
  uint32_t bufsize   = 0;
  for (const LV2_Options_Option* o = options; o->key; ++o) {
    if (o->context == LV2_OPTIONS_INSTANCE &&
        o->key == bufsz_max &&
        o->type == atom_Int) {
      bufsize = *(const int32_t*)o->value;
    }
  }

  if (bufsize == 0) {
    lv2_log_error(&logger, "No maximum buffer size given\n");
    return NULL;
  } else if (bufsize < 64 || bufsize > 8192) {
    lv2_log_error(&logger, "Buffer size %u out of range 64..8192\n", bufsize);
    return NULL;
  } else if (bufsize & (bufsize - 1)) {
    lv2_log_error(&logger, "Buffer size %u not a power of two\n", bufsize);
    return NULL;
  }

  lv2_log_trace(&logger, "Buffer size: %u\n", bufsize);

  convoLV2* self = (convoLV2*)calloc(1, sizeof(convoLV2));
  if (!self) {
    return NULL;
  }

  /* Map URIs and initialise forge */
  map_convolv2_uris(map, &self->uris);
  lv2_atom_forge_init(&self->forge, map);

  self->map = map;
  self->schedule = schedule;
  self->log = log;
  self->logger = logger;
  self->bufsize = bufsize;
  self->rate = rate;
  self->chn_in = 1;
  self->chn_out = 1;
  self->flag_reinit_in_progress = 0;
  self->clv_online = NULL;
  self->clv_offline = NULL;

  self->output_gain_db = 0;
  self->output_gain_target = self->output_gain = 1.0;

  return (LV2_Handle)self;
}

static LV2_Worker_Status
work(LV2_Handle                  instance,
     LV2_Worker_Respond_Function respond,
     LV2_Worker_Respond_Handle   handle,
     uint32_t                    size,
     const void*                 data)
{
  convoLV2* self = (convoLV2*)instance;
  int apply = 0;

  /* prepare new engine instance */
  if (!self->clv_offline) {
    DEBUG_printf("Work: allocate offline instance\n");
    self->clv_offline = clv_alloc();

    if (!self->clv_offline) {
      self->flag_reinit_in_progress = 0;
      return LV2_WORKER_ERR_NO_SPACE; // OOM
    }
    clv_clone_settings(self->clv_offline, self->clv_online);
  }

  if (size == sizeof(int)) {
    switch(*((const int*)data)) {
    case CMD_APPLY:
      DEBUG_printf("Work: apply offline instance\n");
      apply = 1;
      break;
    case CMD_FREE:
      DEBUG_printf("Work: free offline instance\n");
      clv_free(self->clv_offline);
      self->clv_offline=NULL;
      break;
    default:
      DEBUG_printf("Work: invalid command\n");
      break;
    }
  } else {
    /* handle message described in Atom */
    const LV2_Atom_Object* obj = (const LV2_Atom_Object*) data;
    ConvoLV2URIs* uris = &self->uris;

    if (obj->body.otype == uris->patch_Set) {
      DEBUG_printf("Work: Atom Patch\n");
      const LV2_Atom* file_path = read_set_file(uris, obj);
      if (file_path) {
        const char *fn = (const char*)(file_path+1);
        DEBUG_printf("load IR %s\n", fn);
        clv_configure(self->clv_offline, "convolution.ir.file", fn);
        apply = 1;
      }
    } else {
      DEBUG_printf("Work: Invalid Atom Msg\n");
    }
  }

  if (apply) {
    DEBUG_printf("Work: initialize offline instance\n");
    clv_initialize(self->clv_offline, self->rate,
                   self->chn_in, self->chn_out,
                   /*64 <= buffer-size <=4096*/ self->bufsize);
#if 1
    respond(handle, 1, ""); // size must not be 0. A3 before rev 13146 will ignore it
#else
    respond(handle, 0, NULL);
#endif
  }
  return LV2_WORKER_SUCCESS;
}

static void inform_ui(LV2_Handle instance) {
  convoLV2* self = (convoLV2*)instance;
  // message to UI
  char fn[1024];
  if (clv_query_setting(self->clv_online, "convolution.ir.file", fn, 1024) > 0) {
    lv2_atom_forge_frame_time(&self->forge, 0);
    write_set_file(&self->forge, &self->uris, fn);
  }

  // TODO: notify GUI if convolution is running:
  // clv_is_active(clv->clv_online) == 1 if it is
  // TODO: also send plugin channel-counts and convolution configuration to GUI

#if 0 // DEBUG -- not rt-safe
  char *cfg = clv_dump_settings(self->clv_online);
  if (cfg) {
    lv2_atom_forge_frame_time(&self->forge, 0);
    write_set_file(&self->forge, &self->uris, cfg);
    free(cfg);
  }
#endif
}

static LV2_Worker_Status
work_response(LV2_Handle  instance,
              uint32_t    size,
              const void* data)
{
  convoLV2* self = (convoLV2*)instance;

  if (!self->clv_offline) {
    return LV2_WORKER_SUCCESS;
  }

  // swap engine instances
  DEBUG_printf("Work: swap instances\n");
  LV2convolv *old  = self->clv_online;
  self->clv_online  = self->clv_offline;
  self->clv_offline = old;

  inform_ui(instance);

  int d = CMD_FREE;
  self->schedule->schedule_work(self->schedule->handle, sizeof(int), &d);

  self->flag_reinit_in_progress = 0;
  return LV2_WORKER_SUCCESS;
}

#define IOPORT(i) \
    case P_INPUT ## i: \
      self->input[i] = (float*)data; \
      self->chn_in = MAX(self->chn_in,i+1); \
      break; \
    case P_OUTPUT ## i: \
      self->output[i] = (float*)data; \
      self->chn_out = MAX(self->chn_out,i+1); \
      break;

static void
connect_port(LV2_Handle instance,
             uint32_t   port,
             void*      data)
{
  convoLV2* self = (convoLV2*)instance;

  switch ((PortIndex)port) {
    LOOP_DEFINE_PORTS(IOPORT)
    case P_CONTROL:
      self->control_port = (const LV2_Atom_Sequence*)data;
      break;
    case P_NOTIFY:
      self->notify_port = (LV2_Atom_Sequence*)data;
      break;
    case P_OUTGAIN:
      self->p_output_gain = (float*)data;
      break;
  }
}

static void
run(LV2_Handle instance, uint32_t n_samples)
{
  convoLV2* self = (convoLV2*)instance;

  const float *input[MAX_CHN];
  float *output[MAX_CHN];
  int i;

  for (i=0; i < self->chn_in; i++ ) {
    input[i] = self->input[i];
  }
  for (i=0; i < self->chn_out; i++ ) {
    output[i] = self->output[i];
  }

  if (*self->p_output_gain != self->output_gain_db) {
    float g = *self->p_output_gain;
    self->output_gain_db = *self->p_output_gain;
    if (g < -40) g = -40;
    if (g >  40) g =  40;
    self->output_gain_target = powf(10.f, 0.05f * g);
  }

  self->output_gain += .08f * (self->output_gain_target - self->output_gain);

  /* Set up forge to write directly to notify output port. */
  if (self->notify_port) {
    const uint32_t notify_capacity = self->notify_port->atom.size;
    lv2_atom_forge_set_buffer(&self->forge,
                              (uint8_t*)self->notify_port,
                              notify_capacity);

    /* Start a sequence in the notify output port. */
    lv2_atom_forge_sequence_head(&self->forge, &self->notify_frame, 0);
  }

  bool silent = false;

  /* re-init engine if block-size has changed */
  if (self->bufsize != n_samples) {
    /* verify if we support the new block-size */
    if (n_samples < 64 || n_samples > 8192 ||
        /* not power of two */ (n_samples & (n_samples - 1))
        ) {
      /* silence output ports */
      for (i=0; i < self->chn_out; i++ ) {
        memset(output[i], 0, sizeof(float) * n_samples);
      }
      silent = true;
    } else {
      if (!self->flag_reinit_in_progress && clv_is_active(self->clv_online)) {
	self->flag_reinit_in_progress = 1;
	self->bufsize = n_samples;
	int d = CMD_APPLY;
	self->schedule->schedule_work(self->schedule->handle, sizeof(int), &d);
      }
    }
  }

  /* don't touch any settings if re-init is scheduled or in progress
   * TODO re-queue them ?
   */
  if (!self->flag_reinit_in_progress && self->control_port && self->notify_port) {
    /* Read incoming events */
    LV2_ATOM_SEQUENCE_FOREACH(self->control_port, ev) {
      const LV2_Atom_Object* obj = (LV2_Atom_Object*)&ev->body;
      ConvoLV2URIs* uris = &self->uris;
      if (obj->body.otype == uris->patch_Get) {
        self->flag_notify_ui = 0;
        inform_ui(instance);
      } else {
        // TODO: parse message here and set self->flag_reinit_in_progres=1; IFF an apply would be triggered
        self->schedule->schedule_work(self->schedule->handle, lv2_atom_total_size(&ev->body), &ev->body);
      }
    }
  }

  /* send current setting to UI */
  if (self->flag_notify_ui && self->notify_port) {
    self->flag_notify_ui = 0;
    inform_ui(instance);
  }

  if (silent) {
    return;
  }
  clv_convolve(self->clv_online, input, output,
               self->chn_in,
               self->chn_out,
               n_samples, self->output_gain);
}

static void
cleanup(LV2_Handle instance)
{
  convoLV2* self = (convoLV2*)instance;
  clv_free(self->clv_online);
  clv_free(self->clv_offline);
  free(instance);
}

static LV2_State_Status
save(LV2_Handle                instance,
     LV2_State_Store_Function  store,
     LV2_State_Handle          handle,
     uint32_t                  flags,
     const LV2_Feature* const* features)
{
  int i;
  convoLV2* self = (convoLV2*)instance;
  LV2_State_Map_Path* map_path = NULL;

  char *cfg = clv_dump_settings(self->clv_online);
  if (cfg) {
    store(handle, self->uris.clv2_state,
          cfg, strlen(cfg) + 1,
          self->uris.atom_String,
          LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE);
    free(cfg);
  }

  for (i=0; features[i]; ++i) {
    if (!strcmp(features[i]->URI, LV2_STATE__mapPath)) {
      map_path = (LV2_State_Map_Path*)features[i]->data;
    }
  }

  if (map_path) {
    char fn[1024]; // PATH_MAX
    if (clv_query_setting(self->clv_online, "convolution.ir.file", fn, 1024) > 0 ) {
      char* apath = map_path->abstract_path(map_path->handle, fn);
      store(handle, self->uris.clv2_impulse,
            apath, strlen(apath) + 1,
            self->uris.atom_Path,
            LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE);
      free(apath);
    }
  }
  return LV2_STATE_SUCCESS;
}

static LV2_State_Status
restore(LV2_Handle                  instance,
        LV2_State_Retrieve_Function retrieve,
        LV2_State_Handle            handle,
        uint32_t                    flags,
        const LV2_Feature* const*   features)
{
  convoLV2* self = (convoLV2*)instance;
  size_t   size;
  uint32_t type;
  uint32_t valflags;

  /* Get the work scheduler provided to restore() (state:threadSafeRestore
     support), but fall back to instantiate() schedules (spec-violating
     workaround for broken hosts). */
  LV2_Worker_Schedule* schedule = self->schedule;
  for (int i = 0; features[i]; ++i) {
    if (!strcmp(features[i]->URI, LV2_WORKER__schedule)) {
      DEBUG_printf("State: using thread-safe restore scheduler\n");
      schedule = (LV2_Worker_Schedule*)features[i]->data;
    }
  }
  if (schedule == self->schedule) {
    DEBUG_printf("State: warning: using run() scheduler to restore\n");
  }

  if (self->clv_offline) {
    // TODO use lv2_log_error()
    VERBOSE_printf("State: offline instance in-use, state ignored.\n");
    return LV2_STATE_ERR_UNKNOWN; // OOM
  }

  /* prepare new engine instance */
  if (!self->clv_offline) {
    DEBUG_printf("State: allocate offline instance\n");
    self->clv_offline = clv_alloc();

    if (!self->clv_offline) {
      return LV2_STATE_ERR_UNKNOWN; // OOM
    }
  }

#if 1 // prevent concurrent run() mess
  // should use atomic -- but really hosts need to be fixed instead
  self->flag_reinit_in_progress = 1;
#endif

  const void* value = retrieve(handle, self->uris.clv2_state, &size, &type, &valflags);

  bool ok = true;
  if (value) {
    const char* cfg = (const char*)value;
    const char *te,*ts = cfg;
    while (ts && *ts && (te=strchr(ts, '\n'))) {
      char *val;
      char kv[1024];
      memcpy(kv, ts, te-ts);
      kv[te-ts]=0;
      DEBUG_printf("CFG: %s\n", kv);
      if((val=strchr(kv,'='))) {
        *val=0;
        clv_configure(self->clv_offline, kv, val+1);
      }
      ts=te+1;
    }
  } else {
    ok = false;
  }

  value = retrieve(handle, self->uris.clv2_impulse, &size, &type, &valflags);

  if (value) {
    const char* path = (const char*)value;
    DEBUG_printf("PTH: convolution.ir.file=%s\n", path);
    clv_configure(self->clv_offline, "convolution.ir.file", path);
  } else  {
    ok = false;
  }

  if (!ok) {
    DEBUG_printf("State: incomplete state. Free offline instance\n");
    clv_free(self->clv_offline);
    self->clv_offline = 0;
    self->flag_reinit_in_progress = 0;
    return LV2_STATE_ERR_NO_PROPERTY;
  }

#if 0
  /* run() and restore() must not be called concurrently.
   * except some hosts do..
   *
   * in any case re-initialize in a background thread to not
   * block run() for a long time.
   */
  clv_initialize(self->clv_offline, self->rate, self->chn_in, self->chn_out,
                 /*64 <= buffer-size <=4096*/ self->bufsize);

  DEBUG_printf("State: applied offline instance\n");
  LV2convolv *old   = self->clv_online;
  self->clv_online  = self->clv_offline;
  self->clv_offline = old;

  self->flag_reinit_in_progress = 0;
  self->flag_notify_ui = 1;

  DEBUG_printf("State: free offline instance\n");
  clv_free(self->clv_offline);
  self->clv_offline=NULL;
#else
  int d = CMD_APPLY;
  schedule->schedule_work(self->schedule->handle, sizeof(int), &d);
#endif
  return LV2_STATE_SUCCESS;
}

static const void*
extension_data(const char* uri)
{
  static const LV2_Worker_Interface worker = { work, work_response, NULL };
  static const LV2_State_Interface  state  = { save, restore };
  if (!strcmp(uri, LV2_WORKER__interface)) {
    return &worker;
  }
  else if (!strcmp(uri, LV2_STATE__interface)) {
    return &state;
  }
  return NULL;
}

static const LV2_Descriptor descriptor0 = {
  CONVOLV2_URI "#Mono",
  instantiate,
  connect_port,
  NULL, // activate,
  run,
  NULL, // deactivate,
  cleanup,
  extension_data
};

static const LV2_Descriptor descriptor1 = {
  CONVOLV2_URI "#Stereo",
  instantiate,
  connect_port,
  NULL, // activate,
  run,
  NULL, // deactivate,
  cleanup,
  extension_data
};

static const LV2_Descriptor descriptor2 = {
  CONVOLV2_URI "#MonoToStereo",
  instantiate,
  connect_port,
  NULL, // activate,
  run,
  NULL, // deactivate,
  cleanup,
  extension_data
};


#undef LV2_SYMBOL_EXPORT
#ifdef _WIN32
#    define LV2_SYMBOL_EXPORT __declspec(dllexport)
#else
#    define LV2_SYMBOL_EXPORT  __attribute__ ((visibility ("default")))
#endif
LV2_SYMBOL_EXPORT
const LV2_Descriptor*
lv2_descriptor(uint32_t index)
{
  switch (index) {
  case 0:
    return &descriptor0;
  case 1:
    return &descriptor1;
  case 2:
    return &descriptor2;
  default:
    return NULL;
  }
}
/* vi:set ts=8 sts=2 sw=2: */