2016-07-13 04:07:02 -04:00
|
|
|
/* a-reverb -- based on b_reverb (setBfree) and FreeVerb
|
2016-07-12 10:42:29 -04:00
|
|
|
*
|
|
|
|
* Copyright (C) 2003-2004 Fredrik Kilander <fk@dsv.su.se>
|
|
|
|
* Copyright (C) 2008-2016 Robin Gareus <robin@gareus.org>
|
|
|
|
* Copyright (C) 2012 Will Panther <pantherb@setbfree.org>
|
2016-07-13 04:07:02 -04:00
|
|
|
* Copyright (C) 2016 Damien Zammit <damien@zamaudio.com>
|
2016-07-12 10:42: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, 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 <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-07-13 04:07:02 -04:00
|
|
|
#define RV_NZ (8+4)
|
2016-07-12 10:42:29 -04:00
|
|
|
#define DENORMAL_PROTECT (1e-14)
|
|
|
|
|
2016-07-13 04:07:02 -04:00
|
|
|
|
2016-07-12 10:42:29 -04:00
|
|
|
typedef struct {
|
2016-07-13 04:07:02 -04:00
|
|
|
float* delays[2][RV_NZ]; /**< delay line buffer */
|
2016-07-12 10:42:29 -04:00
|
|
|
|
2016-07-13 04:07:02 -04:00
|
|
|
float* idx0[2][RV_NZ]; /**< Reset pointer ref delays[]*/
|
|
|
|
float* idxp[2][RV_NZ]; /**< Index pointer ref delays[]*/
|
|
|
|
float* endp[2][RV_NZ]; /**< End pointer ref delays[]*/
|
2016-07-12 10:42:29 -04:00
|
|
|
|
|
|
|
float gain[RV_NZ]; /**< feedback gains */
|
2016-07-13 04:07:02 -04:00
|
|
|
float yy1_0; /**< Previous output sample */
|
|
|
|
float y_1_0; /**< Feedback sample */
|
|
|
|
float yy1_1; /**< Previous output sample */
|
|
|
|
float y_1_1; /**< Feedback sample */
|
2016-07-12 10:42:29 -04:00
|
|
|
|
2016-07-13 04:07:02 -04:00
|
|
|
int end[2][RV_NZ];
|
2016-07-12 10:42:29 -04:00
|
|
|
|
|
|
|
float inputGain; /**< Input gain value */
|
|
|
|
float fbk; /**< Feedback gain */
|
|
|
|
float wet; /**< Output dry gain */
|
|
|
|
float dry; /**< Output wet gain */
|
|
|
|
} b_reverb;
|
|
|
|
|
|
|
|
static int
|
2016-07-13 04:07:02 -04:00
|
|
|
setReverbPointers (b_reverb *r, int i, int c, const double rate)
|
2016-07-12 10:42:29 -04:00
|
|
|
{
|
2016-07-13 04:07:02 -04:00
|
|
|
int e = (r->end[c][i] * rate / 44100.0);
|
2016-07-12 10:42:29 -04:00
|
|
|
e = e | 1;
|
2016-07-13 04:07:02 -04:00
|
|
|
r->delays[c][i] = (float*)realloc ((void*)r->delays[c][i], (e + 2) * sizeof (float));
|
|
|
|
if (!r->delays[c][i]) {
|
2016-07-12 10:42:29 -04:00
|
|
|
return -1;
|
|
|
|
} else {
|
2016-07-13 04:07:02 -04:00
|
|
|
memset (r->delays[c][i], 0 , (e + 2) * sizeof (float));
|
2016-07-12 10:42:29 -04:00
|
|
|
}
|
2016-07-13 04:07:02 -04:00
|
|
|
r->endp[c][i] = r->delays[c][i] + e + 1;
|
|
|
|
r->idx0[c][i] = r->idxp[c][i] = &(r->delays[c][i][0]);
|
2016-07-12 10:42:29 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
initReverb (b_reverb *r, const double rate)
|
|
|
|
{
|
|
|
|
int err = 0;
|
2016-07-13 04:07:02 -04:00
|
|
|
int stereowidth = 7;
|
|
|
|
|
|
|
|
r->inputGain = powf (10.0, .05 * -20.0); // -20dB
|
2016-07-12 10:42:29 -04:00
|
|
|
r->fbk = -0.015; /* Feedback gain */
|
2016-07-13 04:07:02 -04:00
|
|
|
r->wet = 0.3;
|
|
|
|
r->dry = 0.7;
|
2016-07-12 10:42:29 -04:00
|
|
|
|
|
|
|
/* feedback combfilter */
|
2016-07-13 04:07:02 -04:00
|
|
|
r->gain[0] = 0.75;
|
|
|
|
r->gain[1] = 0.75;
|
|
|
|
r->gain[2] = 0.75;
|
|
|
|
r->gain[3] = 0.75;
|
|
|
|
r->gain[4] = 0.75;
|
|
|
|
r->gain[5] = 0.75;
|
|
|
|
r->gain[6] = 0.75;
|
|
|
|
r->gain[7] = 0.75;
|
2016-07-12 10:42:29 -04:00
|
|
|
|
|
|
|
/* all-pass filter */
|
2016-07-13 04:07:02 -04:00
|
|
|
r->gain[8] = 0.5;
|
|
|
|
r->gain[9] = 0.5;
|
|
|
|
r->gain[10] = 0.5;
|
|
|
|
r->gain[11] = 0.5;
|
|
|
|
|
|
|
|
/* delay lines left */
|
|
|
|
r->end[0][0] = 1116;
|
|
|
|
r->end[0][1] = 1188;
|
|
|
|
r->end[0][2] = 1277;
|
|
|
|
r->end[0][3] = 1356;
|
|
|
|
r->end[0][4] = 1422;
|
|
|
|
r->end[0][5] = 1491;
|
|
|
|
r->end[0][6] = 1557;
|
|
|
|
r->end[0][7] = 1617;
|
|
|
|
|
|
|
|
/* all pass filters left */
|
|
|
|
r->end[0][8] = 556;
|
|
|
|
r->end[0][9] = 441;
|
|
|
|
r->end[0][10] = 341;
|
|
|
|
r->end[0][11] = 225;
|
|
|
|
|
|
|
|
/* delay lines right */
|
|
|
|
r->end[1][0] = 1116 + stereowidth;
|
|
|
|
r->end[1][1] = 1188 + stereowidth;
|
|
|
|
r->end[1][2] = 1277 + stereowidth;
|
|
|
|
r->end[1][3] = 1356 + stereowidth;
|
|
|
|
r->end[1][4] = 1422 + stereowidth;
|
|
|
|
r->end[1][5] = 1491 + stereowidth;
|
|
|
|
r->end[1][6] = 1557 + stereowidth;
|
|
|
|
r->end[1][7] = 1617 + stereowidth;
|
2016-07-12 10:42:29 -04:00
|
|
|
|
|
|
|
/* all pass filters */
|
2016-07-13 04:07:02 -04:00
|
|
|
r->end[1][8] = 556 + stereowidth;
|
|
|
|
r->end[1][9] = 441 + stereowidth;
|
|
|
|
r->end[1][10] = 341 + stereowidth;
|
|
|
|
r->end[1][11] = 225 + stereowidth;
|
2016-07-12 10:42:29 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < RV_NZ; ++i) {
|
2016-07-13 04:07:02 -04:00
|
|
|
r->delays[0][i] = NULL;
|
|
|
|
r->delays[1][i] = NULL;
|
2016-07-12 10:42:29 -04:00
|
|
|
}
|
|
|
|
|
2016-07-13 04:07:02 -04:00
|
|
|
r->yy1_0 = 0.0;
|
|
|
|
r->y_1_0 = 0.0;
|
|
|
|
r->yy1_1 = 0.0;
|
|
|
|
r->y_1_1 = 0.0;
|
2016-07-12 10:42:29 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < RV_NZ; i++) {
|
2016-07-13 04:07:02 -04:00
|
|
|
err |= setReverbPointers (r, i, 0, rate);
|
|
|
|
err |= setReverbPointers (r, i, 1, rate);
|
2016-07-12 10:42:29 -04:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
reverb (b_reverb* r,
|
2016-07-13 04:07:02 -04:00
|
|
|
const float* inbuf0,
|
|
|
|
const float* inbuf1,
|
|
|
|
float* outbuf0,
|
|
|
|
float* outbuf1,
|
2016-07-12 10:42:29 -04:00
|
|
|
size_t n_samples)
|
|
|
|
{
|
2016-07-13 04:07:02 -04:00
|
|
|
float** const idxp0 = r->idxp[0];
|
|
|
|
float** const idxp1 = r->idxp[1];
|
|
|
|
float* const* const endp0 = r->endp[0];
|
|
|
|
float* const* const endp1 = r->endp[1];
|
|
|
|
float* const* const idx00 = r->idx0[0];
|
|
|
|
float* const* const idx01 = r->idx0[1];
|
2016-07-12 10:42:29 -04:00
|
|
|
const float* const gain = r->gain;
|
|
|
|
const float inputGain = r->inputGain;
|
|
|
|
const float fbk = r->fbk;
|
|
|
|
const float wet = r->wet;
|
|
|
|
const float dry = r->dry;
|
|
|
|
|
2016-07-13 04:07:02 -04:00
|
|
|
const float* xp0 = inbuf0;
|
|
|
|
const float* xp1 = inbuf1;
|
|
|
|
float* yp0 = outbuf0;
|
|
|
|
float* yp1 = outbuf1;
|
2016-07-12 10:42:29 -04:00
|
|
|
|
2016-07-13 04:07:02 -04:00
|
|
|
float y_1_0 = r->y_1_0;
|
|
|
|
float yy1_0 = r->yy1_0;
|
|
|
|
float y_1_1 = r->y_1_1;
|
|
|
|
float yy1_1 = r->yy1_1;
|
2016-07-12 10:42:29 -04:00
|
|
|
|
|
|
|
for (size_t i = 0; i < n_samples; ++i) {
|
|
|
|
int j;
|
|
|
|
float y;
|
2016-07-13 04:07:02 -04:00
|
|
|
const float xo0 = *xp0++;
|
|
|
|
const float xo1 = *xp1++;
|
|
|
|
const float x0 = y_1_0 + (inputGain * xo0);
|
|
|
|
const float x1 = y_1_1 + (inputGain * xo1);
|
2016-07-12 10:42:29 -04:00
|
|
|
float xa = 0.0;
|
2016-07-13 04:07:02 -04:00
|
|
|
float xb = 0.0;
|
2016-07-12 10:42:29 -04:00
|
|
|
/* First we do four feedback comb filters (ie parallel delay lines,
|
|
|
|
* each with a single tap at the end that feeds back at the start) */
|
|
|
|
|
2016-07-13 04:07:02 -04:00
|
|
|
for (j = 0; j < 8; ++j) {
|
|
|
|
y = *idxp0[j];
|
|
|
|
*idxp0[j] = x0 + (gain[j] * y);
|
|
|
|
if (endp0[j] <= ++(idxp0[j])) {
|
|
|
|
idxp0[j] = idx00[j];
|
2016-07-12 10:42:29 -04:00
|
|
|
}
|
|
|
|
xa += y;
|
|
|
|
}
|
2016-07-13 04:07:02 -04:00
|
|
|
for (; j < 12; ++j) {
|
|
|
|
y = *idxp0[j];
|
|
|
|
*idxp0[j] = gain[j] * (xa + y);
|
|
|
|
if (endp0[j] <= ++(idxp0[j])) {
|
|
|
|
idxp0[j] = idx00[j];
|
2016-07-12 10:42:29 -04:00
|
|
|
}
|
|
|
|
xa = y - xa;
|
|
|
|
}
|
|
|
|
|
2016-07-13 04:07:02 -04:00
|
|
|
y = 0.5f * (xa + yy1_0);
|
|
|
|
yy1_0 = y;
|
|
|
|
y_1_0 = fbk * xa;
|
|
|
|
|
|
|
|
*yp0++ = ((wet * y) + (dry * xo0));
|
|
|
|
|
|
|
|
for (j = 0; j < 8; ++j) {
|
|
|
|
y = *idxp1[j];
|
|
|
|
*idxp1[j] = x1 + (gain[j] * y);
|
|
|
|
if (endp1[j] <= ++(idxp1[j])) {
|
|
|
|
idxp1[j] = idx01[j];
|
|
|
|
}
|
|
|
|
xb += y;
|
|
|
|
}
|
|
|
|
for (; j < 12; ++j) {
|
|
|
|
y = *idxp1[j];
|
|
|
|
*idxp1[j] = gain[j] * (xb + y);
|
|
|
|
if (endp1[j] <= ++(idxp1[j])) {
|
|
|
|
idxp1[j] = idx01[j];
|
|
|
|
}
|
|
|
|
xb = y - xb;
|
|
|
|
}
|
2016-07-12 10:42:29 -04:00
|
|
|
|
2016-07-13 04:07:02 -04:00
|
|
|
y = 0.5f * (xb + yy1_1);
|
|
|
|
yy1_1 = y;
|
|
|
|
y_1_1 = fbk * xb;
|
|
|
|
|
|
|
|
*yp1++ = ((wet * y) + (dry * xo1));
|
2016-07-12 10:42:29 -04:00
|
|
|
}
|
|
|
|
|
2016-07-13 04:07:02 -04:00
|
|
|
r->y_1_0 = y_1_0 + DENORMAL_PROTECT;
|
|
|
|
r->yy1_0 = yy1_0 + DENORMAL_PROTECT;
|
|
|
|
r->y_1_1 = y_1_1 + DENORMAL_PROTECT;
|
|
|
|
r->yy1_1 = yy1_1 + DENORMAL_PROTECT;
|
2016-07-12 10:42:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* LV2 wrapper
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lv2/lv2plug.in/ns/lv2core/lv2.h"
|
|
|
|
|
|
|
|
typedef enum {
|
2016-07-13 04:07:02 -04:00
|
|
|
AR_INPUT0 = 0,
|
|
|
|
AR_INPUT1 = 1,
|
|
|
|
AR_OUTPUT0 = 2,
|
|
|
|
AR_OUTPUT1 = 3,
|
|
|
|
AR_MIX = 4,
|
|
|
|
AR_ROOMSZ = 5,
|
2016-07-12 10:42:29 -04:00
|
|
|
} PortIndex;
|
|
|
|
|
|
|
|
typedef struct {
|
2016-07-13 04:07:02 -04:00
|
|
|
float* input0;
|
|
|
|
float* input1;
|
|
|
|
float* output0;
|
|
|
|
float* output1;
|
2016-07-12 10:42:29 -04:00
|
|
|
|
|
|
|
float* mix;
|
2016-07-13 04:07:02 -04:00
|
|
|
float* roomsz;
|
2016-07-12 10:42:29 -04:00
|
|
|
|
|
|
|
float v_mix;
|
2016-07-13 04:07:02 -04:00
|
|
|
float v_roomsz;
|
2016-07-12 10:42:29 -04:00
|
|
|
|
|
|
|
b_reverb r;
|
|
|
|
} AReverb;
|
|
|
|
|
|
|
|
static LV2_Handle
|
|
|
|
instantiate (const LV2_Descriptor* descriptor,
|
|
|
|
double rate,
|
|
|
|
const char* bundle_path,
|
|
|
|
const LV2_Feature* const* features)
|
|
|
|
{
|
|
|
|
AReverb* self = (AReverb*)calloc (1, sizeof (AReverb));
|
|
|
|
if (!self) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (initReverb (&self->r, rate)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// these are set in initReverb()
|
2016-07-13 04:07:02 -04:00
|
|
|
self->v_roomsz = 0.75;
|
2016-07-12 10:42:29 -04:00
|
|
|
self->v_mix = 0.1;
|
|
|
|
|
|
|
|
return (LV2_Handle)self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
connect_port (LV2_Handle instance,
|
|
|
|
uint32_t port,
|
|
|
|
void* data)
|
|
|
|
{
|
|
|
|
AReverb* self = (AReverb*)instance;
|
|
|
|
|
|
|
|
switch ((PortIndex)port) {
|
2016-07-13 04:07:02 -04:00
|
|
|
case AR_INPUT0:
|
|
|
|
self->input0 = (float*)data;
|
|
|
|
break;
|
|
|
|
case AR_INPUT1:
|
|
|
|
self->input1 = (float*)data;
|
|
|
|
break;
|
|
|
|
case AR_OUTPUT0:
|
|
|
|
self->output0 = (float*)data;
|
2016-07-12 10:42:29 -04:00
|
|
|
break;
|
2016-07-13 04:07:02 -04:00
|
|
|
case AR_OUTPUT1:
|
|
|
|
self->output1 = (float*)data;
|
2016-07-12 10:42:29 -04:00
|
|
|
break;
|
|
|
|
case AR_MIX:
|
|
|
|
self->mix = (float*)data;
|
|
|
|
break;
|
2016-07-13 04:07:02 -04:00
|
|
|
case AR_ROOMSZ:
|
|
|
|
self->roomsz = (float*)data;
|
2016-07-12 10:42:29 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
run (LV2_Handle instance, uint32_t n_samples)
|
|
|
|
{
|
|
|
|
AReverb* self = (AReverb*)instance;
|
|
|
|
|
2016-07-13 04:07:02 -04:00
|
|
|
const float* const input0 = self->input0;
|
|
|
|
const float* const input1 = self->input1;
|
|
|
|
float* const output0 = self->output0;
|
|
|
|
float* const output1 = self->output1;
|
2016-07-12 10:42:29 -04:00
|
|
|
|
|
|
|
// TODO interpolate
|
|
|
|
if (*self->mix != self->v_mix) {
|
|
|
|
self->v_mix = *self->mix;
|
|
|
|
const float u = self->r.wet + self->r.dry;
|
|
|
|
self->r.wet = self->v_mix * u;
|
|
|
|
self->r.dry = u - (self->v_mix * u);
|
|
|
|
}
|
2016-07-13 04:07:02 -04:00
|
|
|
if (*self->roomsz != self->v_roomsz) {
|
|
|
|
self->v_roomsz = *self->roomsz;
|
|
|
|
self->r.gain[0] = self->v_roomsz;
|
|
|
|
self->r.gain[1] = self->v_roomsz;
|
|
|
|
self->r.gain[2] = self->v_roomsz;
|
|
|
|
self->r.gain[3] = self->v_roomsz;
|
|
|
|
self->r.gain[4] = self->v_roomsz;
|
|
|
|
self->r.gain[5] = self->v_roomsz;
|
|
|
|
self->r.gain[6] = self->v_roomsz;
|
|
|
|
self->r.gain[7] = self->v_roomsz;
|
2016-07-12 10:42:29 -04:00
|
|
|
}
|
|
|
|
|
2016-07-13 04:07:02 -04:00
|
|
|
reverb (&self->r, input0, input1, output0, output1, n_samples);
|
2016-07-12 10:42:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
activate (LV2_Handle instance)
|
|
|
|
{
|
|
|
|
AReverb* self = (AReverb*)instance;
|
2016-07-13 04:07:02 -04:00
|
|
|
|
|
|
|
self->r.y_1_0 = 0;
|
|
|
|
self->r.yy1_0 = 0;
|
|
|
|
self->r.y_1_1 = 0;
|
|
|
|
self->r.yy1_1 = 0;
|
|
|
|
for (int i = 0; i < RV_NZ; ++i) {
|
|
|
|
self->r.delays[0][i] = NULL;
|
|
|
|
self->r.delays[1][i] = NULL;
|
|
|
|
}
|
2016-07-12 10:42:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
deactivate (LV2_Handle instance)
|
|
|
|
{
|
2016-07-13 04:07:02 -04:00
|
|
|
activate(instance);
|
2016-07-12 10:42:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup (LV2_Handle instance)
|
|
|
|
{
|
|
|
|
AReverb* self = (AReverb*)instance;
|
|
|
|
for (int i = 0; i < RV_NZ; ++i) {
|
2016-07-13 04:07:02 -04:00
|
|
|
free (self->r.delays[0][i]);
|
|
|
|
free (self->r.delays[1][i]);
|
2016-07-12 10:42:29 -04:00
|
|
|
}
|
|
|
|
free (instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const void*
|
|
|
|
extension_data (const char* uri)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const LV2_Descriptor descriptor = {
|
|
|
|
"urn:ardour:a-reverb",
|
|
|
|
instantiate,
|
|
|
|
connect_port,
|
|
|
|
activate,
|
|
|
|
run,
|
|
|
|
deactivate,
|
|
|
|
cleanup,
|
|
|
|
extension_data
|
|
|
|
};
|
|
|
|
|
|
|
|
LV2_SYMBOL_EXPORT
|
|
|
|
const LV2_Descriptor*
|
|
|
|
lv2_descriptor (uint32_t index)
|
|
|
|
{
|
|
|
|
switch (index) {
|
|
|
|
case 0:
|
|
|
|
return &descriptor;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|