a-delay: Fix compiler warning
[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_LPF,
46         ADELAY_GAIN,
47         
48         ADELAY_DELAYTIME,
49 } PortIndex;
50
51
52 typedef struct {
53         LV2_URID atom_Blank;
54         LV2_URID atom_Object;
55         LV2_URID atom_Sequence;
56         LV2_URID atom_Long;
57         LV2_URID atom_Int;
58         LV2_URID atom_Float;
59         LV2_URID atom_Double;
60         LV2_URID time_beatUnit;
61         LV2_URID time_beatsPerMinute;
62         LV2_URID time_Position;
63 } DelayURIs;
64
65 typedef struct {
66         float* input;
67         float* output;
68
69         const LV2_Atom_Sequence* atombpm;
70
71         float* inv;
72         float* sync;
73         float* time;
74         float* divisor;
75         float* wetdry;
76         float* lpf;
77         float* gain;
78         
79         float* delaytime;
80
81         float srate;
82         float bpm;
83         float beatunit;
84         int beatuniti;
85         int bpmvalid;
86
87         uint32_t posz;
88         float tap[2];
89         float z[MAX_DELAY];
90         int active;
91         int next;
92         float fbstate;
93         float lpfold;
94         float divisorold;
95         float gainold;
96         float invertold;
97         float timeold;
98         float delaytimeold;
99         float syncold;
100         float wetdryold;
101         float delaysamplesold;
102
103         float A0, A1, A2, A3, A4, A5;
104         float B0, B1, B2, B3, B4, B5;
105         float state[4];
106
107         DelayURIs uris;
108         LV2_Atom_Forge forge;
109         LV2_URID_Map* map;
110 } ADelay;
111
112 static inline void
113 map_uris(LV2_URID_Map* map, DelayURIs* uris)
114 {
115         uris->atom_Blank          = map->map(map->handle, LV2_ATOM__Blank);
116         uris->atom_Object         = map->map(map->handle, LV2_ATOM__Object);
117         uris->atom_Sequence       = map->map(map->handle, LV2_ATOM__Sequence);
118         uris->atom_Long           = map->map(map->handle, LV2_ATOM__Long);
119         uris->atom_Int            = map->map(map->handle, LV2_ATOM__Int);
120         uris->atom_Float          = map->map(map->handle, LV2_ATOM__Float);
121         uris->atom_Double         = map->map(map->handle, LV2_ATOM__Double);
122         uris->time_beatUnit       = map->map(map->handle, LV2_TIME__beatUnit);
123         uris->time_beatsPerMinute = map->map(map->handle, LV2_TIME__beatsPerMinute);
124         uris->time_Position       = map->map(map->handle, LV2_TIME__Position);
125 }
126
127 static LV2_Handle
128 instantiate(const LV2_Descriptor* descriptor,
129             double rate,
130             const char* bundle_path,
131             const LV2_Feature* const* features)
132 {
133         int i;
134         ADelay* adelay = (ADelay*)calloc(1, sizeof(ADelay));
135         if (!adelay) return NULL;
136
137         for (i = 0; features[i]; ++i) {
138                 if (!strcmp(features[i]->URI, LV2_URID__map)) {
139                         adelay->map = (LV2_URID_Map*)features[i]->data;
140                 }
141         }
142
143         if (!adelay->map) {
144                 fprintf(stderr, "a-delay.lv2 error: Host does not support urid:map\n");
145                 free(adelay);
146                 return NULL;
147         }
148
149         map_uris(adelay->map, &adelay->uris);
150         lv2_atom_forge_init(&adelay->forge, adelay->map);
151
152         adelay->srate = rate;
153         adelay->bpmvalid = 0;
154
155         return (LV2_Handle)adelay;
156 }
157
158 static void
159 connect_port(LV2_Handle instance,
160              uint32_t port,
161              void* data)
162 {
163         ADelay* adelay = (ADelay*)instance;
164
165         switch ((PortIndex)port) {
166         case ADELAY_INPUT:
167                 adelay->input = (float*)data;
168                 break;
169         case ADELAY_OUTPUT:
170                 adelay->output = (float*)data;
171                 break;
172         case ADELAY_BPM:
173                 adelay->atombpm = (const LV2_Atom_Sequence*)data;
174                 break;
175         case ADELAY_INV:
176                 adelay->inv = (float*)data;
177                 break;
178         case ADELAY_SYNC:
179                 adelay->sync = (float*)data;
180                 break;
181         case ADELAY_TIME:
182                 adelay->time = (float*)data;
183                 break;
184         case ADELAY_DIVISOR:
185                 adelay->divisor = (float*)data;
186                 break;
187         case ADELAY_WETDRY:
188                 adelay->wetdry = (float*)data;
189                 break;
190         case ADELAY_LPF:
191                 adelay->lpf = (float*)data;
192                 break;
193         case ADELAY_GAIN:
194                 adelay->gain = (float*)data;
195                 break;
196         case ADELAY_DELAYTIME:
197                 adelay->delaytime = (float*)data;
198                 break;
199         }
200 }
201
202 static inline float
203 sanitize_denormal(float value) {
204         if (!isnormal(value)) {
205                 value = 0.f;
206         }
207         return value;
208 }
209
210 static inline float
211 from_dB(float gdb) {
212         return (exp(gdb/20.f*log(10.f)));
213 }
214
215 static inline float
216 to_dB(float g) {
217         return (20.f*log10(g));
218 }
219
220 static void clearfilter(LV2_Handle instance)
221 {
222         ADelay* adelay = (ADelay*)instance;
223
224         adelay->state[0] = adelay->state[1] =
225                 adelay->state[2] = adelay->state[3] = 0.f;
226 }
227
228 static void
229 activate(LV2_Handle instance)
230 {
231         ADelay* adelay = (ADelay*)instance;
232
233         int i;
234         for (i = 0; i < MAX_DELAY; i++) {
235                 adelay->z[i] = 0.f;
236         }
237         adelay->posz = 0;
238         adelay->tap[0] = 0;
239         adelay->tap[1] = 0;
240         adelay->active = 0;
241         adelay->next = 1;
242         adelay->fbstate = 0.f;
243
244         clearfilter(adelay);
245
246         adelay->lpfold = 0.f;
247         adelay->divisorold = 0.f;
248         adelay->gainold = 0.f;
249         adelay->invertold = 0.f;
250         adelay->timeold = 0.f;
251         adelay->delaytimeold = 0.f;
252         adelay->syncold = 0.f;
253         adelay->wetdryold = 0.f;
254         adelay->delaysamplesold = 1.f;
255 }
256
257 static void lpfRbj(LV2_Handle instance, float fc, float srate)
258 {
259         ADelay* adelay = (ADelay*)instance;
260
261         float w0, alpha, cw, sw, q;
262         q = 0.707;
263         w0 = (2. * M_PI * fc / srate);
264         sw = sin(w0);
265         cw = cos(w0);
266         alpha = sw / (2. * q);
267
268         adelay->A0 = 1. + alpha;
269         adelay->A1 = -2. * cw;
270         adelay->A2 = 1. - alpha;
271         adelay->B0 = (1. - cw) / 2.;
272         adelay->B1 = (1. - cw);
273         adelay->B2 = adelay->B0;
274
275         adelay->A3 = 1. + alpha;
276         adelay->A4 = -2. * cw;
277         adelay->A5 = 1. - alpha;
278         adelay->B3 = (1. - cw) / 2.;
279         adelay->B4 = (1. - cw);
280         adelay->B5 = adelay->B3;
281 }
282
283 static float runfilter(LV2_Handle instance, float in)
284 {
285         ADelay* a = (ADelay*)instance;
286
287         float out;
288         in = sanitize_denormal(in);
289
290         out = a->B0/a->A0*in + a->B1/a->A0*a->state[0] + a->B2/a->A0*a->state[1]
291                         -a->A1/a->A0*a->state[2] - a->A2/a->A0*a->state[3] + 1e-20;
292
293         a->state[1] = a->state[0];
294         a->state[0] = in;
295         a->state[3] = a->state[2];
296         a->state[2] = out;
297         return out;
298 }
299
300 static void
301 update_bpm(ADelay* self, const LV2_Atom_Object* obj)
302 {
303         const DelayURIs* uris = &self->uris;
304
305         // Received new transport bpm/beatunit
306         LV2_Atom *beatunit = NULL, *bpm = NULL;
307         lv2_atom_object_get(obj,
308                             uris->time_beatUnit, &beatunit,
309                             uris->time_beatsPerMinute, &bpm,
310                             NULL);
311         // Tempo changed, update BPM
312         if (bpm && bpm->type == uris->atom_Float) {
313                 self->bpm = ((LV2_Atom_Float*)bpm)->body;
314         }
315         // Time signature changed, update beatunit
316         if (beatunit && beatunit->type == uris->atom_Int) {
317                 int b = ((LV2_Atom_Int*)beatunit)->body;
318                 self->beatunit = (float)b;
319         }
320         if (beatunit && beatunit->type == uris->atom_Double) {
321                 double b = ((LV2_Atom_Double*)beatunit)->body;
322                 self->beatunit = (float)b;
323         }
324         if (beatunit && beatunit->type == uris->atom_Float) {
325                 self->beatunit = ((LV2_Atom_Float*)beatunit)->body;
326         }
327         if (beatunit && beatunit->type == uris->atom_Long) {
328                 long int b = ((LV2_Atom_Long*)beatunit)->body;
329                 self->beatunit = (float)b;
330         }
331         self->bpmvalid = 1;
332 }
333
334 static void
335 run(LV2_Handle instance, uint32_t n_samples)
336 {
337         ADelay* adelay = (ADelay*)instance;
338
339         const float* const input = adelay->input;
340         float* const output = adelay->output;
341
342         float srate = adelay->srate;
343
344         uint32_t i;
345         float in;
346         int delaysamples;
347         unsigned int tmp;
348         float inv;
349         float xfade;
350         int recalc;
351         if (*(adelay->inv) < 0.5) {
352                 inv = -1.f;
353         } else {
354                 inv = 1.f;
355         }
356         
357         recalc = 0;
358         if (*(adelay->inv) != adelay->invertold) {
359                 recalc = 1;
360         }
361         if (*(adelay->sync) != adelay->syncold) {
362                 recalc = 1;
363         }
364         if (*(adelay->time) != adelay->timeold) {
365                 recalc = 1;
366         }
367         if (*(adelay->divisor) != adelay->divisorold) {
368                 recalc = 1;
369         }
370         if (*(adelay->lpf) != adelay->lpfold) {
371                 lpfRbj(adelay, *(adelay->lpf), srate);
372         }
373         if (*(adelay->gain) != adelay->gainold) {
374                 recalc = 1;
375         }
376         
377         if (recalc) {
378                 if (*(adelay->sync) > 0.5f && adelay->bpmvalid) {
379                         *(adelay->delaytime) = adelay->beatunit * 1000.f * 60.f / (adelay->bpm * *(adelay->divisor));
380                 } else {
381                         *(adelay->delaytime) = *(adelay->time);
382                 }
383                 delaysamples = (int)(*(adelay->delaytime) * srate) / 1000;
384                 adelay->tap[adelay->next] = delaysamples;
385         }
386
387         xfade = 0.f;
388         for (i = 0; i < n_samples; i++) {
389                 in = input[i];
390                 adelay->z[adelay->posz] = in; // + feedb / 100. * fbstate;
391                 adelay->fbstate = 0.f;
392                 int p = adelay->posz - adelay->tap[adelay->active]; // active line
393                 if (p<0) p += MAX_DELAY;
394                 adelay->fbstate += adelay->z[p];
395                 
396                 if (recalc) {
397                         xfade += 1.0f / (float)n_samples;
398                         adelay->fbstate *= (1.-xfade);
399                         p = adelay->posz - adelay->tap[adelay->next]; // next line
400                         if (p<0) p += MAX_DELAY;
401                         adelay->fbstate += adelay->z[p] * xfade;
402                 }
403                 output[i] = from_dB(*(adelay->gain)) * ((100.-*(adelay->wetdry)) / 100. * in + *(adelay->wetdry) / 100. * -inv * runfilter(adelay, adelay->fbstate));
404                 if (++(adelay->posz) >= MAX_DELAY) {
405                         adelay->posz = 0;
406                 }
407         }
408         adelay->lpfold = *(adelay->lpf);
409         adelay->divisorold = *(adelay->divisor);
410         adelay->gainold = *(adelay->gain);
411         adelay->invertold = *(adelay->inv);
412         adelay->timeold = *(adelay->time);
413         adelay->syncold = *(adelay->sync);
414         adelay->wetdryold = *(adelay->wetdry);
415         adelay->delaytimeold = *(adelay->delaytime);
416         adelay->delaysamplesold = delaysamples;
417         if (recalc) {
418                 tmp = adelay->active;
419                 adelay->active = adelay->next;
420                 adelay->next = tmp;
421         }
422         
423         if (adelay->atombpm) {
424                 LV2_Atom_Event* ev = lv2_atom_sequence_begin(&(adelay->atombpm)->body);
425                 while(!lv2_atom_sequence_is_end(&(adelay->atombpm)->body, (adelay->atombpm)->atom.size, ev)) {
426                         if (ev->body.type == adelay->uris.atom_Blank || ev->body.type == adelay->uris.atom_Object) {
427                                 const LV2_Atom_Object* obj = (LV2_Atom_Object*)&ev->body;
428                                 if (obj->body.otype == adelay->uris.time_Position) {
429                                         update_bpm(adelay, obj);
430                                 }
431                         }
432                         ev = lv2_atom_sequence_next(ev);
433                 }
434         }
435 }
436
437 static void
438 cleanup(LV2_Handle instance)
439 {
440         free(instance);
441 }
442
443 static const void*
444 extension_data(const char* uri)
445 {
446         return NULL;
447 }
448
449 static const LV2_Descriptor descriptor = {
450         ADELAY_URI,
451         instantiate,
452         connect_port,
453         activate,
454         run,
455         NULL,
456         cleanup,
457         extension_data
458 };
459
460 LV2_SYMBOL_EXPORT
461 const LV2_Descriptor*
462 lv2_descriptor(uint32_t index)
463 {
464         switch (index) {
465         case 0:
466                 return &descriptor;
467         default:
468                 return NULL;
469         }
470 }