DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
Smoothers.h
1// DSPark -- Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi -- MIT License
3
4#pragma once
5
6#include "DspMath.h"
7
8#include <algorithm>
9#include <array>
10#include <cmath>
11
36namespace dspark {
37namespace Smoothers
38{
39
40namespace Constants
41{
42 static constexpr float pi = dspark::pi<float>;
43 static constexpr float twoPi = dspark::twoPi<float>;
44 static constexpr float sqrt2 = 1.41421356237309504880f;
45} // namespace Constants
46
47//==============================================================================
48
60{
61public:
62 static constexpr float epsilon = 1e-6f;
63
64 void reset(double sampleRate, float rampTimeMilliseconds, float initialValue = 0.0f) noexcept;
65 void setTargetValue(float newTarget) noexcept;
66 float getNextValue() noexcept;
67
68 [[nodiscard]] float getCurrentValue() const noexcept { return static_cast<float>(current); }
69 [[nodiscard]] float getTargetValue() const noexcept { return static_cast<float>(target); }
70
71 void setCurrentAndTargetValue(float value) noexcept;
72 [[nodiscard]] bool isSmoothing() const noexcept;
73 void skip() noexcept;
74
81 void processBlock(float* buffer, int numSamples, bool multiply = false) noexcept;
82
83private:
84 double current = 0.0;
85 double target = 0.0;
86 double step = 0.0;
87 int stepsToGo = 0;
88 int totalSteps = 0;
89};
90
111{
112public:
113 static constexpr float epsilon = 1e-10f;
114
115 void reset(double sampleRate, float timeConstantMilliseconds, float initialValue = 1.0f) noexcept;
116 void setTargetValue(float newTarget) noexcept;
117 float getNextValue() noexcept;
118
119 [[nodiscard]] float getCurrentValue() const noexcept { return static_cast<float>(current); }
120 [[nodiscard]] float getTargetValue() const noexcept { return static_cast<float>(target); }
121
122 void setCurrentAndTargetValue(float value) noexcept;
123 [[nodiscard]] bool isSmoothing() const noexcept;
124 void skip() noexcept;
125
126private:
127 double current = 1.0;
128 double target = 1.0;
129 double coeff = 0.0;
130 int stepsToGo = 0;
131 int totalSteps = 0;
132};
133
147{
148public:
149 static constexpr float epsilon = 1e-6f;
150
151 void reset(double sampleRate, float timeConstantMilliseconds, float initialValue = 0.0f) noexcept;
152 void setTargetValue(float newTarget) noexcept;
153 float getNextValue() noexcept;
154
155 [[nodiscard]] float getCurrentValue() const noexcept { return static_cast<float>(current); }
156 [[nodiscard]] float getTargetValue() const noexcept { return static_cast<float>(target); }
157 [[nodiscard]] bool isSmoothing() const noexcept;
158 void skip() noexcept;
159
160private:
161 double coeff = 0.0;
162 double current = 0.0;
163 double target = 0.0;
164};
165
170template <std::size_t N>
172{
173public:
174 static_assert(N >= 1, "MultiPoleSmoother needs at least one pole");
175
176 static constexpr float epsilon = 1e-6f;
177
178 void reset(double sampleRate, float timeConstantMilliseconds, float initialValue = 0.0f) noexcept;
179 void setTargetValue(float newTarget) noexcept;
180 float getNextValue() noexcept;
181
182 [[nodiscard]] float getCurrentValue() const noexcept { return poles.back().getCurrentValue(); }
183 [[nodiscard]] float getTargetValue() const noexcept { return poles.front().getTargetValue(); }
184 [[nodiscard]] bool isSmoothing() const noexcept;
185 void skip() noexcept;
186
187private:
188 std::array<OnePoleSmoother, N> poles;
189};
190
199{
200public:
201 static constexpr float epsilon = 1e-6f;
202
203 void reset(double sampleRate, float attackMilliseconds, float releaseMilliseconds, float initialValue = 0.0f) noexcept;
204 void setTargetValue(float newTarget) noexcept;
205 float getNextValue() noexcept;
206
207 [[nodiscard]] float getCurrentValue() const noexcept { return static_cast<float>(current); }
208 [[nodiscard]] float getTargetValue() const noexcept { return static_cast<float>(target); }
209 [[nodiscard]] bool isSmoothing() const noexcept;
210 void skip() noexcept;
211
212private:
213 // Double for the same reason as OnePoleSmoother: float recursion stalls
214 // short of the target and the settle check would never fire.
215 double attackCoeff = 0.0;
216 double releaseCoeff = 0.0;
217 double current = 0.0;
218 double target = 0.0;
219};
220
230{
231public:
232 static constexpr float epsilon = 1e-6f;
233
234 void reset(double sampleRate, float maxRatePerSecond, float initialValue = 0.0f) noexcept;
235 void setTargetValue(float newTarget) noexcept;
236 float getNextValue() noexcept;
237
238 [[nodiscard]] float getCurrentValue() const noexcept { return static_cast<float>(current); }
239 [[nodiscard]] float getTargetValue() const noexcept { return static_cast<float>(target); }
240 [[nodiscard]] bool isSmoothing() const noexcept;
241 void skip() noexcept;
242
243private:
244 double maxDelta = 0.0;
245 double current = 0.0;
246 double target = 0.0;
247};
248
259{
260public:
261 static constexpr float epsilon = 1e-6f;
262
263 void reset(double sampleRate, float timeConstantMilliseconds, float q = 0.707f, float initialValue = 0.0f) noexcept;
264 void setTargetValue(float newTarget) noexcept;
265 float getNextValue() noexcept;
266
267 [[nodiscard]] float getCurrentValue() const noexcept { return static_cast<float>(lowpass); }
268 [[nodiscard]] float getTargetValue() const noexcept { return static_cast<float>(target); }
269 [[nodiscard]] bool isSmoothing() const noexcept;
270 void skip() noexcept;
271
272 [[nodiscard]] float getBandPassOutput() const noexcept { return static_cast<float>(bandpass); }
273 [[nodiscard]] float getHighPassOutput() const noexcept { return static_cast<float>(highpass); }
274
275private:
276 double v1 = 0.0;
277 double v2 = 0.0;
278 double g = 0.0;
279 double k = 0.0;
280 double a1 = 0.0;
281 double a2 = 0.0;
282 double a3 = 0.0;
283 double target = 0.0;
284 double lowpass = 0.0;
285 double bandpass = 0.0;
286 double highpass = 0.0;
287};
288
300{
301public:
302 static constexpr float epsilon = 1e-6f;
303
304 void reset(double sampleRate, float timeConstantMilliseconds, float initialValue = 0.0f) noexcept;
305 void setTargetValue(float newTarget) noexcept;
306 float getNextValue() noexcept;
307
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;
312
313private:
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;
317 double target = 0.0;
318 double lastOut = 0.0;
319 double prevOut = 0.0;
320 double velEps = 0.0;
321};
322
328{
329public:
330 void reset(double sampleRate, float timeConstantMilliseconds, float initialValue = 0.0f) noexcept;
331};
332
354{
355public:
356 void reset(double sampleRate, float rampTimeMilliseconds, float initialValue = 0.0f) noexcept;
357 void setTargetValue(float newTarget) noexcept;
358 float getNextValue() noexcept;
359
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;
364
365private:
366 double current = 0.0, target = 0.0;
367 double start = 0.0;
368 double slope0 = 0.0;
369 double invSteps = 1.0;
370 int totalSteps = 0;
371 int stepsDone = 0;
372 int rampSteps = 0;
373};
374
375} // namespace Smoothers
376
377//==============================================================================
378// Inline definitions
379//==============================================================================
380
381// --- LinearSmoother ---
382
383inline void Smoothers::LinearSmoother::reset(double sampleRate, float rampTimeMilliseconds, float initialValue) noexcept
384{
385 totalSteps = static_cast<int>(sampleRate * rampTimeMilliseconds / 1000.0);
386 stepsToGo = 0;
387 step = 0.0f;
388 current = initialValue;
389 target = initialValue;
390}
391
392inline void Smoothers::LinearSmoother::setTargetValue(float newTarget) noexcept
393{
394 if (newTarget == target) return;
395 target = newTarget;
396 stepsToGo = totalSteps;
397 if (stepsToGo > 0)
398 step = (target - current) / static_cast<double>(stepsToGo);
399 else
400 current = target;
401}
402
403inline float Smoothers::LinearSmoother::getNextValue() noexcept
404{
405 if (stepsToGo <= 0) return static_cast<float>(current);
406 current += step;
407 --stepsToGo;
408 if (stepsToGo == 0) current = target;
409 return static_cast<float>(current);
410}
411
412inline void Smoothers::LinearSmoother::setCurrentAndTargetValue(float value) noexcept
413{
414 current = value;
415 target = value;
416 step = 0.0;
417 stepsToGo = 0;
418}
419
420inline bool Smoothers::LinearSmoother::isSmoothing() const noexcept
421{
422 return std::abs(current - target) > epsilon;
423}
424
425inline void Smoothers::LinearSmoother::skip() noexcept
426{
427 current = target;
428 step = 0.0;
429 stepsToGo = 0;
430}
431
432inline void Smoothers::LinearSmoother::processBlock(float* buffer, int numSamples, bool multiply) noexcept
433{
434 // Split the ramping section from the settled remainder so each loop is
435 // trivially auto-vectorizable (the compiler unswitches the loop-invariant
436 // multiply flag out of both bodies).
437 int stepsToProcess = std::min(numSamples, stepsToGo);
438 int i = 0;
439
440 // Ramping section
441 for (; i < stepsToProcess; ++i)
442 {
443 current += step;
444 const float value = static_cast<float>(current);
445 buffer[i] = multiply ? buffer[i] * value : buffer[i] + value;
446 }
447
448 stepsToGo -= stepsToProcess;
449 if (stepsToGo == 0) current = target;
450
451 // Settled section
452 for (; i < numSamples; ++i)
453 {
454 const float value = static_cast<float>(target);
455 buffer[i] = multiply ? buffer[i] * value : buffer[i] + value;
456 }
457}
458
459// --- ExponentialSmoother ---
460
461inline void Smoothers::ExponentialSmoother::reset(double sampleRate, float timeConstantMilliseconds, float initialValue) noexcept
462{
463 totalSteps = static_cast<int>(sampleRate * timeConstantMilliseconds / 1000.0);
464 stepsToGo = 0;
465 coeff = 1.0;
466 current = initialValue;
467 target = initialValue;
468}
469
470inline void Smoothers::ExponentialSmoother::setTargetValue(float newTarget) noexcept
471{
472 if (std::abs(newTarget) < epsilon)
473 newTarget = (newTarget < 0.0f) ? -epsilon : epsilon;
474
475 if (newTarget == target) return;
476 target = newTarget;
477
478 if (totalSteps > 0 && std::abs(current) > epsilon)
479 {
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;
483 if (ratio > 0.0)
484 {
485 stepsToGo = totalSteps;
486 coeff = std::exp(std::log(ratio) / static_cast<double>(stepsToGo));
487 return;
488 }
489 }
490
491 // Snap. The ramp must also stop here: leaving stepsToGo armed replays the
492 // previous ramp's coefficient, dragging the value away from the target
493 // for a whole ramp and then jumping back (a sign flip after a 1 -> 2 ramp
494 // went -1 -> -1.997 -> -1).
495 current = target;
496 coeff = 1.0;
497 stepsToGo = 0;
498}
499
500inline float Smoothers::ExponentialSmoother::getNextValue() noexcept
501{
502 if (stepsToGo <= 0) return static_cast<float>(current);
503 current *= coeff;
504 --stepsToGo;
505 if (stepsToGo == 0) current = target;
506 return static_cast<float>(current);
507}
508
509inline void Smoothers::ExponentialSmoother::setCurrentAndTargetValue(float value) noexcept
510{
511 current = value;
512 target = value;
513 coeff = 1.0;
514 stepsToGo = 0;
515}
516
517inline bool Smoothers::ExponentialSmoother::isSmoothing() const noexcept
518{
519 return std::abs(current - target) > epsilon;
520}
521
522inline void Smoothers::ExponentialSmoother::skip() noexcept
523{
524 current = target;
525 coeff = 1.0;
526 stepsToGo = 0;
527}
528
529// --- OnePoleSmoother ---
530
531inline void Smoothers::OnePoleSmoother::reset(double sampleRate, float timeConstantMilliseconds, float initialValue) noexcept
532{
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;
538}
539
540inline void Smoothers::OnePoleSmoother::setTargetValue(float newTarget) noexcept
541{
542 target = newTarget;
543}
544
545inline float Smoothers::OnePoleSmoother::getNextValue() noexcept
546{
547 if (!isSmoothing()) { skip(); return static_cast<float>(target); } // Anti-denormal check
548 current = target + coeff * (current - target);
549 return static_cast<float>(current);
550}
551
552inline bool Smoothers::OnePoleSmoother::isSmoothing() const noexcept
553{
554 return std::abs(current - target) > epsilon;
555}
556
557inline void Smoothers::OnePoleSmoother::skip() noexcept
558{
559 current = target;
560}
561
562// --- MultiPoleSmoother ---
563
564template <std::size_t N>
565inline void Smoothers::MultiPoleSmoother<N>::reset(double sampleRate, float timeConstantMilliseconds, float initialValue) noexcept
566{
567 for (auto& pole : poles)
568 pole.reset(sampleRate, timeConstantMilliseconds, initialValue);
569}
570
571template <std::size_t N>
572inline void Smoothers::MultiPoleSmoother<N>::setTargetValue(float newTarget) noexcept
573{
574 poles[0].setTargetValue(newTarget);
575}
576
577template <std::size_t N>
579{
580 float val = poles[0].getNextValue();
581 for (std::size_t i = 1; i < N; ++i)
582 {
583 poles[i].setTargetValue(val);
584 val = poles[i].getNextValue();
585 }
586 return val;
587}
588
589template <std::size_t N>
591{
592 // Compare the chain OUTPUT against the GLOBAL target (pole 0's target).
593 // Asking the last pole alone is wrong on both ends: right after
594 // setTargetValue() only pole 0 knows the new target (the last pole would
595 // report "settled" before the ramp even starts), and mid-ramp each pole
596 // sits close to its neighbour while the whole chain is far from the goal.
597 return std::abs(poles.back().getCurrentValue()
598 - poles.front().getTargetValue()) > epsilon;
599}
600
601template <std::size_t N>
603{
604 // Propagate the global target down the chain first: inner poles still
605 // chase the PREVIOUS output of their predecessor, so skipping them in
606 // place would freeze the chain mid-way instead of landing on the target.
607 const float t = poles.front().getTargetValue();
608 for (auto& pole : poles)
609 {
610 pole.setTargetValue(t);
611 pole.skip();
612 }
613}
614
615// --- AsymmetricSmoother ---
616
617inline void Smoothers::AsymmetricSmoother::reset(double sampleRate, float attackMilliseconds, float releaseMilliseconds, float initialValue) noexcept
618{
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;
625}
626
627inline void Smoothers::AsymmetricSmoother::setTargetValue(float newTarget) noexcept
628{
629 target = newTarget;
630}
631
632inline float Smoothers::AsymmetricSmoother::getNextValue() noexcept
633{
634 if (!isSmoothing()) { skip(); return static_cast<float>(target); } // Anti-denormal check
635 const double c = (target > current) ? attackCoeff : releaseCoeff;
636 current = target + c * (current - target);
637 return static_cast<float>(current);
638}
639
640inline bool Smoothers::AsymmetricSmoother::isSmoothing() const noexcept
641{
642 return std::abs(current - target) > epsilon;
643}
644
645inline void Smoothers::AsymmetricSmoother::skip() noexcept
646{
647 current = target;
648}
649
650// --- SlewLimiter ---
651
652inline void Smoothers::SlewLimiter::reset(double sampleRate, float maxRatePerSecond, float initialValue) noexcept
653{
654 // Clamp at zero: a negative rate would flip the clamp bounds below
655 // (std::clamp with lo > hi is undefined behaviour). Zero legitimately
656 // means "frozen": the value is not allowed to move at all.
657 maxDelta = std::max(0.0, static_cast<double>(maxRatePerSecond) / sampleRate);
658 current = initialValue;
659 target = initialValue;
660}
661
662inline void Smoothers::SlewLimiter::setTargetValue(float newTarget) noexcept
663{
664 target = newTarget;
665}
666
667inline float Smoothers::SlewLimiter::getNextValue() noexcept
668{
669 double delta = target - current;
670 delta = std::clamp(delta, -maxDelta, maxDelta);
671 current += delta;
672 return static_cast<float>(current);
673}
674
675inline bool Smoothers::SlewLimiter::isSmoothing() const noexcept
676{
677 return std::abs(current - target) > epsilon;
678}
679
680inline void Smoothers::SlewLimiter::skip() noexcept
681{
682 current = target;
683}
684
685// --- StateVariableSmoother ---
686
687inline void Smoothers::StateVariableSmoother::reset(double sampleRate, float timeConstantMilliseconds, float q, float initialValue) noexcept
688{
689 const double timeConstantSeconds = static_cast<double>(timeConstantMilliseconds) / 1000.0;
690 const double fs = sampleRate;
691 // A zero time constant means "instantaneous", not "frozen": map it to the
692 // fastest usable cutoff (fc = 0 would zero the coefficients and the
693 // output would never move again). Clamp below Nyquist so tan(pi*fc/fs)
694 // stays finite/stable for very small time constants (otherwise the TPT
695 // prewarp blows up near fs/2).
696 double fc = timeConstantSeconds > 1e-9 ? 1.0 / (static_cast<double>(Constants::twoPi) * timeConstantSeconds) : fs;
697 fc = std::min(fc, fs * 0.49);
698
699 g = std::tan(dspark::pi<double> * fc / fs);
700 k = 1.0 / static_cast<double>(std::max(q, 0.01f)); // TPT damping uses k = 1/Q.
701
702 a1 = 1.0 / (1.0 + g * (g + k));
703 a2 = g * a1;
704 a3 = g * a2;
705
706 v1 = 0.0;
707 v2 = initialValue;
708 target = initialValue;
709 lowpass = initialValue;
710 bandpass = 0.0;
711 highpass = 0.0;
712}
713
714inline void Smoothers::StateVariableSmoother::setTargetValue(float newTarget) noexcept
715{
716 target = newTarget;
717}
718
719inline float Smoothers::StateVariableSmoother::getNextValue() noexcept
720{
721 if (!isSmoothing()) { skip(); return static_cast<float>(target); } // Anti-denormal check
722
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;
729
730 lowpass = v2_new;
731 bandpass = v1_new;
732 highpass = v0 - k * v1_new - v2_new;
733
734 return static_cast<float>(lowpass);
735}
736
737inline bool Smoothers::StateVariableSmoother::isSmoothing() const noexcept
738{
739 // The bandpass term is the filter's velocity. Without it, a Q > 0.5
740 // response crossing the target (overshoot) can momentarily satisfy
741 // |lowpass - target| < epsilon at full speed, and the anti-denormal
742 // check in getNextValue() would snap mid-flight, truncating the
743 // trajectory with a derivative kink.
744 return std::abs(lowpass - target) > epsilon || std::abs(bandpass) > epsilon;
745}
746
747inline void Smoothers::StateVariableSmoother::skip() noexcept
748{
749 v1 = 0.0;
750 v2 = target;
751 lowpass = target;
752 bandpass = 0.0;
753 highpass = 0.0;
754}
755
756// --- ButterworthSmoother ---
757
758inline void Smoothers::ButterworthSmoother::reset(double sampleRate, float timeConstantMilliseconds, float initialValue) noexcept
759{
760 constexpr double kSqrt2 = 1.4142135623730950488;
761 const double timeConstantSeconds = static_cast<double>(timeConstantMilliseconds) / 1000.0;
762 const double fs = sampleRate;
763 // Zero time constant maps to the fastest usable cutoff (see the SVF
764 // smoother): fc = 0 would freeze the filter short of the target forever.
765 double fc = timeConstantSeconds > 1e-9 ? 1.0 / (static_cast<double>(Constants::twoPi) * timeConstantSeconds) : fs;
766 fc = std::min(fc, fs * 0.49); // keep below Nyquist for a stable bilinear prewarp
767 const double tanw = std::tan(dspark::pi<double> * fc / fs);
768 const double tanw2 = tanw * tanw;
769
770 const double denom = 1.0 + kSqrt2 * tanw + tanw2;
771
772 b0 = tanw2 / denom;
773 b1 = 2.0 * tanw2 / denom;
774 b2 = tanw2 / denom;
775
776 a1 = 2.0 * (tanw2 - 1.0) / denom;
777 a2 = (1.0 - kSqrt2 * tanw + tanw2) / denom;
778
779 // The motion threshold for the settle check: the peak per-sample velocity
780 // of a residual oscillation of amplitude epsilon at fc. Near the target
781 // crossing of the overshoot the output moves fast in filter time but by
782 // tiny per-sample amounts (proportional to fc/fs), so comparing the
783 // one-sample difference against the plain epsilon would still snap
784 // mid-flight for small targets.
785 velEps = static_cast<double>(epsilon) * (static_cast<double>(Constants::twoPi) * fc / fs);
786
787 // TDF-II steady state for a constant input/output v requires the s2 term
788 // inside s1: s2* = (b2 - a2)*v and s1* = (b1 - a1)*v + s2*. Without the
789 // + s2 term the first sample of every new ramp overshot to ~2x the value
790 // (an audible click, the opposite of a smoother's job).
791 s2 = (b2 - a2) * initialValue;
792 s1 = (b1 - a1) * initialValue + s2;
793 target = initialValue;
794 lastOut = initialValue;
795 prevOut = initialValue;
796}
797
798inline void Smoothers::ButterworthSmoother::setTargetValue(float newTarget) noexcept
799{
800 target = newTarget;
801}
802
803inline float Smoothers::ButterworthSmoother::getNextValue() noexcept
804{
805 if (!isSmoothing()) { skip(); return static_cast<float>(target); } // Anti-denormal check
806
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;
811
812 prevOut = lastOut;
813 lastOut = y0;
814 return static_cast<float>(y0);
815}
816
817inline bool Smoothers::ButterworthSmoother::isSmoothing() const noexcept
818{
819 // Error AND motion: a Butterworth response (Q = 0.707) overshoots, so
820 // the output crosses the target at full speed. Judging by the error
821 // alone, the anti-denormal check in getNextValue() would snap exactly
822 // at that crossing and truncate the remaining trajectory. Motion is
823 // measured against velEps (velocity of an epsilon-amplitude residual),
824 // the same role the bandpass term plays in the SVF smoother.
825 return std::abs(lastOut - target) > epsilon
826 || std::abs(lastOut - prevOut) > velEps;
827}
828
829inline void Smoothers::ButterworthSmoother::skip() noexcept
830{
831 // Same steady-state form as reset(): s1 must include the settled s2.
832 s2 = (b2 - a2) * target;
833 s1 = (b1 - a1) * target + s2;
834 lastOut = target;
835 prevOut = target;
836}
837
838// --- HermiteSmoother ---
839
840inline void Smoothers::HermiteSmoother::reset(double sampleRate, float rampTimeMilliseconds, float initialValue) noexcept
841{
842 // A NaN or negative ramp is no ramp; the cap keeps the cast defined.
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;
846 slope0 = 0.0;
847 totalSteps = stepsDone = 0;
848 invSteps = 1.0;
849}
850
851inline void Smoothers::HermiteSmoother::setTargetValue(float newTarget) noexcept
852{
853 // NaN/Inf would leave the segment unable to land.
854 if (!std::isfinite(newTarget) || static_cast<double>(newTarget) == target) return;
855 // Velocity of the segment in flight at its current position, in value
856 // per sample: d/du of the cubic, divided by the segment length.
857 double velocity = 0.0;
858 if (stepsDone < totalSteps)
859 {
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;
863 }
864 target = newTarget;
865 start = current;
866 if (rampSteps <= 0)
867 {
868 current = start = target;
869 slope0 = 0.0;
870 totalSteps = stepsDone = 0;
871 return;
872 }
873 totalSteps = rampSteps;
874 stepsDone = 0;
875 invSteps = 1.0 / totalSteps;
876 // Start velocity in value per segment; past three times the distance a
877 // cubic overshoots, so clip there (Fritsch-Carlson limit). Opposite
878 // velocity turns the glide around without passing the target.
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);
883 else m = 0.0;
884 slope0 = m;
885}
886
887inline float Smoothers::HermiteSmoother::getNextValue() noexcept
888{
889 if (stepsDone >= totalSteps) return static_cast<float>(current);
890 if (++stepsDone == totalSteps)
891 {
892 current = target; // exact arrival
893 return static_cast<float>(current);
894 }
895 const double u = stepsDone * invSteps;
896 const double u2 = u * u;
897 const double d = target - start;
898 // Hermite basis with p(0) = start, p'(0) = slope0, p(1) = target, p'(1) = 0.
899 current = start + d * (3.0 * u2 - 2.0 * u2 * u) + slope0 * (u2 * u - 2.0 * u2 + u);
900 return static_cast<float>(current);
901}
902
903inline void Smoothers::HermiteSmoother::skip() noexcept
904{
905 current = start = target;
906 slope0 = 0.0;
907 totalSteps = stepsDone = 0;
908}
909
910// --- CriticallyDampedSmoother ---
911
912inline void Smoothers::CriticallyDampedSmoother::reset(double sampleRate, float timeConstantMilliseconds, float initialValue) noexcept
913{
914 // Critical damping (zeta = 1) corresponds exactly to Q = 0.5, not to the
915 // Butterworth Q = 0.707.
916 StateVariableSmoother::reset(sampleRate, timeConstantMilliseconds, 0.5f, initialValue);
917}
918
919} // namespace dspark
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...).
Definition DspMath.h:60
constexpr T pi
Pi (3.14159...) for the given floating-point type.
Definition DspMath.h:48
constexpr T twoPi
2 * Pi (6.28318...).
Definition DspMath.h:51
Smoother with asymmetric attack/release times.
Definition Smoothers.h:199
float getTargetValue() const noexcept
Definition Smoothers.h:208
Butterworth low-pass smoother for maximally flat response.
Definition Smoothers.h:300
float getTargetValue() const noexcept
Definition Smoothers.h:309
Critically damped smoother (no overshoot, exact Q=0.5).
Definition Smoothers.h:328
Exponential (multiplicative) smoother for natural, perceptual responses.
Definition Smoothers.h:111
float getTargetValue() const noexcept
Definition Smoothers.h:120
Cubic glide that lands exactly on the target after a fixed time, with a continuous slope.
Definition Smoothers.h:354
bool isSmoothing() const noexcept
Definition Smoothers.h:362
float getCurrentValue() const noexcept
Definition Smoothers.h:360
float getTargetValue() const noexcept
Definition Smoothers.h:361
Linear ramp smoother for predictable, uniform interpolation.
Definition Smoothers.h:60
void setCurrentAndTargetValue(float value) noexcept
Definition Smoothers.h:412
void processBlock(float *buffer, int numSamples, bool multiply=false) noexcept
Block processing for SIMD optimization.
Definition Smoothers.h:432
void reset(double sampleRate, float rampTimeMilliseconds, float initialValue=0.0f) noexcept
Definition Smoothers.h:383
float getTargetValue() const noexcept
Definition Smoothers.h:69
void setTargetValue(float newTarget) noexcept
Definition Smoothers.h:392
static constexpr float epsilon
Definition Smoothers.h:62
bool isSmoothing() const noexcept
Definition Smoothers.h:420
float getCurrentValue() const noexcept
Definition Smoothers.h:68
Templated cascaded multi-pole smoother for steeper roll-off.
Definition Smoothers.h:172
bool isSmoothing() const noexcept
Definition Smoothers.h:590
float getTargetValue() const noexcept
Definition Smoothers.h:183
void setTargetValue(float newTarget) noexcept
Definition Smoothers.h:572
void reset(double sampleRate, float timeConstantMilliseconds, float initialValue=0.0f) noexcept
Definition Smoothers.h:565
Authentic one-pole exponential IIR low-pass smoother.
Definition Smoothers.h:147
float getTargetValue() const noexcept
Definition Smoothers.h:156
Rate limiter to cap maximum change per sample.
Definition Smoothers.h:230
float getTargetValue() const noexcept
Definition Smoothers.h:239
Second-order state variable filter (SVF) smoother (TPT implementation).
Definition Smoothers.h:259
float getTargetValue() const noexcept
Definition Smoothers.h:268
float getHighPassOutput() const noexcept
Definition Smoothers.h:273