50#include "AudioBuffer.h"
52#include "DenormalGuard.h"
70template <
typename T,
int MaxChannels = 8>
94 constexpr double kLimit = 1.0 - 1e-12;
96 const double k2 = std::clamp(b.a2, -kLimit, kLimit);
97 const double k1 = std::clamp(b.a1 / (1.0 + k2), -kLimit, kLimit);
99 const double a1 = k1 * (1.0 + k2);
100 const double a2 = k2;
101 l.
s1 = k1; l.
c1 = std::sqrt(1.0 - k1 * k1);
102 l.
s2 = k2; l.
c2 = std::sqrt(1.0 - k2 * k2);
104 const double middle = b.b1 - b.b2 * a1;
105 l.
v1 = middle / l.
c2;
106 l.
v0 = (b.b0 - b.b2 * a2 - middle * k1) / (l.
c1 * l.
c2);
119 b.
a1 = l.s1 * (1.0 + l.s2);
121 b.
b1 = l.v2 * b.
a1 + l.v1 * l.c2;
122 b.
b0 = l.v2 * b.
a2 + l.v1 * l.c2 * l.s1 + l.v0 * l.c1 * l.c2;
149 if (samples <= 0) {
setCoeffs(target);
return; }
152 const double inverse = 1.0 /
static_cast<double>(samples);
153 step_.
c1 = (target_.
c1 - current_.
c1) * inverse; step_.
s1 = (target_.
s1 - current_.
s1) * inverse;
154 step_.
c2 = (target_.
c2 - current_.
c2) * inverse; step_.
s2 = (target_.
s2 - current_.
s2) * inverse;
155 step_.
v0 = (target_.
v0 - current_.
v0) * inverse; step_.
v1 = (target_.
v1 - current_.
v1) * inverse;
156 step_.
v2 = (target_.
v2 - current_.
v2) * inverse;
163 [[nodiscard]]
bool isRamping() const noexcept {
return rampLeft_ > 0; }
168 for (
auto& each : state_) each = {};
174 const State& s = state_[
static_cast<size_t>(std::clamp(channel, 0, MaxChannels - 1))];
175 return s.x1 * s.x1 + s.x2 * s.x2;
189 const int numChannels = std::min(buffer.getNumChannels(), MaxChannels);
190 const int numSamples = buffer.getNumSamples();
191 if (numChannels <= 0 || numSamples <= 0)
return;
194 const int moving = std::min(rampLeft_, numSamples);
196 for (
int ch = 0; ch < numChannels; ++ch)
198 T* data = buffer.getChannel(ch);
199 State& s = state_[
static_cast<size_t>(ch)];
200 double x1 = s.x1, x2 = s.x2;
202 for (
int i = 0; i < moving; ++i)
206 data[i] =
static_cast<T
>(advance(
static_cast<double>(data[i]), c, x1, x2));
208 if (moving > 0 && moving == rampLeft_) c = target_;
209 for (
int i = moving; i < numSamples; ++i)
210 data[i] =
static_cast<T
>(advance(
static_cast<double>(data[i]), c, x1, x2));
211 if (!std::isfinite(x1 + x2)) { x1 = 0.0; x2 = 0.0; }
230 assert(channel >= 0 && channel < MaxChannels &&
"Channel index out of bounds");
231 State& s = state_[
static_cast<size_t>(channel)];
232 return advance(input, current_, s.x1, s.x2);
251 assert(channel >= 0 && channel < MaxChannels &&
"Channel index out of bounds");
252 if (numSamples <= 0)
return;
253 State& s = state_[
static_cast<size_t>(channel)];
254 double x1 = s.x1, x2 = s.x2;
255 const int moving = std::min(rampLeft_, numSamples);
257 for (
int i = 0; i < moving; ++i)
261 data[i] = advance(data[i], c, x1, x2);
263 if (moving > 0 && moving == rampLeft_) c = target_;
264 for (
int i = moving; i < numSamples; ++i)
265 data[i] = advance(data[i], c, x1, x2);
266 if (!std::isfinite(x1 + x2)) { x1 = 0.0; x2 = 0.0; }
277 if (rampLeft_ <= 0 || samples <= 0)
return;
278 if (samples >= rampLeft_)
284 rampLeft_ -= samples;
285 for (
int i = 0; i < samples; ++i)
287 current_.
c1 += step_.
c1; current_.
s1 += step_.
s1; current_.
c2 += step_.
c2; current_.
s2 += step_.
s2;
288 current_.
v0 += step_.
v0; current_.
v1 += step_.
v1; current_.
v2 += step_.
v2;
300 static double advance(
double u,
const Coeffs& c,
double& x1,
double& x2)
noexcept
302 const double f1 = c.c2 * u - c.s2 * x2;
303 const double g2 = c.s2 * u + c.c2 * x2;
304 const double f0 = c.c1 * f1 - c.s1 * x1;
305 const double g1 = c.s1 * f1 + c.c1 * x1;
308 return c.v0 * f0 + c.v1 * g1 + c.v2 * g2;
315 std::array<State, MaxChannels> state_ {};
360template <
typename T,
int Order,
int MaxChannels = 8>
363 static_assert(Order >= 1 && Order <= 16,
"orders 1 to 16");
369 std::array<double, Order + 1>
c {};
370 std::array<double, Order + 1>
s {};
371 std::array<double, Order + 1>
v {};
380 [[nodiscard]]
static bool design(
const double* b,
const double* a,
Coeffs& out)
noexcept
382 if (!(std::abs(a[0]) > 0.0))
return false;
383 std::array<double, Order + 1> poly {}, num {};
384 for (
int i = 0; i <= Order; ++i)
386 poly[
static_cast<size_t>(i)] = a[i] / a[0];
387 num[
static_cast<size_t>(i)] = b[i] / a[0];
390 std::array<std::array<double, Order + 1>, Order + 1> stepped {};
391 stepped[
static_cast<size_t>(Order)] = poly;
393 for (
int m = Order; m >= 1; --m)
395 const auto& am = stepped[
static_cast<size_t>(m)];
396 const double k = am[
static_cast<size_t>(m)];
397 if (!(std::abs(k) < 1.0))
return false;
398 l.s[
static_cast<size_t>(m)] = k;
399 l.c[
static_cast<size_t>(m)] = std::sqrt((1.0 - k) * (1.0 + k));
400 auto& below = stepped[
static_cast<size_t>(m - 1)];
401 const double inverse = 1.0 / ((1.0 - k) * (1.0 + k));
402 for (
int i = 0; i < m; ++i)
403 below[
static_cast<size_t>(i)] = (am[
static_cast<size_t>(i)] - k * am[
static_cast<size_t>(m - i)]) * inverse;
407 std::array<double, Order + 1> prod {};
408 for (
int m = Order; m >= 0; --m)
410 prod[
static_cast<size_t>(m)] = above;
411 if (m >= 1) above *= l.c[
static_cast<size_t>(m)];
413 for (
int m = Order; m >= 0; --m)
415 const double w = num[
static_cast<size_t>(m)];
416 l.v[
static_cast<size_t>(m)] = w / prod[
static_cast<size_t>(m)];
417 const auto& am = stepped[
static_cast<size_t>(m)];
418 for (
int i = 0; i <= m; ++i) num[static_cast<size_t>(i)] -= w * am[
static_cast<size_t>(m - i)];
425 bool setCoeffs(
const double* b,
const double* a)
noexcept
428 if (!
design(b, a, l))
return false;
429 current_ = target_ = l;
435 bool rampCoeffs(
const double* b,
const double* a,
int samples)
noexcept
438 if (!
design(b, a, l))
return false;
441 current_ = target_ = l;
447 const double inverse = 1.0 /
static_cast<double>(samples);
448 for (
int m = 0; m <= Order; ++m)
450 const auto i =
static_cast<size_t>(m);
451 step_.
c[i] = (l.
c[i] - current_.
c[i]) * inverse;
452 step_.
s[i] = (l.
s[i] - current_.
s[i]) * inverse;
453 step_.
v[i] = (l.
v[i] - current_.
v[i]) * inverse;
459 [[nodiscard]]
bool isRamping() const noexcept {
return rampLeft_ > 0; }
464 for (
auto& s : state_) s = {};
470 if (channel < 0 || channel >= MaxChannels)
return 0.0;
472 for (
double x : state_[
static_cast<size_t>(channel)]) e += x * x;
479 const int numChannels = std::min(buffer.getNumChannels(), MaxChannels);
480 const int numSamples = buffer.getNumSamples();
481 if (numChannels <= 0 || numSamples <= 0)
return;
483 const int moving = std::min(rampLeft_, numSamples);
487 for (
int ch = 0; ch < numChannels; ch += 2)
489 const bool pair = ch + 1 < numChannels;
490 auto& left = state_[
static_cast<size_t>(ch)];
491 auto& right = state_[
static_cast<size_t>(pair ? ch + 1 : ch)];
493 if constexpr (kPairChannels)
497 runLanes<simd::kVecDoubleNarrow>(buffer.getChannel(ch), buffer.getChannel(ch + 1), numSamples,
498 moving, c, left, right);
503 runLanes<1>(buffer.getChannel(ch),
nullptr, numSamples, moving, c, left, left);
507 runLanes<1>(buffer.getChannel(ch + 1),
nullptr, numSamples, moving, c, right, right);
522 assert(channel >= 0 && channel < MaxChannels &&
"Channel index out of bounds");
523 return advance(input, current_, state_[
static_cast<size_t>(channel)]);
529 if (rampLeft_ <= 0 || samples <= 0)
return;
530 if (samples >= rampLeft_)
536 rampLeft_ -= samples;
537 const double n =
static_cast<double>(samples);
538 for (
int m = 0; m <= Order; ++m)
540 const auto k =
static_cast<size_t>(m);
541 current_.
c[k] += step_.
c[k] * n;
542 current_.
s[k] += step_.
s[k] * n;
543 current_.
v[k] += step_.
v[k] * n;
548 using State = std::array<double, Order + 1>;
551 static double advance(
double u,
const Coeffs& c, State& x)
noexcept
553 std::array<double, Order + 1> g {};
555 for (
int m = Order; m >= 1; --m)
557 const auto k =
static_cast<size_t>(m);
558 const double xm = x[k];
559 g[k] = c.s[k] * f + c.c[k] * xm;
560 f = c.c[k] * f - c.s[k] * xm;
562 double y = c.v[0] * f;
563 for (
int m = 1; m <= Order; ++m) y += c.v[
static_cast<size_t>(m)] * g[
static_cast<size_t>(m)];
564 for (
int m = Order; m >= 2; --m) x[
static_cast<size_t>(m)] = g[
static_cast<size_t>(m - 1)];
569 static constexpr bool kPairChannels = simd::kVecDoubleNarrow == 2 &&
sizeof(
void*) == 8;
576 void runLanes(T* left, T* right,
int numSamples,
int moving, Coeffs& c, State& memLeft, State& memRight)
noexcept
578 using O = simd::Vec<double, W>;
579 using V =
typename O::V;
580 const auto lanes = [](
double a,
double b)
noexcept -> V {
581 if constexpr (W == 1) { (void)b;
return a; }
582 else return O::set2(a, b);
585 for (
int m = 1; m <= Order; ++m)
586 x[
static_cast<size_t>(m)] = lanes(memLeft[
static_cast<size_t>(m)], memRight[
static_cast<size_t>(m)]);
588 V vc[Order + 1] {}, vs[Order + 1] {}, vv[Order + 1] {};
589 const auto broadcast = [&]()
noexcept {
590 for (
int m = 0; m <= Order; ++m)
592 const auto k =
static_cast<size_t>(m);
593 vc[k] = O::set1(c.c[k]);
594 vs[k] = O::set1(c.s[k]);
595 vv[k] = O::set1(c.v[k]);
598 const auto sample = [&](
int i)
noexcept {
600 if constexpr (W == 1) f =
static_cast<double>(left[i]);
601 else f = lanes(
static_cast<double>(left[i]),
static_cast<double>(right[i]));
603 for (
int m = Order; m >= 1; --m)
605 const auto k =
static_cast<size_t>(m);
607 g[k] = O::mulAdd(vs[k], f, vc[k], xm);
608 f = O::mulSub(vc[k], f, vs[k], xm);
610 V y = O::mul(vv[0], f);
611 for (
int m = 1; m <= Order; ++m) y = O::madd(vv[static_cast<size_t>(m)], g[
static_cast<size_t>(m)], y);
612 for (
int m = Order; m >= 2; --m) x[
static_cast<size_t>(m)] = g[
static_cast<size_t>(m - 1)];
614 if constexpr (W == 1) left[i] =
static_cast<T
>(y);
617 left[i] =
static_cast<T
>(O::lane0(y));
618 right[i] =
static_cast<T
>(O::lane1(y));
621 for (
int i = 0; i < moving; ++i)
623 for (
int m = 0; m <= Order; ++m)
625 const auto k =
static_cast<size_t>(m);
626 c.c[k] += step_.
c[k];
627 c.s[k] += step_.
s[k];
628 c.v[k] += step_.
v[k];
633 if (moving > 0 && moving == rampLeft_) c = target_;
635 for (
int i = moving; i < numSamples; ++i) sample(i);
638 for (
int m = 1; m <= Order; ++m)
640 const auto k =
static_cast<size_t>(m);
641 if constexpr (W == 1) memLeft[k] = x[k];
644 memLeft[k] = O::lane0(x[k]);
645 memRight[k] = O::lane1(x[k]);
647 sum += memLeft[k] + memRight[k];
649 if (!std::isfinite(sum))
656 Coeffs current_ {}, target_ {}, step_ {};
658 std::array<State, MaxChannels> state_ {};
Non-owning view over audio channel data.
RAII scope guard to disable denormalised (subnormal) floating-point numbers.
A whole filter of order N as one normalized ladder (Gray and Markel), designed from its polynomials,...
bool isRamping() const noexcept
void processBlock(AudioBufferView< T > buffer) noexcept
Runs a block, in place; a ramp moves on by its samples. Channels beyond MaxChannels are left untouche...
bool setCoeffs(const double *b, const double *a) noexcept
Takes a filter b(z) / a(z) at once; false (nothing changes) if it is not stable.
double getStateEnergy(int channel) const noexcept
The energy of a channel's memory (the sum of the squares of its states).
Coeffs getLadder() const noexcept
void reset() noexcept
Clears the memory of every channel.
bool rampCoeffs(const double *b, const double *a, int samples) noexcept
Moves every rotation and tap linearly to a filter's over samples; false if it is not stable.
void advanceRamp(int samples) noexcept
Moves a ramp on by a number of samples (for processSample() callers).
double processSample(double input, int channel) noexcept
One sample of one channel, in double, with the coefficients as they are (a ramp moves on with advance...
static bool design(const double *b, const double *a, Coeffs &out) noexcept
The ladder of a digital filter b(z) / a(z), each Order + 1 coefficients from z^0 (a[0] scales both).
One second-order section in normalized ladder form, per channel.
void reset() noexcept
Clears the memory of every channel. Coefficients and ramp are kept.
static BiquadCoeffs toBiquad(const Coeffs &l) noexcept
The biquad a ladder realises (for curves and checks).
void processSegment(double *data, int numSamples, int channel) noexcept
Runs one channel's samples through the section, in double and in place, walking the ramp from where i...
Coeffs getLadder() const noexcept
Rotations and taps in use now (mid ramp, where the ramp is).
void setCoeffs(const BiquadCoeffs &b) noexcept
Makes a biquad's response the section's own from the next sample; ends a ramp.
void setLadder(const Coeffs &l) noexcept
Sets rotations and taps directly from the next sample; ends a ramp.
void advanceRamp(int samples) noexcept
Moves the ramp on by a number of samples (for processSample() callers).
void rampCoeffs(const BiquadCoeffs &target, int samples) noexcept
Moves the rotations and the taps linearly to those of a biquad.
double processSample(double input, int channel) noexcept
One sample of one channel, in double, with the coefficients as they are.
void processBlock(AudioBufferView< T > buffer) noexcept
Runs a block through the section, in place.
double getStateEnergy(int channel) const noexcept
The energy in a channel's memory, x1^2 + x2^2 (it only falls with no input).
bool isRamping() const noexcept
Whether the coefficients are moving.
static Coeffs fromBiquad(const BiquadCoeffs &b) noexcept
The ladder of a biquad.
Main namespace for the DSPark framework.
Stores normalised biquad coefficients (b0, b1, b2, a1, a2), always double.
The ladder's rotations and taps.
std::array< double, Order+1 > v
v_0 .. v_N, the taps.
std::array< double, Order+1 > s
s_1 .. s_N = the reflection coefficients.
std::array< double, Order+1 > c
c_1 .. c_N (index 0 unused).
The two rotations and the three taps (the identity by default).
double s2
Outer rotation: s2 = k2 = a2.
double s1
Inner rotation: s1 = k1 = a1 / (1 + a2).
double v2
Taps on f0, g1 and g2.