update default config (see also 5ec21347a)
[ardour.git] / libs / plugins / a-delay.lv2 / a-delay.c
1 /* a-delay
2  * Copyright (C) 2016 Damien Zammit <damien@zamaudio.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <math.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18
19 #include "lv2/lv2plug.in/ns/lv2core/lv2.h"
20 #include "lv2/lv2plug.in/ns/ext/atom/atom.h"
21 #include "lv2/lv2plug.in/ns/ext/time/time.h"
22 #include "lv2/lv2plug.in/ns/ext/atom/forge.h"
23 #include "lv2/lv2plug.in/ns/ext/urid/urid.h"
24
25 #define ADELAY_URI "urn:ardour:a-delay"
26
27 // 8 seconds of delay at 96kHz
28 #define MAX_DELAY 768000
29
30 #ifndef M_PI
31 # define M_PI 3.1415926
32 #endif
33
34 typedef enum {
35         ADELAY_INPUT = 0,
36         ADELAY_OUTPUT,
37
38         ADELAY_BPM,
39
40         ADELAY_INV,
41         ADELAY_SYNC,
42         ADELAY_TIME,
43         ADELAY_DIVISOR,
44         ADELAY_WETDRY,
45         ADELAY_FEEDBACK,
46         ADELAY_LPF,
47         ADELAY_GAIN,
48         ADELAY_DELAYTIME,
49         ADELAY_ENABLE,
50 } PortIndex;
51
52
53 typedef struct {
54         LV2_URID atom_Blank;
55         LV2_URID atom_Object;
56         LV2_URID atom_Sequence;
57         LV2_URID atom_Long;
58         LV2_URID atom_Int;
59         LV2_URID atom_Float;
60         LV2_URID atom_Double;
61         LV2_URID time_beatUnit;
62         LV2_URID time_beatsPerMinute;
63         LV2_URID time_Position;
64 } DelayURIs;
65
66 typedef struct {
67         float* input;
68         float* output;
69
70         const LV2_Atom_Sequence* atombpm;
71
72         float* inv;
73         float* sync;
74         float* time;
75         float* divisor;
76         float* wetdry;
77         float* feedback;
78         float* lpf;
79         float* gain;
80         
81         float* delaytime;
82         float* enable;
83
84         float srate;
85         float bpm;
86         float beatunit;
87         int bpmvalid;
88
89         uint32_t posz;
90         float tap[2];
91         float z[MAX_DELAY];
92         int active;
93         int next;
94         float fbstate;
95         float lpfold;
96         float feedbackold;
97         float divisorold;
98         float gainold;
99         float invertold;
100         float timeold;
101         float delaytimeold;
102         float syncold;
103         float wetdryold;
104         float delaysamplesold;
105         float tau;
106
107         float A0, A1, A2, A3, A4, A5;
108         float B0, B1, B2, B3, B4, B5;
109         float state[4];
110
111         DelayURIs uris;
112         LV2_Atom_Forge forge;
113         LV2_URID_Map* map;
114 } ADelay;
115
116 static inline void
117 map_uris(LV2_URID_Map* map, DelayURIs* uris)
118 {
119         uris->atom_Blank          = map->map(map->handle, LV2_ATOM__Blank);
120         uris->atom_Object         = map->map(map->handle, LV2_ATOM__Object);
121         uris->atom_Sequence       = map->map(map->handle, LV2_ATOM__Sequence);
122         uris->atom_Long           = map->map(map->handle, LV2_ATOM__Long);
123         uris->atom_Int            = map->map(map->handle, LV2_ATOM__Int);
124         uris->atom_Float          = map->map(map->handle, LV2_ATOM__Float);
125         uris->atom_Double         = map->map(map->handle, LV2_ATOM__Double);
126         uris->time_beatUnit       = map->map(map->handle, LV2_TIME__beatUnit);
127         uris->time_beatsPerMinute = map->map(map->handle, LV2_TIME__beatsPerMinute);
128         uris->time_Position       = map->map(map->handle, LV2_TIME__Position);
129 }
130
131 static LV2_Handle
132 instantiate(const LV2_Descriptor* descriptor,
133             double rate,
134             const char* bundle_path,
135             const LV2_Feature* const* features)
136 {
137         int i;
138         ADelay* adelay = (ADelay*)calloc(1, sizeof(ADelay));
139         if (!adelay) return NULL;
140
141         for (i = 0; features[i]; ++i) {
142                 if (!strcmp(features[i]->URI, LV2_URID__map)) {
143                         adelay->map = (LV2_URID_Map*)features[i]->data;
144                 }
145         }
146
147         if (!adelay->map) {
148                 fprintf(stderr, "a-delay.lv2 error: Host does not support urid:map\n");
149                 free(adelay);
150                 return NULL;
151         }
152
153         map_uris(adelay->map, &adelay->uris);
154         lv2_atom_forge_init(&adelay->forge, adelay->map);
155
156         adelay->srate = rate;
157         adelay->bpmvalid = 0;
158         adelay->tau = (1.0 - exp (-2.f * M_PI * 25.f / adelay->srate));
159
160         return (LV2_Handle)adelay;
161 }
162
163 static void
164 connect_port(LV2_Handle instance,
165              uint32_t port,
166              void* data)
167 {
168         ADelay* adelay = (ADelay*)instance;
169
170         switch ((PortIndex)port) {
171         case ADELAY_INPUT:
172                 adelay->input = (float*)data;
173                 break;
174         case ADELAY_OUTPUT:
175                 adelay->output = (float*)data;
176                 break;
177         case ADELAY_BPM:
178                 adelay->atombpm = (const LV2_Atom_Sequence*)data;
179                 break;
180         case ADELAY_INV:
181                 adelay->inv = (float*)data;
182                 break;
183         case ADELAY_SYNC:
184                 adelay->sync = (float*)data;
185                 break;
186         case ADELAY_TIME:
187                 adelay->time = (float*)data;
188                 break;
189         case ADELAY_DIVISOR:
190                 adelay->divisor = (float*)data;
191                 break;
192         case ADELAY_WETDRY:
193                 adelay->wetdry = (float*)data;
194                 break;
195         case ADELAY_FEEDBACK:
196                 adelay->feedback = (float*)data;
197                 break;
198         case ADELAY_LPF:
199                 adelay->lpf = (float*)data;
200                 break;
201         case ADELAY_GAIN:
202                 adelay->gain = (float*)data;
203                 break;
204         case ADELAY_DELAYTIME:
205                 adelay->delaytime = (float*)data;
206                 break;
207         case ADELAY_ENABLE:
208                 adelay->enable = (float*)data;
209                 break;
210         }
211 }
212
213 static inline float
214 sanitize_denormal(float value) {
215         if (!isnormal(value)) {
216                 value = 0.f;
217         }
218         return value;
219 }
220
221 static inline float
222 from_dB(float gdb) {
223         return (exp(gdb/20.f*log(10.f)));
224 }
225
226 static inline float
227 to_dB(float g) {
228         return (20.f*log10(g));
229 }
230
231 static inline bool
232 is_eq(float a, float b, float small) {
233         return (fabsf(a - b) < small);
234 }
235
236 static void clearfilter(LV2_Handle instance)
237 {
238         ADelay* adelay = (ADelay*)instance;
239
240         adelay->state[0] = adelay->state[1] =
241                 adelay->state[2] = adelay->state[3] = 0.f;
242 }
243
244 static void
245 activate(LV2_Handle instance)
246 {
247         ADelay* adelay = (ADelay*)instance;
248
249         int i;
250         for (i = 0; i < MAX_DELAY; i++) {
251                 adelay->z[i] = 0.f;
252         }
253         adelay->posz = 0;
254         adelay->tap[0] = 0;
255         adelay->tap[1] = 0;
256         adelay->active = 0;
257         adelay->next = 1;
258         adelay->fbstate = 0.f;
259
260         clearfilter(adelay);
261
262         adelay->lpfold = 0.f;
263         adelay->divisorold = 0.f;
264         adelay->gainold = 0.f;
265         adelay->invertold = 0.f;
266         adelay->timeold = 0.f;
267         adelay->delaytimeold = 0.f;
268         adelay->syncold = 0.f;
269         adelay->wetdryold = 0.f;
270         adelay->delaysamplesold = 1.f;
271 }
272
273 static void lpfRbj(LV2_Handle instance, float fc, float srate)
274 {
275         ADelay* adelay = (ADelay*)instance;
276
277         float w0, alpha, cw, sw, q;
278         q = 0.707;
279         w0 = (2. * M_PI * fc / srate);
280         sw = sin(w0);
281         cw = cos(w0);
282         alpha = sw / (2. * q);
283
284         adelay->A0 = 1. + alpha;
285         adelay->A1 = -2. * cw;
286         adelay->A2 = 1. - alpha;
287         adelay->B0 = (1. - cw) / 2.;
288         adelay->B1 = (1. - cw);
289         adelay->B2 = adelay->B0;
290
291         adelay->A3 = 1. + alpha;
292         adelay->A4 = -2. * cw;
293         adelay->A5 = 1. - alpha;
294         adelay->B3 = (1. - cw) / 2.;
295         adelay->B4 = (1. - cw);
296         adelay->B5 = adelay->B3;
297 }
298
299 static float runfilter(LV2_Handle instance, float in)
300 {
301         ADelay* a = (ADelay*)instance;
302
303         float out;
304         in = sanitize_denormal(in);
305
306         out = a->B0/a->A0*in + a->B1/a->A0*a->state[0] + a->B2/a->A0*a->state[1]
307                         -a->A1/a->A0*a->state[2] - a->A2/a->A0*a->state[3] + 1e-20;
308
309         a->state[1] = a->state[0];
310         a->state[0] = in;
311         a->state[3] = a->state[2];
312         a->state[2] = out;
313         return out;
314 }
315
316 static void
317 update_bpm(ADelay* self, const LV2_Atom_Object* obj)
318 {
319         const DelayURIs* uris = &self->uris;
320
321         // Received new transport bpm/beatunit
322         LV2_Atom *beatunit = NULL, *bpm = NULL;
323         lv2_atom_object_get(obj,
324                             uris->time_beatUnit, &beatunit,
325                             uris->time_beatsPerMinute, &bpm,
326                             NULL);
327         // Tempo changed, update BPM
328         if (bpm && bpm->type == uris->atom_Float) {
329                 self->bpm = ((LV2_Atom_Float*)bpm)->body;
330         }
331         // Time signature changed, update beatunit
332         if (beatunit && beatunit->type == uris->atom_Int) {
333                 int b = ((LV2_Atom_Int*)beatunit)->body;
334                 self->beatunit = (float)b;
335         }
336         if (beatunit && beatunit->type == uris->atom_Double) {
337                 double b = ((LV2_Atom_Double*)beatunit)->body;
338                 self->beatunit = (float)b;
339         }
340         if (beatunit && beatunit->type == uris->atom_Float) {
341                 self->beatunit = ((LV2_Atom_Float*)beatunit)->body;
342         }
343         if (beatunit && beatunit->type == uris->atom_Long) {
344                 long int b = ((LV2_Atom_Long*)beatunit)->body;
345                 self->beatunit = (float)b;
346         }
347         self->bpmvalid = 1;
348 }
349
350 static void
351 run(LV2_Handle instance, uint32_t n_samples)
352 {
353         ADelay* adelay = (ADelay*)instance;
354
355         const float* const input = adelay->input;
356         float* const output = adelay->output;
357
358         const float srate = adelay->srate;
359         const float tau = adelay->tau;
360
361         float wetdry_target = *adelay->wetdry / 100.f;
362         float gain_target = from_dB(*adelay->gain);
363         float wetdry = adelay->wetdryold;
364         float gain = adelay->gainold;
365
366         if (*adelay->enable <= 0) {
367                 wetdry_target = 0.f;
368                 gain_target = 1.0;
369         }
370
371         uint32_t i;
372         float in;
373         int delaysamples = 0;
374         unsigned int tmp;
375         float inv;
376         float xfade;
377         int recalc;
378
379         // TODO LPF
380         if (*(adelay->inv) < 0.5) {
381                 inv = -1.f;
382         } else {
383                 inv = 1.f;
384         }
385         
386         recalc = 0;
387         if (*(adelay->inv) != adelay->invertold) {
388                 recalc = 1;
389         }
390         if (*(adelay->sync) != adelay->syncold) {
391                 recalc = 1;
392         }
393         if (*(adelay->time) != adelay->timeold) {
394                 recalc = 1;
395         }
396         if (*(adelay->feedback) != adelay->feedbackold) {
397                 recalc = 1;
398         }
399         if (*(adelay->divisor) != adelay->divisorold) {
400                 recalc = 1;
401         }
402         if (!is_eq(adelay->lpfold, *adelay->lpf, 0.1)) {
403                 float  tc = (1.0 - exp (-2.f * M_PI * n_samples * 25.f / adelay->srate));
404                 adelay->lpfold += tc * (*adelay->lpf - adelay->lpfold);
405                 recalc = 1;
406         }
407         
408         if (recalc) {
409                 lpfRbj(adelay, adelay->lpfold, srate);
410                 if (*(adelay->sync) > 0.5f && adelay->bpmvalid) {
411                         *(adelay->delaytime) = adelay->beatunit * 1000.f * 60.f / (adelay->bpm * *(adelay->divisor));
412                 } else {
413                         *(adelay->delaytime) = *(adelay->time);
414                 }
415                 delaysamples = (int)(*(adelay->delaytime) * srate) / 1000;
416                 adelay->tap[adelay->next] = delaysamples;
417         }
418
419         xfade = 0.f;
420         for (i = 0; i < n_samples; i++) {
421                 in = input[i];
422                 adelay->z[adelay->posz] = in + *adelay->feedback / 100. * adelay->fbstate;
423                 adelay->fbstate = 0.f;
424                 int p = adelay->posz - adelay->tap[adelay->active]; // active line
425                 if (p<0) p += MAX_DELAY;
426                 adelay->fbstate += adelay->z[p];
427                 
428                 if (recalc) {
429                         xfade += 1.0f / (float)n_samples;
430                         adelay->fbstate *= (1.-xfade);
431                         p = adelay->posz - adelay->tap[adelay->next]; // next line
432                         if (p<0) p += MAX_DELAY;
433                         adelay->fbstate += adelay->z[p] * xfade;
434                 }
435
436                 wetdry += tau * (wetdry_target - wetdry) + 1e-12;
437                 gain += tau * (gain_target - gain) + 1e-12;
438
439                 output[i] = (1.f - wetdry) * in;
440                 output[i] += wetdry * -inv * runfilter(adelay, adelay->fbstate);
441                 output[i] *= gain;
442
443                 if (++(adelay->posz) >= MAX_DELAY) {
444                         adelay->posz = 0;
445                 }
446         }
447
448         adelay->feedbackold = *(adelay->feedback);
449         adelay->divisorold = *(adelay->divisor);
450         adelay->invertold = *(adelay->inv);
451         adelay->timeold = *(adelay->time);
452         adelay->syncold = *(adelay->sync);
453         adelay->wetdryold = wetdry;
454         adelay->gainold = gain;
455         adelay->delaytimeold = *(adelay->delaytime);
456         adelay->delaysamplesold = delaysamples;
457
458         if (recalc) {
459                 tmp = adelay->active;
460                 adelay->active = adelay->next;
461                 adelay->next = tmp;
462         }
463         
464         if (adelay->atombpm) {
465                 LV2_Atom_Event* ev = lv2_atom_sequence_begin(&(adelay->atombpm)->body);
466                 while(!lv2_atom_sequence_is_end(&(adelay->atombpm)->body, (adelay->atombpm)->atom.size, ev)) {
467                         if (ev->body.type == adelay->uris.atom_Blank || ev->body.type == adelay->uris.atom_Object) {
468                                 const LV2_Atom_Object* obj = (LV2_Atom_Object*)&ev->body;
469                                 if (obj->body.otype == adelay->uris.time_Position) {
470                                         update_bpm(adelay, obj);
471                                 }
472                         }
473                         ev = lv2_atom_sequence_next(ev);
474                 }
475         }
476 }
477
478 static void
479 cleanup(LV2_Handle instance)
480 {
481         free(instance);
482 }
483
484 static const void*
485 extension_data(const char* uri)
486 {
487         return NULL;
488 }
489
490 static const LV2_Descriptor descriptor = {
491         ADELAY_URI,
492         instantiate,
493         connect_port,
494         activate,
495         run,
496         NULL,
497         cleanup,
498         extension_data
499 };
500
501 LV2_SYMBOL_EXPORT
502 const LV2_Descriptor*
503 lv2_descriptor(uint32_t index)
504 {
505         switch (index) {
506         case 0:
507                 return &descriptor;
508         default:
509                 return NULL;
510         }
511 }