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