-
Notifications
You must be signed in to change notification settings - Fork 408
/
effect_reverb.cpp
262 lines (215 loc) · 6.86 KB
/
effect_reverb.cpp
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
/*
* Copyright (c) 2016 Joao Rossi Filho
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// https://github.com/joaoRossiFilho/teensy_reverb
#include <Arduino.h>
#include "effect_reverb.h"
#include "utility/dspinst.h"
#include "math_helper.h"
void
AudioEffectReverb::_do_comb_apf(struct comb_apf *apf, int32_t *in_buf, int32_t *out_buf)
{
int32_t acc_x, acc_y, g;
int32_t w, z;
uint32_t n, buf_len;
g = apf->g;
buf_len = apf->buf_len;
for (n = 0; n < AUDIO_BLOCK_SAMPLES; n++) {
acc_y = apf->buffer[apf->rd_idx%buf_len];
acc_x = in_buf[n];
w = multiply_32x32_rshift32_rounded(g, acc_y);
acc_x += (w << 1);
z = multiply_32x32_rshift32_rounded(g, acc_x);
acc_y -= (z << 1);
apf->buffer[apf->wr_idx%buf_len] = acc_x;
out_buf[n] = acc_y;
apf->rd_idx++;
apf->wr_idx++;
}
}
void
AudioEffectReverb::_do_comb_lpf(struct comb_lpf *lpf, int32_t *in_buf, int32_t *out_buf)
{
int32_t x, y, w, z, g1, g2, z1;
uint32_t n, buf_len;
g1 = lpf->g1;
g2 = lpf->g2;
z1 = lpf->z1;
buf_len = lpf->buf_len;
for (n = 0; n < AUDIO_BLOCK_SAMPLES; n++) {
y = lpf->buffer[lpf->rd_idx%buf_len];
x = in_buf[n];
w = multiply_accumulate_32x32_rshift32_rounded(y, g2, z1);
z = multiply_accumulate_32x32_rshift32_rounded(x, g1, w);
z1 = w;
lpf->buffer[lpf->wr_idx%buf_len] = z;
out_buf[n] = y;
lpf->rd_idx++;
lpf->wr_idx++;
}
lpf->z1 = z1;
}
void
AudioEffectReverb::init_comb_filters(void)
{
int i;
g_flt_apf[0] = 0.7;
g_flt_apf[1] = -0.54;
g_flt_apf[2] = 0.6;
g2_flt_lpf = 0.985;
arm_float_to_q31(g_flt_apf, g_q31_apf, 3);
arm_float_to_q31(&g2_flt_lpf, &g2_q31_lpf, 1);
apf[0].buffer = apf1_buf;
apf[0].buf_len = APF1_BUF_LEN;
apf[0].delay = APF1_DLY_LEN;
apf[1].buffer = apf2_buf;
apf[1].buf_len = APF2_BUF_LEN;
apf[1].delay = APF2_DLY_LEN;
apf[2].buffer = apf3_buf;
apf[2].buf_len = APF3_BUF_LEN;
apf[2].delay = APF3_DLY_LEN;
for (i = 0; i < 3; i++) {
apf[i].g = g_q31_apf[i];
apf[i].wr_idx = 0;
apf[i].rd_idx = apf[i].wr_idx - apf[i].delay -1;
}
lpf[0].buffer = lpf1_buf;
lpf[0].buf_len = LPF1_BUF_LEN;
lpf[0].delay = LPF1_DLY_LEN;
lpf[1].buffer = lpf2_buf;
lpf[1].buf_len = LPF2_BUF_LEN;
lpf[1].delay = LPF2_DLY_LEN;
lpf[2].buffer = lpf3_buf;
lpf[2].buf_len = LPF3_BUF_LEN;
lpf[2].delay = LPF3_DLY_LEN;
lpf[3].buffer = lpf4_buf;
lpf[3].buf_len = LPF4_BUF_LEN;
lpf[3].delay = LPF4_DLY_LEN;
for (i = 0; i < 4; i++) {
lpf[i].g1 = g1_q31_lpf[i];
lpf[i].g2 = g2_q31_lpf;
lpf[i].wr_idx = 0;
lpf[i].rd_idx = lpf[i].wr_idx - lpf[i].delay -1;
}
}
void
AudioEffectReverb::clear_buffers(void)
{
memset(apf1_buf, 0, sizeof(apf1_buf));
memset(apf2_buf, 0, sizeof(apf2_buf));
memset(apf3_buf, 0, sizeof(apf3_buf));
memset(lpf1_buf, 0, sizeof(lpf1_buf));
memset(lpf2_buf, 0, sizeof(lpf2_buf));
memset(lpf3_buf, 0, sizeof(lpf3_buf));
memset(lpf4_buf, 0, sizeof(lpf4_buf));
}
void
AudioEffectReverb::reverbTime(float rt)
{
if (rt <= 0.0)
return;
reverb_time_sec = rt;
g1_flt_lpf[0] = powf(10.0, -(3.0*LPF1_DLY_SEC)/(reverb_time_sec));
g1_flt_lpf[1] = powf(10.0, -(3.0*LPF2_DLY_SEC)/(reverb_time_sec));
g1_flt_lpf[2] = powf(10.0, -(3.0*LPF3_DLY_SEC)/(reverb_time_sec));
g1_flt_lpf[3] = powf(10.0, -(3.0*LPF4_DLY_SEC)/(reverb_time_sec));
arm_float_to_q31(g1_flt_lpf, g1_q31_lpf, 4);
for (int i = 0; i < 4; i++)
lpf[i].g1 = g1_q31_lpf[i];
}
/**
* @brief Provide guard bits for Input buffer
* @param q31_t* Pointer to input buffer
* @param uint32_t blockSize
* @param uint32_t guard_bits
* @return none
* The function Provides the guard bits for the buffer
* to avoid overflow
*/
void provide_guard_bits_q31 (q31_t * input_buf,
uint32_t blockSize,
uint32_t guard_bits)
{
uint32_t i;
for (i = 0; i < blockSize; i++)
{
input_buf[i] = input_buf[i] >> guard_bits;
}
}
/**
* @brief Remove guard bits and saturate for Input buffer
* @param q31_t* Pointer to input buffer
* @param uint32_t blockSize
* @param uint32_t guard_bits
* @return none
* The function removes the guard bits for the buffer
* and prevents overflow
*/
void remove_guard_bits_q31 (q31_t * input_buf,
uint32_t blockSize,
uint32_t guard_bits)
{
uint32_t i;
int32_t sat_max = 2147483647 >> guard_bits;
int32_t sat_min = -2147483648 >> guard_bits;
int32_t temp;
for (i = 0; i < blockSize; i++)
{
temp = input_buf[i];
temp = temp > sat_max ? sat_max : temp;
temp = temp < sat_min ? sat_min : temp;
input_buf[i] = temp << guard_bits;
//input_buf[i] = input_buf[i] << guard_bits;
}
}
void
AudioEffectReverb::update(void)
{
audio_block_t *block;
if (nullptr == (block = receiveWritable()))
{
if (nullptr == (block = allocate()))
return;
else
memset(block->data,0,sizeof block->data);
}
arm_q15_to_q31(block->data, q31_buf, AUDIO_BLOCK_SAMPLES);
provide_guard_bits_q31(q31_buf, AUDIO_BLOCK_SAMPLES, 8);
_do_comb_apf(&apf[0], q31_buf, q31_buf);
_do_comb_apf(&apf[1], q31_buf, q31_buf);
_do_comb_lpf(&lpf[0], q31_buf, sum_buf);
arm_shift_q31(sum_buf, -3, sum_buf, AUDIO_BLOCK_SAMPLES);
_do_comb_lpf(&lpf[1], q31_buf, aux_buf);
arm_shift_q31(aux_buf, -3, aux_buf, AUDIO_BLOCK_SAMPLES);
arm_add_q31(sum_buf, aux_buf, sum_buf, AUDIO_BLOCK_SAMPLES);
_do_comb_lpf(&lpf[2], q31_buf, aux_buf);
arm_shift_q31(aux_buf, -3, aux_buf, AUDIO_BLOCK_SAMPLES);
arm_add_q31(sum_buf, aux_buf, sum_buf, AUDIO_BLOCK_SAMPLES);
_do_comb_lpf(&lpf[3], q31_buf, aux_buf);
arm_shift_q31(aux_buf, -3, aux_buf, AUDIO_BLOCK_SAMPLES);
arm_add_q31(sum_buf, aux_buf, sum_buf, AUDIO_BLOCK_SAMPLES);
_do_comb_apf(&apf[2], sum_buf, q31_buf);
remove_guard_bits_q31(q31_buf, AUDIO_BLOCK_SAMPLES, 8);
arm_q31_to_q15(q31_buf, block->data, AUDIO_BLOCK_SAMPLES);
transmit(block, 0);
release(block);
}
/* EOF */