MostlyHarmless 0.0.1
 
Loading...
Searching...
No Matches
marvin::utils::SmoothedValue< SampleType, Type > Class Template Reference

A utility class to smooth discrete values over a given period. More...

#include <marvin_SmoothedValue.h>

Public Member Functions

void reset (int stepsSamples, bool skipRemaining=true)
 
void reset (double sampleRate, double timeMs, bool skipRemaining=true)
 
void setCurrentAndTargetValue (SampleType newValue)
 
void setTargetValue (SampleType newValue)
 
SampleType operator() () noexcept
 
bool isSmoothing () const noexcept
 
int getRemainingSamples () const noexcept
 
SampleType getTargetValue () const noexcept
 

Detailed Description

template<FloatType SampleType, SmoothingType Type>
class marvin::utils::SmoothedValue< SampleType, Type >

A utility class to smooth discrete values over a given period.

Mainly useful to help with zippering caused by discrete parameter updates. Can be configured to either smooth linearly or exponentially to the target value.

Member Function Documentation

◆ getRemainingSamples()

template<FloatType SampleType, SmoothingType Type>
int marvin::utils::SmoothedValue< SampleType, Type >::getRemainingSamples ( ) const
nodiscardnoexcept
Returns
The number of samples left to reach the target (if linear), or the number of samples left to reach 1% of the target (if exponential)

◆ getTargetValue()

template<FloatType SampleType, SmoothingType Type>
SampleType marvin::utils::SmoothedValue< SampleType, Type >::getTargetValue ( ) const
nodiscardnoexcept
Returns
The value the smoother is smoothing towards.

◆ isSmoothing()

template<FloatType SampleType, SmoothingType Type>
bool marvin::utils::SmoothedValue< SampleType, Type >::isSmoothing ( ) const
nodiscardnoexcept

Checks if the smoother has reached its target value.

Returns
Whether the smoother has reached its target value.

◆ operator()()

template<FloatType SampleType, SmoothingType Type>
SampleType marvin::utils::SmoothedValue< SampleType, Type >::operator() ( )
nodiscardnoexcept

Performs a single tick of the smoothing function.
If the SmoothedValue is configured to use SmoothingType::Linear, the formula is v = v+slew, where slew has been calculated based on the target value.
If the SmoothedValue is configured to use SmoothingType::Exponential, the formula is v = v + (target = v) * slew.

Returns
The smoothed value.

◆ reset() [1/2]

template<FloatType SampleType, SmoothingType Type>
void marvin::utils::SmoothedValue< SampleType, Type >::reset ( double sampleRate,
double timeMs,
bool skipRemaining = true )

Sets the period of the smoothing, and optionally sets the current value to the target value.

Parameters
sampleRateThe currently configured sample rate.
timeMsThe period of the smoothing, in milliseconds.
skipRemainingIf true, sets the current value to the target value.

◆ reset() [2/2]

template<FloatType SampleType, SmoothingType Type>
void marvin::utils::SmoothedValue< SampleType, Type >::reset ( int stepsSamples,
bool skipRemaining = true )

Sets the period of the smoothing, and optionally sets the current value to the target value. If Type == SmoothingType::Linear, the interpolation will take exactly this many samples. If Type == SmoothingType::Exponential, then the period actually specifies the time (in samples) it will take for the smoother to reach 63.2% (approx 1 - 1/e) of the target value. This is the mathematical definition of an exponential smoother's time constant, so was chosen for that reason. If you'd instead like to set the time taken to reach some arbitary percent distance from the target value, then you can calculate the value for it with something like the below snippet.

constexpr static auto withinPc{ 10.0f }; // Within 10% of target value
const auto period = stepsSamples / -std::log(withinPc / 100.0f); // period samples to reach 90% of the target value.
Parameters
stepsSamplesThe period of the smoothing, in samples.
skipRemainingIf true, sets the current value to the target value.

◆ setCurrentAndTargetValue()

template<FloatType SampleType, SmoothingType Type>
void marvin::utils::SmoothedValue< SampleType, Type >::setCurrentAndTargetValue ( SampleType newValue)

Sets both the current value, and the target value to interpolate to.

Parameters
newValueThe new target (and current) value.

◆ setTargetValue()

template<FloatType SampleType, SmoothingType Type>
void marvin::utils::SmoothedValue< SampleType, Type >::setTargetValue ( SampleType newValue)

Sets the value the smoother should interpolate to.

Parameters
newValueThe new target value.

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