Debian Codegraph @

servers/audio/effects/reverb_filter.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* reverb_filter.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "reverb_filter.h"
32
33 #include "core/math/audio_frame.h"
34 #include "core/os/memory.h"
35
36 const float Reverb::comb_tunings[MAX_COMBS] = {
37 //freeverb comb tunings
38 0.025306122448979593f,
39 0.026938775510204082f,
40 0.028956916099773241f,
41 0.03074829931972789f,
42 0.032244897959183672f,
43 0.03380952380952381f,
44 0.035306122448979592f,
45 0.036666666666666667f
46 };
47
48 const float Reverb::allpass_tunings[MAX_ALLPASS] = {
49 //freeverb allpass tunings
50 0.0051020408163265302f,
51 0.007732426303854875f,
52 0.01f,
53 0.012607709750566893f
54 };
55
56 void Reverb::process(float *p_src, float *p_dst, int p_frames) {
57 if (p_frames > INPUT_BUFFER_MAX_SIZE) {
58 p_frames = INPUT_BUFFER_MAX_SIZE;
59 }
60
61 int predelay_frames = std::rint((params.predelay / 1000.0) * params.mix_rate);
62 if (predelay_frames < 10) {
63 predelay_frames = 10;
64 }
65 if (predelay_frames >= echo_buffer_size) {
66 predelay_frames = echo_buffer_size - 1;
67 }
68
69 for (int i = 0; i < p_frames; i++) {
70 if (echo_buffer_pos >= echo_buffer_size) {
71 echo_buffer_pos = 0;
72 }
73
74 int read_pos = echo_buffer_pos - predelay_frames;
75 while (read_pos < 0) {
76 read_pos += echo_buffer_size;
77 }
78
79 float in = undenormalize(echo_buffer[read_pos] * params.predelay_fb + p_src[i]);
80
81 echo_buffer[echo_buffer_pos] = in;
82
83 input_buffer[i] = in;
84
85 p_dst[i] = 0; //take the chance and clear this
86
87 echo_buffer_pos++;
88 }
89
90 if (params.hpf > 0) {
91 float hpaux = std::exp(-Math::TAU * params.hpf * 6000 / params.mix_rate);
92 float hp_a1 = (1.0 + hpaux) / 2.0;
93 float hp_a2 = -(1.0 + hpaux) / 2.0;
94 float hp_b1 = hpaux;
95
96 for (int i = 0; i < p_frames; i++) {
97 float in = input_buffer[i];
98 input_buffer[i] = in * hp_a1 + hpf_h1 * hp_a2 + hpf_h2 * hp_b1;
99 hpf_h2 = input_buffer[i];
100 hpf_h1 = in;
101 }
102 }
103
104 for (int i = 0; i < MAX_COMBS; i++) {
105 Comb &c = comb[i];
106
107 int size_limit = c.size - std::rint((float)c.extra_spread_frames * (1.0 - params.extra_spread));
108 for (int j = 0; j < p_frames; j++) {
109 if (c.pos >= size_limit) { //reset this now just in case
110 c.pos = 0;
111 }
112
113 float out = undenormalize(c.buffer[c.pos] * c.feedback);
114 out = out * (1.0 - c.damp) + c.damp_h * c.damp; //lowpass
115 c.damp_h = out;
116 c.buffer[c.pos] = input_buffer[j] + out;
117 p_dst[j] += out;
118 c.pos++;
119 }
120 }
121
122 static const float allpass_feedback = 0.7;
123 /* this one works, but the other version is just nicer....
124 int ap_size_limit[MAX_ALLPASS];
125
126 for (int i=0;i<MAX_ALLPASS;i++) {
127 AllPass &a=allpass[i];
128 ap_size_limit[i]=a.size-std::rint((float)a.extra_spread_frames*(1.0-params.extra_spread));
129 }
130
131 for (int i=0;i<p_frames;i++) {
132 float sample=p_dst[i];
133 float aux,in;
134 float AllPass*ap;
135
136 #define PROCESS_ALLPASS(m_ap) \
137 ap=&allpass[m_ap]; \
138 if (ap->pos>=ap_size_limit[m_ap]) \
139 ap->pos=0; \
140 aux=undenormalize(ap->buffer[ap->pos]); \
141 in=sample; \
142 sample=-in+aux; \
143 ap->pos++;
144
145
146 PROCESS_ALLPASS(0);
147 PROCESS_ALLPASS(1);
148 PROCESS_ALLPASS(2);
149 PROCESS_ALLPASS(3);
150
151 p_dst[i]=sample;
152 }
153 */
154
155 for (int i = 0; i < MAX_ALLPASS; i++) {
156 AllPass &a = allpass[i];
157 int size_limit = a.size - std::rint((float)a.extra_spread_frames * (1.0 - params.extra_spread));
158
159 for (int j = 0; j < p_frames; j++) {
160 if (a.pos >= size_limit) {
161 a.pos = 0;
162 }
163
164 float aux = a.buffer[a.pos];
165 a.buffer[a.pos] = undenormalize(allpass_feedback * aux + p_dst[j]);
166 p_dst[j] = aux - allpass_feedback * a.buffer[a.pos];
167 a.pos++;
168 }
169 }
170
171 static const float wet_scale = 0.6;
172
173 for (int i = 0; i < p_frames; i++) {
174 p_dst[i] = p_dst[i] * params.wet * wet_scale + p_src[i] * params.dry;
175 }
176 }
177
178 void Reverb::set_room_size(float p_size) {
179 params.room_size = p_size;
180 update_parameters();
181 }
182
183 void Reverb::set_damp(float p_damp) {
184 params.damp = p_damp;
185 update_parameters();
186 }
187
188 void Reverb::set_wet(float p_wet) {
189 params.wet = p_wet;
190 }
191
192 void Reverb::set_dry(float p_dry) {
193 params.dry = p_dry;
194 }
195
196 void Reverb::set_predelay(float p_predelay) {
197 params.predelay = p_predelay;
198 }
199
200 void Reverb::set_predelay_feedback(float p_predelay_fb) {
201 params.predelay_fb = p_predelay_fb;
202 }
203
204 void Reverb::set_highpass(float p_frq) {
205 if (p_frq > 1) {
206 p_frq = 1;
207 }
208 if (p_frq < 0) {
209 p_frq = 0;
210 }
211 params.hpf = p_frq;
212 }
213
214 void Reverb::set_extra_spread(float p_spread) {
215 params.extra_spread = p_spread;
216 }
217
218 void Reverb::set_mix_rate(float p_mix_rate) {
219 params.mix_rate = p_mix_rate;
220 configure_buffers();
221 }
222
223 void Reverb::set_extra_spread_base(float p_sec) {
224 params.extra_spread_base = p_sec;
225 configure_buffers();
226 }
227
228 void Reverb::configure_buffers() {
229 clear_buffers(); //clear if necessary
230
231 for (int i = 0; i < MAX_COMBS; i++) {
232 Comb &c = comb[i];
233
234 c.extra_spread_frames = std::rint(params.extra_spread_base * params.mix_rate);
235
236 int len = std::rint(comb_tunings[i] * params.mix_rate) + c.extra_spread_frames;
237 if (len < 5) {
238 len = 5; //may this happen?
239 }
240
241 c.buffer = memnew_arr(float, len);
242 c.pos = 0;
243 for (int j = 0; j < len; j++) {
244 c.buffer[j] = 0;
245 }
246 c.size = len;
247 }
248
249 for (int i = 0; i < MAX_ALLPASS; i++) {
250 AllPass &a = allpass[i];
251
252 a.extra_spread_frames = std::rint(params.extra_spread_base * params.mix_rate);
253
254 int len = std::rint(allpass_tunings[i] * params.mix_rate) + a.extra_spread_frames;
255 if (len < 5) {
256 len = 5; //may this happen?
257 }
258
259 a.buffer = memnew_arr(float, len);
260 a.pos = 0;
261 for (int j = 0; j < len; j++) {
262 a.buffer[j] = 0;
263 }
264 a.size = len;
265 }
266
267 echo_buffer_size = (int)(((float)MAX_ECHO_MS / 1000.0) * params.mix_rate + 1.0);
268 echo_buffer = memnew_arr(float, echo_buffer_size);
269 for (int i = 0; i < echo_buffer_size; i++) {
270 echo_buffer[i] = 0;
271 }
272
273 echo_buffer_pos = 0;
274 }
275
276 void Reverb::update_parameters() {
277 //more freeverb derived constants
278 static const float room_scale = 0.28f;
279 static const float room_offset = 0.7f;
280
281 for (int i = 0; i < MAX_COMBS; i++) {
282 Comb &c = comb[i];
283 c.feedback = room_offset + params.room_size * room_scale;
284 if (c.feedback < room_offset) {
285 c.feedback = room_offset;
286 } else if (c.feedback > (room_offset + room_scale)) {
287 c.feedback = (room_offset + room_scale);
288 }
289
290 float auxdmp = params.damp / 2.0 + 0.5; //only half the range (0.5 .. 1.0 is enough)
291 auxdmp *= auxdmp;
292
293 c.damp = std::exp(-Math::TAU * auxdmp * 10000 / params.mix_rate); // 0 .. 10khz
294 }
295 }
296
297 void Reverb::clear_buffers() {
298 if (echo_buffer) {
299 memdelete_arr(echo_buffer);
300 }
301
302 for (int i = 0; i < MAX_COMBS; i++) {
303 if (comb[i].buffer) {
304 memdelete_arr(comb[i].buffer);
305 }
306
307 comb[i].buffer = nullptr;
308 }
309
310 for (int i = 0; i < MAX_ALLPASS; i++) {
311 if (allpass[i].buffer) {
312 memdelete_arr(allpass[i].buffer);
313 }
314
315 allpass[i].buffer = nullptr;
316 }
317 }
318
319 Reverb::Reverb() {
320 params.room_size = 0.8;
321 params.damp = 0.5;
322 params.dry = 1.0;
323 params.wet = 0.0;
324 params.mix_rate = 44100;
325 params.extra_spread_base = 0;
326 params.extra_spread = 1.0;
327 params.predelay = 150;
328 params.predelay_fb = 0.4;
329 params.hpf = 0;
330
331 input_buffer = memnew_arr(float, INPUT_BUFFER_MAX_SIZE);
332
333 configure_buffers();
334 update_parameters();
335 }
336
337 Reverb::~Reverb() {
338 memdelete_arr(input_buffer);
339 clear_buffers();
340 }