Audacity 3.2.0
Reverb_libSoX.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 Reverb_libSoX.h
6 Stereo reverberation effect from libSoX,
7 adapted for Audacity
8
9 Copyright (c) 2007-2013 [email protected]
10 Licence: LGPL v2.1
11 Filter configuration based on freeverb by Jezar Wakefield.
12
13**********************************************************************/
14
15#include <cstring>
16#include <cstdlib>
17#ifdef __WXMSW__
18 #define M_LN10 2.30258509299404568402 /* log_e 10 */
19#else
20 #include <cmath>
21#endif
22#include <algorithm>
23using std::min;
24using std::max;
25
26#define array_length(a) (sizeof(a)/sizeof(a[0]))
27#define dB_to_linear(x) exp((x) * M_LN10 * 0.05)
28#define midi_to_freq(n) (440 * pow(2,((n)-69)/12.))
29#define FIFO_SIZE_T size_t
30#define FIFO_MIN 0x4000
31#define fifo_read_ptr(f) fifo_read(f, (FIFO_SIZE_T)0, NULL)
32#define lsx_zalloc(var, n) var = (float *)calloc(n, sizeof(*var))
33#define filter_advance(p) if (--(p)->ptr < (p)->buffer) (p)->ptr += (p)->size
34#define filter_delete(p) free((p)->buffer)
35
36typedef struct {
37 char * data;
38 size_t allocation; /* Number of bytes allocated for data. */
39 size_t item_size; /* Size of each item in data */
40 size_t begin; /* Offset of the first byte to read. */
41 size_t end; /* 1 + Offset of the last byte to read. */
42} fifo_t;
43
44static void fifo_clear(fifo_t * f)
45{
46 f->end = f->begin = 0;
47}
48
50{
51 f->end = f->begin = 0;
52 memset(f->data, 0, f->allocation);
53}
54
55
56static void * fifo_reserve(fifo_t * f, FIFO_SIZE_T n)
57{
58 n *= f->item_size;
59
60 if (f->begin == f->end)
61 fifo_clear(f);
62
63 while (1) {
64 if (f->end + n <= f->allocation) {
65 void *p = f->data + f->end;
66
67 f->end += n;
68 return p;
69 }
70 if (f->begin > FIFO_MIN) {
71 memmove(f->data, f->data + f->begin, f->end - f->begin);
72 f->end -= f->begin;
73 f->begin = 0;
74 continue;
75 }
76 f->allocation += n;
77 f->data = (char *)realloc(f->data, f->allocation);
78 }
79}
80
81static void * fifo_write(fifo_t * f, FIFO_SIZE_T n, void const * data)
82{
83 void * s = fifo_reserve(f, n);
84 if (data)
85 memcpy(s, data, n * f->item_size);
86 return s;
87}
88
89static void * fifo_read(fifo_t * f, FIFO_SIZE_T n, void * data)
90{
91 char * ret = f->data + f->begin;
92 n *= f->item_size;
93 if (n > (FIFO_SIZE_T)(f->end - f->begin))
94 return NULL;
95 if (data)
96 memcpy(data, ret, (size_t)n);
97 f->begin += n;
98 return ret;
99}
100
101static void fifo_delete(fifo_t * f)
102{
103 free(f->data);
104}
105
106static void fifo_create(fifo_t * f, FIFO_SIZE_T item_size)
107{
108 f->item_size = item_size;
109 f->allocation = FIFO_MIN;
110 f->data = (char *)malloc(f->allocation);
111 fifo_clear(f);
112}
113
114typedef struct {
115 size_t size;
116 float * buffer, * ptr;
117 float store;
118} filter_t;
119
120static float comb_process(filter_t * p, /* gcc -O2 will inline this */
121 float const * input, float const * feedback, float const * hf_damping)
122{
123 float output = *p->ptr;
124 p->store = output + (p->store - output) * *hf_damping;
125 *p->ptr = *input + p->store * *feedback;
127 return output;
128}
129
130static float allpass_process(filter_t * p, /* gcc -O2 will inline this */
131 float const * input)
132{
133 float output = *p->ptr;
134 *p->ptr = *input + output * .5;
136 return output - *input;
137}
138
139typedef struct {double b0, b1, a1, i1, o1;} one_pole_t;
140
141static float one_pole_process(one_pole_t * p, float i0)
142{
143 float o0 = i0*p->b0 + p->i1*p->b1 - p->o1*p->a1;
144 p->i1 = i0;
145 return p->o1 = o0;
146}
147
148static const size_t /* Filter delay lengths in samples (44100Hz sample-rate) */
149 comb_lengths[] = {1116, 1188, 1277, 1356, 1422, 1491, 1557, 1617},
150 allpass_lengths[] = {225, 341, 441, 556}, stereo_adjust = 12;
151
152typedef struct {
155 one_pole_t one_pole[2];
157
158static void one_pole_init(filter_array_t* p, double rate,
159 double fc_highpass, double fc_lowpass)
160{
161 { /* EQ: highpass */
162 one_pole_t* q = &p->one_pole[0];
163 q->a1 = -exp(-2 * M_PI * fc_highpass / rate);
164 q->b0 = (1 - q->a1) / 2, q->b1 = -q->b0;
165 }
166 { /* EQ: lowpass */
167 one_pole_t* q = &p->one_pole[1];
168 q->a1 = -exp(-2 * M_PI * fc_lowpass / rate);
169 q->b0 = 1 + q->a1, q->b1 = 0;
170 }
171}
172
173
174static void filter_array_allocate(filter_array_t* p, double rate,
175 double scale, double offset)
176{
177 size_t i;
178 double r = rate * (1 / 44100.); /* Compensate for actual sample-rate */
179
180 for (i = 0; i < array_length(comb_lengths); ++i)
181 {
182 filter_t* pcomb = &p->comb[i];
183 pcomb->size = (size_t)(scale * r * (comb_lengths[i] + stereo_adjust * offset) + .5);
184 pcomb->ptr = lsx_zalloc(pcomb->buffer, pcomb->size);
185 }
186 for (i = 0; i < array_length(allpass_lengths); ++i)
187 {
188 filter_t* pallpass = &p->allpass[i];
189 pallpass->size = (size_t)(r * (allpass_lengths[i] + stereo_adjust * offset) + .5);
190 pallpass->ptr = lsx_zalloc(pallpass->buffer, pallpass->size);
191 }
192}
193
194static void filter_t_resize(filter_t* p, size_t newSize);
195
196static void filter_array_init(filter_array_t* p, double rate,
197 double scale, double offset)
198{
199 size_t i;
200 double r = rate * (1 / 44100.); /* Compensate for actual sample-rate */
201
202 for (i = 0; i < array_length(comb_lengths); ++i, offset = -offset)
203 {
204 filter_t* pcomb = &p->comb[i];
205 size_t newSize = (size_t)(scale * r * (comb_lengths[i] + stereo_adjust * offset) + .5);
206 filter_t_resize(pcomb, newSize);
207 }
208 for (i = 0; i < array_length(allpass_lengths); ++i, offset = -offset)
209 {
210 filter_t* pallpass = &p->allpass[i];
211 size_t newSize = (size_t)(r * (allpass_lengths[i] + stereo_adjust * offset) + .5);
212 filter_t_resize(pallpass, newSize);
213 }
214}
215
216
217
218static void filter_array_create(filter_array_t * p, double rate,
219 double scale, double offset)
220{
221 size_t i;
222 double r = rate * (1 / 44100.); /* Compensate for actual sample-rate */
223
224 for (i = 0; i < array_length(comb_lengths); ++i, offset = -offset)
225 {
226 filter_t * pcomb = &p->comb[i];
227 pcomb->size = (size_t)(scale * r * (comb_lengths[i] + stereo_adjust * offset) + .5);
228 pcomb->ptr = lsx_zalloc(pcomb->buffer, pcomb->size);
229 }
230 for (i = 0; i < array_length(allpass_lengths); ++i, offset = -offset)
231 {
232 filter_t * pallpass = &p->allpass[i];
233 pallpass->size = (size_t)(r * (allpass_lengths[i] + stereo_adjust * offset) + .5);
234 pallpass->ptr = lsx_zalloc(pallpass->buffer, pallpass->size);
235 }
236}
237
239 size_t length, float const * input, float * output,
240 float const * feedback, float const * hf_damping, float const * gain)
241{
242 while (length--) {
243 float out = 0, in = *input++;
244
245 size_t i = array_length(comb_lengths) - 1;
246 do out += comb_process(p->comb + i, &in, feedback, hf_damping);
247 while (i--);
248
250 do out = allpass_process(p->allpass + i, &out);
251 while (i--);
252
253 out = one_pole_process(&p->one_pole[0], out);
254 out = one_pole_process(&p->one_pole[1], out);
255 *output++ = out * *gain;
256 }
257}
258
260{
261 size_t i;
262
263 for (i = 0; i < array_length(allpass_lengths); ++i)
264 filter_delete(&p->allpass[i]);
265 for (i = 0; i < array_length(comb_lengths); ++i)
266 filter_delete(&p->comb[i]);
267}
268
269typedef struct {
270 float feedback;
272 float gain;
275 float * out[2];
277} reverb_t;
278
279
280// Resizes a filter, resetting its contents
281static void filter_t_resize_resetting(filter_t* p, size_t newSize)
282{
283 memset(p->buffer, 0, newSize * sizeof(float));
284 p->ptr = p->buffer;
285 p->store = 0;
286 p->size = newSize;
287}
288
289
290// Resizes a filter, trying to keep as much of its history as possible.
291static void filter_t_resize_preserving(filter_t* p, size_t newSize)
292{
293 // Imagine we have this filter_t as input:
294 //
295 // ptr goes from right to left and when falling off the left side, it wraps back to the right;
296 // <--ptr-- it reads the most distant sample in the past, and after reading it will write a new sample.
297 // | Imagine someone just recorded "A B C D E", and now we are going to read "A", but a resize request comes.
298 // v
299 // | C B A E D |
300 //
301
302 // Depending on the new requested size and where ptr is, we can have these cases:
303 //
304 // v
305 // | C B A 0 E D | bigger size: right-shift what is right of ptr,
306 // and fill the resulting gap with zeros.
307 //
308 // (with variant: ptr is already at the old right edge - then shift nothing, just append zeros)
309 // v
310 // | C B A D | smaller size, and ptr is within it (but not at the edge): left-shift what is to the right of ptr,
311 // discarding samples.
312 //
313 // V
314 // | C B A | smaller size, and ptr would be at the new right edge; do nothing.
315 //
316 // v
317 // | B A | smaller size, and ptr would be beyond the new right edge: left-shift what is ahead of ptr,
318 // AND move ptr to size-1
319
320 assert(newSize > 0);
321
322 const int sizeDiff = ((ssize_t)newSize - (ssize_t)p->size);
323 const size_t ptrPos = (p->ptr - p->buffer);
324
325 const size_t numSamplesBehindPtr = (p->size - 1) - ptrPos;
326
327 if (sizeDiff > 0)
328 {
329 // case: bigger size
330
331 if (numSamplesBehindPtr > 0)
332 {
333 // right-shift what is right of ptr
334 memcpy(p->ptr + 1 + sizeDiff, p->ptr + 1, numSamplesBehindPtr * sizeof(float));
335 }
336
337 // fill the created gap with zeros
338 memset(p->ptr + 1, 0, sizeDiff * sizeof(float));
339 }
340 else if (sizeDiff < 0)
341 {
342 // case: smaller size
343
344 if (ptrPos < newSize-1)
345 {
346 size_t lenOfBlockToShift = newSize - 1 - ptrPos;
347 float* ptrToBlockToShift = p->buffer + p->size - lenOfBlockToShift;
348 memcpy(p->ptr + 1, ptrToBlockToShift, lenOfBlockToShift * sizeof(float));
349 }
350 else if (ptrPos == newSize - 1)
351 {
352 // sub-case: ptr is at the new edge - no shifting to do, and ptr can stay where it is
353 }
354 else
355 {
356 // sub-case: ptr would be beyond the new edge
357 // left-shift what is ahead of ptr and make ptr point to the new edge
358 memcpy(p->buffer, p->ptr - newSize + 1, newSize * sizeof(float));
359 p->ptr = p->buffer + newSize - 1;
360 }
361 }
362
363 p->size = newSize;
364}
365
366
367static void filter_t_resize(filter_t* p, size_t newSize)
368{
369 // Choose your resize method.
370 //
371 // When moving the Room Size slider:
372 //
373 // - using resize_resetting, you will not hear previous reverberations,
374 // but at the same time you will not hear sound artifacts
375 //
376 // - using resize_preserving, you will hear previous reverberations,
377 // but sometimes you might hear sound artifacts
378
379#if 1
380 filter_t_resize_resetting(p, newSize);
381#else
382 filter_t_resize_preserving(p, newSize);
383#endif
384
385}
386
387
388static void reverb_allocate(reverb_t* p, double sample_rate_Hz, size_t buffer_size, float** out)
389{
390 memset(p, 0, sizeof(*p));
391
392 // Input queue
393 fifo_create(&p->input_fifo, sizeof(float));
394
395 // Outputs
396 out[0] = lsx_zalloc(p->out[0], buffer_size);
397 out[1] = lsx_zalloc(p->out[1], buffer_size);
398
399 // Allpass & Comb filters
400 // pass the numbers that will cause the maximum possible allocation of the
401 // buffer just once, even if it is in excess of what is required by the
402 // settings passed in initialization after the allocation
403 filter_array_allocate(p->chan + 0, sample_rate_Hz, 1.0, 0.0);
404 filter_array_allocate(p->chan + 1, sample_rate_Hz, 1.0, 1.0);
405}
406
407// Some of the params can be set without having to re-init the input fifo
408// or the comb/allpass filters.
410(
411 reverb_t* p,
412 double sample_rate_Hz,
413 double wet_gain_dB,
414 double reverberance,
415 double hf_damping,
416 double tone_low, /* % */
417 double tone_high /* % */
418)
419{
420 // Feedback, Damping, Gain
421 double a = -1 / log(1 - .3 ); /* Set minimum feedback */
422 double b = 100 / (log(1 - .98) * a + 1); /* Set maximum feedback */
423
424 p->feedback = 1 - exp((reverberance - b) / (a * b));
425 p->hf_damping = hf_damping / 100 * .3 + .2;
426 p->gain = dB_to_linear(wet_gain_dB) * .015;
427
428
429 // LP-HP Filters
430 double fc_highpass = midi_to_freq(72 - tone_low / 100 * 48);
431 double fc_lowpass = midi_to_freq(72 + tone_high / 100 * 48);
432 one_pole_init(&p->chan[0], sample_rate_Hz, fc_highpass, fc_lowpass);
433 one_pole_init(&p->chan[1], sample_rate_Hz, fc_highpass, fc_lowpass);
434}
435
436
437static void reverb_init
438(
439 reverb_t* p,
440 double sample_rate_Hz,
441 double wet_gain_dB,
442 double room_scale, /* % */
443 double reverberance,
444 double hf_damping,
445 double pre_delay_ms,
446 double stereo_depth,
447 double tone_low, /* % */
448 double tone_high /* % */
449)
450{
451 // Input queue
453 size_t delay = pre_delay_ms / 1000 * sample_rate_Hz + .5;
454 memset(fifo_write(&p->input_fifo, delay, 0), 0, delay * sizeof(float));
455
456 reverb_set_simple_params(p, sample_rate_Hz, wet_gain_dB, reverberance, hf_damping, tone_low, tone_high);
457
458 // Allpass & Comb filters
459 double scale = room_scale / 100 * .9 + .1;
460 double depth = stereo_depth / 100;
461 for (size_t i = 0; i <= ceil(depth); ++i)
462 {
463 filter_array_init(p->chan + i, sample_rate_Hz, scale, i * depth);
464 }
465
466 // Remember if stereo depth was set to 0, so we do not have to process twice
467 p->initializedWithZeroDepth = (stereo_depth == 0.0);
468
469}
470
471
472static void reverb_create(reverb_t * p, double sample_rate_Hz,
473 double wet_gain_dB,
474 double room_scale, /* % */
475 double reverberance, /* % */
476 double hf_damping, /* % */
477 double pre_delay_ms,
478 double stereo_depth,
479 double tone_low, /* % */
480 double tone_high, /* % */
481 size_t buffer_size,
482 float * * out)
483{
484 reverb_allocate(p, sample_rate_Hz, buffer_size, out);
485
486 reverb_init(p, sample_rate_Hz, wet_gain_dB, room_scale, reverberance, hf_damping, pre_delay_ms, stereo_depth, tone_low, tone_high);
487}
488
489static void reverb_process(reverb_t * p, size_t length)
490{
491 filter_array_process(p->chan, length, (float *) fifo_read_ptr(&p->input_fifo), p->out[0], &p->feedback, &p->hf_damping, &p->gain);
492
494 filter_array_process(p->chan + 1, length, (float*)fifo_read_ptr(&p->input_fifo), p->out[1], &p->feedback, &p->hf_damping, &p->gain);
495
496 fifo_read(&p->input_fifo, length, NULL);
497}
498
499static void reverb_delete(reverb_t * p)
500{
501 size_t i;
502 for (i = 0; i < 2 && p->out[i]; ++i) {
503 free(p->out[i]);
505 }
507}
508
509static void reverb_clear(reverb_t* p)
510{
511 for (size_t c = 0; c < 2; c++)
512 {
513 auto& chn = p->chan[c];
514
515 chn.one_pole[0].i1 = 0.0;
516 chn.one_pole[0].o1 = 0.0;
517
518 chn.one_pole[1].i1 = 0.0;
519 chn.one_pole[1].o1 = 0.0;
520
521 for (size_t combIndex = 0; combIndex < array_length(comb_lengths); combIndex++)
522 {
523 auto& comb = chn.comb[combIndex];
524
525 memset(comb.buffer, 0, comb.size * sizeof(float));
526
527 comb.store = 0.0f;
528 }
529
530 for (size_t allpassIndex = 0; allpassIndex < array_length(allpass_lengths); allpassIndex++)
531 {
532 auto& allpass = chn.allpass[allpassIndex];
533
534 memset(allpass.buffer, 0, allpass.size * sizeof(float));
535
536 allpass.store = 0.0f;
537 }
538 } // loop on channels
539
540 fifo_clear( &(p->input_fifo) );
541}
542
int min(int a, int b)
#define M_PI
Definition: Distortion.cpp:30
static void * fifo_read(fifo_t *f, FIFO_SIZE_T n, void *data)
Definition: Reverb_libSoX.h:89
#define fifo_read_ptr(f)
Definition: Reverb_libSoX.h:31
static float one_pole_process(one_pole_t *p, float i0)
#define midi_to_freq(n)
Definition: Reverb_libSoX.h:28
static void reverb_init(reverb_t *p, double sample_rate_Hz, double wet_gain_dB, double room_scale, double reverberance, double hf_damping, double pre_delay_ms, double stereo_depth, double tone_low, double tone_high)
static void * fifo_reserve(fifo_t *f, FIFO_SIZE_T n)
Definition: Reverb_libSoX.h:56
static const size_t comb_lengths[]
static void fifo_delete(fifo_t *f)
static void fifo_create(fifo_t *f, FIFO_SIZE_T item_size)
static const size_t stereo_adjust
#define dB_to_linear(x)
Definition: Reverb_libSoX.h:27
#define FIFO_MIN
Definition: Reverb_libSoX.h:30
static float allpass_process(filter_t *p, float const *input)
static void filter_t_resize_preserving(filter_t *p, size_t newSize)
static void filter_array_create(filter_array_t *p, double rate, double scale, double offset)
static void filter_array_delete(filter_array_t *p)
static void reverb_set_simple_params(reverb_t *p, double sample_rate_Hz, double wet_gain_dB, double reverberance, double hf_damping, double tone_low, double tone_high)
static void filter_array_process(filter_array_t *p, size_t length, float const *input, float *output, float const *feedback, float const *hf_damping, float const *gain)
static void reverb_allocate(reverb_t *p, double sample_rate_Hz, size_t buffer_size, float **out)
#define array_length(a)
Definition: Reverb_libSoX.h:26
static void reverb_process(reverb_t *p, size_t length)
static float comb_process(filter_t *p, float const *input, float const *feedback, float const *hf_damping)
static void one_pole_init(filter_array_t *p, double rate, double fc_highpass, double fc_lowpass)
static void reverb_delete(reverb_t *p)
#define filter_delete(p)
Definition: Reverb_libSoX.h:34
static void filter_t_resize(filter_t *p, size_t newSize)
static void reverb_clear(reverb_t *p)
static void reverb_create(reverb_t *p, double sample_rate_Hz, double wet_gain_dB, double room_scale, double reverberance, double hf_damping, double pre_delay_ms, double stereo_depth, double tone_low, double tone_high, size_t buffer_size, float **out)
static const size_t allpass_lengths[]
#define filter_advance(p)
Definition: Reverb_libSoX.h:33
static void fifo_clear_and_zero(fifo_t *f)
Definition: Reverb_libSoX.h:49
static void fifo_clear(fifo_t *f)
Definition: Reverb_libSoX.h:44
#define FIFO_SIZE_T
Definition: Reverb_libSoX.h:29
static void filter_array_allocate(filter_array_t *p, double rate, double scale, double offset)
static void filter_array_init(filter_array_t *p, double rate, double scale, double offset)
static void * fifo_write(fifo_t *f, FIFO_SIZE_T n, void const *data)
Definition: Reverb_libSoX.h:81
static void filter_t_resize_resetting(filter_t *p, size_t newSize)
#define lsx_zalloc(var, n)
Definition: Reverb_libSoX.h:32
void free(void *ptr)
Definition: VectorOps.h:34
size_t begin
Definition: Reverb_libSoX.h:40
size_t item_size
Definition: Reverb_libSoX.h:39
size_t end
Definition: Reverb_libSoX.h:41
char * data
Definition: Reverb_libSoX.h:37
size_t allocation
Definition: Reverb_libSoX.h:38
filter_t comb[array_length(comb_lengths)]
one_pole_t one_pole[2]
filter_t allpass[array_length(allpass_lengths)]
size_t size
float store
float * buffer
float * ptr
fifo_t input_fifo
float * out[2]
float feedback
float hf_damping
float gain
bool initializedWithZeroDepth
filter_array_t chan[2]