42#include "../Core/AudioBuffer.h"
43#include "../Core/AudioSpec.h"
44#include "../Core/Biquad.h"
45#include "../Core/BiquadCascade.h"
46#include "../Core/DryWetMixer.h"
47#include "../Core/DspMath.h"
48#include "../Core/Interpolation.h"
49#include "../Core/Oversampling.h"
50#include "../Core/Smoothers.h"
51#include "../Core/AnalogRandom.h"
52#include "../Core/SpscQueue.h"
53#include "../Core/SpinLock.h"
54#include "../Core/StateBlob.h"
62#include "../Core/detail/Adaa.h"
63#include "../Core/detail/LogCosh.h"
77template <
typename SampleType>
class Saturation;
85 static constexpr int kAaCh = 16;
93 virtual void reset() noexcept = 0;
124 const int o =
antialias_.load(std::memory_order_relaxed);
155 const unsigned bit = 1u << (c & (
kAaCh - 1));
156 if ((
armMask_ & bit) == 0u)
return false;
164 const auto c =
static_cast<size_t>(ch & (
kAaCh - 1));
166 const unsigned bit = 1u << c;
172 const T out = line[
static_cast<size_t>(n - 1)];
173 for (
int k =
kMaxDelay - 1; k > 0; --k) line[
static_cast<size_t>(k)] = line[
static_cast<size_t>(k - 1)];
187 constexpr T kOuter = T(-0.0824), kInner = T(0.5824);
188 const auto c =
static_cast<size_t>(ch & (
kAaCh - 1));
190 const unsigned bit = 1u << c;
196 const T out = kOuter * (y + line[2]) + kInner * (line[0] + line[1]);
205 for (
auto& line :
alignHold_) line.fill(T(0));
206 for (
auto& line :
halfHold_) line.fill(T(0));
223 std::array<double, SaturationAlgorithm<T>::kAaCh> prevX_ {};
224 std::array<detail::Adaa2, SaturationAlgorithm<T>::kAaCh> adaa2_ {};
225 std::array<double, SaturationAlgorithm<T>::kAaCh> prevBias_ {};
231 for (
auto& a : adaa2_) a.reset();
241 const T x = sample * drive;
242 const T bias = character * T(0.3);
246 const double u =
static_cast<double>(x) +
static_cast<double>(bias);
247 if (this->
takeArm(c)) [[unlikely]]
249 adaa2_[
static_cast<size_t>(c)].
reset(u);
250 prevBias_[
static_cast<size_t>(c)] =
static_cast<double>(bias);
252 const double y = adaa2_[
static_cast<size_t>(c)].
template process<detail::AdaaTanh>(u);
253 const double tb = std::tanh(prevBias_[
static_cast<size_t>(c)]);
254 prevBias_[
static_cast<size_t>(c)] =
static_cast<double>(bias);
255 prevX_[
static_cast<size_t>(c)] =
static_cast<double>(x);
256 return static_cast<T
>(y - tb);
261 T x = sample * drive;
262 T bias = character * T(0.3);
269 prevX_[
static_cast<size_t>(c)] =
static_cast<double>(x);
274 const double xd =
static_cast<double>(x);
275 const double b =
static_cast<double>(bias);
276 const double x0 = prevX_[
static_cast<size_t>(c)];
277 prevX_[
static_cast<size_t>(c)] = xd;
278 const double tb = std::tanh(b);
279 const double dx = xd - x0;
280 if (std::abs(dx) > 1e-7)
281 return static_cast<T
>((
logCosh(xd + b) -
logCosh(x0 + b)) / dx - tb);
282 return static_cast<T
>(std::tanh(0.5 * (xd + x0) + b) - tb);
298 std::array<double, SaturationAlgorithm<T>::kAaCh> prevX_ {};
299 std::array<detail::Adaa2, SaturationAlgorithm<T>::kAaCh> adaa2_ {};
300 std::array<double, SaturationAlgorithm<T>::kAaCh> prevBias_ {};
307 for (
auto& a : adaa2_) a.reset();
317 const T x = sample * drive;
318 const T b = T(-0.7) * (T(0.15) + character * T(0.5));
322 const double u =
static_cast<double>(x) +
static_cast<double>(b);
323 if (this->
takeArm(c)) [[unlikely]]
325 adaa2_[
static_cast<size_t>(c)].
reset(u);
326 prevBias_[
static_cast<size_t>(c)] =
static_cast<double>(b);
328 const double y = adaa2_[
static_cast<size_t>(c)].
template process<detail::AdaaTanh>(u);
329 const double tb = std::tanh(prevBias_[
static_cast<size_t>(c)]);
330 prevBias_[
static_cast<size_t>(c)] =
static_cast<double>(b);
331 prevX_[
static_cast<size_t>(c)] =
static_cast<double>(x);
332 return static_cast<T
>((y - tb) / (1.0 - tb * tb));
337 const T x = sample * drive;
338 const T b = T(-0.7) * (T(0.15) + character * T(0.5));
343 prevX_[
static_cast<size_t>(c)] =
static_cast<double>(x);
345 return (
fastTanh(x + b) - tb) / (T(1) - tb * tb);
349 const double xd =
static_cast<double>(x);
350 const double bd =
static_cast<double>(b);
351 const double tb = std::tanh(bd);
352 const double norm = 1.0 / (1.0 - tb * tb);
353 const double x0 = prevX_[
static_cast<size_t>(c)];
354 prevX_[
static_cast<size_t>(c)] = xd;
355 const double dx = xd - x0;
356 if (std::abs(dx) > 1e-7)
357 return static_cast<T
>(((
logCosh(xd + bd) -
logCosh(x0 + bd)) / dx - tb) * norm);
358 return static_cast<T
>((std::tanh(0.5 * (xd + x0) + bd) - tb) * norm);
370 std::array<double, SaturationAlgorithm<T>::kAaCh> prevX_ {};
371 std::array<detail::Adaa2, SaturationAlgorithm<T>::kAaCh> adaa2_ {};
372 std::array<double, SaturationAlgorithm<T>::kAaCh> prevBias_ {};
375 static inline double g(
double u)
noexcept
377 const double a = std::abs(u);
378 return (a <= 1.0) ? 0.5 * u * u : a - 0.5;
385 for (
auto& a : adaa2_) a.reset();
395 const T bias = character * T(0.3);
396 const T d = sample * drive;
399 const double u =
static_cast<double>(d) +
static_cast<double>(bias);
400 if (this->
takeArm(c)) [[unlikely]]
402 adaa2_[
static_cast<size_t>(c)].
reset(u);
403 prevBias_[
static_cast<size_t>(c)] =
static_cast<double>(bias);
405 const double y = adaa2_[
static_cast<size_t>(c)].
template process<detail::AdaaHardClip>(u);
406 const double cbPrev = std::clamp(prevBias_[
static_cast<size_t>(c)], -1.0, 1.0);
407 prevBias_[
static_cast<size_t>(c)] =
static_cast<double>(bias);
408 prevX_[
static_cast<size_t>(c)] =
static_cast<double>(d);
409 return static_cast<T
>(y - cbPrev);
414 const T bias = character * T(0.3);
415 const T cb = std::clamp(bias, T(-1), T(1));
416 const T d = sample * drive;
421 prevX_[
static_cast<size_t>(c)] =
static_cast<double>(d);
422 return std::clamp(d + bias, T(-1), T(1)) - cb;
425 const double dd =
static_cast<double>(d), b =
static_cast<double>(bias);
426 const double d0 = prevX_[
static_cast<size_t>(c)];
427 prevX_[
static_cast<size_t>(c)] = dd;
428 const double u = dd + b, u0 = d0 + b, diff = dd - d0;
429 if (std::abs(diff) > 1e-7)
430 return static_cast<T
>((g(u) - g(u0)) / diff -
static_cast<double>(cb));
431 return static_cast<T
>(std::clamp(0.5 * (u + u0), -1.0, 1.0) -
static_cast<double>(cb));
463 const double r = std::sqrt(
A *
A + 3.0 *
kB);
464 crit[0] = (
A - r) / (3.0 *
kB);
465 crit[1] = (
A + r) / (3.0 *
kB);
470 [[nodiscard]]
double p(
double u)
const noexcept {
return u * (1.0 + u * (
A -
kB * u)); }
471 [[nodiscard]]
double f(
double u)
const noexcept
473 return std::clamp(
p(std::clamp(u, -
kLimit,
kLimit)), -1.0, 1.0);
477 [[nodiscard]]
double average(
double u0,
double u1,
double u2)
const noexcept
479 double lo = u0, mid = u1, hi = u2;
480 if (lo > mid) std::swap(lo, mid);
481 if (mid > hi) std::swap(mid, hi);
482 if (lo > mid) std::swap(lo, mid);
483 const double span = hi - lo;
484 if (!(span > 1e-9))
return f((u0 + u1 + u2) * (1.0 / 3.0));
487 double top = std::max(
p(lo),
p(hi)), bottom = std::min(
p(lo),
p(hi));
488 for (
int k = 0; k < 2; ++k)
494 if (top <= 1.0 && bottom >= -1.0)
497 const double h1 = u0 + u1 + u2;
498 const double h2 = u0 * u0 + u1 * u1 + u2 * u2 + u0 * u1 + u1 * u2 + u0 * u2;
499 const double h3 = u0 * u0 * u0 + u1 * u1 * u1 + u2 * u2 * u2
500 + u0 * u0 * (u1 + u2) + u1 * u1 * (u0 + u2) + u2 * u2 * (u0 + u1)
502 return h1 * (1.0 / 3.0) +
A * h2 * (1.0 / 6.0) -
kB * h3 * (1.0 / 10.0);
504 if (bottom >= 1.0)
return 1.0;
505 if (top <= -1.0)
return -1.0;
514 if (edge > lo && edge < hi) cuts[n++] = edge;
515 const double a = std::max(lo, -
kLimit), b = std::min(hi,
kLimit);
518 double piece[4] = { a, 0.0, 0.0, 0.0 };
520 for (
int k = 0; k < 2; ++k)
521 if (
crit[k] > a &&
crit[k] < b) piece[pieces++] =
crit[k];
523 for (
int k = 0; k < pieces; ++k)
524 for (
const double level : { -1.0, 1.0 })
526 double s = piece[k], e = piece[k + 1];
527 double fs =
p(s) - level, fe =
p(e) - level;
528 if (!(fs * fe < 0.0))
continue;
529 double t = s + (e - s) * fs / (fs - fe);
530 for (
int it = 0; it < 60; ++it)
532 const double ft =
p(t) - level;
533 if (ft == 0.0)
break;
534 if ((ft < 0.0) == (fs < 0.0)) { s = t; fs = ft; }
else { e = t; fe = ft; }
535 const double slope = 1.0 + t * (2.0 *
A - 3.0 *
kB * t);
536 double next = t - ft / slope;
537 if (!(next > s && next < e)) next = 0.5 * (s + e);
538 if (std::abs(next - t) <= 1e-15 * (1.0 + std::abs(t))) { t = next;
break; }
541 if (t > lo && t < hi) cuts[n++] = t;
545 for (
int k = 1; k < n; ++k)
546 for (
int j = k; j > 0 && cuts[j - 1] > cuts[j]; --j) std::swap(cuts[j - 1], cuts[j]);
550 const bool rising = mid - lo > 1e-12 * span, falling = hi - mid > 1e-12 * span;
551 const double riseSlope = rising ? 2.0 / (span * (mid - lo)) : 0.0;
552 const double fallSlope = falling ? -2.0 / (span * (hi - mid)) : 0.0;
554 for (
int k = 0; k + 1 < n; ++k)
556 const double s = cuts[k], e = cuts[k + 1];
557 if (!(e > s))
continue;
558 const double c = 0.5 * (s + e), hh = 0.5 * (e - s);
559 const bool up = c < mid;
560 if ((up && !rising) || (!up && !falling))
continue;
561 const double w0 = up ? riseSlope * (c - lo) : -fallSlope * (hi - c);
562 const double w1 = up ? riseSlope : fallSlope;
563 double g0, g1 = 0.0, g2 = 0.0, g3 = 0.0;
564 if (std::abs(c) >
kLimit) g0 =
f(c);
568 if (g0 > 1.0 || g0 < -1.0) g0 = g0 > 1.0 ? 1.0 : -1.0;
571 g1 = 1.0 + c * (2.0 *
A - 3.0 *
kB * c);
572 g2 =
A - 3.0 *
kB * c;
576 const double h3 = hh * hh * hh;
577 sum += 2.0 * hh * g0 * w0 + (2.0 / 3.0) * h3 * (g2 * w0 + g1 * w1)
578 + 0.4 * h3 * hh * hh * g3 * w1;
587 std::array<double, SaturationAlgorithm<T>::kAaCh> u1_ {}, u2_ {};
605 const double u =
static_cast<double>(sample) *
static_cast<double>(drive);
607 if (this->
takeArm(
static_cast<int>(c))) [[unlikely]] u1_[c] = u2_[c] = u;
608 curve_.
setCharacter(0.25 *
static_cast<double>(character));
609 const double y = curve_.
average(u, u1_[c], u2_[c]);
612 return static_cast<T
>(y);
617 T x = std::clamp(sample * drive, T(-10), T(10));
620 T result = x + character * T(0.25) * x2 - T(0.15) * x3;
621 return std::clamp(result, T(-1), T(1));
629 static constexpr int kMaxCh = 16;
630 std::array<T, kMaxCh> lastX_ {};
631 std::array<detail::Adaa2, kMaxCh> adaa2_ {};
632 std::array<double, kMaxCh> prevBias_ {};
639 for (
auto& a : adaa2_) a.reset();
660 const T bias = character * (pi<T> / T(4));
661 const T x = sample * drive + bias;
662 const int c = ch & (kMaxCh - 1);
665 if (this->
takeArm(c)) [[unlikely]]
667 adaa2_[
static_cast<size_t>(c)].
reset(
static_cast<double>(x));
668 prevBias_[
static_cast<size_t>(c)] =
static_cast<double>(bias);
670 const double y = adaa2_[
static_cast<size_t>(c)].
template process<detail::AdaaSine>(
static_cast<double>(x));
671 const double sbPrev = std::sin(prevBias_[
static_cast<size_t>(c)]);
672 prevBias_[
static_cast<size_t>(c)] =
static_cast<double>(bias);
673 lastX_[
static_cast<size_t>(c)] = x;
674 return static_cast<T
>(y - sbPrev);
679 const T bias = character * (pi<T> / T(4));
680 const T x = sample * drive + bias;
681 const int c = ch & (kMaxCh - 1);
682 const T sb = std::sin(bias);
688 const T half = T(0.5) * (x - lastX_[
static_cast<size_t>(c)]);
689 const T mid = T(0.5) * (x + lastX_[
static_cast<size_t>(c)]);
690 const T sinc = (std::abs(half) > T(1e-4)) ? std::sin(half) / half
691 : T(1) - half * half / T(6);
692 lastX_[
static_cast<size_t>(c)] = x;
693 return std::sin(mid) * sinc - sb;
705 uint32_t rngState_ = 0x9E3779B9u;
709 [[nodiscard]]
inline T nextRandom()
noexcept
711 rngState_ ^= rngState_ << 13;
712 rngState_ ^= rngState_ >> 17;
713 rngState_ ^= rngState_ << 5;
714 constexpr T scale = T(1) /
static_cast<T
>(0xFFFFFFFFu);
715 return static_cast<T
>(rngState_) * scale - T(0.5);
721 if (rngState_ == 0) rngState_ = 1;
728 T clamped = std::clamp(drive, T(1), T(100));
729 T bitDepth =
mapRange(clamped, T(1), T(100), T(16), T(2));
730 steps_ = std::pow(T(2), bitDepth);
731 invSteps_ = T(1) / steps_;
737 T dither = (nextRandom() - nextRandom()) * invSteps_;
738 return invSteps_ * std::round((sample + dither) * steps_);
776 static void langevin(
double x,
double& L,
double& Lp)
noexcept
780 const double u = x * x;
781 double l = kL[12], lp = kLp[12];
782 for (
int n = 11; n >= 0; --n) { l = l * u + kL[n]; lp = lp * u + kLp[n]; }
787 const double e = std::exp(-2.0 * x), om = 1.0 - e;
788 L = (1.0 + e) / om - 1.0 / x;
789 Lp = 1.0 / (x * x) - 4.0 * e / (om * om);
793 void antiderivatives(
double x,
double L,
double& G,
double& P,
double& Q)
const noexcept
797 const double u = x * x, x3 = u * x;
798 double g = kG[12], pp = kP[12], q = kQ[12];
799 for (
int n = 11; n >= 0; --n) { g = g * u + kG[n]; pp = pp * u + kP[n]; q = q * u + kQ[n]; }
805 G = x + std::log((1.0 - std::exp(-2.0 * x)) / (2.0 * x));
806 double a = 0.0, b = 0.0;
810 const auto k =
static_cast<size_t>((std::bit_cast<std::uint64_t>(x) >> 49) - kFirstCell);
811 const double t = (x - mid_[k]) * invHalf_[k];
812 a = clenshaw(a_[k], t);
813 b = clenshaw(b_[k], t);
815 P = x * (1.0 + G) - 0.5 * x * x - kPiSquaredOver12 + a;
816 Q = x - L - 2.0 * ((x - G) - kLn2 + 1.0 / x + cR_ + b);
820 static constexpr int kDeg = 11, kCells = 48;
821 static constexpr double kStart = 0.5, kEnd = 32.0;
822 static constexpr std::uint64_t kFirstCell = 0x1FF0u;
823 static constexpr double kPiSquaredOver12 = 0.82246703342411321824;
824 static constexpr double kLn2 = 0.69314718055994530942;
828 static constexpr double kL[13] = { 0.3333333333333333, -0.022222222222222223, 0.0021164021164021165,
829 -0.00021164021164021165, 2.1377799155576935e-05, -2.1644042808063972e-06, 2.1925947851873778e-07,
830 -2.2214608789979678e-08, 2.2507846516808994e-09, -2.2805151204592183e-10, 2.3106432599002624e-11,
831 -2.3411706819824882e-12, 2.3721017400233653e-13 };
832 static constexpr double kLp[13] = { 0.3333333333333333, -0.06666666666666667, 0.010582010582010581,
833 -0.0014814814814814814, 0.0001924001924001924, -2.380844708887037e-05, 2.8503732207435913e-06,
834 -3.332191318496952e-07, 3.8263339078575285e-08, -4.332978728872515e-09, 4.852350845790551e-10,
835 -5.3846925685597234e-11, 5.930254350058414e-12 };
836 static constexpr double kG[13] = { 0.16666666666666666, -0.005555555555555556, 0.0003527336860670194,
837 -2.6455026455026456e-05, 2.1377799155576935e-06, -1.803670234005331e-07, 1.5661391322766983e-08,
838 -1.3884130493737299e-09, 1.2504359176004997e-10, -1.1402575602296091e-11, 1.0502923908637557e-12,
839 -9.754877841593701e-14, 9.123468230859098e-15 };
840 static constexpr double kP[13] = { 0.05555555555555555, -0.0011111111111111111, 5.039052658100277e-05,
841 -2.9394473838918285e-06, 1.9434362868706303e-07, -1.3874386415425623e-08, 1.0440927548511323e-09,
842 -8.167135584551352e-11, 6.581241671581577e-12, -5.429797905855281e-13, 4.5664886559293725e-14,
843 -3.9019511366374804e-15, 3.379062307725592e-16 };
844 static constexpr double kQ[13] = { 0.037037037037037035, -0.002962962962962963, 0.00027210884353741496,
845 -2.6128421190149586e-05, 2.5579323318621057e-06, -2.5299170598315896e-07, 2.5176556628643635e-08,
846 -2.5155828459962992e-09, 2.520569343665452e-10, -2.5307045227479064e-11, 2.5447507412853134e-12,
847 -2.5618698792252346e-13, 2.5814742395034203e-14 };
849 using Row = std::array<double, kDeg + 1>;
851 static double clenshaw(
const Row& c,
double t)
noexcept
853 double b1 = 0.0, b2 = 0.0;
854 const double t2 = 2.0 * t;
855 for (
int n = kDeg; n >= 1; --n)
857 const double b0 = c[
static_cast<size_t>(n)] + t2 * b1 - b2;
861 return c[0] + t * b1 - b2;
865 static double e1(
double z)
noexcept
867 double b = z + 1.0, c = 1e300, d = 1.0 / b, h = d;
868 for (
int i = 1; i < 300; ++i)
870 const double an = -
static_cast<double>(i) * i;
872 d = 1.0 / (an * d + b);
874 const double del = c * d;
876 if (std::abs(del - 1.0) < 4e-16)
break;
878 return h * std::exp(-z);
882 static double tail(
double x)
noexcept
885 for (
int k = 1; k < 400; ++k)
887 const double v = e1(2.0 * k * x);
889 if (v < 1e-25)
break;
896 for (
int k = 0; k < kCells; ++k)
898 const double lo = std::ldexp(1.0 + (k % 8) / 8.0, k / 8 - 1);
899 const double hi = lo + std::ldexp(1.0 / 8.0, k / 8 - 1);
900 mid_[
static_cast<size_t>(k)] = 0.5 * (lo + hi);
901 invHalf_[
static_cast<size_t>(k)] = 2.0 / (hi - lo);
902 double av[kDeg + 1], bv[kDeg + 1];
903 for (
int j = 0; j <= kDeg; ++j)
905 const double x = 0.5 * (lo + hi)
906 + 0.5 * (hi - lo) * std::cos(std::numbers::pi * (j + 0.5) / (kDeg + 1));
907 const double e = std::exp(-2.0 * x);
908 double li = 0.0, ek = e;
909 for (
int m = 1; m < 400 && ek > 1e-26; ++m, ek *= e)
910 li += ek / (
static_cast<double>(m) * m);
911 av[j] = 0.5 * li - x * std::log1p(-e);
912 bv[j] = std::log1p(-e) - tail(x);
914 for (
int n = 0; n <= kDeg; ++n)
916 double sa = 0.0, sb = 0.0;
917 for (
int j = 0; j <= kDeg; ++j)
919 const double w = std::cos(std::numbers::pi * n * (j + 0.5) / (kDeg + 1));
923 a_[
static_cast<size_t>(k)][
static_cast<size_t>(n)] = (n == 0 ? 1.0 : 2.0) * sa / (kDeg + 1);
924 b_[
static_cast<size_t>(k)][
static_cast<size_t>(n)] = (n == 0 ? 1.0 : 2.0) * sb / (kDeg + 1);
929 for (
int n = 12; n >= 0; --n) r = r * 0.25 + kL[n] / (2 * n + 1);
930 cR_ = 0.5 * r + kLn2 - 2.0 + tail(0.5);
933 std::array<Row, kCells> a_ {}, b_ {};
934 std::array<double, kCells> mid_ {}, invHalf_ {};
951 struct Point {
double H = 0.0,
x = 0.0,
f = 0.0,
fp = 0.0,
F1 = 0.0,
F2 = 0.0; };
959 void reset(
double u,
double alphaIn)
noexcept
968 double process(
double H,
double alphaIn)
noexcept
970 if (alphaIn !=
alpha)
988 a = 1.0 / (3.0 * (1.0 -
alpha));
993 [[nodiscard]]
Point point(
double H,
double seed)
const noexcept
995 const double s = H < 0.0 ? -1.0 : 1.0, h = std::abs(H);
996 double x = std::max(0.0, s * seed), L = 0.0, Lp = 0.0;
997 for (
int it = 0; it < 40; ++it)
1000 const double step = (
a * x -
rho * L - h) / (
a -
rho * Lp);
1001 x = std::max(0.0, x - step);
1002 if (!(std::abs(step) > 1e-15 * (1.0 + x)))
break;
1005 double G = 0.0, P = 0.0, Q = 0.0;
1011 p.
fp = Lp / (
a -
rho * Lp);
1012 p.
F1 =
a * G - 0.5 *
rho * L * L;
1013 p.
F2 = s * (
a *
a * P -
a *
rho * G * L + 0.5 *
a *
rho * Q +
rho *
rho * L * L * L * (1.0 / 6.0));
1019 const double d = p.H - q.H;
1020 if (std::abs(d) > 3e-3)
return (p.F2 - q.F2) / d;
1021 return 0.5 * (p.F1 + q.F1) - d * (p.f - q.f) * (1.0 / 12.0);
1026 const double d02 = p0.H -
p2.
H;
1027 if (std::abs(d02) > 1e-3)
return 2.0 * (
div(p0,
p1) -
div(
p1,
p2)) / d02;
1029 const double dd = 0.125 * d02 * d02;
1031 b.
H = 0.5 * (p0.H +
p2.
H);
1032 b.
fp = 0.5 * (p0.fp +
p2.
fp);
1033 b.
f = 0.5 * (p0.f +
p2.
f);
1034 b.
F1 = 0.5 * (p0.F1 +
p2.
F1) - dd * b.
fp;
1035 b.
F2 = 0.5 * (p0.F2 +
p2.
F2) - dd * b.
f;
1036 const double e = b.
H -
p1.
H;
1037 if (std::abs(e) > 1e-4)
return 2.0 * (b.
F1 -
div(b,
p1)) / e;
1043template <
typename T>
1046 static constexpr int kMaxCh = 16;
1047 std::array<Biquad<T, 1>, kMaxCh> preFilters_;
1048 std::array<Biquad<T, 1>, kMaxCh> postFilters_;
1049 std::array<T, kMaxCh> M_ {};
1050 std::array<AdaaTape, kMaxCh> kernel_ {};
1051 int numChannels_ = 0;
1052 T lastDrive_ = T(-1);
1053 double lastSampleRate_ = 0.0;
1058 static inline T langevin(T x)
noexcept
1060 const T ax = std::abs(x);
1064 return x * (T(1) / T(3) - x2 * (T(1) / T(45) - x2 * (T(2) / T(945))));
1066 if (ax > T(20))
return std::copysign(T(1), x) - T(1) / x;
1067 return T(1) / std::tanh(x) - T(1) / x;
1071 static inline T langevinDeriv(T x)
noexcept
1073 const T ax = std::abs(x);
1077 return T(1) / T(3) - x2 * (T(1) / T(15) - x2 * (T(2) / T(189)));
1079 if (ax > T(20))
return T(1) / (x * x);
1080 const T s = std::sinh(x);
1081 return T(1) / (x * x) - T(1) / (s * s);
1087 numChannels_ = std::min(spec.numChannels, kMaxCh);
1093 lastSampleRate_ = 0.0;
1098 for (
auto& f : preFilters_) f.reset();
1099 for (
auto& f : postFilters_) f.reset();
1109 if (drive == lastDrive_ && spec.sampleRate == lastSampleRate_)
return;
1111 lastSampleRate_ = spec.sampleRate;
1114 T bumpGain = T(1.5) + std::min(driveDb * T(0.05), T(3.0));
1116 auto lpFreq = std::max(6000.0, 19000.0 -
static_cast<double>(driveDb) * 200.0);
1119 for (
int ch = 0; ch < numChannels_; ++ch)
1121 preFilters_[ch].setCoeffs(peakCoeffs);
1122 postFilters_[ch].setCoeffs(lpCoeffs);
1143 const T filtered = preFilters_[ch].processSample(sample, 0);
1144 const T H = filtered * drive;
1148 const T alpha = T(0.35) + T(0.15) * character;
1149 const T a = T(1) / (T(3) * (T(1) - alpha));
1150 const T Ms = T(3) * a;
1153 for (
int it = 0; it < 3; ++it)
1155 const T x = (H + alpha * M) / a;
1156 const T f = M - Ms * langevin(x);
1157 const T fp = T(1) - T(3) * alpha * langevinDeriv(x);
1160 M = std::clamp(M, -Ms, Ms);
1163 return postFilters_[ch].processSample((T(1) - alpha) * M, 0);
1171 const T filtered = preFilters_[ch].processSample(sample, 0);
1172 const double H =
static_cast<double>(filtered) *
static_cast<double>(drive);
1173 const T alpha = T(0.35) + T(0.15) * character;
1174 auto& k = kernel_[
static_cast<size_t>(ch)];
1175 if (this->
takeArm(ch)) [[unlikely]] k.reset(H,
static_cast<double>(alpha));
1176 const double y = k.process(H,
static_cast<double>(alpha));
1177 M_[ch] =
static_cast<T
>(k.p1.f / (1.0 - k.alpha));
1178 return postFilters_[ch].processSample(
static_cast<T
>(y), 0);
1183template <
typename T>
1186 static constexpr int kMaxCh = 16;
1187 std::array<Biquad<T, 1>, kMaxCh> lpFilters_;
1188 std::array<detail::Adaa2, kMaxCh> adaaLow_ {}, adaaHigh_ {};
1189 std::array<double, kMaxCh> prevRestLow_ {}, prevRestHigh_ {};
1190 std::array<double, kMaxCh> firstLow_ {}, firstHigh_ {};
1191 std::array<double, kMaxCh> firstLowF_ {}, firstHighF_ {};
1192 int numChannels_ = 0;
1193 double lastSampleRate_ = 0.0;
1198 numChannels_ = std::min(spec.numChannels, kMaxCh);
1199 lastSampleRate_ = 0.0;
1204 for (
auto& f : lpFilters_) f.reset();
1205 for (
auto& a : adaaLow_) a.reset();
1206 for (
auto& a : adaaHigh_) a.reset();
1207 prevRestLow_.fill(0.0);
1208 prevRestHigh_.fill(0.0);
1209 firstLow_.fill(0.0);
1210 firstHigh_.fill(0.0);
1211 firstLowF_.fill(0.0);
1212 firstHighF_.fill(0.0);
1221 if (spec.sampleRate == lastSampleRate_)
return;
1222 lastSampleRate_ = spec.sampleRate;
1225 for (
int ch = 0; ch < numChannels_; ++ch)
1226 lpFilters_[ch].setCoeffs(c);
1243 const T low = lpFilters_[ch].processSample(sample, 0);
1244 const T high = sample - low;
1245 const T bias = character * T(0.2);
1246 const T kLo = drive * T(1.4);
1247 const T kHi = drive * T(0.85);
1248 const auto c =
static_cast<size_t>(ch & (kMaxCh - 1));
1251 const double uLo =
static_cast<double>((low + bias) * kLo);
1252 const double uHi =
static_cast<double>((high + bias) * kHi);
1253 if (this->
takeArm(
static_cast<int>(c))) [[unlikely]]
1254 armSecond(c, uLo, uHi, bias, kLo, kHi);
1255 return static_cast<T
>(secondBands(c, uLo, uHi, bias, kLo, kHi));
1261 inline void processBoth(T sample, T drive, T character,
int ch, T& first, T& second)
noexcept
1263 const T low = lpFilters_[ch].processSample(sample, 0);
1264 const T high = sample - low;
1265 const T bias = character * T(0.2);
1266 const T kLo = drive * T(1.4);
1267 const T kHi = drive * T(0.85);
1268 const auto c =
static_cast<size_t>(ch & (kMaxCh - 1));
1269 const double uLo =
static_cast<double>((low + bias) * kLo);
1270 const double uHi =
static_cast<double>((high + bias) * kHi);
1271 if (this->
takeArm(
static_cast<int>(c))) [[unlikely]]
1273 armSecond(c, uLo, uHi, bias, kLo, kHi);
1274 armFirst(c, uLo, uHi);
1276 first =
static_cast<T
>(firstBands(c, uLo, uHi, bias, kLo, kHi));
1277 second =
static_cast<T
>(secondBands(c, uLo, uHi, bias, kLo, kHi));
1281 void armSecond(
size_t c,
double uLo,
double uHi, T bias, T kLo, T kHi)
noexcept
1283 adaaLow_[c].reset(uLo);
1284 adaaHigh_[c].reset(uHi);
1285 prevRestLow_[c] =
static_cast<double>(bias * kLo);
1286 prevRestHigh_[c] =
static_cast<double>(bias * kHi);
1288 void armFirst(
size_t c,
double uLo,
double uHi)
noexcept
1291 firstHigh_[c] = uHi;
1293 firstHighF_[c] =
logCosh(uHi);
1295 double secondBands(
size_t c,
double uLo,
double uHi, T bias, T kLo, T kHi)
noexcept
1297 const double yLo = adaaLow_[c].template process<detail::AdaaTanh>(uLo) - std::tanh(prevRestLow_[c]);
1298 const double yHi = adaaHigh_[c].template process<detail::AdaaTanh>(uHi) - std::tanh(prevRestHigh_[c]);
1299 prevRestLow_[c] =
static_cast<double>(bias * kLo);
1300 prevRestHigh_[c] =
static_cast<double>(bias * kHi);
1301 return yLo / 1.4 + yHi / 0.85;
1305 double firstBands(
size_t c,
double uLo,
double uHi, T bias, T kLo, T kHi)
noexcept
1307 const auto mean = [](
double u,
double u0,
double f,
double f0)
noexcept {
1308 const double d = u - u0;
1309 return std::abs(d) > 1e-7 ? (f - f0) / d : std::tanh(0.5 * (u + u0));
1312 const double yLo = mean(uLo, firstLow_[c], fLo, firstLowF_[c]) - std::tanh(
static_cast<double>(bias * kLo));
1313 const double yHi = mean(uHi, firstHigh_[c], fHi, firstHighF_[c]) - std::tanh(
static_cast<double>(bias * kHi));
1315 firstHigh_[c] = uHi;
1316 firstLowF_[c] = fLo;
1317 firstHighF_[c] = fHi;
1318 return yLo / 1.4 + yHi / 0.85;
1329 const T low = lpFilters_[ch].processSample(sample, 0);
1330 const T high = sample - low;
1331 const T bias = character * T(0.2);
1332 const T kLo = drive * T(1.4);
1333 const T kHi = drive * T(0.85);
1334 const auto c =
static_cast<size_t>(ch & (kMaxCh - 1));
1335 const double uLo =
static_cast<double>((low + bias) * kLo);
1336 const double uHi =
static_cast<double>((high + bias) * kHi);
1337 if (this->
takeArm(
static_cast<int>(c))) [[unlikely]]
1338 armFirst(c, uLo, uHi);
1339 return static_cast<T
>(firstBands(c, uLo, uHi, bias, kLo, kHi));
1344 const T low = lpFilters_[ch].processSample(sample, 0);
1345 const T high = sample - low;
1346 const T bias = character * T(0.2);
1347 const T kLo = drive * T(1.4);
1348 const T kHi = drive * T(0.85);
1349 const T satLow = (
fastTanh((low + bias) * kLo) -
fastTanh(bias * kLo)) / T(1.4);
1350 const T satHigh = (
fastTanh((high + bias) * kHi) -
fastTanh(bias * kHi)) / T(0.85);
1351 return satLow + satHigh;
1356template <
typename T>
1359 static constexpr int kMaxCh = 16;
1360 std::array<Biquad<T, 1>, kMaxCh> aaFilters_;
1361 std::array<T, kMaxCh> lastSample_ {};
1362 std::array<int, kMaxCh> counter_ {};
1363 int numChannels_ = 0;
1369 numChannels_ = std::min(spec.numChannels, kMaxCh);
1374 for (
auto& f : aaFilters_) f.reset();
1375 lastSample_.fill(T(0));
1383 T clamped = std::clamp(drive, T(1), T(100));
1384 reduction_ = std::max(1,
static_cast<int>(
mapRange(clamped, T(1), T(100), T(1), T(50))));
1387 for (
int ch = 0; ch < numChannels_; ++ch)
1388 aaFilters_[ch].setCoeffs(c);
1393 T filtered = aaFilters_[ch].processSample(sample, 0);
1394 if (++counter_[ch] >= reduction_)
1397 lastSample_[ch] = filtered;
1399 return lastSample_[ch];
1422template <
typename T>
1425 std::atomic<T> hardness_ { T(0) }, curvature_ { T(1) }, asymmetry_ { T(0) };
1426 std::atomic<int> base_ { 0 };
1427 T knee_ = T(0), span_ = T(1), invSpan_ = T(1), power_ = T(1), invPower_ = T(1), negative_ = T(1), positive_ = T(1);
1436 void setShape(T hardness, T curvature, T asymmetry)
noexcept
1438 if (std::isfinite(hardness)) hardness_.store(std::clamp(hardness, T(0), T(0.95)), std::memory_order_relaxed);
1439 if (std::isfinite(curvature)) curvature_.store(std::clamp(curvature, T(0.5), T(4)), std::memory_order_relaxed);
1440 if (std::isfinite(asymmetry)) asymmetry_.store(std::clamp(asymmetry, T(-1), T(1)), std::memory_order_relaxed);
1442 [[nodiscard]] T
getHardness() const noexcept {
return hardness_.load(std::memory_order_relaxed); }
1443 [[nodiscard]] T
getCurvature() const noexcept {
return curvature_.load(std::memory_order_relaxed); }
1444 [[nodiscard]] T
getAsymmetry() const noexcept {
return asymmetry_.load(std::memory_order_relaxed); }
1446 void setBase(
int base)
noexcept { base_.store(std::clamp(base, 0, 6), std::memory_order_relaxed); }
1447 [[nodiscard]]
int getBase() const noexcept {
return base_.load(std::memory_order_relaxed); }
1451 baseNow_ = base_.load(std::memory_order_relaxed);
1452 knee_ = hardness_.load(std::memory_order_relaxed);
1453 span_ = T(1) - knee_;
1454 invSpan_ = T(1) / span_;
1455 power_ = curvature_.load(std::memory_order_relaxed);
1456 invPower_ = T(1) / power_;
1457 const T asym = asymmetry_.load(std::memory_order_relaxed);
1458 negative_ = (asym > T(0)) ? std::pow(T(2), asym) : T(1);
1459 positive_ = (asym < T(0)) ? std::pow(T(2), -asym) : T(1);
1471 [[nodiscard]]
inline T
bend(T v,
bool negative)
const noexcept
1475 case 1:
return v / std::sqrt(T(1) + v * v);
1476 case 2:
return T(0.6366197723675814) * std::atan(T(1.5707963267948966) * v);
1477 case 3:
return std::erf(T(0.886226925452758) * v);
1478 case 4:
return v >= T(1.5) ? T(1) : v - T(4.0 / 27.0) * v * v * v;
1479 case 5:
return negative ? v / std::sqrt(T(1) + v * v) : -std::expm1(-v);
1480 case 6:
return negative ? std::tanh(v) * std::exp(-v) : std::tanh(v);
1481 default:
return std::tanh(v);
1486 [[nodiscard]]
inline T
shape(T x)
const noexcept
1488 const T side = (x < T(0)) ? negative_ : positive_;
1489 const T u = std::abs(x) * side;
1490 if (u <= knee_)
return x;
1491 const T v = (u - knee_) * invSpan_;
1493 if (baseNow_ == 0) [[likely]]
1494 bent = (power_ == T(1)) ? std::tanh(v) : std::pow(std::tanh(std::pow(v, power_)), invPower_);
1497 const bool neg = x < T(0);
1498 bent = (power_ == T(1)) ?
bend(v, neg) : std::pow(std::max(T(0),
bend(std::pow(v, power_), neg)), invPower_);
1500 return std::copysign((knee_ + span_ * bent) / side, x);
1507template <
typename T>
1517 tube_.prepare(spec);
1532 const bool second = this->
order_ == 2;
1533 const int delay = this->
delay_;
1534 tube_.setAntialias(second ? (delay == 2 || delay == 4 ? 1 : 2) : this->
order_);
1540 tape_.
update(drive * T(0.6), character, spec);
1541 xfmr_.
update(drive * T(0.8), character, spec);
1552 T tubeOut = tube_.processSample(sample, drive * T(0.5), character, ch) * T(2);
1553 T tapeOut = tape_.
processSample(tubeOut, drive * T(0.6), character, ch) * (T(1) / T(0.6));
1554 return xfmr_.
processSample(tapeOut, drive * T(0.8), character, ch) * (T(1) / T(0.8));
1590 const int delay = this->
delay_;
1593 T tubeOut = tube_.processSecond(sample, drive * T(0.5), character, ch) * T(2);
1594 T tapeOut = tape_.
processSample(tubeOut, drive * T(0.6), character, ch) * (T(1) / T(0.6));
1595 return xfmr_.
processSample(tapeOut, drive * T(0.8), character, ch) * (T(1) / T(0.8));
1599 T tubeOut = tube_.processSample(sample, drive * T(0.5), character, ch) * T(2);
1600 T tapeOut = tape_.
processSecond(tubeOut, drive * T(0.6), character, ch) * (T(1) / T(0.6));
1601 return xfmr_.
processFirst(tapeOut, drive * T(0.8), character, ch) * (T(1) / T(0.8));
1607 T tubeOut = tube_.processSample(sample, drive * T(0.5), character, ch) * T(2);
1608 T tapeOut = tape_.
processSecond(tubeOut, drive * T(0.6), character, ch) * (T(1) / T(0.6));
1609 T first = T(0), second = T(0);
1610 xfmr_.
processBoth(tapeOut, drive * T(0.8), character, ch, first, second);
1611 const T low = this->
delayBy(first * (T(1) / T(0.8)), ch, 2);
1616 constexpr T kFrom = T(5.011872336272722), kSpan = T(6.309573444801933 - 5.011872336272722);
1617 const T u = std::clamp((drive - kFrom) / kSpan, T(0), T(1));
1618 return low + (u * u * (T(3) - T(2) * u)) * (high - low);
1620 T tubeOut = tube_.processSecond(sample, drive * T(0.5), character, ch) * T(2);
1621 T tapeOut = tape_.
processSecond(tubeOut, drive * T(0.6), character, ch) * (T(1) / T(0.6));
1622 return xfmr_.
processSecond(tapeOut, drive * T(0.8), character, ch) * (T(1) / T(0.8));
1632template <FloatType T,
int MaxBands>
1633class MultibandSaturation;
1651template <
typename SampleType>
1654 static_assert(std::is_floating_point_v<SampleType>,
1655 "Saturation: SampleType must be float or double.");
1736 pool_[0] = std::make_unique<detail::TubeAlgorithm<SampleType>>();
1737 pool_[1] = std::make_unique<detail::TapeAlgorithm<SampleType>>();
1738 pool_[2] = std::make_unique<detail::TransformerAlgorithm<SampleType>>();
1739 pool_[3] = std::make_unique<detail::TanhAlgorithm<SampleType>>();
1740 pool_[4] = std::make_unique<detail::HardClipAlgorithm<SampleType>>();
1741 pool_[5] = std::make_unique<detail::ExciterAlgorithm<SampleType>>();
1742 pool_[6] = std::make_unique<detail::WavefolderAlgorithm<SampleType>>();
1743 pool_[7] = std::make_unique<detail::BitcrusherAlgorithm<SampleType>>();
1744 pool_[8] = std::make_unique<detail::DownsampleAlgorithm<SampleType>>();
1745 pool_[9] = std::make_unique<detail::MultiStageAlgorithm<SampleType>>();
1746 pool_[10] = std::make_unique<detail::CurveAlgorithm<SampleType>>();
1749 next_.store(
nullptr);
1780 for (
auto& algo :
pool_)
1781 if (algo) algo->prepare(spec);
1796 if (!
tone_)
tone_ = std::make_unique<BiquadCascade<SampleType, 4, 16>>();
1801 toneDirty_.store(
true, std::memory_order_release);
1879 if (
auto* pending =
next_.load())
1882 next_.store(
nullptr);
1885 for (
auto& algo :
pool_)
1886 if (algo) algo->reset();
1961 const int total = buffer.getNumSamples();
1964 for (
int off = 0; off < total; off += maxBlock)
1966 const int n = std::min(maxBlock, total - off);
1967 processChunk(buffer.getSubView(off, n), dryOut, off, dryTurn);
1977 [[nodiscard]]
static std::unique_ptr<Oversampling<SampleType>> makeOversampler(
int factor,
OversamplingMode mode,
1980 using Os = Oversampling<SampleType>;
1981 return std::make_unique<Os>(factor, Os::Quality::High,
1983 : Os::Mode::LinearPhaseFir,
1984 factor >= 8 ? Os::Depth::Deep : Os::Depth::Preset, band);
2003 bool aimDryPhase(
int samples,
bool asked,
bool askedTurn)
noexcept
2008 const bool heard = asked || delta ||
adaptiveBlend_.load(std::memory_order_relaxed) || (normal && mix < 1.0f);
2013 const bool turn = (!normal || mix > 0.0f) && (!asked || askedTurn);
2031 void processChunk(AudioBufferView<SampleType> buffer,
const AudioBufferView<SampleType>* dryOut =
nullptr,
2032 int dryOffset = 0,
bool dryTurn =
false) noexcept
2045 const SampleType pan =
drivePan_.load(std::memory_order_relaxed);
2049 const auto gains = [](SampleType p) {
2050 const SampleType g =
static_cast<SampleType
>(std::pow(10.0,
kDrivePanDb *
static_cast<double>(p) / 20.0));
2051 return std::array<SampleType, 2> { SampleType(1) / g, g };
2058 const bool dryWork = aimDryPhase(buffer.getNumSamples(), dryOut !=
nullptr, dryTurn);
2063 const bool second =
antialiasOrder_.load(std::memory_order_relaxed) == 2;
2075 for (
int ch = 0; ch < nCh; ++ch)
2077 const SampleType* in = buffer.getChannel(ch);
2079 SampleType& hold =
dryHold_[
static_cast<size_t>(ch)];
2082 SampleType& older =
dryHold2_[
static_cast<size_t>(ch)];
2083 if (seed && nS > 0) hold = older = in[0];
2084 for (
int i = 0; i < nS; ++i)
2092 if (seed && nS > 0) hold = in[0];
2093 for (
int i = 0; i < nS; ++i)
2106 for (
int ch = 0; ch < nCh; ++ch)
2108 static_cast<std::size_t
>(nS) *
sizeof(SampleType));
2118 const int numSamples = buffer.getNumSamples();
2119 constexpr int kCoefRefresh = 16;
2120 for (
int i = 0; i < numSamples; i += kCoefRefresh)
2122 const int chunk = std::min(kCoefRefresh, numSamples - i);
2124 for (
int k = 0; k < chunk; ++k)
2134 auto subView = buffer.getSubView(i, chunk);
2158 if (dryOut !=
nullptr)
2162 for (
int ch = 0; ch < channels; ++ch)
2177 const int keepChannel =
2190 if (keepChannel >= 0 && keepChannel < upView.getNumChannels() && upSamples > 0)
2192 static_cast<std::size_t
>(upSamples) *
sizeof(SampleType));
2198 if (keepChannel >= 0 && keepChannel < upView.getNumChannels() && upSamples > 0)
2200 static_cast<std::size_t
>(upSamples) *
sizeof(SampleType));
2207 if (keepChannel >= 0 && keepChannel < buffer.getNumChannels() && baseSamples > 0)
2209 static_cast<std::size_t
>(baseSamples) *
sizeof(SampleType));
2215 if (keepChannel >= 0 && keepChannel < buffer.getNumChannels() && baseSamples > 0)
2217 static_cast<std::size_t
>(baseSamples) *
sizeof(SampleType));
2228 tone_->processBlock(buffer);
2233 int& left =
toneLeft_[
static_cast<std::size_t
>(s)];
2234 if (left < 0)
continue;
2235 left -= buffer.getNumSamples();
2236 if (left <= 0 && !tone_->isRamping(s))
2238 tone_->setSectionEnabled(s,
false);
2253 const int numSamples = buffer.getNumSamples();
2254 constexpr int kCoefRefresh = 16;
2255 for (
int i = 0; i < numSamples; i += kCoefRefresh)
2257 const int chunk = std::min(kCoefRefresh, numSamples - i);
2260 for (
int k = 0; k < chunk; ++k)
2276 auto subView = buffer.getSubView(i, chunk);
2291 for (
int ch = 0; ch < nCh; ++ch)
2293 SampleType* wet = buffer.getChannel(ch);
2295 for (
int i = 0; i < nS; ++i) wet[i] -= dry[i];
2428 const int order = std::clamp(
static_cast<int>(mode), 0, 2);
2430 for (
auto& a :
pool_)
if (a) a->setAntialias(order);
2450 void setTone(
int section, SampleType hz, SampleType gainDb)
noexcept
2452 if (section < 0 || section >=
kToneSections || !std::isfinite(hz) || !std::isfinite(gainDb))
return;
2453 const auto s =
static_cast<std::size_t
>(section);
2454 toneHz_[s].store(std::clamp(hz, SampleType(20), SampleType(20000)), std::memory_order_relaxed);
2455 toneDb_[s].store(std::clamp(gainDb, SampleType(-18), SampleType(18)), std::memory_order_relaxed);
2456 toneDirty_.store(
true, std::memory_order_release);
2462 return toneHz_[
static_cast<std::size_t
>(std::clamp(section, 0,
kToneSections - 1))].load(std::memory_order_relaxed);
2468 return toneDb_[
static_cast<std::size_t
>(std::clamp(section, 0,
kToneSections - 1))].load(std::memory_order_relaxed);
2483 if (std::isfinite(pan))
drivePan_.store(std::clamp(pan, SampleType(-1), SampleType(1)), std::memory_order_relaxed);
2497 if (!std::isfinite(amount))
return;
2498 slewSensitivity_.store(std::clamp(amount, SampleType(0), SampleType(1)), std::memory_order_relaxed);
2527 void setCurveShape(SampleType hardness, SampleType curvature, SampleType asymmetry)
noexcept
2529 curve().setShape(hardness, curvature, asymmetry);
2552 if (!std::isfinite(amount))
return;
2553 asymmetry_.store(std::clamp(amount, SampleType(-1), SampleType(1)), std::memory_order_relaxed);
2573 if (!std::isfinite(levelDb))
return;
2598 if (!std::isfinite(amount))
return;
2599 levelTracking_.store(std::clamp(amount, SampleType(-1), SampleType(1)), std::memory_order_relaxed);
2618 if (!std::isfinite(hz))
return;
2619 trackingCornerHz_.store(hz <= SampleType(0) ? SampleType(0) : std::clamp(hz, SampleType(20), SampleType(2000)),
2620 std::memory_order_relaxed);
2648 if (std::isfinite(amount))
2649 combLoop_.store(std::clamp(amount, SampleType(-0.95), SampleType(0.95)), std::memory_order_relaxed);
2650 if (std::isfinite(hz) && hz > SampleType(0))
2725 pushParam([&](
auto& p){ p.postFilterTiltFreq = centerHz; p.postFilterTiltGain = amountDb; });
2749 if (factor < 1 || factor > 16 || (factor & (factor - 1)) != 0)
return;
2751 applyOversampling(factor);
2795 mode =
static_cast<OversamplingMode>(std::clamp(
static_cast<int>(mode), 0, 1));
2820 band =
static_cast<OversamplingBand>(std::clamp(
static_cast<int>(band), 0, 1));
2830 void applyOversampling(
int factor)
2917 w.
write(
"mix",
static_cast<float>(p.
mix));
2936 w.
write(
"curveBase",
static_cast<int32_t
>(
curve().getBase()));
2941 std::snprintf(key,
sizeof(key),
"tone%dHz", s);
2943 std::snprintf(key,
sizeof(key),
"tone%dDb", s);
2952 w.
write(
"combLoopHz",
static_cast<float>(
combLoopHz_.load(std::memory_order_relaxed)));
2967 setDrive(
static_cast<SampleType
>(r.
read(
"drive", 0.0f)));
2968 setMix(
static_cast<SampleType
>(r.
read(
"mix", 1.0f)));
2973 static_cast<SampleType
>(r.
read(
"tiltGain", 0.0f)));
2979 r.
read(
"antialiasOrder", r.
read(
"antialias",
false) ? 1 : 0)));
2987 static_cast<SampleType
>(r.
read(
"curveCurvature", 1.0f)),
2988 static_cast<SampleType
>(r.
read(
"curveAsymmetry", 0.0f)));
2993 constexpr float kDefault[4] = { 100.0f, 500.0f, 2500.0f, 8000.0f };
2994 char hz[16], db[16];
2995 std::snprintf(hz,
sizeof(hz),
"tone%dHz", s);
2996 std::snprintf(db,
sizeof(db),
"tone%dDb", s);
2997 setTone(s,
static_cast<SampleType
>(r.
read(hz, kDefault[s])),
static_cast<SampleType
>(r.
read(db, 0.0f)));
3004 static_cast<SampleType
>(r.
read(
"combLoopHz", 200.0f)));
3050 SampleType
mix = SampleType(1);
3068 const auto keepFinite = [](SampleType& v, SampleType old)
noexcept
3070 if (!std::isfinite(v)) v = old;
3072 keepFinite(p.driveDb, prev.driveDb);
3073 keepFinite(p.mix, prev.mix);
3074 keepFinite(p.character, prev.character);
3075 keepFinite(p.analogDrift, prev.analogDrift);
3076 keepFinite(p.preFilterHpFreq, prev.preFilterHpFreq);
3077 keepFinite(p.postFilterTiltFreq, prev.postFilterTiltFreq);
3078 keepFinite(p.postFilterTiltGain, prev.postFilterTiltGain);
3079 keepFinite(p.outputGain, prev.outputGain);
3081 const auto clampEnum = [](
auto& e,
int hi)
noexcept
3083 using E = std::remove_reference_t<
decltype(e)>;
3084 e =
static_cast<E
>(std::clamp(
static_cast<int>(e), 0, hi));
3091 template <
typename Fn>
3135 if (
asymmetry_.load(std::memory_order_relaxed) != SampleType(0))
return true;
3136 const auto makes = [
this](
Algorithm a) {
3154 for (
int a = 0; a < static_cast<int>(
pool_.size()); ++a)
3155 if (
pool_[
static_cast<std::size_t
>(a)].get() ==
active_.load() && makes(
static_cast<Algorithm>(a)))
return true;
3168 if (wanted && share >= 1.0)
3170 filter.processBlock(view);
3173 if (wanted && share <= 0.0) filter.reset();
3176 for (
int ch = 0; ch < nCh; ++ch)
3180 for (
int i = 0; i < nS; ++i)
3182 s = wanted ? std::min(1.0, s +
dcStep_) : std::max(0.0, s -
dcStep_);
3183 const SampleType g =
static_cast<SampleType
>(s);
3184 for (
int ch = 0; ch < nCh; ++ch)
3186 SampleType* x = view.getChannel(ch);
3202 for (
int i = 0; i < nS; ++i)
3206 const SampleType g =
static_cast<SampleType
>(
modeDip_);
3207 for (
int ch = 0; ch < nCh; ++ch)
3209 SampleType* wet = buffer.getChannel(ch);
3217 for (
auto& algo :
pool_)
3218 if (algo) algo->reset();
3240 +
static_cast<float>(
compensationDb_.load(std::memory_order_relaxed)));
3268 auto* nxt =
next_.load();
3269 if (requested == act)
3285 next_.store(
nullptr);
3289 else if (requested == nxt)
3304 next_.store(requested);
3326 auto* primary =
active_.load();
3327 auto* secondary =
next_.load();
3337 const int nS = buffer.getNumSamples();
3347 if (primary) { primary->latchOrder(commonDelay); primary->update(driveGainTarget, characterTarget, updateSpec); }
3348 if (secondary) { secondary->latchOrder(commonDelay); secondary->update(driveGainTarget, characterTarget, updateSpec); }
3350 SampleType peakInOriginal = SampleType(0);
3351 for (
int ch = 0; ch < nCh; ++ch) {
3352 const SampleType* d = buffer.getChannel(ch);
3353 for (
int i = 0; i < nS; ++i) peakInOriginal = std::max(peakInOriginal, std::abs(d[i]));
3358 if (slewAmt > SampleType(0))
3360 for (
int ch = 0; ch < nCh; ++ch) {
3361 SampleType* data = buffer.getChannel(ch);
3362 for (
int i = 0; i < nS; ++i) {
3363 SampleType dry = data[i];
3365 data[i] += std::tanh(std::abs(delta)) * slewAmt * dry;
3373 bool useDrift = driftIntensity > 0.01f;
3377 for (
int i = 0; i < nS; ++i) {
3386 for (
int ch = 0; ch < nCh; ++ch) {
3387 driftView.getChannel(ch)[i] =
3388 SampleType(1) +
static_cast<SampleType
>(driftS) * (ch == 0 ? noiseL : noiseR);
3402 for (
int i = 0; i < nS; ++i)
3409 for (
int i = 0; i < nS; ++i)
3420 for (
int ch = 0; ch < nCh; ++ch)
3421 std::memcpy(tempView.getChannel(ch), buffer.getChannel(ch),
static_cast<std::size_t
>(nS) *
sizeof(SampleType));
3426 for (
int i = 0; i < nS; ++i) {
3428 for (
int ch = 0; ch < nCh; ++ch) {
3429 SampleType* out = buffer.getChannel(ch);
3430 const SampleType* alt = tempView.getChannel(ch);
3431 out[i] = out[i] * fade + alt[i] * (SampleType(1) - fade);
3439 next_.store(
nullptr);
3440 if (primary) primary->reset();
3445 next_.store(
nullptr);
3459 SampleType peakOut = SampleType(0);
3460 for (
int ch = 0; ch < nCh; ++ch) {
3461 const SampleType* d = buffer.getChannel(ch);
3462 for (
int i = 0; i < nS; ++i) peakOut = std::max(peakOut, std::abs(d[i]));
3465 SampleType peakInDriven = peakInOriginal * driveGainTarget;
3466 if (peakInDriven > SampleType(1e-6)) {
3467 SampleType ratio = std::min(peakOut / peakInDriven, SampleType(1));
3485 int factor,
bool seed)
noexcept
3488 const int nS = view.getNumSamples();
3489 const int target = factor == 2 ? 4 : std::clamp(factor, 1,
kAlignRing - 1);
3490 const int pad = target - (factor == 2 ? 4 : std::clamp(factor, 1, 3));
3493 for (
int ch = 0; ch < nCh; ++ch)
3495 if (ch == keepChannel)
continue;
3496 SampleType* d = view.getChannel(ch);
3497 auto& ring =
alignRing_[
static_cast<size_t>(ch)];
3498 if (seed && nS > 0) ring.fill(d[0]);
3500 for (
int i = 0; i < nS; ++i)
3502 ring[
static_cast<size_t>(pos)] = d[i];
3503 d[i] = ring[
static_cast<size_t>((pos - pad) & (
kAlignRing - 1))];
3507 if (keepChannel >= 0 && keepSamples > 0)
3511 if (seed) ring.fill(d[0]);
3513 const int delay = target;
3514 for (
int i = 0; i < keepSamples; ++i)
3516 ring[
static_cast<size_t>(pos)] = d[i];
3517 d[i] = ring[
static_cast<size_t>((pos - delay) & (
kAlignRing - 1))];
3529 const int nCh = std::min({ buffer.getNumChannels(),
3531 const int nS = std::min(buffer.getNumSamples(),
3534 for (
int ch = 0; ch < nCh; ++ch)
3536 SampleType* wetData = buffer.getChannel(ch);
3538 for (
int i = 0; i < nS; ++i)
3540 SampleType dry = dryData[i];
3541 SampleType wet = wetData[i];
3542 SampleType avg = (std::abs(
prevBlendSample_[ch]) + std::abs(dry)) * SampleType(0.5);
3543 SampleType apply = std::clamp(avg, SampleType(0), SampleType(1));
3544 wetData[i] = dry * (SampleType(1) - apply) + wet * apply;
3553 if (!baseAlgo)
return;
3554 switch (baseAlgo->getType())
3570 template <
typename ExactAlgo>
3576 const int nS = buffer.getNumSamples();
3579 if (algo->order() == 2)
3583 for (
int i = 0; i < nS; ++i)
3585 const SampleType driveGainS =
driveRamp_[
static_cast<size_t>(i)];
3586 const SampleType charS =
charRamp_[
static_cast<size_t>(i)];
3587 for (
int ch = 0; ch < nCh; ++ch)
3589 SampleType drift = useDrift ? driftView.getChannel(ch)[i] : SampleType(1);
3590 SampleType* data = buffer.getChannel(ch);
3591 const SampleType pg =
panning_ && ch < 2 ?
panGain(ch, i, nS) : SampleType(1);
3592 if constexpr (
requires { ExactAlgo::kSelfAligned; })
3593 data[i] = algo->processSecond(data[i], driveGainS * drift * pg, charS * drift, ch);
3594 else if constexpr (
requires { algo->processSecond(data[i], driveGainS, charS, ch); })
3595 data[i] = algo->padKernel(algo->processSecond(data[i], driveGainS * drift * pg, charS * drift, ch), ch);
3597 data[i] = algo->align(algo->processSample(data[i], driveGainS * drift * pg, charS * drift, ch), ch);
3598 if (pg != SampleType(1)) data[i] /= pg;
3604 for (
int i = 0; i < nS; ++i)
3606 const SampleType driveGainS =
driveRamp_[
static_cast<size_t>(i)];
3607 const SampleType charS =
charRamp_[
static_cast<size_t>(i)];
3609 for (
int ch = 0; ch < nCh; ++ch)
3611 SampleType drift = useDrift ? driftView.getChannel(ch)[i] : SampleType(1);
3612 SampleType* data = buffer.getChannel(ch);
3613 if (
panning_ && ch < 2) [[unlikely]]
3615 const SampleType pg =
panGain(ch, i, nS);
3616 data[i] = algo->processSample(data[i], driveGainS * drift * pg, charS * drift, ch) / pg;
3619 data[i] = algo->processSample(data[i], driveGainS * drift, charS * drift, ch);
3631 if (!
tone_ || !
toneDirty_.load(std::memory_order_relaxed) || !
toneDirty_.exchange(
false, std::memory_order_acquire))
return;
3635 const auto i =
static_cast<std::size_t
>(s);
3636 const double hz = std::min(
static_cast<double>(
toneHz_[i].load(std::memory_order_relaxed)), 0.45 * fs);
3637 const double db =
static_cast<double>(
toneDb_[i].load(std::memory_order_relaxed));
3644 tone_->rampToBypass(s, ramp);
3652 if (!
tone_->isSectionEnabled(s))
3655 tone_->setSectionEnabled(s,
true);
3658 tone_->rampCoeffs(s, c, ramp);
3666 const double disc = c.a1 * c.a1 - 4.0 * c.a2;
3667 const double radius = disc < 0.0 ? std::sqrt(std::max(0.0, c.a2))
3668 : 0.5 * (std::abs(c.a1) + std::sqrt(disc));
3669 if (!(radius > 0.0))
return 0;
3671 return static_cast<int>(std::min(
spec_.
sampleRate, std::ceil(std::log(1e-7) / std::log(radius))));
3675 [[nodiscard]] SampleType
panGain(
int ch,
int i,
int samples)
const noexcept
3677 const auto c =
static_cast<std::size_t
>(ch);
3678 return panFrom_[c] + (
panTo_[c] -
panFrom_[c]) *
static_cast<SampleType
>(i + 1) /
static_cast<SampleType
>(std::max(1, samples));
3686 buffer.applyGain(
decibelsToGain(
static_cast<SampleType
>(targetGainDb)));
3690 const int nCh = buffer.getNumChannels();
3691 const int nS = buffer.getNumSamples();
3692 for (
int i = 0; i < nS; ++i)
3695 for (
int ch = 0; ch < nCh; ++ch) buffer.getChannel(ch)[i] *= gain;
3714 const double hz =
static_cast<double>(
combLoopHz_.load(std::memory_order_relaxed));
3715 const int period =
static_cast<int>(std::lround(
spec_.
sampleRate / std::max(hz, 1.0)));
3723 const auto pow2 = [](
int n) {
int p = 64;
while (p < n) p <<= 1;
return p; };
3730 stageRate_ = 1.0 - std::exp(-1.0 / (0.02 * fs));
3731 evenRate_ = 1.0 - std::exp(-2.0 * std::numbers::pi * 10.0 / fs);
3732 followUp_ = 1.0 - std::exp(-1.0 / (0.010 * fs));
3733 followDown_ = 1.0 - std::exp(-1.0 / (0.150 * fs));
3738 noiseFall_ = 1.0 - std::exp(-1.0 / (0.150 * fs));
3775 const SampleType noiseDb =
programNoiseDb_.load(std::memory_order_relaxed);
3777 const double focusHz =
static_cast<double>(
trackingCornerHz_.load(std::memory_order_relaxed));
3793 if (!feeding)
return std::numeric_limits<int>::max();
3802 static void approach(
double& now,
double target,
double rate)
noexcept
3804 now += rate * (target - now);
3805 if (target == 0.0 && std::abs(now) < 1e-5) now = 0.0;
3811 uint32_t& s =
noiseSeed_[
static_cast<size_t>(ch)];
3812 s = s * 1664525u + 1013904223u;
3813 return static_cast<SampleType
>(
static_cast<double>(
static_cast<int32_t
>(s)) * (1.0 / 2147483648.0));
3825 const int nS = buffer.getNumSamples();
3827 for (
int i = 0; i < nS; ++i)
3837 SampleType amount =
static_cast<SampleType
>(
feedbackNow_);
3841 for (
int ch = 0; ch < nCh; ++ch)
3842 if (ch != keepChannel)
3844 const double level = std::abs(
static_cast<double>(buffer.getChannel(ch)[i]));
3845 if (std::isfinite(level)) top = std::max(top, level);
3855 for (
int ch = 0; ch < nCh; ++ch)
3857 if (ch == keepChannel)
continue;
3859 buffer.getChannel(ch)[i] -= amount * back;
3869 const auto part =
static_cast<SampleType
>(
loopDelayNow_ -
static_cast<double>(whole));
3871 for (
int ch = 0; ch < nCh; ++ch)
3873 if (ch == keepChannel)
continue;
3875 const SampleType back =
interpolateHermite(line[
static_cast<size_t>((at + 1) & feedbackMask)],
3876 line[
static_cast<size_t>(at & feedbackMask)],
3877 line[
static_cast<size_t>((at - 1) & feedbackMask)],
3878 line[
static_cast<size_t>((at - 2) & feedbackMask)], part);
3879 buffer.getChannel(ch)[i] -= amount * back;
3885 SampleType gain = SampleType(1);
3889 for (
int ch = 0; ch < nCh; ++ch)
3891 if (ch == keepChannel)
continue;
3892 double heard =
static_cast<double>(buffer.getChannel(ch)[i]);
3893 if (!std::isfinite(heard)) heard = 0.0;
3896 double& low =
focusState_[
static_cast<size_t>(ch)];
3900 top = std::max(top, std::abs(heard));
3902 const double power = top * top;
3904 const double levelDb = 10.0 * std::log10(
followPower_ + 1e-12);
3919 for (
int ch = 0; ch < nCh; ++ch)
3921 if (ch == keepChannel)
continue;
3922 SampleType& x = buffer.getChannel(ch)[i];
3926 const double square =
static_cast<double>(x) *
static_cast<double>(x);
3927 double& mean =
evenMean_[
static_cast<size_t>(ch)];
3928 if (std::isfinite(square)) mean +=
evenRate_ * (square - mean);
3936 for (
int ch = 0; ch < nCh; ++ch)
3937 if (ch != keepChannel)
3939 const double level = std::abs(
static_cast<double>(buffer.getChannel(ch)[i]));
3940 if (std::isfinite(level)) top = std::max(top, level);
3947 for (
int ch = 0; ch < nCh; ++ch)
3948 if (ch != keepChannel) buffer.getChannel(ch)[i] += level *
stageNoise(ch);
3961 const int nS = buffer.getNumSamples();
3964 for (
int i = 0; i < nS; ++i)
3967 const SampleType back = (gain != SampleType(1)) ? SampleType(1) / gain : SampleType(1);
3968 const auto at =
static_cast<size_t>((
feedbackPos_ + i) & feedbackMask);
3969 for (
int ch = 0; ch < nCh; ++ch)
3971 if (ch == keepChannel)
continue;
3972 SampleType& x = buffer.getChannel(ch)[i];
3978 std::isfinite(fed) ? std::clamp(fed, SampleType(-2), SampleType(2)) : SampleType(0);
4010 uint32_t seed = 0x2545F491u;
4011 double b0 = 0.0, b1 = 0.0, b2 = 0.0, sum = 0.0;
4014 seed = seed * 1664525u + 1013904223u;
4015 const double white =
static_cast<double>(
static_cast<int32_t
>(seed)) * (1.0 / 2147483648.0);
4016 b0 = 0.99765 * b0 + white * 0.0990460;
4017 b1 = 0.96300 * b1 + white * 0.2965164;
4018 b2 = 0.57000 * b2 + white * 1.0526913;
4019 const double pink = b0 + b1 + b2 + white * 0.1848;
4020 sample =
static_cast<SampleType
>(pink);
4024 const double pink =
static_cast<double>(
calibrationNoise_[
static_cast<size_t>(i)]);
4029 for (
auto& sample :
calibrationNoise_) sample =
static_cast<SampleType
>(
static_cast<double>(sample) * scale);
4040 double compensation = 0.0;
4045 p.
mix = SampleType(1);
4058 const auto energy = [&](
const Params& run,
double scale)
noexcept {
4071 const double y =
static_cast<double>(
calibrationWork_[
static_cast<size_t>(i)]);
4077 linear.
driveDb = SampleType(0);
4079 const double driven = energy(p, 1.0);
4080 if (reference > 0.0 && driven > 0.0 && std::isfinite(driven) && std::isfinite(reference))
4081 compensation = std::clamp(-
kCompensationShare * 10.0 * std::log10(driven / reference), -48.0, 24.0);
4083 compensationDb_.store(
static_cast<SampleType
>(compensation), std::memory_order_relaxed);
4129 std::array<uint32_t, kStageChannels>
noiseSeed_ { 0x1234567u, 0x89ABCDEu, 0x3C6EF37u, 0x5851F42u,
4130 0x14057B7u, 0x2B992DDu, 0x7F4A7C1u, 0x61C8864u,
4131 0x0BADF00u, 0x1F123BBu, 0x5DEECE6u, 0x41C64E6u,
4132 0x6C07896u, 0x27D4EB2u, 0x165667Bu, 0x4F6CDD1u };
4157 std::atomic<detail::SaturationAlgorithm<SampleType>*>
active_ {
nullptr };
4158 std::atomic<detail::SaturationAlgorithm<SampleType>*>
next_ {
nullptr };
4201 std::array<SampleType, 2>
panFrom_ { SampleType(1), SampleType(1) },
panTo_ { SampleType(1), SampleType(1) };
4207 std::array<std::atomic<SampleType>, 4>
toneHz_ { SampleType(100), SampleType(500), SampleType(2500), SampleType(8000) };
4208 std::array<std::atomic<SampleType>, 4>
toneDb_ { SampleType(0), SampleType(0), SampleType(0), SampleType(0) };
4210 std::unique_ptr<BiquadCascade<SampleType, 4, 16>>
tone_;
Main generator class for analog-style random modulation.
void setSmoothing(bool shouldBeEnabled, Real timeInMs=static_cast< Real >(50.0)) noexcept
Enables one-pole smoothing of the held targets.
void reseed(std::uint64_t newSeed) noexcept
Request a lock-free reseed of the internal PRNG.
void prepare(double sampleRate) noexcept
Prepare the generator with the audio sample rate.
Real getNextSample() noexcept
Generate and return the next modulation sample.
Non-owning view over audio channel data.
Owning audio buffer with contiguous, 32-byte aligned storage.
AudioBufferView< T, MaxChannels > toView() noexcept
Returns a non-owning mutable view of this buffer. The view's channel capacity is propagated from MaxC...
int getNumChannels() const noexcept
Returns the number of active channels.
T * getChannel(int ch) noexcept
Returns a pointer to the sample data.
int getNumSamples() const noexcept
Returns the number of samples per channel.
void resize(int numChannels, int numSamples)
Allocates the buffer for the given dimensions.
Biquad filter using Transposed Direct Form II (TDF-II) with thread-safe updates.
void setCoeffs(const BiquadCoeffs &c) noexcept
Sets the filter coefficients asynchronously (control thread).
void reset() noexcept
Resets all per-channel filter states to zero to avoid ringing/clicks.
void processBlock(AudioBufferView< T > buffer) noexcept
Processes a full audio buffer in-place.
DC blocking filter with configurable Butterworth order (1-10).
void prepare(double sampleRate, int numChannels=2, double cutoffHz=-1.0)
Prepares the DC blocker, resetting internal states and precalculating coefficients.
void setOrder(int order) noexcept
Sets the filter order (1-10). Thread-safe.
void reset() noexcept
Clears the internal history states to zero.
Pre-allocated, SIMD-friendly dry/wet blender for real-time audio.
void setLatencyCompensation(int samples)
Delays the captured dry signal to compensate for an effect's internal latency (e.g....
int getDryCapturedSamples() const noexcept
Returns the number of samples valid from the last pushDry() call.
void mixWet(AudioBufferView< T > wetBuffer, T targetMix) noexcept
Blends the stored dry signal with the current (wet) buffer in-place.
void reset() noexcept
Resets the internal buffer and smoothing states to zero.
int getDryNumChannels() const noexcept
Returns the internal capacity of channels in the dry buffer.
void pushDry(const AudioBufferView< const T > &input) noexcept
Captures a snapshot of the dry (unprocessed) signal.
void prepare(const AudioSpec &spec)
Allocates the internal dry buffer for the given audio spec.
const T * getDryChannel(int ch) const noexcept
Retrieves a read-only pointer to the captured dry channel data.
Multi-band saturation: crossover split, a Saturation per band, sum.
Carries a dry signal through the same round trip as an Oversampling engine, so that a dry/wet mix add...
void process(AudioBufferView< T > dry) noexcept
Passes a block of the dry signal through the path, in place.
void prepare(const AudioSpec &spec, const Engine &engine)
Sizes the path for an engine.
void reset() noexcept
Clears the path's memory.
Band
The band the first stage keeps (see the constructor).
static constexpr int factorForRate(double sampleRate, double minimumInternalRate=176400.0, int maxFactor=1<< kMaxStages) noexcept
The smallest factor that brings a host rate up to an internal rate.
Professional multi-algorithm saturation processor with analog simulation.
static constexpr double kModeDipSeconds
The fall of a mode change's dip.
void pushParam(Fn &&mutate)
void calibrate() noexcept
std::vector< SampleType > calibrationWork_
std::atomic< bool > combLoopClosing_
static constexpr double kAutomaticInternalRate
Rate the automatic factor reaches.
void stagesBefore(AudioBufferView< SampleType > buffer, int keepChannel) noexcept
std::array< double, kStageChannels > evenMean_
static constexpr double kDryHoldSeconds
It is kept up this long after the mix has gone full wet.
SampleType getCurveCurvature() const noexcept
How fast the Curve algorithm turns (see setCurveShape()).
void dispatchSaturator(detail::SaturationAlgorithm< SampleType > *baseAlgo, AudioBufferView< SampleType > buffer, bool useDrift) noexcept
Smoothers::StateVariableSmoother driveSmoother_
SampleType getCurveAsymmetry() const noexcept
How unlike the two halves of the Curve algorithm are (see setCurveShape()).
SampleType getCurveHardness() const noexcept
How far the Curve algorithm stays straight (see setCurveShape()).
@ LinearPhase
Linear-phase half-bands: the latency of the factor, the dry path a delay.
@ MinimumPhase
Elliptic allpass half-bands: a few samples of latency, the dry path through the same filters.
void setDrivePan(SampleType pan) noexcept
The panorama of the drive, -1 to 1 (0, the default, is centred): the first channel the curve sees get...
std::atomic< SampleType > gainReductionDb_
OversamplingBand getOversamplingBand() const noexcept
The band of the oversampler's first stage (see setOversamplingBand()).
int feedbackDelayFor(int latency) const noexcept
Antialiasing getAntialiasing() const noexcept
The selected antialiasing order.
bool oversamplingAutomatic_
The factor follows the host rate (setAutomaticOversampling()).
void setProcessingMode(ProcessingMode m)
Sets the routing configuration for multi-channel processing.
detail::DryPhase dryPreLead_
double dynamicsSmoothRate_
static constexpr SampleType kMinFeedbackHz
Lowest frequency of the tuned feedback.
void setMix(SampleType mix01)
Sets the global Dry/Wet blend.
void maybeCalibrate(int samples) noexcept
AudioBuffer< SampleType > dryAligned_
std::array< SampleType, kAlignRing > keepRing_
SampleType getToneFrequency(int section) const noexcept
void designTone(int ramp) noexcept
The tone's sections from what was published: a section at 0 dB ramps to the identity and leaves; one ...
std::atomic< bool > adaptiveBlend_
std::atomic< SampleType > slewSensitivity_
SampleType getCombLoopFrequency() const noexcept
The frequency of the tuned feedback as it runs: what the latency allows of the one asked for.
std::atomic< bool > paramsPending_
AnalogRandom::Generator< SampleType > rightDrift_
ProcessingMode pendingMode_
detail::CurveAlgorithm< SampleType > & curve() noexcept
void setAlgorithm(Algorithm algo)
Sets the saturation algorithm topology.
void resetStages() noexcept
void setOversampling(int factor)
Configures internal polyphase oversampling to reduce aliasing.
double loopOpen_
The loop that closes: the level of the input, its fall, how open the loop is.
std::atomic< bool > toneDirty_
void setLevelTracking(SampleType amount) noexcept
Dynamics around the curve: how much the level into it depends on the level of the programme.
std::atomic< int > antialiasOrder_
Antialiasing order, mirror for getState.
void setOversamplingMode(OversamplingMode mode)
Chooses the filters of the oversampler (NOT real-time safe, as setOversampling()).
std::atomic< SampleType > asymmetry_
std::vector< SampleType > driveRamp_
SampleType slewRateScale_
Processing rate / 96 kHz.
void applyOutputGain(AudioBufferView< SampleType > buffer) noexcept
std::unique_ptr< Saturation > calibrator_
AudioBuffer< SampleType > msKeepBuffer_
MidOnly/SideOnly channel snapshot.
std::atomic< SampleType > levelTracking_
static constexpr double kLoopOctaveSeconds
Time the loop's delay takes to glide an octave.
bool curveMakesDc() noexcept
Whether a curve in play makes DC from a signal without it (the pre-filter takes the input's away): me...
static constexpr int kAlignRing
largest delay (16 processing samples).
static constexpr double kModeRiseShare
Its rise takes four times as long (the curves settle from rest).
void setCurveBase(CurveBase base) noexcept
The base curve of the Curve algorithm, the shape its three controls bend (see CurveBase); tanh by def...
void setCharacter(SampleType c)
Adjusts the specific character/bias of the selected algorithm.
std::atomic< SampleType > programNoiseDb_
std::unique_ptr< Oversampling< SampleType > > oversampler_
int poleDecaySamples(const BiquadCoeffs &c) const noexcept
Samples for the poles of a section to fall 140 dB (what it adds once transparent).
bool calibrationDirty_
The curve moved since the last measurement (audio thread).
std::atomic< detail::SaturationAlgorithm< SampleType > * > active_
OversamplingBand oversamplingBand_
setOversamplingBand() (setup thread).
static constexpr double kDynamicsReachDb
Most gain the dynamics apply, either way.
std::atomic< detail::SaturationAlgorithm< SampleType > * > next_
void setOutputGain(SampleType dB)
Sets the post-saturation make-up or trim gain.
static constexpr double kDcFadeSeconds
The DC filter comes in or goes out in this time.
static constexpr double kCompensationShare
Share of the measured gain taken back.
SampleType stageNoise(int ch) noexcept
std::array< std::atomic< SampleType >, 4 > toneHz_
static constexpr SampleType kNoiseOffDb
Programme noise at or under this is off.
static constexpr double kCalibrationEvery
Seconds between two measurements while the curve moves.
std::atomic< Algorithm > currentAlgoType_
std::array< SampleType, kMaxCh > prevSlewSample_
void setProgramNoise(SampleType levelDb) noexcept
Adds noise that follows the programme, before the curve.
static constexpr int kNumAlgorithms
SpscQueue< Params > paramQueue_
AudioBuffer< SampleType > tempBuffer_
static constexpr double kCalibrationQuiet
Level of the reference run (-40 dB).
Smoothers::LinearSmoother crossfader_
std::atomic< bool > calibrationRequested_
A setter outside the parameter queue moved it.
DryWetMixer< SampleType > dryWetMixer_
OversamplingMode oversamplingMode_
Smoothers::LinearSmoother mixSmoother_
AudioBuffer< SampleType > driftBuffer_
Biquad< SampleType > preFilter_
int secondOrderDelay() const noexcept
Smoothers::StateVariableSmoother postTiltFreqSmoother_
void setCurveShape(SampleType hardness, SampleType curvature, SampleType asymmetry) noexcept
Shapes the Curve algorithm.
std::array< int, 4 > toneLeft_
SampleType getGainReductionDb() const noexcept
Calculates the peak gain reduction (clipping amount) for metering.
void applyModeDip(AudioBufferView< SampleType > &buffer) noexcept
A change of processing mode: the wet side goes to the aligned dry over a ramp, the mode changes at th...
void processSaturationPipeline(AudioBufferView< SampleType > buffer) noexcept
void setAntialiasing(Antialiasing mode) noexcept
Selects the antialiasing order (see Antialiasing). Any thread; SecondOrder changes getLatency() at on...
void setAntialiasing(bool on) noexcept
Enables first-order antiderivative anti-aliasing (ADAA) on the memoryless curves (SoftClip / Tube / H...
Params applied_
The snapshot the audio thread applied last.
typename Oversampling< SampleType >::Band OversamplingBand
The band the oversampler's first stage keeps (see setOversamplingBand()).
const detail::CurveAlgorithm< SampleType > & curve() const noexcept
static constexpr double kDynamicsReferenceDb
Level the dynamics bring the programme to.
Algorithm
Defines the harmonic generation topology.
SampleType feedbackScale_
Smoothers::StateVariableSmoother preHpSmoother_
void setPreFilterHpFrequency(SampleType hz)
Configures a pre-saturation high-pass filter.
std::atomic< bool > driveCompensation_
static constexpr double kCalibrationLevelDb
RMS level of the measuring noise.
std::array< std::array< SampleType, kAlignRing >, kMaxCh > alignRing_
static constexpr double kDrivePanDb
std::vector< SampleType > charRamp_
Per-sample drive/character (processing rate).
void reset() noexcept
Clears all internal states, phase memory, and history buffers.
void setAdaptiveBlend(bool on) noexcept
Enables program-dependent saturation density.
std::array< SampleType, 2 > panTo_
static constexpr int kStageChannels
Channels the stages keep a memory for.
int getOversamplingFactor() const noexcept
Retrieves the current oversampling factor.
static constexpr int kToneSections
double loopGlide_
The loop's delay now, in samples (0: not started), and its glide per sample.
static constexpr int kCalibrationSkip
Samples left out while the twin settles.
static constexpr double kDcCornerHz
void setOversamplingBand(OversamplingBand band)
Chooses the band the oversampler's first stage keeps (NOT real-time safe, as setOversampling()).
SampleType getDrivePan() const noexcept
std::atomic< SampleType > combLoop_
OutputMode
Determines the final output signal routing.
SampleType getDriveCompensationDb() const noexcept
The gain the drive compensation is applying, in decibels (0 while it is off).
OversamplingDryPath< SampleType > dryPath_
bool isOversamplingAutomatic() const noexcept
Whether the factor follows the host rate (setAutomaticOversampling()).
std::array< SampleType, 2 > panFrom_
static constexpr double kLoopCloseSeconds
Time constant the programme's level falls with, for the loop that closes.
void applyAdaptiveBlend(AudioBufferView< SampleType > buffer) noexcept
Program-dependent dry/wet density blend (base rate, L/R domain). The dry reference is the DryWetMixer...
void applyParamSnapshot(const Params &p)
static constexpr double kToneRampSeconds
void processCore(ExactAlgo *algo, AudioBufferView< SampleType > buffer, bool useDrift) noexcept
int syncStages() noexcept
static void approach(double &now, double target, double rate) noexcept
void setCombLoop(SampleType amount, SampleType hz) noexcept
Feeds the output of the curve back into it, inverted, one period of a frequency late.
void setCombLoopClosing(bool on) noexcept
Closes the tuned feedback when the programme stops.
double applyDcShare(DCBlocker< SampleType > &filter, AudioBufferView< SampleType > view, double share) noexcept
The DC filter on a block by its share, which moves toward 1 while wanted and toward 0 otherwise (the ...
void stagesAfter(AudioBufferView< SampleType > buffer, int keepChannel) noexcept
void setDriveCompensation(bool on)
Takes back, at the output, the level the drive adds.
bool setState(const uint8_t *data, size_t size)
Restores parameters from a blob (setup/UI threads: it forwards the stored oversampling factor to setO...
void process(AudioBufferView< SampleType > buffer) noexcept
Compatibility alias of processBlock(), with the same thread and preparation contract....
void setOutputMode(OutputMode m)
Sets the output signal path.
SampleType getCombLoop() const noexcept
The amount of the tuned feedback (see setCombLoop()).
std::unique_ptr< BiquadCascade< SampleType, 4, 16 > > tone_
bool getCombLoopClosing() const noexcept
std::atomic< SampleType > drivePan_
std::atomic< SampleType > compensationDb_
static constexpr double kLoopCloseLevel
-70 dBFS: under it there is no programme.
Smoothers::LinearSmoother postTiltGainSmoother_
std::atomic< SampleType > combLoopHz_
void alignSecondOrder(AudioBufferView< SampleType > view, int keepChannel, int keepSamples, int factor, bool seed) noexcept
Completes the second-order delay to whole base samples.
std::atomic< SampleType > trackingCornerHz_
ProcessingMode
Determines how the stereo field is processed.
bool feedingBefore_
The feedback ran in the last block.
std::vector< uint8_t > getState() const
Serializes the parameter state (setup/UI threads; allocates).
SampleType getTrackingCorner() const noexcept
The band the dynamics follower listens to, in hertz (0: all of it).
SampleType panGain(int ch, int i, int samples) const noexcept
The drive's gain of a channel at a sample of the block (a straight line between its ends).
void setTrackingCorner(SampleType hz) noexcept
The band the follower of setLevelTracking() listens to.
static constexpr int kMaxCh
std::vector< SampleType > calibrationNoise_
int oversamplingFactor_
2x by default: the curves alias at 1x (see setOversampling()).
void processBlock(AudioBufferView< SampleType > buffer) noexcept
Processes an audio block in-place (AudioProcessor standard contract).
int getLatency() const noexcept
Reports the processor's algorithmic latency in samples.
DCBlocker< SampleType > dcBlocker_
std::array< double, kStageChannels > focusState_
static constexpr int kCalibrationBlock
std::array< SampleType, kMaxCh > dryHold2_
Second dry sample (2x).
SampleType getAsymmetry() const noexcept
The second-order term before the curve (see setAsymmetry()).
static constexpr int kCalibrationSamples
Length of a measurement.
AnalogRandom::Generator< SampleType > leftDrift_
Smoothers::LinearSmoother driftSmoother_
OversamplingMode getOversamplingMode() const noexcept
The filters of the oversampler (see setOversamplingMode()).
static constexpr double kEvenShare
Share of the square added at asymmetry 1.
Smoothers::LinearSmoother characterSmoother_
bool isDriveCompensation() const noexcept
Whether the level the drive adds is taken back (see setDriveCompensation()).
SampleType getLevelTracking() const noexcept
The dynamics around the curve (see setLevelTracking()).
void setPostFilterTilt(SampleType centerHz, SampleType amountDb)
Configures a post-saturation first-order tilt EQ.
SampleType getToneGain(int section) const noexcept
void setAsymmetry(SampleType amount) noexcept
Adds a second-order term before the curve of any algorithm.
void setTone(int section, SampleType hz, SampleType gainDb) noexcept
A section of the tone after the curve: 0 a low shelf (100 Hz by default), 1 and 2 bells of Q 0....
void setAutomaticOversampling(bool on)
Lets the host rate choose the factor (NOT real-time safe, as setOversampling()).
Saturation & operator=(const Saturation &)=delete
AudioBuffer< SampleType > dcScratch_
void handleParameterChanges()
CurveBase
How the oversampler filters (see setOversamplingMode()).
void setDcBlocking(bool on)
Enables or disables the fixed 5 Hz DC blocker.
int getLatencySamples() const noexcept
Compatibility alias of getLatency(), in prepared-rate samples.
std::array< std::atomic< SampleType >, 4 > toneDb_
CurveBase getCurveBase() const noexcept
DCBlocker< SampleType > dryDc_
Smoothers::LinearSmoother outputGainSmoother_
void prepare(const AudioSpec &spec)
Prepares all internal resources, filters, and buffers.
std::array< std::unique_ptr< detail::SaturationAlgorithm< SampleType > >, kNumAlgorithms > pool_
void setAnalogDrift(SampleType i)
Injects true-stereo pseudo-random low-frequency modulation (drift) into the saturation drive.
std::array< SampleType, kMaxCh > dryHold_
static constexpr int kMinFeedbackDelay
Shortest feedback line (and piece of a block).
Biquad< SampleType > dryPre_
std::vector< SampleType > feedbackRing_
void setDrive(SampleType dB)
Sets the input drive gain.
void configureProcessingRate(bool keepTargets)
double modeDipStep_
0 = the wet side, 1 = the dry
void setSlewSensitivity(SampleType amount) noexcept
Sets a derivative-based (slew rate) saturation multiplier.
int calibrationWait_
Samples before the next measurement may run (audio thread).
std::array< uint32_t, kStageChannels > noiseSeed_
detail::DryPhase dryDcLead_
std::array< SampleType, kMaxCh > prevBlendSample_
Biquad< SampleType > postFilter_
Antialiasing
Antiderivative antialiasing order (see setAntialiasing()).
std::vector< SampleType > gainRing_
int secondOrderBaseDelay() const noexcept
SampleType getProgramNoise() const noexcept
Level of the programme noise under the peak, in decibels (see setProgramNoise()).
static void sanitizeParams(Params &p, const Params &prev) noexcept
Saturation(const Saturation &)=delete
Algorithm getCurrentAlgorithm() const noexcept
Retrieves the currently active underlying algorithm.
RAII wrapper that acquires the lock on construction and releases on destruction.
RAII wrapper that tries to acquire the lock without blocking.
bool isLocked() const noexcept
Queries whether the lock acquisition was successful.
A minimal, real-time safe spin lock with a TTAS wait loop.
Tolerant reader: missing keys yield defaults, unknown keys are skipped.
float read(const char *key, float defaultValue) const
Reads a float, or defaultValue when the key is absent.
bool isValid() const noexcept
uint32_t processorId() const noexcept
Serializes key/value parameters into a versioned blob.
std::vector< uint8_t > blob() const
Finalizes and returns the blob.
void write(const char *key, float value)
Writes a float parameter.
static void langevin(double x, double &L, double &Lp) noexcept
void antiderivatives(double x, double L, double &G, double &P, double &Q) const noexcept
static const AdaaTapeTable & instance()
void reset() noexcept override
Resets internal states (filters, phase, memory).
void update(T drive, T, const AudioSpec &) noexcept override
Updates internal coefficients dependent on block-rate parameters.
Saturation< T >::Algorithm getType() const noexcept override
Identifies the exact algorithm type for CRTP static dispatch.
T processSample(T sample, T, T, int) noexcept
void prepare(const AudioSpec &) noexcept override
Prepares the algorithm with the current audio specification.
void setShape(T hardness, T curvature, T asymmetry) noexcept
void setBase(int base) noexcept
T shape(T x) const noexcept
void prepare(const AudioSpec &) noexcept override
Prepares the algorithm with the current audio specification.
T processSample(T sample, T drive, T, int) noexcept
T getAsymmetry() const noexcept
int getBase() const noexcept
T getCurvature() const noexcept
void reset() noexcept override
Resets internal states (filters, phase, memory).
T getHardness() const noexcept
Saturation< T >::Algorithm getType() const noexcept override
Identifies the exact algorithm type for CRTP static dispatch.
void update(T, T, const AudioSpec &) noexcept override
Updates internal coefficients dependent on block-rate parameters.
T bend(T v, bool negative) const noexcept
T processSample(T sample, T, T, int ch) noexcept
void reset() noexcept override
Resets internal states (filters, phase, memory).
void update(T drive, T, const AudioSpec &spec) noexcept override
Updates internal coefficients dependent on block-rate parameters.
void prepare(const AudioSpec &spec) noexcept override
Prepares the algorithm with the current audio specification.
Saturation< T >::Algorithm getType() const noexcept override
Identifies the exact algorithm type for CRTP static dispatch.
Gives the dry side of a mix the phase that a high-pass leaves in the wet side, and none of its magnit...
void reset() noexcept
Clears the memory (the amount stays).
void process(AudioBufferView< T > buffer) noexcept
A stretch of a buffer, in place.
void prepare(double sampleRate, int channels)
Sizes the state (allocates).
void aim(bool on) noexcept
Where the amount goes.
void land() noexcept
Puts the amount where it goes, without the fade.
void setCorner(double hz) noexcept
Moves the corner (a tangent, when it changes).
static constexpr double kButterworthShare
T processSecond(T sample, T drive, T character, int ch) noexcept
T processSample(T sample, T drive, T character, int) noexcept
Saturation< T >::Algorithm getType() const noexcept override
Identifies the exact algorithm type for CRTP static dispatch.
void reset() noexcept override
Resets internal states (filters, phase, memory).
void prepare(const AudioSpec &) noexcept override
Prepares the algorithm with the current audio specification.
T processSample(T sample, T drive, T character, int ch) noexcept
T processSecond(T sample, T drive, T character, int ch) noexcept
void reset() noexcept override
Resets internal states (filters, phase, memory).
Saturation< T >::Algorithm getType() const noexcept override
Identifies the exact algorithm type for CRTP static dispatch.
void prepare(const AudioSpec &) noexcept override
Prepares the algorithm with the current audio specification.
void reset() noexcept override
Resets internal states (filters, phase, memory).
void prepare(const AudioSpec &spec) noexcept override
Prepares the algorithm with the current audio specification.
Saturation< T >::Algorithm getType() const noexcept override
Identifies the exact algorithm type for CRTP static dispatch.
T processSecond(T sample, T drive, T character, int ch) noexcept
T processSample(T sample, T drive, T character, int ch) noexcept
static constexpr bool kSelfAligned
processSecond() delays by exactly the common delay (no padding).
void update(T drive, T character, const AudioSpec &spec) noexcept override
Updates internal coefficients dependent on block-rate parameters.
static constexpr int kMaxDelay
int delay_
Latched common second-order delay.
unsigned delayArm_
Channels whose delay line must be seeded.
T delayBy(T y, int ch, int n) noexcept
int order_
Latched order (audio thread).
bool takeArm(int c) noexcept
virtual void reset() noexcept=0
Resets internal states (filters, phase, memory).
virtual void update(T, T, const AudioSpec &) noexcept
Updates internal coefficients dependent on block-rate parameters.
int commonDelay() const noexcept
The latched common second-order delay (processing samples).
void setAntialias(int order) noexcept
Antiderivative antialiasing order on the memoryless curves: 0 off, 1 first order (SoftClip/Tube/HardC...
std::array< std::array< T, kMaxDelay >, kAaCh > alignHold_
T padKernel(T y, int ch) noexcept
unsigned halfArm_
Channels whose interpolator must be seeded.
static constexpr int kAaCh
std::atomic< int > antialias_
void latchOrder(int commonDelay=1) noexcept
Latches the order for the coming block (audio thread, before update()) and the common second-order de...
T align(T y, int ch) noexcept
virtual Saturation< T >::Algorithm getType() const noexcept=0
Identifies the exact algorithm type for CRTP static dispatch.
virtual void prepare(const AudioSpec &spec) noexcept=0
Prepares the algorithm with the current audio specification.
void resetAlign() noexcept
std::array< std::array< T, 3 >, kAaCh > halfHold_
virtual ~SaturationAlgorithm()=default
T delayHalfSample(T y, int ch) noexcept
unsigned armMask_
Channels whose order-2 history must be seeded.
int order() const noexcept
The latched order (audio thread).
Saturation< T >::Algorithm getType() const noexcept override
Identifies the exact algorithm type for CRTP static dispatch.
void reset() noexcept override
Resets internal states (filters, phase, memory).
T processSample(T sample, T drive, T character, int ch) noexcept
void prepare(const AudioSpec &) noexcept override
Prepares the algorithm with the current audio specification.
T processSecond(T sample, T drive, T character, int ch) noexcept
T processSecond(T sample, T drive, T character, int ch) noexcept
void reset() noexcept override
Resets internal states (filters, phase, memory).
void prepare(const AudioSpec &spec) noexcept override
Prepares the algorithm with the current audio specification.
T processSample(T sample, T drive, T character, int ch) noexcept
void update(T drive, T, const AudioSpec &spec) noexcept override
Updates internal coefficients dependent on block-rate parameters.
Saturation< T >::Algorithm getType() const noexcept override
Identifies the exact algorithm type for CRTP static dispatch.
T processSecond(T sample, T drive, T character, int ch) noexcept
void reset() noexcept override
Resets internal states (filters, phase, memory).
void prepare(const AudioSpec &) noexcept override
Prepares the algorithm with the current audio specification.
T processSample(T sample, T drive, T character, int ch) noexcept
Saturation< T >::Algorithm getType() const noexcept override
Identifies the exact algorithm type for CRTP static dispatch.
T processSecond(T sample, T drive, T character, int ch) noexcept
Saturation< T >::Algorithm getType() const noexcept override
Identifies the exact algorithm type for CRTP static dispatch.
void prepare(const AudioSpec &) noexcept override
Prepares the algorithm with the current audio specification.
void reset() noexcept override
Resets internal states (filters, phase, memory).
T processSample(T sample, T drive, T character, int ch) noexcept
double logCosh(double x) noexcept
Stable log(cosh(x)), shared by the tanh antiderivative kernels.
Main namespace for the DSPark framework.
T decibelsToGain(T dB, T minusInfinityDb=T(-100)) noexcept
Converts a value in decibels to linear gain.
T interpolateHermite(T y0, T y1, T y2, T y3, T frac) noexcept
4-point, 3rd-order Hermite interpolation (optimized x-form).
T mapRange(T value, T inMin, T inMax, T outMin, T outMax) noexcept
Maps a value from one range to another (linear interpolation).
T gainToDecibels(T gain, T minusInfinityDb=T(-100)) noexcept
Converts a linear gain value to decibels.
T fastTanh(T x) noexcept
Fast tanh approximation using a Pade [5,4] rational function.
constexpr uint32_t stateId(const char(&tag)[5]) noexcept
Builds a FOURCC processor id, e.g. dspark::stateId("COMP").
Describes the audio environment for a DSP processor.
constexpr bool isValid() const noexcept
Checks if the specification contains valid, processable parameters.
int numChannels
Number of audio channels (e.g., 1 = mono, 2 = stereo).
int maxBlockSize
Maximum number of samples per processing block.
double sampleRate
Sample rate in Hz.
Stores normalised biquad coefficients (b0, b1, b2, a1, a2), always double.
static BiquadCoeffs makeHighPass(double sampleRate, double freq, double Q=0.7071067811865476) noexcept
High-pass filter.
static BiquadCoeffs makePeak(double sampleRate, double freq, double Q, double gainDb) noexcept
Peak (parametric EQ) filter.
static BiquadCoeffs makeTilt(double sampleRate, double pivotFreq, double gainDb) noexcept
Creates a first-order tilt filter.
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 makeLowShelf(double sampleRate, double freq, double gainDb, double slope=1.0) noexcept
Low-shelf filter.
static BiquadCoeffs makeHighShelf(double sampleRate, double freq, double gainDb, double slope=1.0) noexcept
High-shelf filter.
Static utility for Mid/Side stereo encoding and decoding.
static void encode(AudioBufferView< T > buffer) noexcept
Encodes an entire stereo buffer from Left/Right to Mid/Side.
static void decode(AudioBufferView< T > buffer) noexcept
Decodes an entire stereo buffer from Mid/Side back to Left/Right.
ProcessingMode processingMode
SampleType postFilterTiltFreq
SampleType preFilterHpFreq
SampleType postFilterTiltGain
Linear ramp smoother for predictable, uniform interpolation.
void setCurrentAndTargetValue(float value) noexcept
void reset(double sampleRate, float rampTimeMilliseconds, float initialValue=0.0f) noexcept
float getTargetValue() const noexcept
float getNextValue() noexcept
void setTargetValue(float newTarget) noexcept
bool isSmoothing() const noexcept
float getCurrentValue() const noexcept
Second-order state variable filter (SVF) smoother (TPT implementation).
void reset(double sampleRate, float timeConstantMilliseconds, float q=0.707f, float initialValue=0.0f) noexcept
void setTargetValue(float newTarget) noexcept
float getNextValue() noexcept
float getTargetValue() const noexcept
bool isSmoothing() const noexcept
static constexpr double kB
double p(double u) const noexcept
void setCharacter(double a) noexcept
double f(double u) const noexcept
static constexpr double kLimit
double average(double u0, double u1, double u2) const noexcept
double crit[2]
Critical points of p, ascending.
double A
Quadratic coefficient (0.25 character).
double h2
Inputs one and two samples ago.
Point point(double H, double seed) const noexcept
static double div(const Point &p, const Point &q) noexcept
void reset(double u, double alphaIn) noexcept
const AdaaTapeTable * table
static double average(const Point &p0, const Point &p1, const Point &p2) noexcept
double rho
Curve of the points held.
double process(double H, double alphaIn) noexcept
void setCurve(double alphaIn) noexcept