61#include "AudioBuffer.h"
62#include "DenormalGuard.h"
69#ifndef DSPARK_NOINLINE
71 #define DSPARK_NOINLINE __declspec(noinline)
72 #elif defined(__GNUC__) || defined(__clang__)
73 #define DSPARK_NOINLINE __attribute__((noinline))
75 #define DSPARK_NOINLINE
121 freq = std::clamp(freq, 1.0, std::max(1.0, sampleRate * 0.499));
122 Q = std::max(Q, 0.001);
123 const double w0 = 2.0 * std::numbers::pi * freq / sampleRate;
124 const double cosw0 = std::cos(w0);
125 const double sinw0 = std::sin(w0);
126 const double alpha = sinw0 / (2.0 * Q);
128 const double a0 = 1.0 + alpha;
145 freq = std::clamp(freq, 1.0, std::max(1.0, sampleRate * 0.499));
146 Q = std::max(Q, 0.001);
147 const double w0 = 2.0 * std::numbers::pi * freq / sampleRate;
148 const double cosw0 = std::cos(w0);
149 const double sinw0 = std::sin(w0);
150 const double alpha = sinw0 / (2.0 * Q);
152 const double a0 = 1.0 + alpha;
174 freq = std::clamp(freq, 1.0, std::max(1.0, sampleRate * 0.499));
175 Q = std::max(Q, 0.001);
176 const double w0 = 2.0 * std::numbers::pi * freq / sampleRate;
177 const double cosw0 = std::cos(w0);
178 const double sinw0 = std::sin(w0);
179 const double alpha = sinw0 / (2.0 * Q);
181 const double a0 = 1.0 + alpha;
203 freq = std::clamp(freq, 1.0, std::max(1.0, sampleRate * 0.499));
204 Q = std::max(Q, 0.001);
205 const double A = std::pow(10.0, gainDb / 40.0);
206 const double w0 = 2.0 * std::numbers::pi * freq / sampleRate;
207 const double cosw0 = std::cos(w0);
208 const double sinw0 = std::sin(w0);
209 const double alpha = sinw0 / (2.0 * Q);
211 const double a0 = 1.0 + alpha / A;
249 double Q,
double gainDb)
noexcept
251 freq = std::clamp(freq, 1.0, std::max(1.0, sampleRate * 0.499));
252 Q = std::max(Q, 0.001);
253 if (std::abs(gainDb) < 0.01)
254 return { 1.0, 0.0, 0.0, 0.0, 0.0 };
256 const double G = std::pow(10.0, gainDb / 20.0);
257 const double w0 = 2.0 * std::numbers::pi * freq / sampleRate;
261 const double q = 1.0 / (2.0 * Q * std::sqrt(G));
263 const double a2 = std::exp(-2.0 * q * w0);
266 a1 = -2.0 * std::exp(-q * w0) * std::cos(std::sqrt(1.0 - q * q) * w0);
274 const double r = std::sqrt(q * q - 1.0);
275 a1 = -(std::exp(-w0 / (q + r)) + std::exp(-(q + r) * w0));
280 const double A0 = (1.0 +
a1 +
a2) * (1.0 +
a1 +
a2);
281 const double A1 = (1.0 -
a1 +
a2) * (1.0 -
a1 +
a2);
282 const double A2 = -4.0 *
a2;
283 const double s = std::sin(0.5 * w0);
284 const double phi1 = s * s;
285 const double phi0 = 1.0 - phi1;
286 const double phi2 = 4.0 * phi0 * phi1;
288 const double G2 = G * G;
289 const double B0 = A0;
290 const double R1 = (A0 * phi0 + A1 * phi1 + A2 * phi2) * G2;
291 const double R2 = (-A0 + A1 + 4.0 * (phi0 - phi1) * A2) * G2;
292 const double B2 = (R1 - R2 * phi1 - B0) / (4.0 * phi1 * phi1);
293 const double B1 = R2 + B0 + 4.0 * (phi1 - phi0) * B2;
296 const double sqrtB0 = std::sqrt(std::max(0.0, B0));
297 const double sqrtB1 = std::sqrt(std::max(0.0, B1));
298 const double W = 0.5 * (sqrtB0 + sqrtB1);
300 c.
b0 = 0.5 * (W + std::sqrt(std::max(0.0, W * W + B2)));
301 c.
b1 = 0.5 * (sqrtB0 - sqrtB1);
302 c.
b2 = -B2 / (4.0 * c.
b0);
322 double Q = 0.7071067811865476) noexcept
324 freq = std::clamp(freq, 1.0, std::max(1.0, sampleRate * 0.499));
325 Q = std::max(Q, 0.001);
326 const MatchedPoles m = matchedPoles(2.0 * std::numbers::pi * freq / sampleRate, 1.0 / (2.0 * Q));
327 const double R1 = (m.A0 * m.phi0 + m.A1 * m.phi1 + m.A2 * m.phi2) * Q * Q;
328 const double B0 = m.A0;
329 const double B1 = (R1 - B0 * m.phi0) / m.phi1;
331 c.
b0 = 0.5 * (std::sqrt(B0) + std::sqrt(std::max(0.0, B1)));
332 c.
b1 = std::sqrt(B0) - c.
b0;
348 double Q = 0.7071067811865476) noexcept
350 freq = std::clamp(freq, 1.0, std::max(1.0, sampleRate * 0.499));
351 Q = std::max(Q, 0.001);
352 const MatchedPoles m = matchedPoles(2.0 * std::numbers::pi * freq / sampleRate, 1.0 / (2.0 * Q));
354 c.
b0 = std::sqrt(std::max(0.0, m.A0 * m.phi0 + m.A1 * m.phi1 + m.A2 * m.phi2)) * Q / (4.0 * m.phi1);
371 double Q = 0.7071067811865476) noexcept
373 freq = std::clamp(freq, 1.0, std::max(1.0, sampleRate * 0.499));
374 Q = std::max(Q, 0.001);
375 const MatchedPoles m = matchedPoles(2.0 * std::numbers::pi * freq / sampleRate, 1.0 / (2.0 * Q));
376 const double R1 = m.A0 * m.phi0 + m.A1 * m.phi1 + m.A2 * m.phi2;
377 const double R2 = -m.A0 + m.A1 + 4.0 * (m.phi0 - m.phi1) * m.A2;
378 const double B2 = (R1 - R2 * m.phi1) / (4.0 * m.phi1 * m.phi1);
379 const double B1 = R2 + 4.0 * (m.phi1 - m.phi0) * B2;
381 c.
b1 = -0.5 * std::sqrt(std::max(0.0, B1));
382 c.b0 = 0.5 * (std::sqrt(std::max(0.0, B2 + c.b1 * c.b1)) - c.b1);
401 return matchedShelf(sampleRate, freq, gainDb,
true);
407 return matchedShelf(sampleRate, freq, gainDb,
false);
419 freq = std::clamp(freq, 1.0, std::max(1.0, sampleRate * 0.499));
422 slope = std::clamp(slope, 0.0001, 1.0);
423 const double A = std::pow(10.0, gainDb / 40.0);
424 const double w0 = 2.0 * std::numbers::pi * freq / sampleRate;
425 const double cosw0 = std::cos(w0);
426 const double sinw0 = std::sin(w0);
427 const double radicand = (A + 1.0 / A) * (1.0 / slope - 1.0) + 2.0;
428 const double alpha = sinw0 / 2.0 * std::sqrt(std::max(0.0, radicand));
429 const double twoSqrtAAlpha = 2.0 * std::sqrt(A) * alpha;
431 const double a0 = (A + 1.0) + (A - 1.0) * cosw0 + twoSqrtAAlpha;
433 A * ((A + 1.0) - (A - 1.0) * cosw0 + twoSqrtAAlpha),
434 2.0 * A * ((A - 1.0) - (A + 1.0) * cosw0),
435 A * ((A + 1.0) - (A - 1.0) * cosw0 - twoSqrtAAlpha),
436 -2.0 * ((A - 1.0) + (A + 1.0) * cosw0),
437 (A + 1.0) + (A - 1.0) * cosw0 - twoSqrtAAlpha);
449 freq = std::clamp(freq, 1.0, std::max(1.0, sampleRate * 0.499));
452 slope = std::clamp(slope, 0.0001, 1.0);
453 const double A = std::pow(10.0, gainDb / 40.0);
454 const double w0 = 2.0 * std::numbers::pi * freq / sampleRate;
455 const double cosw0 = std::cos(w0);
456 const double sinw0 = std::sin(w0);
457 const double radicand = (A + 1.0 / A) * (1.0 / slope - 1.0) + 2.0;
458 const double alpha = sinw0 / 2.0 * std::sqrt(std::max(0.0, radicand));
459 const double twoSqrtAAlpha = 2.0 * std::sqrt(A) * alpha;
461 const double a0 = (A + 1.0) - (A - 1.0) * cosw0 + twoSqrtAAlpha;
463 A * ((A + 1.0) + (A - 1.0) * cosw0 + twoSqrtAAlpha),
464 -2.0 * A * ((A - 1.0) + (A + 1.0) * cosw0),
465 A * ((A + 1.0) + (A - 1.0) * cosw0 - twoSqrtAAlpha),
466 2.0 * ((A - 1.0) - (A + 1.0) * cosw0),
467 (A + 1.0) - (A - 1.0) * cosw0 - twoSqrtAAlpha);
478 freq = std::clamp(freq, 1.0, std::max(1.0, sampleRate * 0.499));
479 Q = std::max(Q, 0.001);
480 const double w0 = 2.0 * std::numbers::pi * freq / sampleRate;
481 const double cosw0 = std::cos(w0);
482 const double sinw0 = std::sin(w0);
483 const double alpha = sinw0 / (2.0 * Q);
485 const double a0 = 1.0 + alpha;
502 freq = std::clamp(freq, 1.0, std::max(1.0, sampleRate * 0.499));
503 Q = std::max(Q, 0.001);
504 const double w0 = 2.0 * std::numbers::pi * freq / sampleRate;
505 const double cosw0 = std::cos(w0);
506 const double sinw0 = std::sin(w0);
507 const double alpha = sinw0 / (2.0 * Q);
509 const double a0 = 1.0 + alpha;
531 frequency = std::clamp(frequency, 1.0, std::max(1.0, sampleRate * 0.499));
532 const double w = std::tan(std::numbers::pi * frequency / sampleRate);
533 const double n = 1.0 / (1.0 + w);
538 c.
a1 = (w - 1.0) * n;
554 frequency = std::clamp(frequency, 1.0, std::max(1.0, sampleRate * 0.499));
555 const double w = std::tan(std::numbers::pi * frequency / sampleRate);
556 const double n = 1.0 / (1.0 + w);
561 c.
a1 = (w - 1.0) * n;
580 pivotFreq = std::clamp(pivotFreq, 1.0, std::max(1.0, sampleRate * 0.499));
581 const double g = std::pow(10.0, gainDb / 20.0);
582 const double sqrtG = std::sqrt(g);
583 const double c = std::tan(std::numbers::pi * pivotFreq / sampleRate);
588 const double norm = 1.0 / (1.0 + sqrtG * c);
591 coeffs.
b0 = (sqrtG + c) * norm;
592 coeffs.
b1 = (c - sqrtG) * norm;
594 coeffs.
a1 = (sqrtG * c - 1.0) * norm;
609 const double G = 3.999843853973347;
610 const double Q = 0.7071752369554196;
611 const double fc = 1681.9744509555319;
612 const double K = std::tan(std::numbers::pi * fc / sampleRate);
613 const double Vh = std::pow(10.0, G / 20.0);
614 const double Vb = std::pow(Vh, 0.4996667741545416);
615 const double a0 = 1.0 + K / Q + K * K;
617 c.
b0 = (Vh + Vb * K / Q + K * K) / a0;
618 c.
b1 = 2.0 * (K * K - Vh) / a0;
619 c.
b2 = (Vh - Vb * K / Q + K * K) / a0;
620 c.
a1 = 2.0 * (K * K - 1.0) / a0;
621 c.
a2 = (1.0 - K / Q + K * K) / a0;
634 const double Q = 0.5003270373238773;
635 const double fc = 38.13547087602444;
636 const double K = std::tan(std::numbers::pi * fc / sampleRate);
637 const double a0 = 1.0 + K / Q + K * K;
642 c.
a1 = 2.0 * (K * K - 1.0) / a0;
643 c.
a2 = (1.0 - K / Q + K * K) / a0;
683 [[nodiscard]]
double getMagnitude(
double frequency,
double sampleRate)
const noexcept
685 const double w = 2.0 * std::numbers::pi * frequency / sampleRate;
686 const double cosW = std::cos(w);
687 const double cos2W = std::cos(2.0 * w);
688 const double sinW = std::sin(w);
689 const double sin2W = std::sin(2.0 * w);
691 const double nRe =
b0 +
b1 * cosW +
b2 * cos2W;
692 const double nIm = -
b1 * sinW -
b2 * sin2W;
693 const double dRe = 1.0 +
a1 * cosW +
a2 * cos2W;
694 const double dIm = -
a1 * sinW -
a2 * sin2W;
696 const double numMag2 = nRe * nRe + nIm * nIm;
697 const double denMag2 = dRe * dRe + dIm * dIm;
699 return (denMag2 > 1e-30) ? std::sqrt(numMag2 / denMag2) : 0.0;
714 template <
typename U>
716 std::span<U> magnitudes,
717 double sampleRate)
const noexcept
719 assert(magnitudes.size() >= frequencies.size());
720 for (
size_t i = 0; i < frequencies.size(); ++i)
721 magnitudes[i] =
static_cast<U
>(
getMagnitude(
static_cast<double>(frequencies[i]), sampleRate));
727 struct MatchedPoles {
double a1, a2, A0, A1, A2, phi0, phi1, phi2; };
728 [[nodiscard]]
static MatchedPoles matchedPoles(
double w0,
double q)
noexcept
731 m.a2 = std::exp(-2.0 * q * w0);
733 m.a1 = -2.0 * std::exp(-q * w0) * std::cos(std::sqrt(1.0 - q * q) * w0);
738 const double r = std::sqrt(q * q - 1.0);
739 m.a1 = -(std::exp(-w0 / (q + r)) + std::exp(-(q + r) * w0));
741 m.A0 = (1.0 + m.a1 + m.a2) * (1.0 + m.a1 + m.a2);
742 m.A1 = (1.0 - m.a1 + m.a2) * (1.0 - m.a1 + m.a2);
744 const double s = std::sin(0.5 * w0);
746 m.phi0 = 1.0 - m.phi1;
747 m.phi2 = 4.0 * m.phi0 * m.phi1;
754 [[nodiscard]]
static BiquadCoeffs matchedShelf(
double sampleRate,
double freq,
double gainDb,
bool low)
noexcept
756 freq = std::clamp(freq, 1.0, std::max(1.0, sampleRate * 0.499));
757 if (!(std::abs(gainDb) >= 0.01))
return { 1.0, 0.0, 0.0, 0.0, 0.0 };
758 constexpr double kPi = std::numbers::pi;
759 const double G = std::pow(10.0, (low ? -gainDb : gainDb) / 20.0);
760 const double fc = freq / (0.5 * sampleRate);
761 const double fc2 = fc * fc, fc4 = fc2 * fc2;
762 const double hNy = (fc4 + G) / (fc4 + 1.0 / G);
763 const double f1 = fc / std::sqrt(0.160 + 1.543 * fc2);
764 const double f2 = fc / std::sqrt(0.947 + 3.806 * fc2);
765 double ph1 = std::sin(0.5 * kPi * f1); ph1 *= ph1;
766 double ph2 = std::sin(0.5 * kPi * f2); ph2 *= ph2;
767 const double f14 = f1 * f1 * f1 * f1, f24 = f2 * f2 * f2 * f2;
768 const double h1 = (fc4 + f14 * G) / (fc4 + f14 / G);
769 const double h2 = (fc4 + f24 * G) / (fc4 + f24 / G);
770 const double d1 = (h1 - 1.0) * (1.0 - ph1), d2 = (h2 - 1.0) * (1.0 - ph2);
771 const double c11 = -ph1 * d1, c21 = -ph2 * d2;
772 const double c12 = (hNy - h1) * ph1 * ph1, c22 = (hNy - h2) * ph2 * ph2;
773 const double alpha1 = (c22 * d1 - c12 * d2) / (c11 * c22 - c12 * c21);
774 const double alpha2 = (d1 - c11 * alpha1) / c12;
775 const double beta1 = alpha1, beta2 = hNy * alpha2;
776 const double A1 = alpha2, A2 = 0.25 * (alpha1 - alpha2);
777 const double B1 = beta2, B2 = 0.25 * (beta1 - beta2);
778 const double V = 0.5 * (1.0 + std::sqrt(std::max(0.0, A1)));
779 const double Wn = 0.5 * (1.0 + std::sqrt(std::max(0.0, B1)));
780 const double a0 = 0.5 * (V + std::sqrt(std::max(0.0, V * V + A2)));
781 const double b0 = 0.5 * (Wn + std::sqrt(std::max(0.0, Wn * Wn + B2)));
782 const double scale = low ? std::pow(10.0, gainDb / 20.0) : 1.0;
784 c.b0 = scale *
b0 / a0;
785 c.b1 = scale * (1.0 - Wn) / a0;
786 c.b2 = scale * (-0.25 * B2 /
b0) / a0;
787 c.a1 = (1.0 - V) / a0;
788 c.a2 = (-0.25 * A2 / a0) / a0;
796 [[nodiscard]]
static BiquadCoeffs normalise(
double a0,
double b0r,
double b1r,
double b2r,
797 double a1r,
double a2r)
noexcept
799 const double invA0 = 1.0 / a0;
800 return { b0r * invA0, b1r * invA0, b2r * invA0, a1r * invA0, a2r * invA0 };
851template <
typename T,
int MaxChannels = 8>
865 : activeCoeffs_(other.activeCoeffs_),
866 coeffsDirty_(other.coeffsDirty_.load(std::memory_order_relaxed)),
870 copyStagedRelaxed(other);
875 if (
this == &other)
return *
this;
876 activeCoeffs_ = other.activeCoeffs_;
877 copyStagedRelaxed(other);
878 coeffsDirty_.store(other.coeffsDirty_.load(std::memory_order_relaxed),
879 std::memory_order_relaxed);
880 coeffsSeq_.store(0, std::memory_order_relaxed);
881 state_ = other.state_;
920 coeffsSeq_.fetch_add(1, std::memory_order_acq_rel);
921 std::atomic_thread_fence(std::memory_order_release);
922 stagedCoeffs_.b0.store(c.b0, std::memory_order_relaxed);
923 stagedCoeffs_.b1.store(c.b1, std::memory_order_relaxed);
924 stagedCoeffs_.b2.store(c.b2, std::memory_order_relaxed);
925 stagedCoeffs_.a1.store(c.a1, std::memory_order_relaxed);
926 stagedCoeffs_.a2.store(c.a2, std::memory_order_relaxed);
927 coeffsSeq_.fetch_add(1, std::memory_order_release);
928 coeffsDirty_.store(
true, std::memory_order_release);
988 if (!coeffsDirty_.exchange(
false, std::memory_order_acquire))
return false;
989 return adoptStagedCoeffs();
1005 for (
auto& s : state_)
1051 assert(channel >= 0 && channel < MaxChannels &&
"Channel index out of bounds");
1057 if (coeffsDirty_.load(std::memory_order_relaxed)) [[unlikely]]
1060 auto& s = state_[channel];
1062 const double output = activeCoeffs_.
b0 * input + s.z1;
1063 s.z1 = activeCoeffs_.
b1 * input - activeCoeffs_.
a1 * output + s.z2;
1064 s.z2 = activeCoeffs_.
b2 * input - activeCoeffs_.
a2 * output;
1095 assert(channel >= 0 && channel < MaxChannels &&
"Channel index out of bounds");
1097 auto& s = state_[channel];
1098 double z1 = s.z1, z2 = s.z2;
1099 double b0 = c0.b0, b1 = c0.b1, b2 = c0.b2, a1 = c0.a1, a2 = c0.a2;
1100 for (
int k = 0; k < n; ++k)
1102 b0 += step.b0; b1 += step.b1; b2 += step.b2; a1 += step.a1; a2 += step.a2;
1103 const double input = data[k];
1104 const double output = b0 * input + z1;
1105 z1 = b1 * input - a1 * output + z2;
1106 z2 = b2 * input - a2 * output;
1111 activeCoeffs_.
b0 = b0;
1112 activeCoeffs_.
b1 = b1;
1113 activeCoeffs_.
b2 = b2;
1114 activeCoeffs_.
a1 = a1;
1115 activeCoeffs_.
a2 = a2;
1153 const int numChannels = std::min(buffer.getNumChannels(), MaxChannels);
1154 const int numSamples = buffer.getNumSamples();
1161 if constexpr (kPairChannels)
1162 for (; ch + 1 < numChannels; ch += 2)
1163 processPair<simd::kVecDoubleNarrow>(buffer.getChannel(ch), buffer.getChannel(ch + 1),
1164 numSamples, c, state_[ch], state_[ch + 1]);
1166 for (; ch < numChannels; ++ch)
1168 T* data = buffer.getChannel(ch);
1169 auto& s = state_[ch];
1173 for (
int i = 0; i < numSamples; ++i)
1175 const double input =
static_cast<double>(data[i]);
1176 const double output = c.
b0 * input + z1;
1177 z1 = c.
b1 * input - c.
a1 * output + z2;
1178 z2 = c.
b2 * input - c.
a2 * output;
1179 data[i] =
static_cast<T
>(output);
1182 recoverState(z1, z2);
1194 static constexpr int kSeqlockMaxAttempts = 3;
1224 DSPARK_NOINLINE
bool adoptStagedCoeffs() noexcept
1227 bool adopted =
false;
1228 for (
int attempt = 0; attempt < kSeqlockMaxAttempts; ++attempt)
1230 const unsigned s0 = coeffsSeq_.load(std::memory_order_acquire);
1236 if ((s0 & 1u) != 0u)
continue;
1241 tmp.
b0 = stagedCoeffs_.b0.load(std::memory_order_relaxed);
1242 tmp.
b1 = stagedCoeffs_.b1.load(std::memory_order_relaxed);
1243 tmp.
b2 = stagedCoeffs_.b2.load(std::memory_order_relaxed);
1244 tmp.
a1 = stagedCoeffs_.a1.load(std::memory_order_relaxed);
1245 tmp.
a2 = stagedCoeffs_.a2.load(std::memory_order_relaxed);
1253 std::atomic_thread_fence(std::memory_order_acquire);
1254 if (s0 == coeffsSeq_.load(std::memory_order_relaxed))
1263 coeffsDirty_.store(
true, std::memory_order_release);
1266 activeCoeffs_ = tmp;
1281 static void recoverState(
double& z1,
double& z2)
noexcept
1283 if (!std::isfinite(z1 + z2)) { z1 = 0.0; z2 = 0.0; }
1288 static constexpr bool kPairChannels = simd::kVecDoubleNarrow == 2 &&
sizeof(
void*) == 8;
1292 static void processPair(T* left, T* right,
int numSamples,
const BiquadCoeffs& c,
1293 State& sl, State& sr)
noexcept
1295 using O = simd::Vec<double, W>;
1296 const auto b0 = O::set1(c.b0), b1 = O::set1(c.b1), b2 = O::set1(c.b2);
1297 const auto a1 = O::set1(c.a1), a2 = O::set1(c.a2);
1298 auto z1 = O::set2(sl.z1, sr.z1);
1299 auto z2 = O::set2(sl.z2, sr.z2);
1300 for (
int i = 0; i < numSamples; ++i)
1302 const auto x = O::set2(
static_cast<double>(left[i]),
static_cast<double>(right[i]));
1303 const auto y = O::add(O::mul(b0, x), z1);
1304 z1 = O::add(O::sub(O::mul(b1, x), O::mul(a1, y)), z2);
1305 z2 = O::sub(O::mul(b2, x), O::mul(a2, y));
1306 left[i] =
static_cast<T
>(O::lane0(y));
1307 right[i] =
static_cast<T
>(O::lane1(y));
1309 double l1 = O::lane0(z1), l2 = O::lane0(z2), r1 = O::lane1(z1), r2 = O::lane1(z2);
1310 recoverState(l1, l2);
1311 recoverState(r1, r2);
1312 sl.z1 = l1; sl.z2 = l2;
1313 sr.z1 = r1; sr.z2 = r2;
1327 std::atomic<double> b0{1.0}, b1{0.0}, b2{0.0};
1328 std::atomic<double> a1{0.0}, a2{0.0};
1332 void copyStagedRelaxed(
const Biquad& other)
noexcept
1334 stagedCoeffs_.b0.store(other.stagedCoeffs_.b0.load(std::memory_order_relaxed),
1335 std::memory_order_relaxed);
1336 stagedCoeffs_.b1.store(other.stagedCoeffs_.b1.load(std::memory_order_relaxed),
1337 std::memory_order_relaxed);
1338 stagedCoeffs_.b2.store(other.stagedCoeffs_.b2.load(std::memory_order_relaxed),
1339 std::memory_order_relaxed);
1340 stagedCoeffs_.a1.store(other.stagedCoeffs_.a1.load(std::memory_order_relaxed),
1341 std::memory_order_relaxed);
1342 stagedCoeffs_.a2.store(other.stagedCoeffs_.a2.load(std::memory_order_relaxed),
1343 std::memory_order_relaxed);
1346 BiquadCoeffs activeCoeffs_ {};
1347 StagedCoeffs stagedCoeffs_ {};
1348 std::atomic<bool> coeffsDirty_{
false};
1349 std::atomic<unsigned> coeffsSeq_{0};
1351 std::array<State, MaxChannels> state_ {};
Non-owning view over audio channel data.
Biquad filter using Transposed Direct Form II (TDF-II) with thread-safe updates.
Biquad(const Biquad &)=delete
void setCoeffs(const BiquadCoeffs &c) noexcept
Sets the filter coefficients asynchronously (control thread).
void setCoeffsNow(const BiquadCoeffs &c) noexcept
Stream-owner direct set: makes c the active set immediately.
void reset() noexcept
Resets all per-channel filter states to zero to avoid ringing/clicks.
const BiquadCoeffs & getCoeffs() const noexcept
Returns the active coefficient set currently in use by the DSP thread.
bool applyPendingCoeffs() noexcept
Promotes any pending staged coefficients to active.
Biquad & operator=(Biquad &&other) noexcept
void processBlock(AudioBufferView< T > buffer) noexcept
Processes a full audio buffer in-place.
Biquad & operator=(const Biquad &)=delete
void processRampCore(double *data, int n, int channel, const BiquadCoeffs &c0, const BiquadCoeffs &step) noexcept
Runs n samples of one channel through the filter while the coefficients move linearly,...
T processSample(T input, int channel) noexcept
Processes a single sample for a specific channel.
double processSampleCore(double input, int channel) noexcept
One recursion step in the core's own precision (double).
Biquad() noexcept=default
RAII scope guard to disable denormalised (subnormal) floating-point numbers.
Main namespace for the DSPark framework.
Stores normalised biquad coefficients (b0, b1, b2, a1, a2), always double.
static BiquadCoeffs makeFirstOrderHighPass(double sampleRate, double frequency) noexcept
First-order (6 dB/oct) high-pass filter.
static BiquadCoeffs makePeakMatched(double sampleRate, double freq, double Q, double gainDb) noexcept
Analog-matched ("de-cramped") peaking filter (Vicanek design).
void getMagnitudeForFrequencyArray(std::span< const U > frequencies, std::span< U > magnitudes, double sampleRate) const noexcept
Computes magnitude responses for a batch of frequencies.
static BiquadCoeffs makeHighPass(double sampleRate, double freq, double Q=0.7071067811865476) noexcept
High-pass filter.
static BiquadCoeffs makeHighShelfMatched(double sampleRate, double freq, double gainDb) noexcept
Analog-matched Butterworth high shelf (see makeLowShelfMatched()).
static BiquadCoeffs makeAllPass(double sampleRate, double freq, double Q=0.7071067811865476) noexcept
All-pass filter.
static BiquadCoeffs makeBandPass(double sampleRate, double freq, double Q=0.7071067811865476) noexcept
Band-pass filter (constant 0 dB peak gain).
static BiquadCoeffs makeLowShelfMatched(double sampleRate, double freq, double gainDb) noexcept
Analog-matched Butterworth low shelf (Vicanek, "Matched Two-Pole Digital Shelving Filters",...
static BiquadCoeffs makePeak(double sampleRate, double freq, double Q, double gainDb) noexcept
Peak (parametric EQ) filter.
static BiquadCoeffs makeKWeightingHighPass(double sampleRate) noexcept
ITU-R BS.1770 K-weighting, stage 2: the RLB high-pass.
double getMagnitude(double frequency, double sampleRate) const noexcept
Evaluates magnitude response |H(f)| at a single frequency.
static BiquadCoeffs makeFirstOrderLowPass(double sampleRate, double frequency) noexcept
First-order (6 dB/oct) low-pass filter.
static BiquadCoeffs makeTilt(double sampleRate, double pivotFreq, double gainDb) noexcept
Creates a first-order tilt filter.
static BiquadCoeffs makeKWeightingShelf(double sampleRate) noexcept
ITU-R BS.1770 K-weighting, stage 1: the head-related high shelf.
BiquadCoeffs bypassed() const noexcept
The set with these poles whose numerator equals its denominator.
static BiquadCoeffs makeLowPass(double sampleRate, double freq, double Q=0.7071067811865476) noexcept
Low-pass filter.
static BiquadCoeffs makeBandPassMatched(double sampleRate, double freq, double Q=0.7071067811865476) noexcept
Analog-matched band-pass, 0 dB at the centre (Vicanek 2016).
static BiquadCoeffs makeLowShelf(double sampleRate, double freq, double gainDb, double slope=1.0) noexcept
Low-shelf filter.
static BiquadCoeffs makeLowPassMatched(double sampleRate, double freq, double Q=0.7071067811865476) noexcept
Analog-matched second-order low-pass (Vicanek, "Matched Second Order Digital Filters",...
static BiquadCoeffs makeHighShelf(double sampleRate, double freq, double gainDb, double slope=1.0) noexcept
High-shelf filter.
static BiquadCoeffs makeHighPassMatched(double sampleRate, double freq, double Q=0.7071067811865476) noexcept
Analog-matched second-order high-pass (Vicanek 2016).
static BiquadCoeffs makeNotch(double sampleRate, double freq, double Q=0.7071067811865476) noexcept
Notch (band-reject) filter.