DSPark 1.6.1
Header-only audio DSP framework in pure C++20 — zero dependencies
Loading...
Searching...
No Matches
dspark::SmoothedValue< T > Class Template Reference

Zero-allocation parameter smoother for real-time audio. More...

#include <SmoothedValue.h>

Public Types

enum class  SmoothingType { Exponential , Linear , Disabled , Chase }
 

Public Member Functions

 SmoothedValue (const SmoothedValue &)=delete
 Prevents accidental copying of stateful DSP objects.
 
SmoothedValueoperator= (const SmoothedValue &)=delete
 
 SmoothedValue () noexcept
 
void prepare (double sampleRate, double rampTimeMs=20.0) noexcept
 Precalculates internal coefficients based on sample rate and timing.
 
void setSmoothingType (SmoothingType type) noexcept
 Sets the smoothing algorithm.
 
SmoothingType getSmoothingType () const noexcept
 Returns the current smoothing algorithm.
 
void setTargetValue (T newTarget) noexcept
 Updates the target value. Safe to call continuously (e.g., from host automation).
 
getNextValue () noexcept
 Calculates and returns the next smoothed value.
 
void processBlock (std::span< T > buffer) noexcept
 Computes a block of smoothed values into an output buffer.
 
getCurrentValue () const noexcept
 Returns the current internal value without advancing the state.
 
getTargetValue () const noexcept
 Returns the set target value.
 
bool isSmoothing () const noexcept
 Evaluates if the smoother is actively transitioning.
 
void skip () noexcept
 Forces the current value to instantly match the target, bypassing time.
 
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.
 

Detailed Description

template<FloatType T>
class dspark::SmoothedValue< T >

Zero-allocation parameter smoother for real-time audio.

Provides click-free parameter transitions using various curves, processed per-sample or per-block in the audio thread. A default-constructed smoother is functional at 44.1 kHz with a 20 ms ramp; prepare() adapts it to the real rate.

Timing semantics of rampTimeMs per curve:

  • Exponential: one-pole time constant (63% arrival). The value snaps exactly onto the target once within a relative epsilon, about 16 time constants after a unit step, and isSmoothing() then reports false.
  • Linear: constant velocity sized to traverse a UNIT step in rampTimeMs; smaller steps arrive proportionally sooner, exactly on target.
  • Chase: adaptive (not clocked): gentle right after a target change, accelerating as it settles.

The recursion state is double regardless of T (the framework rule for recursive state): in float the one-pole stalls hundreds of epsilons short of the target when the per-sample increment rounds to zero, which would leave isSmoothing() true forever (and callers that gate work on it, like Gain's bulk path or CrossoverFilter's coefficient updates, stuck on their slow path).

Note
Thread Safety: This class is not inherently thread-safe to avoid atomic memory barriers in the DSP hot-path. Parameter updates via setTargetValue() should occur synchronously within the audio thread (e.g., processing an event queue at the start of a block).
Template Parameters
TValue type (float or double, constrained by FloatType).

Definition at line 60 of file SmoothedValue.h.

Member Enumeration Documentation

◆ SmoothingType

template<FloatType T>
enum class dspark::SmoothedValue::SmoothingType
strong
Enumerator
Exponential 

One-pole IIR. Natural, musical interpolation.

Linear 

Rate-limited ramp. Constant velocity, exact target arrival.

Disabled 

Instant snapping, no smoothing applied.

Chase 

Adaptive speed (Airwindows style). Gentle start after a jump, accelerating settle.

Definition at line 63 of file SmoothedValue.h.

Constructor & Destructor Documentation

◆ SmoothedValue() [1/2]

template<FloatType T>
dspark::SmoothedValue< T >::SmoothedValue ( const SmoothedValue< T > &  )
delete

Prevents accidental copying of stateful DSP objects.

◆ SmoothedValue() [2/2]

template<FloatType T>
dspark::SmoothedValue< T >::SmoothedValue ( )
inlinenoexcept

Definition at line 75 of file SmoothedValue.h.

Member Function Documentation

◆ getCurrentValue()

template<FloatType T>
T dspark::SmoothedValue< T >::getCurrentValue ( ) const
inlinenoexcept

Returns the current internal value without advancing the state.

Definition at line 255 of file SmoothedValue.h.

◆ getNextValue()

template<FloatType T>
T dspark::SmoothedValue< T >::getNextValue ( )
inlinenoexcept

Calculates and returns the next smoothed value.

Returns
The updated current value.

Definition at line 127 of file SmoothedValue.h.

◆ getSmoothingType()

template<FloatType T>
SmoothingType dspark::SmoothedValue< T >::getSmoothingType ( ) const
inlinenoexcept

Returns the current smoothing algorithm.

Definition at line 105 of file SmoothedValue.h.

◆ getTargetValue()

template<FloatType T>
T dspark::SmoothedValue< T >::getTargetValue ( ) const
inlinenoexcept

Returns the set target value.

Definition at line 258 of file SmoothedValue.h.

◆ isSmoothing()

template<FloatType T>
bool dspark::SmoothedValue< T >::isSmoothing ( ) const
inlinenoexcept

Evaluates if the smoother is actively transitioning.

Definition at line 261 of file SmoothedValue.h.

◆ operator=()

template<FloatType T>
SmoothedValue & dspark::SmoothedValue< T >::operator= ( const SmoothedValue< T > &  )
delete

◆ prepare()

template<FloatType T>
void dspark::SmoothedValue< T >::prepare ( double  sampleRate,
double  rampTimeMs = 20.0 
)
inlinenoexcept

Precalculates internal coefficients based on sample rate and timing.

Parameters
sampleRateSample rate in Hz (must be > 0).
rampTimeMsSmoothing duration in milliseconds (must be > 0). See the class notes for the per-curve semantics.

Definition at line 83 of file SmoothedValue.h.

◆ processBlock()

template<FloatType T>
void dspark::SmoothedValue< T >::processBlock ( std::span< T >  buffer)
inlinenoexcept

Computes a block of smoothed values into an output buffer.

Recommended over per-sample getNextValue() calls: the curve branch is resolved once and the recursion state stays in registers. (The recursion itself is serial, so the loop does not vectorise; a settled smoother short-circuits to a plain fill.) Sample-exact match with getNextValue().

Parameters
bufferSpan representing the output buffer to fill.

Definition at line 181 of file SmoothedValue.h.

◆ reset()

template<FloatType T>
void dspark::SmoothedValue< T >::reset ( value = T(0))
inlinenoexcept

Hard-resets both current and target states to a specific value.

Definition at line 267 of file SmoothedValue.h.

◆ setRampTime()

template<FloatType T>
void dspark::SmoothedValue< T >::setRampTime ( double  sampleRate,
double  rampTimeMs 
)
inlinenoexcept

Updates sample rate and/or ramp time, recalculating internal steps.

Definition at line 275 of file SmoothedValue.h.

◆ setSmoothingType()

template<FloatType T>
void dspark::SmoothedValue< T >::setSmoothingType ( SmoothingType  type)
inlinenoexcept

Sets the smoothing algorithm.

Definition at line 102 of file SmoothedValue.h.

◆ setTargetValue()

template<FloatType T>
void dspark::SmoothedValue< T >::setTargetValue ( newTarget)
inlinenoexcept

Updates the target value. Safe to call continuously (e.g., from host automation).

Parameters
newTargetThe destination value. NaN is ignored (it would poison the recursion and the arrival checks permanently).

Definition at line 112 of file SmoothedValue.h.

◆ skip()

template<FloatType T>
void dspark::SmoothedValue< T >::skip ( )
inlinenoexcept

Forces the current value to instantly match the target, bypassing time.

Definition at line 264 of file SmoothedValue.h.


The documentation for this class was generated from the following file: