42 static constexpr float pi = dspark::pi<float>;
43 static constexpr float twoPi = dspark::twoPi<float>;
44 static constexpr float sqrt2 = 1.41421356237309504880f;
64 void reset(
double sampleRate,
float rampTimeMilliseconds,
float initialValue = 0.0f) noexcept;
68 [[nodiscard]]
float getCurrentValue() const noexcept {
return static_cast<float>(current); }
69 [[nodiscard]]
float getTargetValue() const noexcept {
return static_cast<float>(target); }
81 void processBlock(
float* buffer,
int numSamples,
bool multiply = false) noexcept;
115 void reset(
double sampleRate,
float timeConstantMilliseconds,
float initialValue = 1.0f) noexcept;
119 [[nodiscard]]
float getCurrentValue() const noexcept {
return static_cast<float>(current); }
120 [[nodiscard]]
float getTargetValue() const noexcept {
return static_cast<float>(target); }
124 void skip() noexcept;
127 double current = 1.0;
151 void reset(
double sampleRate,
float timeConstantMilliseconds,
float initialValue = 0.0f) noexcept;
155 [[nodiscard]]
float getCurrentValue() const noexcept {
return static_cast<float>(current); }
156 [[nodiscard]]
float getTargetValue() const noexcept {
return static_cast<float>(target); }
158 void skip() noexcept;
162 double current = 0.0;
170template <std::
size_t N>
174 static_assert(N >= 1,
"MultiPoleSmoother needs at least one pole");
178 void reset(
double sampleRate,
float timeConstantMilliseconds,
float initialValue = 0.0f) noexcept;
182 [[nodiscard]]
float getCurrentValue() const noexcept {
return poles.back().getCurrentValue(); }
183 [[nodiscard]]
float getTargetValue() const noexcept {
return poles.front().getTargetValue(); }
203 void reset(
double sampleRate,
float attackMilliseconds,
float releaseMilliseconds,
float initialValue = 0.0f) noexcept;
207 [[nodiscard]]
float getCurrentValue() const noexcept {
return static_cast<float>(current); }
208 [[nodiscard]]
float getTargetValue() const noexcept {
return static_cast<float>(target); }
210 void skip() noexcept;
215 double attackCoeff = 0.0;
216 double releaseCoeff = 0.0;
217 double current = 0.0;
234 void reset(
double sampleRate,
float maxRatePerSecond,
float initialValue = 0.0f) noexcept;
238 [[nodiscard]]
float getCurrentValue() const noexcept {
return static_cast<float>(current); }
239 [[nodiscard]]
float getTargetValue() const noexcept {
return static_cast<float>(target); }
241 void skip() noexcept;
244 double maxDelta = 0.0;
245 double current = 0.0;
263 void reset(
double sampleRate,
float timeConstantMilliseconds,
float q = 0.707f,
float initialValue = 0.0f) noexcept;
267 [[nodiscard]]
float getCurrentValue() const noexcept {
return static_cast<float>(lowpass); }
268 [[nodiscard]]
float getTargetValue() const noexcept {
return static_cast<float>(target); }
270 void skip() noexcept;
272 [[nodiscard]]
float getBandPassOutput() const noexcept {
return static_cast<float>(bandpass); }
284 double lowpass = 0.0;
285 double bandpass = 0.0;
286 double highpass = 0.0;
302 static constexpr float epsilon = 1e-6f;
304 void reset(
double sampleRate,
float timeConstantMilliseconds,
float initialValue = 0.0f) noexcept;
305 void setTargetValue(
float newTarget) noexcept;
306 float getNextValue() noexcept;
308 [[nodiscard]]
float getCurrentValue() const noexcept {
return static_cast<float>(lastOut); }
309 [[nodiscard]]
float getTargetValue() const noexcept {
return static_cast<float>(target); }
310 [[nodiscard]]
bool isSmoothing() const noexcept;
311 void skip() noexcept;
314 double b0 = 0.0, b1 = 0.0, b2 = 0.0;
315 double a1 = 0.0, a2 = 0.0;
316 double s1 = 0.0, s2 = 0.0;
318 double lastOut = 0.0;
319 double prevOut = 0.0;
330 void reset(
double sampleRate,
float timeConstantMilliseconds,
float initialValue = 0.0f)
noexcept;
356 void reset(
double sampleRate,
float rampTimeMilliseconds,
float initialValue = 0.0f)
noexcept;
357 void setTargetValue(
float newTarget)
noexcept;
358 float getNextValue()
noexcept;
360 [[nodiscard]]
float getCurrentValue() const noexcept {
return static_cast<float>(current); }
361 [[nodiscard]]
float getTargetValue() const noexcept {
return static_cast<float>(target); }
362 [[nodiscard]]
bool isSmoothing() const noexcept {
return stepsDone < totalSteps; }
363 void skip() noexcept;
366 double current = 0.0, target = 0.0;
369 double invSteps = 1.0;
385 totalSteps =
static_cast<int>(sampleRate * rampTimeMilliseconds / 1000.0);
388 current = initialValue;
389 target = initialValue;
392inline void Smoothers::LinearSmoother::setTargetValue(
float newTarget)
noexcept
394 if (newTarget == target)
return;
396 stepsToGo = totalSteps;
398 step = (target - current) /
static_cast<double>(stepsToGo);
403inline float Smoothers::LinearSmoother::getNextValue() noexcept
405 if (stepsToGo <= 0)
return static_cast<float>(current);
408 if (stepsToGo == 0) current = target;
409 return static_cast<float>(current);
412inline void Smoothers::LinearSmoother::setCurrentAndTargetValue(
float value)
noexcept
420inline bool Smoothers::LinearSmoother::isSmoothing() const noexcept
422 return std::abs(current - target) > epsilon;
425inline void Smoothers::LinearSmoother::skip() noexcept
432inline void Smoothers::LinearSmoother::processBlock(
float* buffer,
int numSamples,
bool multiply)
noexcept
437 int stepsToProcess = std::min(numSamples, stepsToGo);
441 for (; i < stepsToProcess; ++i)
444 const float value =
static_cast<float>(current);
445 buffer[i] = multiply ? buffer[i] * value : buffer[i] + value;
448 stepsToGo -= stepsToProcess;
449 if (stepsToGo == 0) current = target;
452 for (; i < numSamples; ++i)
454 const float value =
static_cast<float>(target);
455 buffer[i] = multiply ? buffer[i] * value : buffer[i] + value;
461inline void Smoothers::ExponentialSmoother::reset(
double sampleRate,
float timeConstantMilliseconds,
float initialValue)
noexcept
463 totalSteps =
static_cast<int>(sampleRate * timeConstantMilliseconds / 1000.0);
466 current = initialValue;
467 target = initialValue;
470inline void Smoothers::ExponentialSmoother::setTargetValue(
float newTarget)
noexcept
472 if (std::abs(newTarget) < epsilon)
473 newTarget = (newTarget < 0.0f) ? -epsilon : epsilon;
475 if (newTarget == target)
return;
478 if (totalSteps > 0 && std::abs(current) > epsilon)
480 const double eps =
static_cast<double>(epsilon);
481 const double safeCur = (current > 0.0) ? std::max(current, eps) : std::min(current, -eps);
482 const double ratio = target / safeCur;
485 stepsToGo = totalSteps;
486 coeff = std::exp(std::log(ratio) /
static_cast<double>(stepsToGo));
500inline float Smoothers::ExponentialSmoother::getNextValue() noexcept
502 if (stepsToGo <= 0)
return static_cast<float>(current);
505 if (stepsToGo == 0) current = target;
506 return static_cast<float>(current);
509inline void Smoothers::ExponentialSmoother::setCurrentAndTargetValue(
float value)
noexcept
517inline bool Smoothers::ExponentialSmoother::isSmoothing() const noexcept
519 return std::abs(current - target) > epsilon;
522inline void Smoothers::ExponentialSmoother::skip() noexcept
531inline void Smoothers::OnePoleSmoother::reset(
double sampleRate,
float timeConstantMilliseconds,
float initialValue)
noexcept
533 const double timeConstantSeconds =
static_cast<double>(timeConstantMilliseconds) / 1000.0;
534 const double tau = sampleRate * timeConstantSeconds;
535 coeff = tau > 0.0 ? std::exp(-1.0 / tau) : 0.0;
536 current = initialValue;
537 target = initialValue;
540inline void Smoothers::OnePoleSmoother::setTargetValue(
float newTarget)
noexcept
545inline float Smoothers::OnePoleSmoother::getNextValue() noexcept
547 if (!isSmoothing()) { skip();
return static_cast<float>(target); }
548 current = target + coeff * (current - target);
549 return static_cast<float>(current);
552inline bool Smoothers::OnePoleSmoother::isSmoothing() const noexcept
554 return std::abs(current - target) > epsilon;
557inline void Smoothers::OnePoleSmoother::skip() noexcept
564template <std::
size_t N>
567 for (
auto& pole : poles)
568 pole.
reset(sampleRate, timeConstantMilliseconds, initialValue);
571template <std::
size_t N>
577template <std::
size_t N>
581 for (std::size_t i = 1; i < N; ++i)
583 poles[i].setTargetValue(val);
584 val = poles[i].getNextValue();
589template <std::
size_t N>
597 return std::abs(poles.back().getCurrentValue()
598 - poles.front().getTargetValue()) > epsilon;
601template <std::
size_t N>
608 for (
auto& pole : poles)
610 pole.setTargetValue(t);
617inline void Smoothers::AsymmetricSmoother::reset(
double sampleRate,
float attackMilliseconds,
float releaseMilliseconds,
float initialValue)
noexcept
619 const double attackSeconds =
static_cast<double>(attackMilliseconds) / 1000.0;
620 const double releaseSeconds =
static_cast<double>(releaseMilliseconds) / 1000.0;
621 attackCoeff = attackSeconds > 0.0 ? std::exp(-1.0 / (sampleRate * attackSeconds)) : 0.0;
622 releaseCoeff = releaseSeconds > 0.0 ? std::exp(-1.0 / (sampleRate * releaseSeconds)) : 0.0;
623 current = initialValue;
624 target = initialValue;
627inline void Smoothers::AsymmetricSmoother::setTargetValue(
float newTarget)
noexcept
632inline float Smoothers::AsymmetricSmoother::getNextValue() noexcept
634 if (!isSmoothing()) { skip();
return static_cast<float>(target); }
635 const double c = (target > current) ? attackCoeff : releaseCoeff;
636 current = target + c * (current - target);
637 return static_cast<float>(current);
640inline bool Smoothers::AsymmetricSmoother::isSmoothing() const noexcept
642 return std::abs(current - target) > epsilon;
645inline void Smoothers::AsymmetricSmoother::skip() noexcept
652inline void Smoothers::SlewLimiter::reset(
double sampleRate,
float maxRatePerSecond,
float initialValue)
noexcept
657 maxDelta = std::max(0.0,
static_cast<double>(maxRatePerSecond) / sampleRate);
658 current = initialValue;
659 target = initialValue;
662inline void Smoothers::SlewLimiter::setTargetValue(
float newTarget)
noexcept
667inline float Smoothers::SlewLimiter::getNextValue() noexcept
669 double delta = target - current;
670 delta = std::clamp(delta, -maxDelta, maxDelta);
672 return static_cast<float>(current);
675inline bool Smoothers::SlewLimiter::isSmoothing() const noexcept
677 return std::abs(current - target) > epsilon;
680inline void Smoothers::SlewLimiter::skip() noexcept
687inline void Smoothers::StateVariableSmoother::reset(
double sampleRate,
float timeConstantMilliseconds,
float q,
float initialValue)
noexcept
689 const double timeConstantSeconds =
static_cast<double>(timeConstantMilliseconds) / 1000.0;
690 const double fs = sampleRate;
696 double fc = timeConstantSeconds > 1e-9 ? 1.0 / (
static_cast<double>(Constants::twoPi) * timeConstantSeconds) : fs;
697 fc = std::min(fc, fs * 0.49);
699 g = std::tan(dspark::pi<double> * fc / fs);
700 k = 1.0 /
static_cast<double>(std::max(q, 0.01f));
702 a1 = 1.0 / (1.0 + g * (g + k));
708 target = initialValue;
709 lowpass = initialValue;
714inline void Smoothers::StateVariableSmoother::setTargetValue(
float newTarget)
noexcept
719inline float Smoothers::StateVariableSmoother::getNextValue() noexcept
721 if (!isSmoothing()) { skip();
return static_cast<float>(target); }
723 const double v0 = target;
724 const double v3 = v0 - v2;
725 const double v1_new = a1 * v1 + a2 * v3;
726 const double v2_new = v2 + a2 * v1 + a3 * v3;
727 v1 = 2.0 * v1_new - v1;
728 v2 = 2.0 * v2_new - v2;
732 highpass = v0 - k * v1_new - v2_new;
734 return static_cast<float>(lowpass);
737inline bool Smoothers::StateVariableSmoother::isSmoothing() const noexcept
744 return std::abs(lowpass - target) > epsilon || std::abs(bandpass) > epsilon;
747inline void Smoothers::StateVariableSmoother::skip() noexcept
758inline void Smoothers::ButterworthSmoother::reset(
double sampleRate,
float timeConstantMilliseconds,
float initialValue)
noexcept
760 constexpr double kSqrt2 = 1.4142135623730950488;
761 const double timeConstantSeconds =
static_cast<double>(timeConstantMilliseconds) / 1000.0;
762 const double fs = sampleRate;
765 double fc = timeConstantSeconds > 1e-9 ? 1.0 / (
static_cast<double>(Constants::twoPi) * timeConstantSeconds) : fs;
766 fc = std::min(fc, fs * 0.49);
767 const double tanw = std::tan(dspark::pi<double> * fc / fs);
768 const double tanw2 = tanw * tanw;
770 const double denom = 1.0 + kSqrt2 * tanw + tanw2;
773 b1 = 2.0 * tanw2 / denom;
776 a1 = 2.0 * (tanw2 - 1.0) / denom;
777 a2 = (1.0 - kSqrt2 * tanw + tanw2) / denom;
785 velEps =
static_cast<double>(epsilon) * (
static_cast<double>(Constants::twoPi) * fc / fs);
791 s2 = (b2 - a2) * initialValue;
792 s1 = (b1 - a1) * initialValue + s2;
793 target = initialValue;
794 lastOut = initialValue;
795 prevOut = initialValue;
798inline void Smoothers::ButterworthSmoother::setTargetValue(
float newTarget)
noexcept
803inline float Smoothers::ButterworthSmoother::getNextValue() noexcept
805 if (!isSmoothing()) { skip();
return static_cast<float>(target); }
807 const double x0 = target;
808 const double y0 = b0 * x0 + s1;
809 s1 = b1 * x0 - a1 * y0 + s2;
810 s2 = b2 * x0 - a2 * y0;
814 return static_cast<float>(y0);
817inline bool Smoothers::ButterworthSmoother::isSmoothing() const noexcept
825 return std::abs(lastOut - target) > epsilon
826 || std::abs(lastOut - prevOut) > velEps;
829inline void Smoothers::ButterworthSmoother::skip() noexcept
832 s2 = (b2 - a2) * target;
833 s1 = (b1 - a1) * target + s2;
840inline void Smoothers::HermiteSmoother::reset(
double sampleRate,
float rampTimeMilliseconds,
float initialValue)
noexcept
843 const double steps = sampleRate *
static_cast<double>(rampTimeMilliseconds) / 1000.0;
844 rampSteps = steps > 0.0 ?
static_cast<int>(std::min(steps + 0.5, 1.0e9)) : 0;
845 current = target = start = initialValue;
847 totalSteps = stepsDone = 0;
851inline void Smoothers::HermiteSmoother::setTargetValue(
float newTarget)
noexcept
854 if (!std::isfinite(newTarget) ||
static_cast<double>(newTarget) == target)
return;
857 double velocity = 0.0;
858 if (stepsDone < totalSteps)
860 const double u = stepsDone * invSteps;
861 const double d = target - start;
862 velocity = ((6.0 * u - 6.0 * u * u) * d + (3.0 * u * u - 4.0 * u + 1.0) * slope0) * invSteps;
868 current = start = target;
870 totalSteps = stepsDone = 0;
873 totalSteps = rampSteps;
875 invSteps = 1.0 / totalSteps;
879 const double d = target - start;
880 double m = velocity * totalSteps;
881 if (d > 0.0) m = std::min(m, 3.0 * d);
882 else if (d < 0.0) m = std::max(m, 3.0 * d);
887inline float Smoothers::HermiteSmoother::getNextValue() noexcept
889 if (stepsDone >= totalSteps)
return static_cast<float>(current);
890 if (++stepsDone == totalSteps)
893 return static_cast<float>(current);
895 const double u = stepsDone * invSteps;
896 const double u2 = u * u;
897 const double d = target - start;
899 current = start + d * (3.0 * u2 - 2.0 * u2 * u) + slope0 * (u2 * u - 2.0 * u2 + u);
900 return static_cast<float>(current);
903inline void Smoothers::HermiteSmoother::skip() noexcept
905 current = start = target;
907 totalSteps = stepsDone = 0;
912inline void Smoothers::CriticallyDampedSmoother::reset(
double sampleRate,
float timeConstantMilliseconds,
float initialValue)
noexcept
916 StateVariableSmoother::reset(sampleRate, timeConstantMilliseconds, 0.5f, initialValue);
A collection of real-time safe smoothing filters for parameter interpolation in audio.
Main namespace for the DSPark framework.
constexpr T sqrt2
Square root of 2 (1.41421...).
constexpr T pi
Pi (3.14159...) for the given floating-point type.
constexpr T twoPi
2 * Pi (6.28318...).
Smoother with asymmetric attack/release times.
float getTargetValue() const noexcept
Butterworth low-pass smoother for maximally flat response.
float getTargetValue() const noexcept
Critically damped smoother (no overshoot, exact Q=0.5).
Exponential (multiplicative) smoother for natural, perceptual responses.
float getTargetValue() const noexcept
Cubic glide that lands exactly on the target after a fixed time, with a continuous slope.
bool isSmoothing() const noexcept
float getCurrentValue() const noexcept
float getTargetValue() const noexcept
Linear ramp smoother for predictable, uniform interpolation.
void setCurrentAndTargetValue(float value) noexcept
void processBlock(float *buffer, int numSamples, bool multiply=false) noexcept
Block processing for SIMD optimization.
void reset(double sampleRate, float rampTimeMilliseconds, float initialValue=0.0f) noexcept
float getTargetValue() const noexcept
float getNextValue() noexcept
void setTargetValue(float newTarget) noexcept
static constexpr float epsilon
bool isSmoothing() const noexcept
float getCurrentValue() const noexcept
Templated cascaded multi-pole smoother for steeper roll-off.
bool isSmoothing() const noexcept
float getNextValue() noexcept
float getTargetValue() const noexcept
void setTargetValue(float newTarget) noexcept
void reset(double sampleRate, float timeConstantMilliseconds, float initialValue=0.0f) noexcept
Authentic one-pole exponential IIR low-pass smoother.
float getTargetValue() const noexcept
Rate limiter to cap maximum change per sample.
float getTargetValue() const noexcept
Second-order state variable filter (SVF) smoother (TPT implementation).
float getTargetValue() const noexcept
float getHighPassOutput() const noexcept