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

Holds a sample value for N samples or until externally triggered. More...

#include <SampleAndHold.h>

Public Types

enum class  Mode { Counter , Trigger }
 Defines the capture behavior of the processor. More...
 

Public Member Functions

void setMode (Mode mode) noexcept
 Sets the operating mode.
 
void setHoldSamples (int numSamples) noexcept
 Sets the hold duration for Counter mode in samples.
 
void setHoldRate (double targetRate, double actualRate) noexcept
 Sets the hold period based on a target effective sample rate.
 
process (T input, bool trigger=false) noexcept
 Processes a single sample.
 
void processBlock (T *data, int numSamples) noexcept
 Processes a block of samples in-place (Counter mode optimized).
 
void processBlock (T *data, const bool *triggers, int numSamples) noexcept
 Processes a block of samples in-place using an external trigger buffer.
 
getHeldValue () const noexcept
 Retrieves the current held value without advancing the state.
 
void reset (T initialValue=T(0)) noexcept
 Resets the processor state and sets an initial output value.
 

Detailed Description

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

Holds a sample value for N samples or until externally triggered.

Template Parameters
TSample type (must satisfy FloatType concept).

Definition at line 37 of file SampleAndHold.h.

Member Enumeration Documentation

◆ Mode

template<FloatType T>
enum class dspark::SampleAndHold::Mode
strong

Defines the capture behavior of the processor.

Enumerator
Counter 

Holds for a fixed number of samples automatically (Decimation).

Trigger 

Holds until an external trigger captures a new value (level-sensitive).

Definition at line 41 of file SampleAndHold.h.

Member Function Documentation

◆ getHeldValue()

template<FloatType T>
T dspark::SampleAndHold< T >::getHeldValue ( ) const
inlinenoexcept

Retrieves the current held value without advancing the state.

Returns
The held sample value.

Definition at line 198 of file SampleAndHold.h.

◆ process()

template<FloatType T>
T dspark::SampleAndHold< T >::process ( input,
bool  trigger = false 
)
inlinenoexcept

Processes a single sample.

Warning
For optimal performance on blocks, use processBlock() instead, as this scalar method contains internal branching based on the Mode.
Parameters
inputThe audio or modulation input sample.
triggerExternal trigger state (ignored in Counter mode). The trigger is level-sensitive: while it stays true, the input is tracked sample-by-sample; send single-sample pulses for classic S&H capture.
Returns
The currently held output sample.

Definition at line 105 of file SampleAndHold.h.

◆ processBlock() [1/2]

template<FloatType T>
void dspark::SampleAndHold< T >::processBlock ( T *  data,
const bool *  triggers,
int  numSamples 
)
inlinenoexcept

Processes a block of samples in-place using an external trigger buffer.

Only relevant when Mode is set to Trigger (level-sensitive, same semantics as process()). If Mode is Counter, the triggers buffer is safely ignored.

Parameters
dataAudio buffer to process in-place.
triggersArray of boolean triggers, one per sample. A null pointer is treated as "no triggers" (the current value is held).
numSamplesNumber of samples in the buffer.

Definition at line 174 of file SampleAndHold.h.

◆ processBlock() [2/2]

template<FloatType T>
void dspark::SampleAndHold< T >::processBlock ( T *  data,
int  numSamples 
)
inlinenoexcept

Processes a block of samples in-place (Counter mode optimized).

If the processor is in Trigger mode, calling this method will simply fill the buffer with the last held value. For Trigger mode processing, use the overloaded processBlock with the trigger buffer.

Parameters
dataAudio buffer to process in-place.
numSamplesNumber of samples in the buffer.

Definition at line 136 of file SampleAndHold.h.

◆ reset()

template<FloatType T>
void dspark::SampleAndHold< T >::reset ( initialValue = T(0))
inlinenoexcept

Resets the processor state and sets an initial output value.

Parameters
initialValueValue to output until the next capture triggers.

Definition at line 205 of file SampleAndHold.h.

◆ setHoldRate()

template<FloatType T>
void dspark::SampleAndHold< T >::setHoldRate ( double  targetRate,
double  actualRate 
)
inlinenoexcept

Sets the hold period based on a target effective sample rate.

The period is rounded to the nearest integer (truncating would bias the effective rate upward) and clamped to a valid range.

Parameters
targetRateThe desired effective sample rate in Hz. Invalid values (non-positive or NaN, either argument) reset the period to 1.
actualRateThe current system sample rate in Hz.

Definition at line 78 of file SampleAndHold.h.

◆ setHoldSamples()

template<FloatType T>
void dspark::SampleAndHold< T >::setHoldSamples ( int  numSamples)
inlinenoexcept

Sets the hold duration for Counter mode in samples.

A value of 1 passes the signal transparently. A value of N reduces the effective sample rate by a factor of N. Capture phase: after reset(), the first capture happens on the Nth call (the initial value is output for the first N-1 samples), then every N samples.

Parameters
numSamplesHold period in samples. Clamped to a minimum of 1.

Definition at line 63 of file SampleAndHold.h.

◆ setMode()

template<FloatType T>
void dspark::SampleAndHold< T >::setMode ( Mode  mode)
inlinenoexcept

Sets the operating mode.

Parameters
modeMode::Counter or Mode::Trigger.

Definition at line 51 of file SampleAndHold.h.


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