91 void prepare(
double sampleRate,
const double* centresHz,
int bands,
int channels,
double qScale = 1.0,
92 const double* widthsHz =
nullptr)
94 if (!std::isfinite(sampleRate) || !(sampleRate > 0.0) || centresHz ==
nullptr || bands < 2)
return;
97 const double qs = std::clamp(qScale, 0.25, 8.0);
98 cw_.resize(
static_cast<size_t>(nb_));
99 alpha_.resize(
static_cast<size_t>(nb_));
100 centre_.resize(
static_cast<size_t>(nb_));
101 for (
int s = 0; s < nb_; ++s)
103 const double f = std::clamp(centresHz[s], 10.0, 0.49 * fs_);
104 centre_[
static_cast<size_t>(s)] = f;
105 const double w = 2.0 * 3.141592653589793 * f / fs_;
106 cw_[
static_cast<size_t>(s)] = std::cos(w);
107 const double width = widthsHz !=
nullptr && std::isfinite(widthsHz[s]) && widthsHz[s] > 0.0
109 const double q = s == 0 || s == nb_ - 1 ? 0.0 : qs * f / width;
110 alpha_[
static_cast<size_t>(s)] = (s == 0 || s == nb_ - 1) ? std::sin(w) * 0.7071067811865476
111 : std::sin(w) / (2.0 * q);
115 z1_.resize(
static_cast<size_t>(grid_));
116 for (
int g = 0; g < grid_; ++g)
118 const double f = g % 2 == 0 ? centre_[
static_cast<size_t>(g / 2)]
119 : std::sqrt(centre_[
static_cast<size_t>(g / 2)] * centre_[
static_cast<size_t>(g / 2 + 1)]);
120 z1_[
static_cast<size_t>(g)] = std::polar(1.0, -2.0 * 3.141592653589793 * f / fs_);
124 std::vector<double> a(
static_cast<size_t>(grid_ * nb_));
125 for (
int s = 0; s < nb_; ++s)
128 for (
int g = 0; g < grid_; ++g) a[static_cast<size_t>(g * nb_ + s)] = dbAt(c, g);
130 std::vector<double> m(
static_cast<size_t>(nb_ * nb_), 0.0);
131 for (
int i = 0; i < nb_; ++i)
132 for (
int j = 0; j < nb_; ++j)
135 for (
int g = 0; g < grid_; ++g) acc += weightOf(g) * a[static_cast<size_t>(g * nb_ + i)] * a[
static_cast<size_t>(g * nb_ + j)];
136 m[
static_cast<size_t>(i * nb_ + j)] = acc;
139 for (
int i = 0; i < nb_; ++i) trace += m[static_cast<size_t>(i * nb_ + i)];
140 const double lambda = kRegularization * trace / nb_;
141 for (
int i = 0; i < nb_; ++i) m[static_cast<size_t>(i * nb_ + i)] += lambda;
143 for (
int j = 0; j < nb_; ++j)
145 double d = m[
static_cast<size_t>(j * nb_ + j)];
146 for (
int k = 0; k < j; ++k) d -= m[
static_cast<size_t>(j * nb_ + k)] * m[
static_cast<size_t>(j * nb_ + k)];
147 const double l = std::sqrt(std::max(d, 1e-30));
148 m[
static_cast<size_t>(j * nb_ + j)] = l;
149 for (
int i = j + 1; i < nb_; ++i)
151 double v = m[
static_cast<size_t>(i * nb_ + j)];
152 for (
int k = 0; k < j; ++k) v -= m[
static_cast<size_t>(i * nb_ + k)] * m[
static_cast<size_t>(j * nb_ + k)];
153 m[
static_cast<size_t>(i * nb_ + j)] = v / l;
156 p_.assign(
static_cast<size_t>(nb_ * grid_), 0.0);
157 std::vector<double> col(
static_cast<size_t>(nb_));
158 for (
int g = 0; g < grid_; ++g)
160 for (
int i = 0; i < nb_; ++i) col[static_cast<size_t>(i)] = weightOf(g) * a[
static_cast<size_t>(g * nb_ + i)];
161 for (
int i = 0; i < nb_; ++i)
163 double v = col[
static_cast<size_t>(i)];
164 for (
int k = 0; k < i; ++k) v -= m[
static_cast<size_t>(i * nb_ + k)] * col[
static_cast<size_t>(k)];
165 col[
static_cast<size_t>(i)] = v / m[
static_cast<size_t>(i * nb_ + i)];
167 for (
int i = nb_ - 1; i >= 0; --i)
169 double v = col[
static_cast<size_t>(i)];
170 for (
int k = i + 1; k < nb_; ++k) v -= m[
static_cast<size_t>(k * nb_ + i)] * col[
static_cast<size_t>(k)];
171 col[
static_cast<size_t>(i)] = v / m[
static_cast<size_t>(i * nb_ + i)];
173 for (
int i = 0; i < nb_; ++i) p_[static_cast<size_t>(i * grid_ + g)] = col[
static_cast<size_t>(i)];
175 target_.assign(
static_cast<size_t>(grid_), 0.0);
176 response_.assign(
static_cast<size_t>(grid_), 0.0);
177 gains_.assign(
static_cast<size_t>(nb_), 0.0);
178 candidate_.assign(
static_cast<size_t>(nb_), 0.0);
179 correction_.assign(
static_cast<size_t>(nb_), 0.0);
180 useLanes_ = channels <= 1;
183 lanes_.prepare(nb_, 1);
184 for (
int s = 0; s < nb_; ++s) lanes_.setCoeffs(s, sectionAt(s, 0.0));
189 for (
int s = 0; s < nb_; ++s) serial_.
setCoeffs(s, sectionAt(s, 0.0));
195 [[nodiscard]]
double getCentre(
int band)
const noexcept {
return centre_[
static_cast<size_t>(std::clamp(band, 0, nb_ - 1))]; }
197 [[nodiscard]]
int getLatency() const noexcept {
return useLanes_ ? Lanes - 1 : 0; }
199 [[nodiscard]]
double getSectionGain(
int section)
const noexcept {
return gains_[
static_cast<size_t>(std::clamp(section, 0, nb_ - 1))]; }
208 void setGains(
const T* gainsDb,
int rampSamples,
int refine = 1) noexcept
211 for (
int g = 0; g < grid_; ++g)
213 const double v = g % 2 == 0 ?
static_cast<double>(gainsDb[g / 2])
214 : 0.5 * (
static_cast<double>(gainsDb[g / 2]) +
static_cast<double>(gainsDb[g / 2 + 1]));
215 target_[
static_cast<size_t>(g)] = std::isfinite(v) ? std::clamp(v, -24.0, 24.0) : 0.0;
217 solve(target_.data(), gains_.data(),
false);
218 for (
auto& g : gains_) g = std::clamp(g, -kMaxSectionDb, kMaxSectionDb);
221 for (
int s = 0; s < nb_; ++s)
222 gains_[
static_cast<size_t>(s)] = std::clamp(gains_[
static_cast<size_t>(s)] + correction_[
static_cast<size_t>(s)], -kMaxSectionDb, kMaxSectionDb);
226 correction_ = gains_;
231 const int steps = std::clamp(refine, 0, 3);
234 double err = missing(gains_.data());
235 for (
int r = 0; r < steps; ++r)
238 solve(response_.data(), candidate_.data(),
true);
239 for (
auto& g : candidate_) g = std::clamp(g, -kMaxSectionDb, kMaxSectionDb);
240 const double next = missing(candidate_.data());
241 if (!(next < err))
break;
248 for (
int s = 0; s < nb_; ++s)
249 correction_[
static_cast<size_t>(s)] = gains_[
static_cast<size_t>(s)] - correction_[
static_cast<size_t>(s)];
252 for (
int s = 0; s < nb_; ++s)
253 lanes_.setTargetCoeffs(s, sectionAt(s, gains_[
static_cast<size_t>(s)]));
254 lanes_.startRamp(rampSamples);
258 for (
int s = 0; s < nb_; ++s)
259 serial_.
rampCoeffs(s, sectionAt(s, gains_[
static_cast<size_t>(s)]), rampSamples);
266 const std::complex<double> z = std::polar(1.0, -2.0 * 3.141592653589793 * hz / fs_);
268 for (
int s = 0; s < nb_; ++s) db += dbOf(sectionAt(s, gains_[static_cast<size_t>(s)]), z);
273 void reset() noexcept {
if (useLanes_) lanes_.reset();
else serial_.
reset(); }
278 if (useLanes_) lanes_.processBlock(buffer);
283 for (
int ch = 0; ch < buffer.getNumChannels(); ++ch)
285 T* d = buffer.getChannel(ch);
286 for (
int i = 0; i < buffer.getNumSamples(); ++i)
287 if (!std::isfinite(d[i])) { d[i] = T(0); broken =
true; }
294 [[nodiscard]]
static double weightOf(
int g)
noexcept {
return g % 2 == 0 ? 1.0 : kMidWeight; }
295 static constexpr double kMidWeight = 0.1;
296 static constexpr double kRegularization = 1e-2;
297 static constexpr double kMaxSectionDb = 30.0;
300 [[nodiscard]] BiquadCoeffs sectionAt(
int s,
double gainDb)
const noexcept
302 const double A = std::pow(10.0, gainDb / 40.0);
303 const double cw = cw_[
static_cast<size_t>(s)], al = alpha_[
static_cast<size_t>(s)];
305 if (s == 0 || s == nb_ - 1)
307 const double t = 2.0 * std::sqrt(A) * al;
308 const double sign = s == 0 ? 1.0 : -1.0;
309 const double a0 = (A + 1.0) + sign * (A - 1.0) * cw + t;
310 c.b0 = A * ((A + 1.0) - sign * (A - 1.0) * cw + t) / a0;
311 c.b1 = sign * 2.0 * A * ((A - 1.0) - sign * (A + 1.0) * cw) / a0;
312 c.b2 = A * ((A + 1.0) - sign * (A - 1.0) * cw - t) / a0;
313 c.a1 = -sign * 2.0 * ((A - 1.0) + sign * (A + 1.0) * cw) / a0;
314 c.a2 = ((A + 1.0) + sign * (A - 1.0) * cw - t) / a0;
317 const double ia = A / (A + al);
318 c.b0 = (1.0 + al * A) * ia;
319 c.b1 = -2.0 * cw * ia;
320 c.b2 = (1.0 - al * A) * ia;
322 c.a2 = (A - al) / (A + al);
326 [[nodiscard]]
static double dbOf(
const BiquadCoeffs& c, std::complex<double> z1)
noexcept
328 const std::complex<double> z2 = z1 * z1;
329 const std::complex<double> num = c.b0 + c.b1 * z1 + c.b2 * z2;
330 const std::complex<double> den = 1.0 + c.a1 * z1 + c.a2 * z2;
331 return 10.0 * std::log10(std::max(std::norm(num), 1e-30) / std::max(std::norm(den), 1e-30));
334 [[nodiscard]]
double dbAt(
const BiquadCoeffs& c,
int g)
const noexcept {
return dbOf(c, z1_[
static_cast<size_t>(g)]); }
340 double missing(
const double* gains)
noexcept
342 for (
int g = 0; g < grid_; ++g) response_[static_cast<size_t>(g)] = 0.0;
343 for (
int s = 0; s < nb_; ++s)
345 const BiquadCoeffs c = sectionAt(s, gains[s]);
346 for (
int g = 0; g < grid_; ++g) response_[static_cast<size_t>(g)] += dbAt(c, g);
349 for (
int g = 0; g < grid_; ++g)
351 const double d = target_[
static_cast<size_t>(g)] - response_[
static_cast<size_t>(g)];
352 response_[
static_cast<size_t>(g)] = d;
353 err += weightOf(g) * d * d;
359 void solve(
const double* v,
double* x,
bool add)
const noexcept
361 for (
int s = 0; s < nb_; ++s)
364 const double* row = p_.data() +
static_cast<size_t>(s * grid_);
365 for (
int g = 0; g < grid_; ++g) acc += row[g] * v[g];
366 x[s] =
add ? x[s] + acc : acc;
370 double fs_ = 48000.0;
371 int nb_ = 0, grid_ = 0;
372 std::vector<double> cw_, alpha_, centre_, p_, target_, response_, gains_, candidate_, correction_;
373 std::vector<std::complex<double>> z1_;
374 bool useLanes_ =
true;
375 LaneCascade<T, Lanes> lanes_;
376 BiquadCascade<T, kMaxBands, 8> serial_;
void prepare(double sampleRate, const double *centresHz, int bands, int channels, double qScale=1.0, const double *widthsHz=nullptr)
Lays the bank out (allocates).