96 void prepare(
double sampleRate,
double rampTimeMs = 20.0) noexcept
98 sampleRate_ = std::max(1.0, sampleRate);
100 rampTimeMs_ = std::max(0.0, rampTimeMs);
105 const double tauSamples = sampleRate_ * rampTimeMs_ / 1000.0;
106 expCoeff_ = (tauSamples > 0.0) ? std::exp(-1.0 / tauSamples) : 0.0;
110 rampSamples_ = std::max(1.0, rampTimeMs_ * sampleRate_ / 1000.0);
123 const double srRatio = 44100.0 / sampleRate_;
124 const double m = std::pow(0.9999, srRatio);
125 const double stretch = std::max(rampTimeMs_, 1e-3) / kChaseNaturalMs;
129 chaseAddDecay_ = 0.01 * srRatio;
133 const double ms = std::pow(m, 1.0 / stretch);
134 chaseMultDecay_ = ms;
135 chaseAddDecay_ = 0.01 * srRatio * stretch * (1.0 - ms) / (1.0 - m);
137 chaseMinSpeed_ = kChaseMinSpeed * stretch;
138 chaseMaxSpeed_ = kChaseMaxSpeed * stretch;
139 chaseRateScale_ = 1.0 / srRatio;
140 if (chaseSpeed_ < chaseMinSpeed_ || chaseSpeed_ > chaseMaxSpeed_) chaseSpeed_ = chaseMinSpeed_;
171 if (!(span > 0.0) || !std::isfinite(span) || !std::isfinite(fastMs) || !std::isfinite(slowMs))
177 adaptiveSpan_ = span;
178 adaptiveFastMs_ = std::max(0.0, fastMs);
179 adaptiveSlowMs_ = std::max(0.0, slowMs);
195 if (!std::isfinite(newTarget))
return;
196 if (newTarget != target_)
199 snapEps_ = snapEpsilonFor(newTarget);
202 chaseSpeed_ = chaseMaxSpeed_;
212 const double target =
static_cast<double>(target_);
213 if (current_ == target)
return target_;
219 current_ = target + expCoeff_ * (current_ - target);
224 if (current_ < target)
225 current_ = std::min(current_ + linearRate_, target);
227 current_ = std::max(current_ - linearRate_, target);
237 if (rampTimeMs_ <= 0.0) { current_ = target;
break; }
240 chaseSpeed_ = std::max(chaseMinSpeed_, chaseSpeed_ * chaseMultDecay_ - chaseAddDecay_);
241 const double speed = chaseSpeed_ * chaseRateScale_;
242 current_ = (current_ * speed + target) / (speed + 1.0);
250 if (std::abs(current_ - target) < snapEps_)
253 return static_cast<T
>(current_);
269 const double target =
static_cast<double>(target_);
270 const size_t n = buffer.size();
271 if (current_ == target)
273 std::fill(buffer.begin(), buffer.end(), target_);
281 double current = current_;
282 const double eps = snapEps_;
289 const double coeff = expCoeff_;
292 current = target + coeff * (current - target);
293 if (std::abs(current - target) < eps) { current = target;
break; }
294 buffer[i] =
static_cast<T
>(current);
301 const double rate = linearRate_;
304 if (current < target) current = std::min(current + rate, target);
305 else if (current > target) current = std::max(current - rate, target);
306 if (std::abs(current - target) < eps) { current = target;
break; }
307 buffer[i] =
static_cast<T
>(current);
318 if (rampTimeMs_ <= 0.0) { current = target;
break; }
319 double speed = chaseSpeed_;
320 const double multDecay = chaseMultDecay_;
321 const double addDecay = chaseAddDecay_;
322 const double rateScale = chaseRateScale_;
323 const double lo = chaseMinSpeed_;
326 speed = std::max(lo, speed * multDecay - addDecay);
327 const double scaled = speed * rateScale;
328 current = (current * scaled + target) / (scaled + 1.0);
329 if (std::abs(current - target) < eps) { current = target;
break; }
330 buffer[i] =
static_cast<T
>(current);
351 [[nodiscard]]
bool isSmoothing() const noexcept {
return current_ !=
static_cast<double>(target_); }
354 void skip() noexcept { current_ =
static_cast<double>(target_); chaseSpeed_ = chaseMinSpeed_; }
360 snapEps_ = snapEpsilonFor(value);
361 current_ =
static_cast<double>(value);
362 chaseSpeed_ = chaseMinSpeed_;
368 prepare(sampleRate, rampTimeMs);
373 [[nodiscard]]
static double snapEpsilonFor(T target)
noexcept
375 return kEpsilon * std::max(1.0, std::abs(
static_cast<double>(target)));
378 void updateLinearRate() noexcept
380 const double distance = std::abs(
static_cast<double>(target_) - current_);
381 double samples = rampSamples_;
382 if (adaptiveSpan_ > 0.0)
384 const double closeness = std::max(0.0, 1.0 - distance / adaptiveSpan_);
385 const double ms = adaptiveFastMs_ + (adaptiveSlowMs_ - adaptiveFastMs_) * closeness * closeness;
386 samples = std::max(1.0, ms * sampleRate_ / 1000.0);
388 linearRate_ = distance / samples;
391 double current_{ 0.0 };
396 double expCoeff_{ 0.0 };
397 double linearRate_{ 0.0 };
398 double rampSamples_{ 882.0 };
401 double adaptiveSpan_{ 0.0 };
402 double adaptiveFastMs_{ 20.0 };
403 double adaptiveSlowMs_{ 150.0 };
406 static constexpr double kChaseNaturalMs = 171.0;
407 static constexpr double kChaseMinSpeed = 350.0;
408 static constexpr double kChaseMaxSpeed = 2500.0;
409 double chaseSpeed_{ 350.0 };
410 double chaseMultDecay_{ 0.9999 };
411 double chaseAddDecay_{ 0.01 };
412 double chaseRateScale_{ 1.0 };
413 double chaseMinSpeed_{ 350.0 };
414 double chaseMaxSpeed_{ 2500.0 };
416 double sampleRate_{ 44100.0 };
417 double rampTimeMs_{ 20.0 };
419 static constexpr double kEpsilon = 1e-7;
420 double snapEps_ { kEpsilon };
Zero-allocation parameter smoother for real-time audio.
void setSmoothingType(SmoothingType type) noexcept
Sets the smoothing algorithm.
SmoothedValue & operator=(const SmoothedValue &)=delete
void reset(T value=T(0)) noexcept
Hard-resets both current and target states to a specific value.
void setRampTime(double sampleRate, double rampTimeMs) noexcept
Updates sample rate and/or ramp time, recalculating internal steps.
T getNextValue() noexcept
Calculates and returns the next smoothed value.
T getCurrentValue() const noexcept
Returns the current internal value without advancing the state.
bool hasAdaptiveRamp() const noexcept
Whether step-dependent ramps are on (see setAdaptiveRamp()).
void skip() noexcept
Forces the current value to instantly match the target, bypassing time.
bool isSmoothing() const noexcept
Evaluates if the smoother is actively transitioning.
void setTargetValue(T newTarget) noexcept
Updates the target value. Safe to call continuously (e.g., from host automation).
@ Linear
Rate-limited ramp. Constant velocity, exact target arrival.
@ Disabled
Instant snapping, no smoothing applied.
@ Exponential
One-pole IIR. Natural, musical interpolation.
@ Chase
Adaptive speed. Gentle start after a jump, accelerating settle.
void processBlock(std::span< T > buffer) noexcept
Computes a block of smoothed values into an output buffer.
SmoothedValue(const SmoothedValue &)=delete
Prevents accidental copying of stateful DSP objects.
SmoothingType getSmoothingType() const noexcept
Returns the current smoothing algorithm.
T getTargetValue() const noexcept
Returns the set target value.
void prepare(double sampleRate, double rampTimeMs=20.0) noexcept
Precalculates internal coefficients based on sample rate and timing.
void setAdaptiveRamp(double span, double fastMs, double slowMs) noexcept
Makes the duration of every Linear ramp depend on its step.
Main namespace for the DSPark framework.