2 * Copyright (C) 2016 Damien Zammit <damien@zamaudio.com>
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.
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.
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"
25 #define ADELAY_URI "urn:ardour:a-delay"
27 // 8 seconds of delay at 96kHz
28 #define MAX_DELAY 768000
31 # define M_PI 3.1415926
56 LV2_URID atom_Sequence;
61 LV2_URID time_beatUnit;
62 LV2_URID time_beatsPerMinute;
63 LV2_URID time_Position;
70 const LV2_Atom_Sequence* atombpm;
103 float delaysamplesold;
106 float A0, A1, A2, A3, A4, A5;
107 float B0, B1, B2, B3, B4, B5;
111 LV2_Atom_Forge forge;
116 map_uris(LV2_URID_Map* map, DelayURIs* uris)
118 uris->atom_Blank = map->map(map->handle, LV2_ATOM__Blank);
119 uris->atom_Object = map->map(map->handle, LV2_ATOM__Object);
120 uris->atom_Sequence = map->map(map->handle, LV2_ATOM__Sequence);
121 uris->atom_Long = map->map(map->handle, LV2_ATOM__Long);
122 uris->atom_Int = map->map(map->handle, LV2_ATOM__Int);
123 uris->atom_Float = map->map(map->handle, LV2_ATOM__Float);
124 uris->atom_Double = map->map(map->handle, LV2_ATOM__Double);
125 uris->time_beatUnit = map->map(map->handle, LV2_TIME__beatUnit);
126 uris->time_beatsPerMinute = map->map(map->handle, LV2_TIME__beatsPerMinute);
127 uris->time_Position = map->map(map->handle, LV2_TIME__Position);
131 instantiate(const LV2_Descriptor* descriptor,
133 const char* bundle_path,
134 const LV2_Feature* const* features)
137 ADelay* adelay = (ADelay*)calloc(1, sizeof(ADelay));
138 if (!adelay) return NULL;
140 for (i = 0; features[i]; ++i) {
141 if (!strcmp(features[i]->URI, LV2_URID__map)) {
142 adelay->map = (LV2_URID_Map*)features[i]->data;
147 fprintf(stderr, "a-delay.lv2 error: Host does not support urid:map\n");
152 map_uris(adelay->map, &adelay->uris);
153 lv2_atom_forge_init(&adelay->forge, adelay->map);
155 adelay->srate = rate;
156 adelay->bpmvalid = 0;
157 // 25Hz time constant @ 64fpp
158 adelay->tau = (1.0 - exp(-2.0 * M_PI * 64. * 25. / adelay->srate));
160 return (LV2_Handle)adelay;
164 connect_port(LV2_Handle instance,
168 ADelay* adelay = (ADelay*)instance;
170 switch ((PortIndex)port) {
172 adelay->input = (float*)data;
175 adelay->output = (float*)data;
178 adelay->atombpm = (const LV2_Atom_Sequence*)data;
181 adelay->inv = (float*)data;
184 adelay->sync = (float*)data;
187 adelay->time = (float*)data;
190 adelay->divisor = (float*)data;
193 adelay->wetdry = (float*)data;
195 case ADELAY_FEEDBACK:
196 adelay->feedback = (float*)data;
199 adelay->lpf = (float*)data;
202 adelay->gain = (float*)data;
204 case ADELAY_DELAYTIME:
205 adelay->delaytime = (float*)data;
211 sanitize_denormal(float value) {
212 if (!isnormal(value)) {
220 return (exp(gdb/20.f*log(10.f)));
225 return (20.f*log10(g));
229 is_eq(float a, float b, float small) {
230 return (fabsf(a - b) < small);
233 static void clearfilter(LV2_Handle instance)
235 ADelay* adelay = (ADelay*)instance;
237 adelay->state[0] = adelay->state[1] =
238 adelay->state[2] = adelay->state[3] = 0.f;
242 activate(LV2_Handle instance)
244 ADelay* adelay = (ADelay*)instance;
247 for (i = 0; i < MAX_DELAY; i++) {
255 adelay->fbstate = 0.f;
259 adelay->lpfold = 0.f;
260 adelay->divisorold = 0.f;
261 adelay->gainold = 0.f;
262 adelay->invertold = 0.f;
263 adelay->timeold = 0.f;
264 adelay->delaytimeold = 0.f;
265 adelay->syncold = 0.f;
266 adelay->wetdryold = 0.f;
267 adelay->delaysamplesold = 1.f;
270 static void lpfRbj(LV2_Handle instance, float fc, float srate)
272 ADelay* adelay = (ADelay*)instance;
274 float w0, alpha, cw, sw, q;
276 w0 = (2. * M_PI * fc / srate);
279 alpha = sw / (2. * q);
281 adelay->A0 = 1. + alpha;
282 adelay->A1 = -2. * cw;
283 adelay->A2 = 1. - alpha;
284 adelay->B0 = (1. - cw) / 2.;
285 adelay->B1 = (1. - cw);
286 adelay->B2 = adelay->B0;
288 adelay->A3 = 1. + alpha;
289 adelay->A4 = -2. * cw;
290 adelay->A5 = 1. - alpha;
291 adelay->B3 = (1. - cw) / 2.;
292 adelay->B4 = (1. - cw);
293 adelay->B5 = adelay->B3;
296 static float runfilter(LV2_Handle instance, float in)
298 ADelay* a = (ADelay*)instance;
301 in = sanitize_denormal(in);
303 out = a->B0/a->A0*in + a->B1/a->A0*a->state[0] + a->B2/a->A0*a->state[1]
304 -a->A1/a->A0*a->state[2] - a->A2/a->A0*a->state[3] + 1e-20;
306 a->state[1] = a->state[0];
308 a->state[3] = a->state[2];
314 update_bpm(ADelay* self, const LV2_Atom_Object* obj)
316 const DelayURIs* uris = &self->uris;
318 // Received new transport bpm/beatunit
319 LV2_Atom *beatunit = NULL, *bpm = NULL;
320 lv2_atom_object_get(obj,
321 uris->time_beatUnit, &beatunit,
322 uris->time_beatsPerMinute, &bpm,
324 // Tempo changed, update BPM
325 if (bpm && bpm->type == uris->atom_Float) {
326 self->bpm = ((LV2_Atom_Float*)bpm)->body;
328 // Time signature changed, update beatunit
329 if (beatunit && beatunit->type == uris->atom_Int) {
330 int b = ((LV2_Atom_Int*)beatunit)->body;
331 self->beatunit = (float)b;
333 if (beatunit && beatunit->type == uris->atom_Double) {
334 double b = ((LV2_Atom_Double*)beatunit)->body;
335 self->beatunit = (float)b;
337 if (beatunit && beatunit->type == uris->atom_Float) {
338 self->beatunit = ((LV2_Atom_Float*)beatunit)->body;
340 if (beatunit && beatunit->type == uris->atom_Long) {
341 long int b = ((LV2_Atom_Long*)beatunit)->body;
342 self->beatunit = (float)b;
348 run(LV2_Handle instance, uint32_t n_samples)
350 ADelay* adelay = (ADelay*)instance;
352 const float* const input = adelay->input;
353 float* const output = adelay->output;
355 float srate = adelay->srate;
356 float tau = adelay->tau;
360 int delaysamples = 0;
365 if (*(adelay->inv) < 0.5) {
372 if (*(adelay->inv) != adelay->invertold) {
375 if (*(adelay->sync) != adelay->syncold) {
378 if (*(adelay->time) != adelay->timeold) {
381 if (*(adelay->feedback) != adelay->feedbackold) {
384 if (*(adelay->divisor) != adelay->divisorold) {
387 if (!is_eq(adelay->lpfold, *adelay->lpf, 0.1)) {
388 adelay->lpfold += tau * (*adelay->lpf - adelay->lpfold);
391 if (*(adelay->gain) != adelay->gainold) {
396 lpfRbj(adelay, adelay->lpfold, srate);
397 if (*(adelay->sync) > 0.5f && adelay->bpmvalid) {
398 *(adelay->delaytime) = adelay->beatunit * 1000.f * 60.f / (adelay->bpm * *(adelay->divisor));
400 *(adelay->delaytime) = *(adelay->time);
402 delaysamples = (int)(*(adelay->delaytime) * srate) / 1000;
403 adelay->tap[adelay->next] = delaysamples;
407 for (i = 0; i < n_samples; i++) {
409 adelay->z[adelay->posz] = in + *adelay->feedback / 100. * adelay->fbstate;
410 adelay->fbstate = 0.f;
411 int p = adelay->posz - adelay->tap[adelay->active]; // active line
412 if (p<0) p += MAX_DELAY;
413 adelay->fbstate += adelay->z[p];
416 xfade += 1.0f / (float)n_samples;
417 adelay->fbstate *= (1.-xfade);
418 p = adelay->posz - adelay->tap[adelay->next]; // next line
419 if (p<0) p += MAX_DELAY;
420 adelay->fbstate += adelay->z[p] * xfade;
422 output[i] = from_dB(*(adelay->gain)) * ((100.-*(adelay->wetdry)) / 100. * in + *(adelay->wetdry) / 100. * -inv * runfilter(adelay, adelay->fbstate));
423 if (++(adelay->posz) >= MAX_DELAY) {
427 adelay->feedbackold = *(adelay->feedback);
428 adelay->divisorold = *(adelay->divisor);
429 adelay->gainold = *(adelay->gain);
430 adelay->invertold = *(adelay->inv);
431 adelay->timeold = *(adelay->time);
432 adelay->syncold = *(adelay->sync);
433 adelay->wetdryold = *(adelay->wetdry);
434 adelay->delaytimeold = *(adelay->delaytime);
435 adelay->delaysamplesold = delaysamples;
437 tmp = adelay->active;
438 adelay->active = adelay->next;
442 if (adelay->atombpm) {
443 LV2_Atom_Event* ev = lv2_atom_sequence_begin(&(adelay->atombpm)->body);
444 while(!lv2_atom_sequence_is_end(&(adelay->atombpm)->body, (adelay->atombpm)->atom.size, ev)) {
445 if (ev->body.type == adelay->uris.atom_Blank || ev->body.type == adelay->uris.atom_Object) {
446 const LV2_Atom_Object* obj = (LV2_Atom_Object*)&ev->body;
447 if (obj->body.otype == adelay->uris.time_Position) {
448 update_bpm(adelay, obj);
451 ev = lv2_atom_sequence_next(ev);
457 cleanup(LV2_Handle instance)
463 extension_data(const char* uri)
468 static const LV2_Descriptor descriptor = {
480 const LV2_Descriptor*
481 lv2_descriptor(uint32_t index)