Debian Codegraph @

servers/audio/effects/eq_filter.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* eq_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 "eq_filter.h"
32
33 #include "core/error/error_macros.h"
34 #include "core/math/math_funcs.h"
35
36 #define POW2(v) ((v) * (v))
37
38 /* Helper */
39 static int solve_quadratic(double a, double b, double c, double *r1, double *r2) {
40 //solves quadractic and returns number of roots
41
42 double base = 2 * a;
43 if (base == 0.0f) {
44 return 0;
45 }
46
47 double squared = b * b - 4 * a * c;
48 if (squared < 0.0) {
49 return 0;
50 }
51
52 squared = std::sqrt(squared);
53
54 *r1 = (-b + squared) / base;
55 *r2 = (-b - squared) / base;
56
57 if (*r1 == *r2) {
58 return 1;
59 } else {
60 return 2;
61 }
62 }
63
64 EQ::BandProcess::BandProcess() {
65 c1 = c2 = c3 = history.a1 = history.a2 = history.a3 = 0;
66 history.b1 = history.b2 = history.b3 = 0;
67 }
68
69 void EQ::recalculate_band_coefficients() {
70 #define BAND_LOG(m_f) (std::log((m_f)) / std::log(2.))
71
72 for (int i = 0; i < band.size(); i++) {
73 double octave_size;
74
75 double frq = band[i].freq;
76
77 if (i == 0) {
78 octave_size = BAND_LOG(band[1].freq) - BAND_LOG(frq);
79 } else if (i == (band.size() - 1)) {
80 octave_size = BAND_LOG(frq) - BAND_LOG(band[i - 1].freq);
81 } else {
82 double next = BAND_LOG(band[i + 1].freq) - BAND_LOG(frq);
83 double prev = BAND_LOG(frq) - BAND_LOG(band[i - 1].freq);
84 octave_size = (next + prev) / 2.0;
85 }
86
87 double frq_l = std::round(frq / std::pow(2.0, octave_size / 2.0));
88
89 double side_gain2 = POW2(Math::SQRT12);
90 double th = Math::TAU * frq / mix_rate;
91 double th_l = Math::TAU * frq_l / mix_rate;
92
93 double c2a = side_gain2 * POW2(std::cos(th)) - 2.0 * side_gain2 * std::cos(th_l) * std::cos(th) + side_gain2 - POW2(std::sin(th_l));
94
95 double c2b = 2.0 * side_gain2 * POW2(std::cos(th_l)) + side_gain2 * POW2(std::cos(th)) - 2.0 * side_gain2 * std::cos(th_l) * std::cos(th) - side_gain2 + POW2(std::sin(th_l));
96
97 double c2c = 0.25 * side_gain2 * POW2(std::cos(th)) - 0.5 * side_gain2 * std::cos(th_l) * std::cos(th) + 0.25 * side_gain2 - 0.25 * POW2(std::sin(th_l));
98
99 //printf("band %i, precoefs = %f,%f,%f\n",i,c2a,c2b,c2c);
100
101 // Default initializing to silence compiler warning about potential uninitialized use.
102 // Both variables are properly set in _solve_quadratic before use, or we continue if roots == 0.
103 double r1 = 0, r2 = 0; //roots
104 int roots = solve_quadratic(c2a, c2b, c2c, &r1, &r2);
105
106 ERR_CONTINUE(roots == 0);
107
108 band.write[i].c1 = 2.0 * ((0.5 - r1) / 2.0);
109 band.write[i].c2 = 2.0 * r1;
110 band.write[i].c3 = 2.0 * (0.5 + r1) * std::cos(th);
111 //printf("band %i, coefs = %f,%f,%f\n",i,(float)bands[i].c1,(float)bands[i].c2,(float)bands[i].c3);
112 }
113 }
114
115 void EQ::set_preset_band_mode(Preset p_preset) {
116 band.clear();
117
118 #define PUSH_BANDS(m_bands) \
119 for (int i = 0; i < m_bands; i++) { \
120 Band b; \
121 b.freq = bands[i]; \
122 b.c1 = b.c2 = b.c3 = 0; \
123 band.push_back(b); \
124 }
125
126 switch (p_preset) {
127 case PRESET_6_BANDS: {
128 static const double bands[] = { 32, 100, 320, 1e3, 3200, 10e3 };
129 PUSH_BANDS(6);
130
131 } break;
132
133 case PRESET_8_BANDS: {
134 static const double bands[] = { 32, 72, 192, 512, 1200, 3000, 7500, 16e3 };
135
136 PUSH_BANDS(8);
137 } break;
138
139 case PRESET_10_BANDS: {
140 static const double bands[] = { 31.25, 62.5, 125, 250, 500, 1e3, 2e3, 4e3, 8e3, 16e3 };
141
142 PUSH_BANDS(10);
143
144 } break;
145
146 case PRESET_21_BANDS: {
147 static const double bands[] = { 22, 32, 44, 63, 90, 125, 175, 250, 350, 500, 700, 1e3, 1400, 2e3, 2800, 4e3, 5600, 8e3, 11e3, 16e3, 22e3 };
148 PUSH_BANDS(21);
149
150 } break;
151
152 case PRESET_31_BANDS: {
153 static const double bands[] = { 20, 25, 31.5, 40, 50, 63, 80, 100, 125, 160, 200, 250, 315, 400, 500, 630, 800, 1e3, 1250, 1600, 2e3, 2500, 3150, 4e3, 5e3, 6300, 8e3, 10e3, 12500, 16e3, 20e3 };
154 PUSH_BANDS(31);
155 } break;
156 };
157
158 recalculate_band_coefficients();
159 }
160
161 int EQ::get_band_count() const {
162 return band.size();
163 }
164
165 float EQ::get_band_frequency(int p_band) {
166 ERR_FAIL_INDEX_V(p_band, band.size(), 0);
167 return band[p_band].freq;
168 }
169
170 void EQ::set_bands(const Vector<float> &p_bands) {
171 band.resize(p_bands.size());
172 for (int i = 0; i < p_bands.size(); i++) {
173 band.write[i].freq = p_bands[i];
174 }
175
176 recalculate_band_coefficients();
177 }
178
179 void EQ::set_mix_rate(float p_mix_rate) {
180 mix_rate = p_mix_rate;
181 recalculate_band_coefficients();
182 }
183
184 EQ::BandProcess EQ::get_band_processor(int p_band) const {
185 EQ::BandProcess band_proc;
186
187 ERR_FAIL_INDEX_V(p_band, band.size(), band_proc);
188
189 band_proc.c1 = band[p_band].c1;
190 band_proc.c2 = band[p_band].c2;
191 band_proc.c3 = band[p_band].c3;
192
193 return band_proc;
194 }
195
196 EQ::EQ() {
197 mix_rate = 44100;
198 }
199
200 EQ::~EQ() {
201 }