2019-08-03 08:34:29 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016-2017 Robin Gareus <robin@gareus.org>
|
2016-07-05 11:53:59 -04:00
|
|
|
* Copyright (C) 2016 Damien Zammit <damien@zamaudio.com>
|
|
|
|
*
|
2019-08-03 08:34:29 -04:00
|
|
|
* 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 of the License, or
|
|
|
|
* (at your option) any later version.
|
2016-07-05 11:53:59 -04:00
|
|
|
*
|
|
|
|
* 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.
|
2019-08-03 08:34:29 -04:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2016-07-05 11:53:59 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "lv2/lv2plug.in/ns/lv2core/lv2.h"
|
|
|
|
#include "lv2/lv2plug.in/ns/ext/atom/atom.h"
|
|
|
|
#include "lv2/lv2plug.in/ns/ext/time/time.h"
|
|
|
|
#include "lv2/lv2plug.in/ns/ext/atom/forge.h"
|
|
|
|
#include "lv2/lv2plug.in/ns/ext/urid/urid.h"
|
|
|
|
|
|
|
|
#define ADELAY_URI "urn:ardour:a-delay"
|
|
|
|
|
|
|
|
// 8 seconds of delay at 96kHz
|
|
|
|
#define MAX_DELAY 768000
|
|
|
|
|
|
|
|
#ifndef M_PI
|
|
|
|
# define M_PI 3.1415926
|
|
|
|
#endif
|
|
|
|
|
2016-11-29 02:41:10 -05:00
|
|
|
#ifdef COMPILER_MSVC
|
|
|
|
#include <float.h>
|
|
|
|
#define isfinite_local(val) (bool)_finite((double)val)
|
|
|
|
#else
|
|
|
|
#define isfinite_local isfinite
|
|
|
|
#endif
|
|
|
|
|
2016-07-05 11:53:59 -04:00
|
|
|
typedef enum {
|
|
|
|
ADELAY_INPUT = 0,
|
|
|
|
ADELAY_OUTPUT,
|
|
|
|
|
|
|
|
ADELAY_BPM,
|
|
|
|
|
|
|
|
ADELAY_INV,
|
|
|
|
ADELAY_SYNC,
|
|
|
|
ADELAY_TIME,
|
|
|
|
ADELAY_DIVISOR,
|
|
|
|
ADELAY_WETDRY,
|
2016-07-19 00:42:53 -04:00
|
|
|
ADELAY_FEEDBACK,
|
2016-07-05 11:53:59 -04:00
|
|
|
ADELAY_LPF,
|
|
|
|
ADELAY_GAIN,
|
|
|
|
ADELAY_DELAYTIME,
|
2016-08-27 10:59:15 -04:00
|
|
|
ADELAY_ENABLE,
|
2020-08-30 13:14:44 -04:00
|
|
|
ADELAY_DOTTED,
|
2016-07-05 11:53:59 -04:00
|
|
|
} PortIndex;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
LV2_URID atom_Blank;
|
|
|
|
LV2_URID atom_Object;
|
|
|
|
LV2_URID atom_Sequence;
|
|
|
|
LV2_URID atom_Long;
|
|
|
|
LV2_URID atom_Int;
|
|
|
|
LV2_URID atom_Float;
|
|
|
|
LV2_URID atom_Double;
|
|
|
|
LV2_URID time_beatUnit;
|
|
|
|
LV2_URID time_beatsPerMinute;
|
|
|
|
LV2_URID time_Position;
|
|
|
|
} DelayURIs;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
float* input;
|
|
|
|
float* output;
|
|
|
|
|
|
|
|
const LV2_Atom_Sequence* atombpm;
|
|
|
|
|
|
|
|
float* inv;
|
2020-07-05 16:24:38 -04:00
|
|
|
float* dotted;
|
2016-07-05 11:53:59 -04:00
|
|
|
float* sync;
|
|
|
|
float* time;
|
|
|
|
float* divisor;
|
|
|
|
float* wetdry;
|
2016-07-19 00:42:53 -04:00
|
|
|
float* feedback;
|
2016-07-05 11:53:59 -04:00
|
|
|
float* lpf;
|
|
|
|
float* gain;
|
|
|
|
|
|
|
|
float* delaytime;
|
2016-08-27 10:59:15 -04:00
|
|
|
float* enable;
|
2016-07-05 11:53:59 -04:00
|
|
|
|
|
|
|
float srate;
|
|
|
|
float bpm;
|
|
|
|
float beatunit;
|
|
|
|
int bpmvalid;
|
|
|
|
|
|
|
|
uint32_t posz;
|
|
|
|
float tap[2];
|
|
|
|
float z[MAX_DELAY];
|
|
|
|
int active;
|
|
|
|
int next;
|
|
|
|
float fbstate;
|
|
|
|
float lpfold;
|
2016-07-19 00:42:53 -04:00
|
|
|
float feedbackold;
|
2016-07-05 11:53:59 -04:00
|
|
|
float divisorold;
|
|
|
|
float gainold;
|
2020-07-05 16:24:38 -04:00
|
|
|
float dottedold;
|
2016-07-05 11:53:59 -04:00
|
|
|
float invertold;
|
|
|
|
float timeold;
|
|
|
|
float delaytimeold;
|
|
|
|
float syncold;
|
|
|
|
float wetdryold;
|
|
|
|
float delaysamplesold;
|
2016-07-20 07:17:51 -04:00
|
|
|
float tau;
|
2016-07-05 11:53:59 -04:00
|
|
|
|
|
|
|
float A0, A1, A2, A3, A4, A5;
|
|
|
|
float B0, B1, B2, B3, B4, B5;
|
|
|
|
float state[4];
|
|
|
|
|
|
|
|
DelayURIs uris;
|
|
|
|
LV2_Atom_Forge forge;
|
|
|
|
LV2_URID_Map* map;
|
|
|
|
} ADelay;
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
map_uris(LV2_URID_Map* map, DelayURIs* uris)
|
|
|
|
{
|
|
|
|
uris->atom_Blank = map->map(map->handle, LV2_ATOM__Blank);
|
|
|
|
uris->atom_Object = map->map(map->handle, LV2_ATOM__Object);
|
|
|
|
uris->atom_Sequence = map->map(map->handle, LV2_ATOM__Sequence);
|
|
|
|
uris->atom_Long = map->map(map->handle, LV2_ATOM__Long);
|
|
|
|
uris->atom_Int = map->map(map->handle, LV2_ATOM__Int);
|
|
|
|
uris->atom_Float = map->map(map->handle, LV2_ATOM__Float);
|
|
|
|
uris->atom_Double = map->map(map->handle, LV2_ATOM__Double);
|
|
|
|
uris->time_beatUnit = map->map(map->handle, LV2_TIME__beatUnit);
|
|
|
|
uris->time_beatsPerMinute = map->map(map->handle, LV2_TIME__beatsPerMinute);
|
|
|
|
uris->time_Position = map->map(map->handle, LV2_TIME__Position);
|
|
|
|
}
|
|
|
|
|
|
|
|
static LV2_Handle
|
|
|
|
instantiate(const LV2_Descriptor* descriptor,
|
|
|
|
double rate,
|
|
|
|
const char* bundle_path,
|
|
|
|
const LV2_Feature* const* features)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
ADelay* adelay = (ADelay*)calloc(1, sizeof(ADelay));
|
|
|
|
if (!adelay) return NULL;
|
|
|
|
|
|
|
|
for (i = 0; features[i]; ++i) {
|
|
|
|
if (!strcmp(features[i]->URI, LV2_URID__map)) {
|
|
|
|
adelay->map = (LV2_URID_Map*)features[i]->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!adelay->map) {
|
|
|
|
fprintf(stderr, "a-delay.lv2 error: Host does not support urid:map\n");
|
|
|
|
free(adelay);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
map_uris(adelay->map, &adelay->uris);
|
|
|
|
lv2_atom_forge_init(&adelay->forge, adelay->map);
|
|
|
|
|
|
|
|
adelay->srate = rate;
|
|
|
|
adelay->bpmvalid = 0;
|
2016-08-27 10:59:15 -04:00
|
|
|
adelay->tau = (1.0 - exp (-2.f * M_PI * 25.f / adelay->srate));
|
2016-07-05 11:53:59 -04:00
|
|
|
|
|
|
|
return (LV2_Handle)adelay;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
connect_port(LV2_Handle instance,
|
|
|
|
uint32_t port,
|
|
|
|
void* data)
|
|
|
|
{
|
|
|
|
ADelay* adelay = (ADelay*)instance;
|
|
|
|
|
|
|
|
switch ((PortIndex)port) {
|
|
|
|
case ADELAY_INPUT:
|
|
|
|
adelay->input = (float*)data;
|
|
|
|
break;
|
|
|
|
case ADELAY_OUTPUT:
|
|
|
|
adelay->output = (float*)data;
|
|
|
|
break;
|
|
|
|
case ADELAY_BPM:
|
|
|
|
adelay->atombpm = (const LV2_Atom_Sequence*)data;
|
|
|
|
break;
|
|
|
|
case ADELAY_INV:
|
|
|
|
adelay->inv = (float*)data;
|
|
|
|
break;
|
|
|
|
case ADELAY_SYNC:
|
|
|
|
adelay->sync = (float*)data;
|
|
|
|
break;
|
|
|
|
case ADELAY_TIME:
|
|
|
|
adelay->time = (float*)data;
|
|
|
|
break;
|
|
|
|
case ADELAY_DIVISOR:
|
|
|
|
adelay->divisor = (float*)data;
|
|
|
|
break;
|
2020-07-05 16:24:38 -04:00
|
|
|
case ADELAY_DOTTED:
|
|
|
|
adelay->dotted = (float*)data;
|
|
|
|
break;
|
2016-07-05 11:53:59 -04:00
|
|
|
case ADELAY_WETDRY:
|
|
|
|
adelay->wetdry = (float*)data;
|
|
|
|
break;
|
2016-07-19 00:42:53 -04:00
|
|
|
case ADELAY_FEEDBACK:
|
|
|
|
adelay->feedback = (float*)data;
|
|
|
|
break;
|
2016-07-05 11:53:59 -04:00
|
|
|
case ADELAY_LPF:
|
|
|
|
adelay->lpf = (float*)data;
|
|
|
|
break;
|
|
|
|
case ADELAY_GAIN:
|
|
|
|
adelay->gain = (float*)data;
|
|
|
|
break;
|
|
|
|
case ADELAY_DELAYTIME:
|
|
|
|
adelay->delaytime = (float*)data;
|
|
|
|
break;
|
2016-08-27 10:59:15 -04:00
|
|
|
case ADELAY_ENABLE:
|
|
|
|
adelay->enable = (float*)data;
|
|
|
|
break;
|
2016-07-05 11:53:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float
|
2016-11-29 02:41:10 -05:00
|
|
|
sanitize_denormal(const float value) {
|
2016-07-05 11:53:59 -04:00
|
|
|
if (!isnormal(value)) {
|
2016-11-29 02:41:10 -05:00
|
|
|
return 0.f;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float
|
|
|
|
sanitize_input(const float value) {
|
|
|
|
if (!isfinite_local (value)) {
|
|
|
|
return 0.f;
|
2016-07-05 11:53:59 -04:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float
|
|
|
|
from_dB(float gdb) {
|
|
|
|
return (exp(gdb/20.f*log(10.f)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float
|
|
|
|
to_dB(float g) {
|
|
|
|
return (20.f*log10(g));
|
|
|
|
}
|
|
|
|
|
2016-07-20 07:17:51 -04:00
|
|
|
static inline bool
|
|
|
|
is_eq(float a, float b, float small) {
|
|
|
|
return (fabsf(a - b) < small);
|
|
|
|
}
|
|
|
|
|
2016-07-05 11:53:59 -04:00
|
|
|
static void clearfilter(LV2_Handle instance)
|
|
|
|
{
|
|
|
|
ADelay* adelay = (ADelay*)instance;
|
|
|
|
|
|
|
|
adelay->state[0] = adelay->state[1] =
|
|
|
|
adelay->state[2] = adelay->state[3] = 0.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
activate(LV2_Handle instance)
|
|
|
|
{
|
|
|
|
ADelay* adelay = (ADelay*)instance;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < MAX_DELAY; i++) {
|
|
|
|
adelay->z[i] = 0.f;
|
|
|
|
}
|
|
|
|
adelay->posz = 0;
|
|
|
|
adelay->tap[0] = 0;
|
|
|
|
adelay->tap[1] = 0;
|
|
|
|
adelay->active = 0;
|
|
|
|
adelay->next = 1;
|
|
|
|
adelay->fbstate = 0.f;
|
|
|
|
|
|
|
|
clearfilter(adelay);
|
|
|
|
|
|
|
|
adelay->lpfold = 0.f;
|
|
|
|
adelay->divisorold = 0.f;
|
|
|
|
adelay->gainold = 0.f;
|
|
|
|
adelay->invertold = 0.f;
|
2020-07-05 16:24:38 -04:00
|
|
|
adelay->dottedold = 0.f;
|
2016-07-05 11:53:59 -04:00
|
|
|
adelay->timeold = 0.f;
|
|
|
|
adelay->delaytimeold = 0.f;
|
|
|
|
adelay->syncold = 0.f;
|
|
|
|
adelay->wetdryold = 0.f;
|
|
|
|
adelay->delaysamplesold = 1.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lpfRbj(LV2_Handle instance, float fc, float srate)
|
|
|
|
{
|
|
|
|
ADelay* adelay = (ADelay*)instance;
|
|
|
|
|
|
|
|
float w0, alpha, cw, sw, q;
|
|
|
|
q = 0.707;
|
|
|
|
w0 = (2. * M_PI * fc / srate);
|
|
|
|
sw = sin(w0);
|
|
|
|
cw = cos(w0);
|
|
|
|
alpha = sw / (2. * q);
|
|
|
|
|
|
|
|
adelay->A0 = 1. + alpha;
|
|
|
|
adelay->A1 = -2. * cw;
|
|
|
|
adelay->A2 = 1. - alpha;
|
|
|
|
adelay->B0 = (1. - cw) / 2.;
|
|
|
|
adelay->B1 = (1. - cw);
|
|
|
|
adelay->B2 = adelay->B0;
|
|
|
|
|
|
|
|
adelay->A3 = 1. + alpha;
|
|
|
|
adelay->A4 = -2. * cw;
|
|
|
|
adelay->A5 = 1. - alpha;
|
|
|
|
adelay->B3 = (1. - cw) / 2.;
|
|
|
|
adelay->B4 = (1. - cw);
|
|
|
|
adelay->B5 = adelay->B3;
|
|
|
|
}
|
|
|
|
|
2016-11-29 02:41:10 -05:00
|
|
|
static float runfilter(LV2_Handle instance, const float in)
|
2016-07-05 11:53:59 -04:00
|
|
|
{
|
|
|
|
ADelay* a = (ADelay*)instance;
|
|
|
|
|
|
|
|
float out;
|
|
|
|
|
|
|
|
out = a->B0/a->A0*in + a->B1/a->A0*a->state[0] + a->B2/a->A0*a->state[1]
|
|
|
|
-a->A1/a->A0*a->state[2] - a->A2/a->A0*a->state[3] + 1e-20;
|
|
|
|
|
|
|
|
a->state[1] = a->state[0];
|
|
|
|
a->state[0] = in;
|
|
|
|
a->state[3] = a->state[2];
|
2016-11-29 02:41:10 -05:00
|
|
|
a->state[2] = sanitize_input (out);
|
2016-07-05 11:53:59 -04:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2017-03-11 17:10:44 -05:00
|
|
|
static bool
|
2016-07-05 11:53:59 -04:00
|
|
|
update_bpm(ADelay* self, const LV2_Atom_Object* obj)
|
|
|
|
{
|
2017-03-11 17:10:44 -05:00
|
|
|
bool changed = false;
|
2016-07-05 11:53:59 -04:00
|
|
|
const DelayURIs* uris = &self->uris;
|
|
|
|
|
|
|
|
// Received new transport bpm/beatunit
|
|
|
|
LV2_Atom *beatunit = NULL, *bpm = NULL;
|
|
|
|
lv2_atom_object_get(obj,
|
|
|
|
uris->time_beatUnit, &beatunit,
|
|
|
|
uris->time_beatsPerMinute, &bpm,
|
|
|
|
NULL);
|
|
|
|
// Tempo changed, update BPM
|
|
|
|
if (bpm && bpm->type == uris->atom_Float) {
|
2017-03-11 17:10:44 -05:00
|
|
|
float b = ((LV2_Atom_Float*)bpm)->body;
|
|
|
|
if (self->bpm != b) {
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
self->bpm = b;
|
2016-07-05 11:53:59 -04:00
|
|
|
}
|
|
|
|
// Time signature changed, update beatunit
|
|
|
|
if (beatunit && beatunit->type == uris->atom_Int) {
|
|
|
|
int b = ((LV2_Atom_Int*)beatunit)->body;
|
2017-03-11 17:10:44 -05:00
|
|
|
if (self->beatunit != b) {
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
self->beatunit = b;
|
2016-07-05 11:53:59 -04:00
|
|
|
}
|
|
|
|
self->bpmvalid = 1;
|
2017-03-11 17:10:44 -05:00
|
|
|
return changed;
|
2016-07-05 11:53:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
run(LV2_Handle instance, uint32_t n_samples)
|
|
|
|
{
|
|
|
|
ADelay* adelay = (ADelay*)instance;
|
|
|
|
|
|
|
|
const float* const input = adelay->input;
|
|
|
|
float* const output = adelay->output;
|
|
|
|
|
2016-08-27 10:59:15 -04:00
|
|
|
const float srate = adelay->srate;
|
|
|
|
const float tau = adelay->tau;
|
|
|
|
|
|
|
|
float wetdry_target = *adelay->wetdry / 100.f;
|
|
|
|
float gain_target = from_dB(*adelay->gain);
|
|
|
|
float wetdry = adelay->wetdryold;
|
|
|
|
float gain = adelay->gainold;
|
|
|
|
|
|
|
|
if (*adelay->enable <= 0) {
|
|
|
|
wetdry_target = 0.f;
|
|
|
|
gain_target = 1.0;
|
|
|
|
}
|
2016-07-05 11:53:59 -04:00
|
|
|
|
|
|
|
uint32_t i;
|
|
|
|
float in;
|
2016-07-28 07:28:38 -04:00
|
|
|
int delaysamples = 0;
|
2016-07-05 11:53:59 -04:00
|
|
|
unsigned int tmp;
|
|
|
|
float inv;
|
|
|
|
float xfade;
|
2017-03-11 17:10:44 -05:00
|
|
|
bool recalc = false;
|
2016-08-27 10:59:15 -04:00
|
|
|
|
|
|
|
// TODO LPF
|
2016-07-05 11:53:59 -04:00
|
|
|
if (*(adelay->inv) < 0.5) {
|
|
|
|
inv = -1.f;
|
|
|
|
} else {
|
|
|
|
inv = 1.f;
|
|
|
|
}
|
2017-03-11 17:10:44 -05:00
|
|
|
|
|
|
|
if (adelay->atombpm) {
|
|
|
|
LV2_Atom_Event* ev = lv2_atom_sequence_begin(&(adelay->atombpm)->body);
|
|
|
|
while(!lv2_atom_sequence_is_end(&(adelay->atombpm)->body, (adelay->atombpm)->atom.size, ev)) {
|
|
|
|
if (ev->body.type == adelay->uris.atom_Blank || ev->body.type == adelay->uris.atom_Object) {
|
|
|
|
const LV2_Atom_Object* obj = (LV2_Atom_Object*)&ev->body;
|
|
|
|
if (obj->body.otype == adelay->uris.time_Position) {
|
|
|
|
recalc = update_bpm(adelay, obj);
|
|
|
|
// TODO: split process on BPM change. (independent of buffer-size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ev = lv2_atom_sequence_next(ev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-05 11:53:59 -04:00
|
|
|
if (*(adelay->inv) != adelay->invertold) {
|
2017-03-11 17:10:44 -05:00
|
|
|
recalc = true;
|
2016-07-05 11:53:59 -04:00
|
|
|
}
|
2020-07-05 16:24:38 -04:00
|
|
|
if (*(adelay->dotted) != adelay->dottedold) {
|
|
|
|
recalc = true;
|
|
|
|
}
|
2016-07-05 11:53:59 -04:00
|
|
|
if (*(adelay->sync) != adelay->syncold) {
|
2017-03-11 17:10:44 -05:00
|
|
|
recalc = true;
|
2016-07-05 11:53:59 -04:00
|
|
|
}
|
|
|
|
if (*(adelay->time) != adelay->timeold) {
|
2017-03-11 17:10:44 -05:00
|
|
|
recalc = true;
|
2016-07-05 11:53:59 -04:00
|
|
|
}
|
2016-07-19 00:42:53 -04:00
|
|
|
if (*(adelay->feedback) != adelay->feedbackold) {
|
2017-03-11 17:10:44 -05:00
|
|
|
recalc = true;
|
2016-07-19 00:42:53 -04:00
|
|
|
}
|
2016-07-05 11:53:59 -04:00
|
|
|
if (*(adelay->divisor) != adelay->divisorold) {
|
2017-03-11 17:10:44 -05:00
|
|
|
recalc = true;
|
2016-07-05 11:53:59 -04:00
|
|
|
}
|
2016-07-20 07:17:51 -04:00
|
|
|
if (!is_eq(adelay->lpfold, *adelay->lpf, 0.1)) {
|
2016-08-27 10:59:15 -04:00
|
|
|
float tc = (1.0 - exp (-2.f * M_PI * n_samples * 25.f / adelay->srate));
|
|
|
|
adelay->lpfold += tc * (*adelay->lpf - adelay->lpfold);
|
2017-03-11 17:10:44 -05:00
|
|
|
recalc = true;
|
2016-07-05 11:53:59 -04:00
|
|
|
}
|
|
|
|
|
2017-03-11 17:10:44 -05:00
|
|
|
// rg says: in case the delay-time changes, oversampling/interpolate + LPF
|
|
|
|
// would me more appropriate.
|
2016-07-05 11:53:59 -04:00
|
|
|
if (recalc) {
|
2016-07-20 07:17:51 -04:00
|
|
|
lpfRbj(adelay, adelay->lpfold, srate);
|
2016-07-05 11:53:59 -04:00
|
|
|
if (*(adelay->sync) > 0.5f && adelay->bpmvalid) {
|
2021-03-28 12:16:34 -04:00
|
|
|
/* quarter notes / min : 4 qn * 1000 ms/s * 60 s/min = 24k */
|
|
|
|
*(adelay->delaytime) = 240000.f / (adelay->bpm * *(adelay->divisor));
|
2020-07-05 16:24:38 -04:00
|
|
|
if (*(adelay->dotted) > 0.5f) {
|
|
|
|
*(adelay->delaytime) *= 1.5;
|
|
|
|
}
|
2016-07-05 11:53:59 -04:00
|
|
|
} else {
|
|
|
|
*(adelay->delaytime) = *(adelay->time);
|
|
|
|
}
|
|
|
|
delaysamples = (int)(*(adelay->delaytime) * srate) / 1000;
|
|
|
|
adelay->tap[adelay->next] = delaysamples;
|
|
|
|
}
|
|
|
|
|
|
|
|
xfade = 0.f;
|
2016-11-29 02:41:10 -05:00
|
|
|
|
|
|
|
float fbstate = adelay->fbstate;
|
|
|
|
const float feedback = *adelay->feedback;
|
|
|
|
|
2016-07-05 11:53:59 -04:00
|
|
|
for (i = 0; i < n_samples; i++) {
|
2016-11-29 02:41:10 -05:00
|
|
|
in = sanitize_input (input[i]);
|
|
|
|
adelay->z[adelay->posz] = sanitize_denormal (in + feedback / 100.f * fbstate);
|
2016-07-05 11:53:59 -04:00
|
|
|
int p = adelay->posz - adelay->tap[adelay->active]; // active line
|
|
|
|
if (p<0) p += MAX_DELAY;
|
2016-11-29 02:41:10 -05:00
|
|
|
fbstate = adelay->z[p];
|
2016-07-05 11:53:59 -04:00
|
|
|
|
|
|
|
if (recalc) {
|
|
|
|
xfade += 1.0f / (float)n_samples;
|
2016-11-29 02:41:10 -05:00
|
|
|
fbstate *= (1.-xfade);
|
2016-07-05 12:18:10 -04:00
|
|
|
p = adelay->posz - adelay->tap[adelay->next]; // next line
|
2016-07-05 11:53:59 -04:00
|
|
|
if (p<0) p += MAX_DELAY;
|
2016-11-29 02:41:10 -05:00
|
|
|
fbstate += adelay->z[p] * xfade;
|
2016-07-05 11:53:59 -04:00
|
|
|
}
|
2016-08-27 10:59:15 -04:00
|
|
|
|
|
|
|
wetdry += tau * (wetdry_target - wetdry) + 1e-12;
|
|
|
|
gain += tau * (gain_target - gain) + 1e-12;
|
|
|
|
|
|
|
|
output[i] = (1.f - wetdry) * in;
|
2016-11-29 02:41:10 -05:00
|
|
|
output[i] += wetdry * -inv * runfilter(adelay, fbstate);
|
2016-08-27 10:59:15 -04:00
|
|
|
output[i] *= gain;
|
|
|
|
|
2016-07-05 11:53:59 -04:00
|
|
|
if (++(adelay->posz) >= MAX_DELAY) {
|
|
|
|
adelay->posz = 0;
|
|
|
|
}
|
|
|
|
}
|
2016-08-27 10:59:15 -04:00
|
|
|
|
2016-11-29 02:41:10 -05:00
|
|
|
adelay->fbstate = fbstate;
|
2016-07-19 00:42:53 -04:00
|
|
|
adelay->feedbackold = *(adelay->feedback);
|
2016-07-05 11:53:59 -04:00
|
|
|
adelay->divisorold = *(adelay->divisor);
|
|
|
|
adelay->invertold = *(adelay->inv);
|
2020-07-05 16:24:38 -04:00
|
|
|
adelay->dottedold = *(adelay->dotted);
|
2016-07-05 11:53:59 -04:00
|
|
|
adelay->timeold = *(adelay->time);
|
|
|
|
adelay->syncold = *(adelay->sync);
|
2016-08-27 10:59:15 -04:00
|
|
|
adelay->wetdryold = wetdry;
|
|
|
|
adelay->gainold = gain;
|
2016-07-05 11:53:59 -04:00
|
|
|
adelay->delaytimeold = *(adelay->delaytime);
|
|
|
|
adelay->delaysamplesold = delaysamples;
|
2016-08-27 10:59:15 -04:00
|
|
|
|
2016-07-05 11:53:59 -04:00
|
|
|
if (recalc) {
|
|
|
|
tmp = adelay->active;
|
|
|
|
adelay->active = adelay->next;
|
|
|
|
adelay->next = tmp;
|
|
|
|
}
|
2017-03-11 17:10:44 -05:00
|
|
|
|
2016-07-05 11:53:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup(LV2_Handle instance)
|
|
|
|
{
|
|
|
|
free(instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const void*
|
|
|
|
extension_data(const char* uri)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const LV2_Descriptor descriptor = {
|
|
|
|
ADELAY_URI,
|
|
|
|
instantiate,
|
|
|
|
connect_port,
|
|
|
|
activate,
|
|
|
|
run,
|
|
|
|
NULL,
|
|
|
|
cleanup,
|
|
|
|
extension_data
|
|
|
|
};
|
|
|
|
|
|
|
|
LV2_SYMBOL_EXPORT
|
|
|
|
const LV2_Descriptor*
|
|
|
|
lv2_descriptor(uint32_t index)
|
|
|
|
{
|
|
|
|
switch (index) {
|
|
|
|
case 0:
|
|
|
|
return &descriptor;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|