summaryrefslogtreecommitdiff
path: root/libs/plugins/a-reverb.lv2/a-reverb.c
blob: c39be36f389324b9bba61743546d1fc641d03530 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * 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>
 * Copyright (C) 2016 Damien Zammit <damien@zamaudio.com>
 *
 * 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.
 *
 * 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef _GNU_SOURCE
#define _GNU_SOURCE // needed for M_PI
#endif

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

#define RV_NZ 7
#define DENORMAL_PROTECT (1e-14)

#ifdef COMPILER_MSVC
#include <float.h>
#define isfinite_local(val) (bool)_finite((double)val)
#else
#define isfinite_local isfinite
#endif

typedef struct {
	float* delays[2][RV_NZ]; /**< delay line buffer */
	size_t size[2][RV_NZ];

	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[]*/

	float gain[RV_NZ]; /**< feedback gains */
	float yy1_0; /**< Previous output sample */
	float y_1_0; /**< Feedback sample */
	float yy1_1; /**< Previous output sample */
	float y_1_1; /**< Feedback sample */

	int end[2][RV_NZ];

	float inputGain;	/**< Input gain value */
	float fbk;	/**< Feedback gain */
	float wet;	/**< Output dry gain */
	float dry;	/**< Output wet gain */
} b_reverb;

static int
setReverbPointers (b_reverb *r, int i, int c, const double rate)
{
	int e = (r->end[c][i] * rate / 25000.0);
	e = e | 1;
	r->size[c][i] = e + 2;
	r->delays[c][i] = (float*)realloc ((void*)r->delays[c][i], r->size[c][i] * sizeof (float));
	if (!r->delays[c][i]) {
		return -1;
	} else {
		memset (r->delays[c][i], 0 , r->size[c][i] * sizeof (float));
	}
	r->endp[c][i] = r->delays[c][i] + e + 1;
	r->idx0[c][i] = r->idxp[c][i] = &(r->delays[c][i][0]);

	return 0;
}

static int
initReverb (b_reverb *r, const double rate)
{
	int err = 0;
	int stereowidth = 7;

	r->inputGain = powf (10.0, .05 * -20.0);  // -20dB
	r->fbk = -0.015; /* Feedback gain */
	r->wet = 0.3;
	r->dry = 0.7;

	/* feedback combfilter */
	r->gain[0] = 0.773;
	r->gain[1] = 0.802;
	r->gain[2] = 0.753;
	r->gain[3] = 0.733;

	/* all-pass filter */
	r->gain[4] = sqrtf (0.5);
	r->gain[5] = sqrtf (0.5);
	r->gain[6] = sqrtf (0.5);

	/* delay lines left */
	r->end[0][0] = 1687;
	r->end[0][1] = 1601;
	r->end[0][2] = 2053;
	r->end[0][3] = 2251;

	/* all pass filters left */
	r->end[0][4] = 347;
	r->end[0][5] = 113;
	r->end[0][6] = 37;

	/* delay lines right */
	r->end[1][0] = 1687 + stereowidth;
	r->end[1][1] = 1601 + stereowidth;
	r->end[1][2] = 2053 + stereowidth;
	r->end[1][3] = 2251 + stereowidth;

	/* all pass filters right */
	r->end[1][4] = 347 + stereowidth;
	r->end[1][5] = 113 + stereowidth;
	r->end[1][6] = 37 + stereowidth;

	for (int i = 0; i < RV_NZ; ++i) {
		r->delays[0][i] = NULL;
		r->delays[1][i] = NULL;
	}

	r->yy1_0 = 0.0;
	r->y_1_0 = 0.0;
	r->yy1_1 = 0.0;
	r->y_1_1 = 0.0;

	for (int i = 0; i < RV_NZ; ++i) {
		err |= setReverbPointers (r, i, 0, rate);
		err |= setReverbPointers (r, i, 1, rate);
	}
	return err;
}

static void
reverb (b_reverb* r,
        const float* inbuf0,
        const float* inbuf1,
        float* outbuf0,
        float* outbuf1,
        size_t n_samples)
{
	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];
	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;

	const float* xp0 = inbuf0;
	const float* xp1 = inbuf1;
	float* yp0 = outbuf0;
	float* yp1 = outbuf1;

	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;

	for (size_t i = 0; i < n_samples; ++i) {
		int j;
		float y;
		float xo0 = *xp0++;
		float xo1 = *xp1++;
		if (!isfinite_local(xo0) || fabsf (xo0) > 10.f) { xo0 = 0; }
		if (!isfinite_local(xo1) || fabsf (xo1) > 10.f) { xo1 = 0; }
		xo0 += DENORMAL_PROTECT;
		xo1 += DENORMAL_PROTECT;
		const float x0 = y_1_0 + (inputGain * xo0);
		const float x1 = y_1_1 + (inputGain * xo1);

		float xa = 0.0;
		float xb = 0.0;
		/* 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) */

		for (j = 0; j < 4; ++j) {
			y = *idxp0[j];
			*idxp0[j] = x0 + (gain[j] * y);
			if (endp0[j] <= ++(idxp0[j])) {
				idxp0[j] = idx00[j];
			}
			xa += y;
		}
		for (; j < 7; ++j) {
			y = *idxp0[j];
			*idxp0[j] = gain[j] * (xa + y);
			if (endp0[j] <= ++(idxp0[j])) {
				idxp0[j] = idx00[j];
			}
			xa = y - xa;
		}

		y = 0.5f * (xa + yy1_0);
		yy1_0 = y;
		y_1_0 = fbk * xa;

		*yp0++ = ((wet * y) + (dry * xo0));

		for (j = 0; j < 4; ++j) {
			y = *idxp1[j];
			*idxp1[j] = x1 + (gain[j] * y);
			if (endp1[j] <= ++(idxp1[j])) {
				idxp1[j] = idx01[j];
			}
			xb += y;
		}
		for (; j < 7; ++j) {
			y = *idxp1[j];
			*idxp1[j] = gain[j] * (xb + y);
			if (endp1[j] <= ++(idxp1[j])) {
				idxp1[j] = idx01[j];
			}
			xb = y - xb;
		}

		y = 0.5f * (xb + yy1_1);
		yy1_1 = y;
		y_1_1 = fbk * xb;

		*yp1++ = ((wet * y) + (dry * xo1));
	}

	if (!isfinite_local(y_1_0)) { y_1_0 = 0; }
	if (!isfinite_local(yy1_1)) { yy1_0 = 0; }
	if (!isfinite_local(y_1_1)) { y_1_1 = 0; }
	if (!isfinite_local(yy1_1)) { yy1_1 = 0; }

	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;
}

/******************************************************************************
 * LV2 wrapper
 */

#include "lv2/lv2plug.in/ns/lv2core/lv2.h"

typedef enum {
	AR_INPUT0     = 0,
	AR_INPUT1     = 1,
	AR_OUTPUT0    = 2,
	AR_OUTPUT1    = 3,
	AR_MIX        = 4,
	AR_ROOMSZ     = 5,
	AR_ENABLE     = 6,
} PortIndex;

typedef struct {
	float* input0;
	float* input1;
	float* output0;
	float* output1;

	float* mix;
	float* roomsz;
	float* enable;

	float v_mix;
	float v_roomsz;
	float srate;
	float tau;

	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()
	self->v_roomsz = 0.75;
	self->v_mix = 0.1;
	self->srate = rate;
	self->tau = 1.f - expf (-2.f * M_PI * 64.f * 15.f / self->srate); // 15Hz, 64fpp

	return (LV2_Handle)self;
}

static void
connect_port (LV2_Handle instance,
              uint32_t   port,
              void*      data)
{
	AReverb* self = (AReverb*)instance;

	switch ((PortIndex)port) {
		case AR_INPUT0:
			self->input0 = (float*)data;
			break;
		case AR_INPUT1:
			self->input1 = (float*)data;
			break;
		case AR_OUTPUT0:
			self->output0 = (float*)data;
			break;
		case AR_OUTPUT1:
			self->output1 = (float*)data;
			break;
		case AR_MIX:
			self->mix = (float*)data;
			break;
		case AR_ROOMSZ:
			self->roomsz = (float*)data;
			break;
		case AR_ENABLE:
			self->enable = (float*)data;
			break;
	}
}

static void
activate (LV2_Handle instance)
{
	AReverb* self = (AReverb*)instance;

	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) {
		for (int c = 0; c < 2; ++c) {
			memset (self->r.delays[c][i], 0, self->r.size[c][i] * sizeof (float));
		}
	}
}

static void
deactivate (LV2_Handle instance)
{
	activate(instance);
}

static void
run (LV2_Handle instance, uint32_t n_samples)
{
	AReverb* self = (AReverb*)instance;

	const float* const input0 = self->input0;
	const float* const input1 = self->input1;
	float* const      output0 = self->output0;
	float* const      output1 = self->output1;

	const float tau = self->tau;
	const float mix = *self->enable <= 0 ? 0 : *self->mix;

	uint32_t remain = n_samples;
	uint32_t offset = 0;
	uint32_t iterpolate = 0;

	if (fabsf (mix - self->v_mix) < .01) { // 40dB
		if (self->v_mix != mix && *self->enable <= 0) {
			// entering bypass, reset reverb
			activate (self);
		}
		self->v_mix = mix;
		self->r.wet = self->v_mix;
		self->r.dry = 1.0 - self->v_mix;
	} else {
		iterpolate |= 1;
	}

	if (fabsf (*self->roomsz  - self->v_roomsz) < .01) {
		self->v_roomsz = *self->roomsz;
	} else {
		iterpolate |= 2;
	}

	while (remain > 0) {
		uint32_t p_samples = remain;
		if (iterpolate && p_samples > 64) {
			p_samples = 64;
		}

		if (iterpolate & 1) {
			self->v_mix += tau * (mix - self->v_mix);
			self->r.wet = self->v_mix;
			self->r.dry = 1.0 - self->v_mix;
		}
		if (iterpolate & 2) {
			self->v_roomsz += tau * ( *self->roomsz - self->v_roomsz);
			self->r.gain[0] = 0.773 * self->v_roomsz;
			self->r.gain[1] = 0.802 * self->v_roomsz;
			self->r.gain[2] = 0.753 * self->v_roomsz;
			self->r.gain[3] = 0.733 * self->v_roomsz;
		}

		reverb (&self->r,
				&input0[offset], &input1[offset],
				&output0[offset], &output1[offset],
				p_samples);

		offset += p_samples;
		remain -= p_samples;
	}
}


static void
cleanup (LV2_Handle instance)
{
	AReverb* self = (AReverb*)instance;
	for (int i = 0; i < RV_NZ; ++i) {
		free (self->r.delays[0][i]);
		free (self->r.delays[1][i]);
	}
	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;
	}
}