Debian Codegraph @

servers/audio/audio_filter_sw.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* audio_filter_sw.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 "audio_filter_sw.h"
32
33 #include "core/math/math_funcs.h"
34
35 void AudioFilterSW::set_mode(Mode p_mode) {
36 mode = p_mode;
37 }
38
39 void AudioFilterSW::set_cutoff(float p_cutoff) {
40 cutoff = p_cutoff;
41 }
42
43 void AudioFilterSW::set_resonance(float p_resonance) {
44 resonance = p_resonance;
45 }
46
47 void AudioFilterSW::set_gain(float p_gain) {
48 gain = p_gain;
49 }
50
51 void AudioFilterSW::set_sampling_rate(float p_srate) {
52 sampling_rate = p_srate;
53 }
54
55 void AudioFilterSW::prepare_coefficients(Coeffs *p_coeffs) {
56 int sr_limit = (sampling_rate / 2) + 512;
57
58 double final_cutoff = (cutoff > sr_limit) ? sr_limit : cutoff;
59 if (final_cutoff < 1) {
60 final_cutoff = 1; //don't allow less than this
61 }
62
63 double omega = Math::TAU * final_cutoff / sampling_rate;
64
65 double sin_v = Math::sin(omega);
66 double cos_v = Math::cos(omega);
67
68 double Q = resonance;
69 if (Q <= 0.0) {
70 Q = 0.0001;
71 }
72
73 if (mode == BANDPASS) {
74 Q *= 2.0;
75 } else if (mode == PEAK) {
76 Q *= 3.0;
77 }
78
79 double tmpgain = gain;
80
81 if (tmpgain < 0.001) {
82 tmpgain = 0.001;
83 }
84
85 if (stages > 1) {
86 Q = (Q > 1.0 ? Math::pow(Q, 1.0 / stages) : Q);
87 tmpgain = Math::pow(tmpgain, 1.0 / (stages + 1));
88 }
89 double alpha = sin_v / (2 * Q);
90
91 double a0 = 1.0 + alpha;
92
93 switch (mode) {
94 case LOWPASS: {
95 p_coeffs->b0 = (1.0 - cos_v) / 2.0;
96 p_coeffs->b1 = 1.0 - cos_v;
97 p_coeffs->b2 = (1.0 - cos_v) / 2.0;
98 p_coeffs->a1 = -2.0 * cos_v;
99 p_coeffs->a2 = 1.0 - alpha;
100 } break;
101
102 case HIGHPASS: {
103 p_coeffs->b0 = (1.0 + cos_v) / 2.0;
104 p_coeffs->b1 = -(1.0 + cos_v);
105 p_coeffs->b2 = (1.0 + cos_v) / 2.0;
106 p_coeffs->a1 = -2.0 * cos_v;
107 p_coeffs->a2 = 1.0 - alpha;
108 } break;
109
110 case BANDPASS: {
111 p_coeffs->b0 = alpha * std::sqrt(Q + 1);
112 p_coeffs->b1 = 0.0;
113 p_coeffs->b2 = -alpha * std::sqrt(Q + 1);
114 p_coeffs->a1 = -2.0 * cos_v;
115 p_coeffs->a2 = 1.0 - alpha;
116 } break;
117
118 case NOTCH: {
119 p_coeffs->b0 = 1.0;
120 p_coeffs->b1 = -2.0 * cos_v;
121 p_coeffs->b2 = 1.0;
122 p_coeffs->a1 = -2.0 * cos_v;
123 p_coeffs->a2 = 1.0 - alpha;
124 } break;
125 case PEAK: {
126 p_coeffs->b0 = (1.0 + alpha * tmpgain);
127 p_coeffs->b1 = (-2.0 * cos_v);
128 p_coeffs->b2 = (1.0 - alpha * tmpgain);
129 p_coeffs->a1 = -2 * cos_v;
130 p_coeffs->a2 = (1 - alpha / tmpgain);
131 } break;
132 case BANDLIMIT: {
133 //this one is extra tricky
134 double hicutoff = resonance;
135 double centercutoff = (cutoff + resonance) / 2.0;
136 double bandwidth = (Math::log(centercutoff) - Math::log(hicutoff)) / Math::log((double)2);
137 omega = Math::TAU * centercutoff / sampling_rate;
138 alpha = Math::sin(omega) * Math::sinh(Math::log((double)2) / 2 * bandwidth * omega / Math::sin(omega));
139 a0 = 1 + alpha;
140
141 p_coeffs->b0 = alpha;
142 p_coeffs->b1 = 0;
143 p_coeffs->b2 = -alpha;
144 p_coeffs->a1 = -2 * Math::cos(omega);
145 p_coeffs->a2 = 1 - alpha;
146
147 } break;
148 case LOWSHELF: {
149 double tmpq = Math::sqrt(Q);
150 if (tmpq <= 0) {
151 tmpq = 0.001;
152 }
153 double beta = Math::sqrt(tmpgain) / tmpq;
154
155 a0 = (tmpgain + 1.0) + (tmpgain - 1.0) * cos_v + beta * sin_v;
156 p_coeffs->b0 = tmpgain * ((tmpgain + 1.0) - (tmpgain - 1.0) * cos_v + beta * sin_v);
157 p_coeffs->b1 = 2.0 * tmpgain * ((tmpgain - 1.0) - (tmpgain + 1.0) * cos_v);
158 p_coeffs->b2 = tmpgain * ((tmpgain + 1.0) - (tmpgain - 1.0) * cos_v - beta * sin_v);
159 p_coeffs->a1 = -2.0 * ((tmpgain - 1.0) + (tmpgain + 1.0) * cos_v);
160 p_coeffs->a2 = ((tmpgain + 1.0) + (tmpgain - 1.0) * cos_v - beta * sin_v);
161
162 } break;
163 case HIGHSHELF: {
164 double tmpq = Math::sqrt(Q);
165 if (tmpq <= 0) {
166 tmpq = 0.001;
167 }
168 double beta = Math::sqrt(tmpgain) / tmpq;
169
170 a0 = (tmpgain + 1.0) - (tmpgain - 1.0) * cos_v + beta * sin_v;
171 p_coeffs->b0 = tmpgain * ((tmpgain + 1.0) + (tmpgain - 1.0) * cos_v + beta * sin_v);
172 p_coeffs->b1 = -2.0 * tmpgain * ((tmpgain - 1.0) + (tmpgain + 1.0) * cos_v);
173 p_coeffs->b2 = tmpgain * ((tmpgain + 1.0) + (tmpgain - 1.0) * cos_v - beta * sin_v);
174 p_coeffs->a1 = 2.0 * ((tmpgain - 1.0) - (tmpgain + 1.0) * cos_v);
175 p_coeffs->a2 = ((tmpgain + 1.0) - (tmpgain - 1.0) * cos_v - beta * sin_v);
176
177 } break;
178 }
179
180 p_coeffs->b0 /= a0;
181 p_coeffs->b1 /= a0;
182 p_coeffs->b2 /= a0;
183 p_coeffs->a1 /= 0.0 - a0;
184 p_coeffs->a2 /= 0.0 - a0;
185 }
186
187 void AudioFilterSW::set_stages(int p_stages) {
188 stages = p_stages;
189 }
190
191 /* Fourier transform kernel to obtain response */
192
193 float AudioFilterSW::get_response(float p_freq, Coeffs *p_coeffs) {
194 float freq = p_freq / sampling_rate * Math::TAU;
195
196 float cx = p_coeffs->b0, cy = 0.0;
197
198 cx += std::cos(freq) * p_coeffs->b1;
199 cy -= std::sin(freq) * p_coeffs->b1;
200 cx += std::cos(2 * freq) * p_coeffs->b2;
201 cy -= std::sin(2 * freq) * p_coeffs->b2;
202
203 float H = cx * cx + cy * cy;
204 cx = 1.0;
205 cy = 0.0;
206
207 cx -= std::cos(freq) * p_coeffs->a1;
208 cy += std::sin(freq) * p_coeffs->a1;
209 cx -= std::cos(2 * freq) * p_coeffs->a2;
210 cy += std::sin(2 * freq) * p_coeffs->a2;
211
212 H = H / (cx * cx + cy * cy);
213 return H;
214 }
215
216 AudioFilterSW::Processor::Processor() {
217 set_filter(nullptr);
218 }
219
220 void AudioFilterSW::Processor::set_filter(AudioFilterSW *p_filter, bool p_clear_history) {
221 if (p_clear_history) {
222 ha1 = ha2 = hb1 = hb2 = 0;
223 }
224 filter = p_filter;
225 }
226
227 void AudioFilterSW::Processor::update_coeffs(int p_interp_buffer_len) {
228 if (!filter) {
229 return;
230 }
231
232 if (p_interp_buffer_len) { //interpolate
233 Coeffs old_coeffs = coeffs;
234 filter->prepare_coefficients(&coeffs);
235 incr_coeffs.a1 = (coeffs.a1 - old_coeffs.a1) / p_interp_buffer_len;
236 incr_coeffs.a2 = (coeffs.a2 - old_coeffs.a2) / p_interp_buffer_len;
237 incr_coeffs.b0 = (coeffs.b0 - old_coeffs.b0) / p_interp_buffer_len;
238 incr_coeffs.b1 = (coeffs.b1 - old_coeffs.b1) / p_interp_buffer_len;
239 incr_coeffs.b2 = (coeffs.b2 - old_coeffs.b2) / p_interp_buffer_len;
240 coeffs = old_coeffs;
241 } else {
242 filter->prepare_coefficients(&coeffs);
243 }
244 }
245
246 void AudioFilterSW::Processor::process(float *p_samples, int p_amount, int p_stride, bool p_interpolate) {
247 if (!filter) {
248 return;
249 }
250
251 if (p_interpolate) {
252 for (int i = 0; i < p_amount; i++) {
253 process_one_interp(*p_samples);
254 p_samples += p_stride;
255 }
256 } else {
257 for (int i = 0; i < p_amount; i++) {
258 process_one(*p_samples);
259 p_samples += p_stride;
260 }
261 }
262 }