17#include "../DspMath.h"
55 c.
ig = T(1) / (T(1) + g);
70template <LadderTap Tap, FloatType T>
75 const T Sest = c.
G3 * c.ig * z[0]
85 fbSignal =
fastTanh(fbSignal * drive) / drive;
88 const T u = (input - c.k * fbSignal) * c.invFbDen;
93 std::array<T, 4> y {};
95 for (
int i = 0; i < 4; ++i)
97 const T v = (x - z[i]) * c.G;
116 return u - T(4)*y[0] + T(6)*y[1] - T(4)*y[2] + y[3];
152template <FloatType T>
157 c.
k = 4.0 *
static_cast<double>(res);
158 c.
d =
static_cast<double>(drive);
164 c.
tol = std::is_same_v<T, float> ? 1e-4 : 1e-7;
165 const double ig = 1.0 / (1.0 + c.
g);
171 c.
invLoop = 1.0 / (1.0 + c.
k * (c.
G * c.
G) * (c.
G * c.
G));
184 constexpr double kClamp = 6.297019181717114;
185 if (a > kClamp)
return 1.0;
186 if (a < -kClamp)
return -1.0;
187 const double a2 = a * a;
188 const double num = 34459425.0 + a2 * (4729725.0 + a2 * (135135.0 + a2 * (990.0 + a2)));
189 const double den = 34459425.0 + a2 * (16216200.0 + a2 * (945945.0 + a2 * (13860.0 + a2 * 45.0)));
190 return a * num / den;
196 constexpr double kClamp = 6.297019181717114;
197 const double m = a < 0.0 ? -a : a;
198 if (m > kClamp)
return 1.0 / m;
199 const double a2 = a * a;
200 const double num = 34459425.0 + a2 * (4729725.0 + a2 * (135135.0 + a2 * (990.0 + a2)));
201 const double den = 34459425.0 + a2 * (16216200.0 + a2 * (945945.0 + a2 * (13860.0 + a2 * 45.0)));
277template <LadderTap Tap, FloatType T>
280 const double x =
static_cast<double>(input);
281 const double g = c.
g, k = c.k, d = c.d, gd = c.gOverD;
282 const double s0 =
static_cast<double>(z[0]), s1 =
static_cast<double>(z[1]);
283 const double s2 =
static_cast<double>(z[2]), s3 =
static_cast<double>(z[3]);
291 double u = 0.0, y0 = s0, y1 = s1, y2 = s2, y3 = s3;
297 const double G = c.G, H = 1.0 - G;
298 const double h0 = H * s0, h1 = H * s1, h2 = H * s2, h3 = H * s3;
299 u = (x - k * ((c.w0 * s0 + c.w1 * s1) + (c.w2 * s2 + c.w3 * s3))) * c.invLoop;
300 const double G2 = G * G;
301 const double l0 = G * u + h0;
302 const double l1 = G2 * u + (G * h0 + h1);
303 const double l2 = (G2 * G) * u + (G2 * h0 + (G * h1 + h2));
304 const double l3 = (G2 * G2) * u + ((G2 * G) * h0 + (G2 * h1 + (G * h2 + h3)));
305 const double reach = std::max(std::max(std::abs(u), std::abs(l0)),
306 std::max(std::max(std::abs(l1), std::abs(l2)), std::abs(l3))) * d;
308 y0 = l0; y1 = l1; y2 = l2; y3 = l3;
315 const double i1 = 1.0 / (1.0 + g * r1), i2 = 1.0 / (1.0 + g * r2);
316 const double i3 = 1.0 / (1.0 + g * r3), i4 = 1.0 / (1.0 + g * r4);
317 const double a1 = g * r0 * i1, a2 = g * r1 * i2, a3 = g * r2 * i3, a4 = g * r3 * i4;
318 const double b1 = s0 * i1, b2 = s1 * i2, b3 = s2 * i3, b4 = s3 * i4;
319 const double A = a1 * a2 * a3 * a4;
320 const double B = ((b1 * a2 + b2) * a3 + b3) * a4 + b4;
321 u = (x - k * B) / (1.0 + k * A);
335 const double p0 = 1.0 - t0 * t0, p1 = 1.0 - t1 * t1, p2 = 1.0 - t2 * t2;
336 const double p3 = 1.0 - t3 * t3, p4 = 1.0 - t4 * t4;
339 const double q1 = 1.0 / (1.0 + g * p1), q2 = 1.0 / (1.0 + g * p2);
340 const double q3 = 1.0 / (1.0 + g * p3), q4 = 1.0 / (1.0 + g * p4);
341 const double e1 = y0 - s0 - gd * (t0 - t1);
342 const double e2 = y1 - s1 - gd * (t1 - t2);
343 const double e3 = y2 - s2 - gd * (t2 - t3);
344 const double e4 = y3 - s3 - gd * (t3 - t4);
346 const double P1 = -e1 * q1, Q1 = -g * k * p0 * q1;
347 const double P2 = (g * p1 * P1 - e2) * q2, Q2 = g * p1 * Q1 * q2;
348 const double P3 = (g * p2 * P2 - e3) * q3, Q3 = g * p2 * Q2 * q3;
349 const double P4 = (g * p3 * P3 - e4) * q4, Q4 = g * p3 * Q3 * q4;
350 const double d4 = P4 / (1.0 - Q4);
351 const double d1 = P1 + Q1 * d4, d2 = P2 + Q2 * d4, d3 = P3 + Q3 * d4;
352 double m = std::max(std::max(std::abs(d1), std::abs(d2)),
353 std::max(std::abs(d3), std::abs(d4))) * d;
354 const double scale = m > 1.0 ? 1.0 / m : 1.0;
359 if (m < c.tol)
break;
363 z[0] =
static_cast<T
>(2.0 * y0 - s0);
364 z[1] =
static_cast<T
>(2.0 * y1 - s1);
365 z[2] =
static_cast<T
>(2.0 * y2 - s2);
366 z[3] =
static_cast<T
>(2.0 * y3 - s3);
374 else out = u - 4.0 * y0 + 6.0 * y1 - 4.0 * y2 + y3;
375 return static_cast<T
>(out);
382#if defined(_MSC_VER) && !defined(__clang__)
383 #define DSPARK_LADDER_INLINE __forceinline
384#elif defined(__GNUC__) || defined(__clang__)
385 #define DSPARK_LADDER_INLINE inline __attribute__((always_inline))
387 #define DSPARK_LADDER_INLINE inline
404 const double a4 = a2 * a2;
405 const double a6 = a4 * a2;
406 num = ((34459425.0 + 4729725.0 * a2) + 135135.0 * a4) + a6 * (990.0 + a2);
407 den = ((34459425.0 + 16216200.0 * a2) + 945945.0 * a4) + a6 * (13860.0 + 45.0 * a2);
413 double G = 0.0,
H = 1.0;
445 h.
ahead = ((c.cAhead[0] * h.q[0] + c.cAhead[1] * h.q[1]) + c.cAhead[2] * h.q[2]) + c.cAhead[3] * h.q[3];
446 h.loopStates = c.kd * ((c.cS[0] * h.q[0] + c.cS[1] * h.q[1]) + (c.cS[2] * h.q[2] + c.cS[3] * h.q[3]));
464template <FloatType T>
468 const double ratio = cutoffRatio > 1e-6 ? std::min(cutoffRatio, 0.499) : 1e-6;
472 const double G2 = c.
G * c.
G;
473 const double power[5] = { 1.0, c.
G, G2, G2 * c.
G, G2 * G2 };
474 const double k = 4.0 *
static_cast<double>(res);
475 c.
d =
static_cast<double>(drive);
483 double a[4][4] = {}, b[4] = {};
484 for (
int i = 0; i < 4; ++i)
486 for (
int j = 0; j < 4; ++j)
487 a[i][j] = j < i ? 2.0 * power[i - j] * c.
H : (j == i ? 1.0 - 2.0 * c.
G : 0.0);
488 b[i] = 2.0 * power[i + 1];
489 c.
cS[i] = c.
H * power[3 - i];
492 double perUnit = 0.0;
493 for (
int j = 0; j < 4; ++j)
496 for (
int i = 0; i < 4; ++i) sum += c.
cS[i] * a[i][j];
498 perUnit += c.
cS[j] * b[j];
500 c.
kappa = k * perUnit;
518 template <FloatType T>
521 const double xPrev = h.input;
522 x = (1.0 + c.pre) *
static_cast<double>(input) - c.pre * xPrev;
533 R = (c.d *
xs - h.loopStates) - h.loopDrive;
538 template <LadderTap Tap>
551 const double r2 =
R *
R;
556 const double nd = nu * de;
557 const double den = c.onePlusC * (de * de) - (c.c * r2) * (nu * nu);
559 kw = ((c.kappa *
R) * nd) / den;
563 wk =
R > 0.0 ? 1.0 : -1.0;
564 kw =
R > 0.0 ? c.kappa : -c.kappa;
570 const double w = wk * c.invD;
571 const double y0 = c.G * w + c.H *
q0;
572 const double y1 = c.G * y0 + c.H *
q1;
573 const double y2 = c.G * y1 + c.H *
q2;
574 const double y3 = c.G * y2 + c.H *
q3;
584 acc += ((w - 4.0 * y0) + 6.0 * y1) + (y3 - 4.0 * y2);
591 ahead = ((c.cAhead[0] *
q0 + c.cAhead[1] *
q1) + c.cAhead[2] *
q2) + c.cAhead[3] *
q3;
595 template <FloatType T>
608 return static_cast<T
>(y);
643template <LadderTap Tap, FloatType T>
647 lane.
begin(input, h, c);
649 return lane.
finish<T>(h, c);
665template <LadderTap Tap, FloatType T>
681#undef DSPARK_LADDER_INLINE
constexpr double kLadderStagesLinearStartLimit
Largest stage level of the linear prediction, in knee widths, at which ladderStepStages() starts Newt...
double ladderTanh(double a) noexcept
tanh(a) by its [9/8] continued-fraction convergent, clamped to +-1 where the convergent reaches 1 (|a...
constexpr int kLadderInputSubsteps
Sub-steps per sample of the input-saturating ladder.
constexpr int kLadderStagesMaxIterations
Newton step cap of ladderStepStages() (the worst case measured is 10).
constexpr double kLadderStagesMaxGain
Largest prewarped integrator gain of the per-stage model: tan(0.45 pi), a cutoff of 0....
constexpr double kLadderTanhLimit
Where ladderTanh()'s rational function reaches 1, and that squared.
DSPARK_LADDER_INLINE void ladderStepInputPair(T &a, T &b, LadderInputState &ha, LadderInputState &hb, const LadderInputCoeffs &c) noexcept
ladderStepInput() on two channels at once, sub-step by sub-step.
void ladderInputRefresh(LadderInputState &h, const LadderInputCoeffs &c) noexcept
Recomputes the carried sums after the states or the coefficients changed.
double ladderTanhRatio(double a) noexcept
ladderTanh(a) / a, finite at a = 0 (where it is 1).
LadderInputCoeffs makeLadderInputCoeffs(double cutoffRatio, T res, T drive) noexcept
Derives the coefficients of the input-saturating ladder.
constexpr double kLadderStagesStateStartGain
Integrator gain below which ladderStepStages() falls back to the integrator states instead of the fix...
void ladderTanhParts(double a2, double &num, double &den) noexcept
The two polynomials of ladderTanh() in a^2 (tanh(a) = a num / den), each summed as two halves so that...
T ladderStep(T input, std::array< T, 4 > &z, const LadderCoeffs< T > &c, T drive) noexcept
Advances the four TPT one-pole stages and the resolved feedback loop by one sample.
LadderStageCoeffs makeLadderStageCoeffs(T g, T res, T drive) noexcept
Derives the coefficients of the per-stage saturating ladder.
DSPARK_LADDER_INLINE T ladderStepInput(T input, LadderInputState &h, const LadderInputCoeffs &c) noexcept
Advances the input-saturating ladder by one sample.
constexpr double kLadderTanhLimit2
LadderCoeffs< T > makeLadderCoeffs(T g, T res) noexcept
Derives the ladder coefficients.
T ladderStepStages(T input, std::array< T, 4 > &z, const LadderStageCoeffs &c) noexcept
Advances the per-stage saturating ladder by one sample.
LadderTap
Output tap mix of the four-stage cascade.
T fastTanh(T x) noexcept
Fast tanh approximation using a Pade [5,4] rational function.
Coefficients derived from g and the resonance.
Coefficients of ladderStepStages(), derived by makeLadderStageCoeffs().