-
Notifications
You must be signed in to change notification settings - Fork 10
/
fx_reverb.h
306 lines (272 loc) · 8.42 KB
/
fx_reverb.h
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
/*
* reverb stuff
*
* This is a port of the public code from YetAnotherElectronicsChannel
* Based on main.c I've created this module
*
* src: https://github.com/YetAnotherElectronicsChannel/STM32_DSP_Reverb/blob/master/code/Src/main.c
*
* The explanation of the original module can be found here: https://youtu.be/nRLXNmLmHqM
*
* Changes:
* - optimized for buffer processing
* - added interface to set the level
*
*/
#ifdef NO_PSRAM
#define REV_MULTIPLIER 0.35f
#else
#define REV_MULTIPLIER 0.8f
#endif
#define l_CB0 (int)( 3604 * REV_MULTIPLIER)
#define l_CB1 (int)( 3112 * REV_MULTIPLIER)
#define l_CB2 (int)( 4044 * REV_MULTIPLIER)
#define l_CB3 (int)( 4492 * REV_MULTIPLIER)
#define l_AP0 (int)( 500 * REV_MULTIPLIER)
#define l_AP1 (int)( 168 * REV_MULTIPLIER)
#define l_AP2 (int)( 48 * REV_MULTIPLIER)
#define STATIC_REV_BUFFER
//rev_time 0.0 <-> 1.0
//rev_delay 0.0 <-> 1.0
class FxReverb {
public:
FxReverb() {}
inline void Process( float *signal_l, float *signal_r ){
float inSample;
// create mono sample
inSample = *signal_l + *signal_r; // it may cause unwanted audible effects
//inSample *= 0.5f;
// float newsample = (Do_Comb0(inSample) + Do_Comb1(inSample) + Do_Comb2(inSample) + Do_Comb3(inSample)) / 4.0f;
float newsample = (Do_Comb0(inSample) + Do_Comb1(inSample) + Do_Comb2(inSample) + Do_Comb3(inSample)) * 0.125f;
newsample = Do_Allpass0(newsample);
newsample = Do_Allpass1(newsample);
newsample = Do_Allpass2(newsample);
// apply reverb level
newsample *= rev_level;
*signal_l += newsample;
*signal_r += newsample;
};
inline void Init(){
SetLevel( 1.0f );
SetTime( 0.75f );
// #ifdef NO_PSRAM
#ifndef STATIC_REV_BUFFER
cfbuf0 = (float *)malloc(sizeof(float) * l_CB0);
if( cfbuf0 == NULL){
DEBUG("No more RAM for reverb cb0!");
} else {
DEBUG("Memory allocated for reverb cb0");
};
cfbuf1 = (float *)malloc(sizeof(float) * l_CB1);
if( cfbuf1 == NULL){
DEBUG("No more RAM for reverb cb1!");
} else {
DEBUG("Memory allocated for reverb cb1");
};
cfbuf2 = (float *)malloc(sizeof(float) * l_CB2);
if( cfbuf2 == NULL){
DEBUG("No more RAM for reverb cb2!");
} else {
DEBUG("Memory allocated for reverb cb2");
};
cfbuf3 = (float *)malloc(sizeof(float) * l_CB3);
if( cfbuf3 == NULL){
DEBUG("No more RAM for reverb cb3!");
} else {
DEBUG("Memory allocated for reverb cb3");
};
apbuf0 = (float *)malloc(sizeof(float) * l_AP0);
if( apbuf0 == NULL){
DEBUG("No more RAM for reverb apbuf0!");
} else {
DEBUG("Memory allocated for reverb apbuf0");
};
apbuf1 = (float *)malloc(sizeof(float) * l_AP1);
if( apbuf1 == NULL){
DEBUG("No more RAM for reverb apbuf1!");
} else {
DEBUG("Memory allocated for reverb apbuf1");
};
apbuf2 = (float *)malloc(sizeof(float) * l_AP2);
if( apbuf2 == NULL){
DEBUG("No more RAM for reverb apbuf2!");
} else {
DEBUG("Memory allocated for reverb apbuf2");
};
#endif
/*#else
// PSRAM is too slow for that
cfbuf0 = (float *)ps_malloc(sizeof(float) * l_CB0);
if( cfbuf0 == NULL){
DEBUG("No more RAM for reverb cb0!");
} else {
DEBUG("Memory allocated for reverb cb0");
};
cfbuf1 = (float *)ps_malloc(sizeof(float) * l_CB1);
if( cfbuf1 == NULL){
DEBUG("No more RAM for reverb cb1!");
} else {
DEBUG("Memory allocated for reverb cb1");
};
cfbuf2 = (float *)ps_malloc(sizeof(float) * l_CB2);
if( cfbuf2 == NULL){
DEBUG("No more RAM for reverb cb2!");
} else {
DEBUG("Memory allocated for reverb cb2");
};
cfbuf3 = (float *)ps_malloc(sizeof(float) * l_CB3);
if( cfbuf3 == NULL){
DEBUG("No more RAM for reverb cb3!");
} else {
DEBUG("Memory allocated for reverb cb3");
};
apbuf0 = (float *)ps_malloc(sizeof(float) * l_AP0);
if( apbuf0 == NULL){
DEBUG("No more RAM for reverb apbuf0!");
} else {
DEBUG("Memory allocated for reverb apbuf0");
};
apbuf1 = (float *)ps_malloc(sizeof(float) * l_AP1);
if( apbuf1 == NULL){
DEBUG("No more RAM for reverb apbuf1!");
} else {
DEBUG("Memory allocated for reverb apbuf1");
};
apbuf2 = (float *)ps_malloc(sizeof(float) * l_AP2);
if( apbuf2 == NULL){
DEBUG("No more RAM for reverb apbuf2!");
} else {
DEBUG("Memory allocated for reverb apbuf2");
};
#endif
*/
};
inline void SetTime( float value ){
rev_time = 0.92f * value + 0.02f ;
cf0_lim = (int)(rev_time * (l_CB0));
cf1_lim = (int)(rev_time * (l_CB1));
cf2_lim = (int)(rev_time * (l_CB2));
cf3_lim = (int)(rev_time * (l_CB3));
ap0_lim = (int)(rev_time * (l_AP0));
ap1_lim = (int)(rev_time * (l_AP1));
ap2_lim = (int)(rev_time * (l_AP2));
#ifdef DEBUG_FX
DEBF("reverb time: %0.3f\n", value);
#endif
};
inline void SetLevel( float value ){
rev_level = value;
#ifdef DEBUG_FX
DEBF("reverb level: %0.3f\n", value);
#endif
};
private:
float rev_time = 0.5f;
float rev_level = 0.5f;
#ifdef STATIC_REV_BUFFER
float cfbuf0[l_CB0];
float cfbuf1[l_CB1];
float cfbuf2[l_CB2];
float cfbuf3[l_CB3];
float apbuf0[l_AP0];
float apbuf1[l_AP1];
float apbuf2[l_AP2];
#else
float * cfbuf0 = NULL;
float * cfbuf1 = NULL;
float * cfbuf2 = NULL;
float * cfbuf3 = NULL;
float * apbuf0 = NULL;
float * apbuf1 = NULL;
float * apbuf2 = NULL;
#endif
//define pointer limits = delay time
int cf0_lim, cf1_lim, cf2_lim, cf3_lim, ap0_lim, ap1_lim, ap2_lim;
inline float Do_Comb0( float inSample ){
static int cf0_p = 0;
static float cf0_g = 0.805f;
float readback = cfbuf0[cf0_p];
float newV = readback * cf0_g + inSample;
cfbuf0[cf0_p] = newV;
cf0_p++;
if( cf0_p >= cf0_lim ){
cf0_p = 0;
}
return readback;
};
inline float Do_Comb1( float inSample ){
static int cf1_p = 0;
static float cf1_g = 0.827f;
float readback = cfbuf1[cf1_p];
float newV = readback * cf1_g + inSample;
cfbuf1[cf1_p] = newV;
cf1_p++;
if( cf1_p >= cf1_lim ){
cf1_p = 0;
}
return readback;
};
inline float Do_Comb2( float inSample ){
static int cf2_p = 0;
static float cf2_g = 0.783f;
float readback = cfbuf2[cf2_p];
float newV = readback * cf2_g + inSample;
cfbuf2[cf2_p] = newV;
cf2_p++;
if( cf2_p >= cf2_lim ){
cf2_p = 0;
}
return readback;
};
inline float Do_Comb3( float inSample ){
static int cf3_p = 0;
static float cf3_g = 0.764f;
float readback = cfbuf3[cf3_p];
float newV = readback * cf3_g + inSample;
cfbuf3[cf3_p] = newV;
cf3_p++;
if( cf3_p >= cf3_lim ){
cf3_p = 0;
}
return readback;
};
inline float Do_Allpass0( float inSample ){
static int ap0_p = 0;
static float ap0_g = 0.7f;
float readback = apbuf0[ap0_p];
readback += (-ap0_g) * inSample;
float newV = readback * ap0_g + inSample;
apbuf0[ap0_p] = newV;
ap0_p++;
if( ap0_p >= ap0_lim ){
ap0_p = 0;
}
return readback;
};
inline float Do_Allpass1( float inSample ){
static int ap1_p = 0;
static float ap1_g = 0.7f;
float readback = apbuf1[ap1_p];
readback += (-ap1_g) * inSample;
float newV = readback * ap1_g + inSample;
apbuf1[ap1_p] = newV;
ap1_p++;
if( ap1_p >= ap1_lim ){
ap1_p = 0;
}
return readback;
};
inline float Do_Allpass2( float inSample ){
static int ap2_p = 0;
static float ap2_g = 0.7f;
float readback = apbuf2[ap2_p];
readback += (-ap2_g) * inSample;
float newV = readback * ap2_g + inSample;
apbuf2[ap2_p] = newV;
ap2_p++;
if( ap2_p >= ap2_lim ){
ap2_p = 0;
}
return readback;
};
};