DSPark 1.8.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
dspark::Delay< SampleType > Class Template Referencefinal

#include <Delay.h>

Classes

struct  ChannelState
 

Public Types

enum class  SmootherType {
  None , Linear , Exponential , OnePole ,
  MultiPole2 , Asymmetric , SlewLimiter , StateVariable ,
  Butterworth , CriticallyDamped
}
 
enum class  FeedbackMode { Clean , Analog }
 Feedback path colour. More...
 

Public Member Functions

void setFeedbackMode (FeedbackMode mode) noexcept
 Selects clean or analog feedback regeneration. Thread-safe.
 
FeedbackMode getFeedbackMode () const noexcept
 Returns the active feedback mode.
 
void prepare (const AudioSpec &spec)
 Prepares for insert use with the default capacity (4 seconds).
 
void prepare (const AudioSpec &spec, double maxDelaySeconds)
 Prepares the delay structures and allocates memory. Must be called before processing.
 
void prepareMs (const AudioSpec &spec, double maxDelayMs)
 Prepares the delay with a maximum capacity in milliseconds.
 
void reset () noexcept
 Clears the delay buffers and resets all filter and feedback states.
 
void setSmoother (SmootherType type) noexcept
 Sets the smoothing algorithm used for delay time changes.
 
void setSmoothingTime (float ms) noexcept
 Sets the smoothing transition time.
 
void setDelaySamples (SampleType samples) noexcept
 Sets the delay time in samples.
 
void setDelayMs (SampleType ms) noexcept
 Sets the delay time in milliseconds.
 
void setDelaySeconds (SampleType secs) noexcept
 Sets the delay time in seconds.
 
SampleType getCurrentDelaySamples () const noexcept
 Returns the current target delay time in samples.
 
void setFeedback (SampleType gain) noexcept
 Sets the global feedback amount.
 
void setFeedbackLpHz (SampleType freq) noexcept
 Sets the cutoff frequency for the feedback low-pass filter (0 to disable). Non-finite values are ignored.
 
void setFeedbackHpHz (SampleType freq) noexcept
 Sets the cutoff frequency for the feedback high-pass filter (0 to disable). Non-finite values are ignored.
 
SampleType processSample (int ch, SampleType input) noexcept
 Processes a single sample for a specific channel.
 
void processBlock (AudioBufferView< SampleType > buffer) noexcept
 Insert-style processing: the dry/wet blend, in place, with the stored parameters (setDelayMs(), setFeedback(), the feedback filters and mode, setMix()).
 
void setMix (SampleType mix) noexcept
 Sets the dry/wet blend of the insert-style processBlock(buffer).
 
SampleType getMix () const noexcept
 Returns the insert dry/wet blend.
 
void setPingPong (bool on) noexcept
 Ping-pong for the insert-style processBlock(buffer): the first two channels feed their echoes to each other (left to right, right to left), with the same wiring as processPingPong() and the insert's blend, dry + (wet - dry) * mix. Further channels keep a plain delay. Off by default; needs two or more channels. Thread-safe.
 
bool getPingPong () const noexcept
 True when the insert path runs as a ping-pong delay.
 
int getLatency () const noexcept
 Latency in samples: none (the delay is the effect, not a latency).
 
void processBlock (AudioBufferView< SampleType > buffer, SampleType delayMs, SampleType feedback=0, SampleType lpHz=0, SampleType hpHz=0) noexcept
 Processes a multi-channel block in-place.
 
void processChannel (AudioBufferView< SampleType > buffer, int ch, SampleType delayMs, SampleType feedback=0, SampleType lpHz=0, SampleType hpHz=0) noexcept
 Processes a single channel block in-place. Safe to call sequentially for different channels.
 
void pushDryToWet (AudioBufferView< const SampleType > dry) noexcept
 Copies a dry buffer into the internal wet buffer.
 
void pushDryToWet (AudioBufferView< SampleType > dry) noexcept
 Copies a dry buffer into the internal wet buffer.
 
void processWet (SampleType delayMs, SampleType feedback=0, SampleType lpHz=0, SampleType hpHz=0) noexcept
 Processes the wet buffer in place with current settings. Processes exactly the samples of the most recent pushDryToWet() call: running the full capacity regardless of the pushed length would advance the delay line past the real stream on short blocks, skewing every subsequent echo's timing.
 
void processPingPong (SampleType delayMs, SampleType feedback=0, SampleType lpHz=0, SampleType hpHz=0) noexcept
 Processes a true ping-pong delay (L feeds R, R feeds L) on the samples of the most recent pushDryToWet() call.
 
void mixWetToDry (AudioBufferView< SampleType > dry, SampleType mix) noexcept
 Mixes the processed wet buffer back into the provided dry buffer.
 
AudioBufferView< SampleType > getWetView () noexcept
 Exposes the internal wet buffer view.
 
int getMaxDelaySamples () const noexcept
 Returns maximum capacity in samples.
 
std::vector< uint8_t > getState () const
 Serializes the parameter state (setup/UI threads; allocates).
 
bool setState (const uint8_t *data, size_t size)
 Restores parameters from a blob (tolerant; rejects foreign ids).
 
void advanceWriteIndex (int ch) noexcept
 Advances the write index for a specific channel.
 

Static Protected Attributes

static constexpr int kMaxChannels = 16
 
static constexpr double kDefaultMaxDelaySeconds = 4.0
 

Detailed Description

template<typename SampleType>
class dspark::Delay< SampleType >

Definition at line 54 of file Delay.h.

Member Enumeration Documentation

◆ FeedbackMode

template<typename SampleType >
enum class dspark::Delay::FeedbackMode
strong

Feedback path colour.

Enumerator
Clean 

Pristine digital regeneration: no per-pass saturation, the decay time matches the feedback gain exactly. A hard safety clamp at +-2.0 guards against runaway (|fb| >= 1).

Analog 

Tape/BBD-style tanh soft saturation on every pass (default, matches the original DSPark behaviour). Slightly shortens the decay versus the nominal feedback and adds warmth.

Definition at line 64 of file Delay.h.

◆ SmootherType

template<typename SampleType >
enum class dspark::Delay::SmootherType
strong
Enumerator
None 
Linear 
Exponential 
OnePole 
MultiPole2 
Asymmetric 
SlewLimiter 
StateVariable 
Butterworth 
CriticallyDamped 

Definition at line 57 of file Delay.h.

Member Function Documentation

◆ advanceWriteIndex()

template<typename SampleType >
void dspark::Delay< SampleType >::advanceWriteIndex ( int  ch)
inlinenoexcept

Advances the write index for a specific channel.

Parameters
chChannel index to advance. Out-of-range indices are ignored (an unchecked index would write past the index array).

Definition at line 654 of file Delay.h.

◆ getCurrentDelaySamples()

template<typename SampleType >
SampleType dspark::Delay< SampleType >::getCurrentDelaySamples ( ) const
inlinenoexcept

Returns the current target delay time in samples.

Definition at line 219 of file Delay.h.

◆ getFeedbackMode()

template<typename SampleType >
FeedbackMode dspark::Delay< SampleType >::getFeedbackMode ( ) const
inlinenoexcept

Returns the active feedback mode.

Definition at line 81 of file Delay.h.

◆ getLatency()

template<typename SampleType >
int dspark::Delay< SampleType >::getLatency ( ) const
inlinenoexcept

Latency in samples: none (the delay is the effect, not a latency).

Definition at line 379 of file Delay.h.

◆ getMaxDelaySamples()

template<typename SampleType >
int dspark::Delay< SampleType >::getMaxDelaySamples ( ) const
inlinenoexcept

Returns maximum capacity in samples.

Definition at line 586 of file Delay.h.

◆ getMix()

template<typename SampleType >
SampleType dspark::Delay< SampleType >::getMix ( ) const
inlinenoexcept

Returns the insert dry/wet blend.

Definition at line 363 of file Delay.h.

◆ getPingPong()

template<typename SampleType >
bool dspark::Delay< SampleType >::getPingPong ( ) const
inlinenoexcept

True when the insert path runs as a ping-pong delay.

Definition at line 376 of file Delay.h.

◆ getState()

template<typename SampleType >
std::vector< uint8_t > dspark::Delay< SampleType >::getState ( ) const
inline

Serializes the parameter state (setup/UI threads; allocates).

Definition at line 590 of file Delay.h.

◆ getWetView()

template<typename SampleType >
AudioBufferView< SampleType > dspark::Delay< SampleType >::getWetView ( )
inlinenoexcept

Exposes the internal wet buffer view.

Definition at line 583 of file Delay.h.

◆ mixWetToDry()

template<typename SampleType >
void dspark::Delay< SampleType >::mixWetToDry ( AudioBufferView< SampleType >  dry,
SampleType  mix 
)
inlinenoexcept

Mixes the processed wet buffer back into the provided dry buffer.

Parameters
dryBuffer containing the dry signal.
mixMix ratio (0.0 = 100% dry, 1.0 = 100% wet). A non-finite value resolves to 0 (dry).

Definition at line 550 of file Delay.h.

◆ prepare() [1/2]

template<typename SampleType >
void dspark::Delay< SampleType >::prepare ( const AudioSpec &  spec)
inline

Prepares for insert use with the default capacity (4 seconds).

Parameters
specThe audio specification. An invalid spec is ignored.

Definition at line 92 of file Delay.h.

◆ prepare() [2/2]

template<typename SampleType >
void dspark::Delay< SampleType >::prepare ( const AudioSpec &  spec,
double  maxDelaySeconds 
)
inline

Prepares the delay structures and allocates memory. Must be called before processing.

Parameters
specThe audio specification (sample rate, block size, etc.). An invalid spec (non-positive or NaN fields) is ignored.
maxDelaySecondsThe maximum theoretical delay time required. Negative or NaN values are ignored; the capacity is capped at 2^29 samples.

Definition at line 105 of file Delay.h.

◆ prepareMs()

template<typename SampleType >
void dspark::Delay< SampleType >::prepareMs ( const AudioSpec &  spec,
double  maxDelayMs 
)
inline

Prepares the delay with a maximum capacity in milliseconds.

Parameters
specThe audio specification.
maxDelayMsMaximum delay capacity in milliseconds.

Definition at line 147 of file Delay.h.

◆ processBlock() [1/2]

template<typename SampleType >
void dspark::Delay< SampleType >::processBlock ( AudioBufferView< SampleType >  buffer)
inlinenoexcept

Insert-style processing: the dry/wet blend, in place, with the stored parameters (setDelayMs(), setFeedback(), the feedback filters and mode, setMix()).

Channels beyond the prepared count pass through. Zero latency.

Parameters
bufferThe audio buffer to process in place.

Definition at line 288 of file Delay.h.

◆ processBlock() [2/2]

template<typename SampleType >
void dspark::Delay< SampleType >::processBlock ( AudioBufferView< SampleType >  buffer,
SampleType  delayMs,
SampleType  feedback = 0,
SampleType  lpHz = 0,
SampleType  hpHz = 0 
)
inlinenoexcept

Processes a multi-channel block in-place.

Note
The per-call parameters are published to the same state the individual setters write, so every call overwrites previously set values (including the defaults of the omitted arguments).
Parameters
bufferThe audio buffer to process.
delayMsDelay time in milliseconds.
feedbackFeedback gain multiplier.
lpHzLow-pass filter cutoff in Hz.
hpHzHigh-pass filter cutoff in Hz.

Definition at line 394 of file Delay.h.

◆ processChannel()

template<typename SampleType >
void dspark::Delay< SampleType >::processChannel ( AudioBufferView< SampleType >  buffer,
int  ch,
SampleType  delayMs,
SampleType  feedback = 0,
SampleType  lpHz = 0,
SampleType  hpHz = 0 
)
inlinenoexcept

Processes a single channel block in-place. Safe to call sequentially for different channels.

Definition at line 432 of file Delay.h.

◆ processPingPong()

template<typename SampleType >
void dspark::Delay< SampleType >::processPingPong ( SampleType  delayMs,
SampleType  feedback = 0,
SampleType  lpHz = 0,
SampleType  hpHz = 0 
)
inlinenoexcept

Processes a true ping-pong delay (L feeds R, R feeds L) on the samples of the most recent pushDryToWet() call.

The cross-feed obeys the active FeedbackMode exactly like the straight feedback path: tanh soft saturation in Analog mode, a +-2.0 safety clamp in Clean mode (without it, |feedback| >= 1 grew without bound).

Definition at line 494 of file Delay.h.

◆ processSample()

template<typename SampleType >
SampleType dspark::Delay< SampleType >::processSample ( int  ch,
SampleType  input 
)
inlinenoexcept

Processes a single sample for a specific channel.

Parameters
chChannel index.
inputInput sample.
Returns
Processed delayed sample (wet only).

Definition at line 259 of file Delay.h.

◆ processWet()

template<typename SampleType >
void dspark::Delay< SampleType >::processWet ( SampleType  delayMs,
SampleType  feedback = 0,
SampleType  lpHz = 0,
SampleType  hpHz = 0 
)
inlinenoexcept

Processes the wet buffer in place with current settings. Processes exactly the samples of the most recent pushDryToWet() call: running the full capacity regardless of the pushed length would advance the delay line past the real stream on short blocks, skewing every subsequent echo's timing.

Definition at line 481 of file Delay.h.

◆ pushDryToWet() [1/2]

template<typename SampleType >
void dspark::Delay< SampleType >::pushDryToWet ( AudioBufferView< const SampleType >  dry)
inlinenoexcept

Copies a dry buffer into the internal wet buffer.

Definition at line 465 of file Delay.h.

◆ pushDryToWet() [2/2]

template<typename SampleType >
void dspark::Delay< SampleType >::pushDryToWet ( AudioBufferView< SampleType >  dry)
inlinenoexcept

Copies a dry buffer into the internal wet buffer.

Definition at line 471 of file Delay.h.

◆ reset()

template<typename SampleType >
void dspark::Delay< SampleType >::reset ( )
inlinenoexcept

Clears the delay buffers and resets all filter and feedback states.

Definition at line 155 of file Delay.h.

◆ setDelayMs()

template<typename SampleType >
void dspark::Delay< SampleType >::setDelayMs ( SampleType  ms)
inlinenoexcept

Sets the delay time in milliseconds.

Definition at line 213 of file Delay.h.

◆ setDelaySamples()

template<typename SampleType >
void dspark::Delay< SampleType >::setDelaySamples ( SampleType  samples)
inlinenoexcept

Sets the delay time in samples.

Parameters
samplesTarget delay time (can be fractional). Non-finite values are ignored (a NaN here would poison the read position and, through the feedback path, the whole buffer).

Definition at line 199 of file Delay.h.

◆ setDelaySeconds()

template<typename SampleType >
void dspark::Delay< SampleType >::setDelaySeconds ( SampleType  secs)
inlinenoexcept

Sets the delay time in seconds.

Definition at line 216 of file Delay.h.

◆ setFeedback()

template<typename SampleType >
void dspark::Delay< SampleType >::setFeedback ( SampleType  gain)
inlinenoexcept

Sets the global feedback amount.

Parameters
gainFeedback gain multiplier [-1.0, 1.0]. Will be saturated internally if exceeded. Non-finite values are ignored (a NaN would recirculate through the delay buffer forever).

Definition at line 227 of file Delay.h.

◆ setFeedbackHpHz()

template<typename SampleType >
void dspark::Delay< SampleType >::setFeedbackHpHz ( SampleType  freq)
inlinenoexcept

Sets the cutoff frequency for the feedback high-pass filter (0 to disable). Non-finite values are ignored.

Definition at line 244 of file Delay.h.

◆ setFeedbackLpHz()

template<typename SampleType >
void dspark::Delay< SampleType >::setFeedbackLpHz ( SampleType  freq)
inlinenoexcept

Sets the cutoff frequency for the feedback low-pass filter (0 to disable). Non-finite values are ignored.

Definition at line 235 of file Delay.h.

◆ setFeedbackMode()

template<typename SampleType >
void dspark::Delay< SampleType >::setFeedbackMode ( FeedbackMode  mode)
inlinenoexcept

Selects clean or analog feedback regeneration. Thread-safe.

Definition at line 75 of file Delay.h.

◆ setMix()

template<typename SampleType >
void dspark::Delay< SampleType >::setMix ( SampleType  mix)
inlinenoexcept

Sets the dry/wet blend of the insert-style processBlock(buffer).

Parameters
mix0 = dry only, 1 = delayed signal only (default 0.3). Clamped to [0, 1]; non-finite values are ignored. Thread-safe.

Definition at line 356 of file Delay.h.

◆ setPingPong()

template<typename SampleType >
void dspark::Delay< SampleType >::setPingPong ( bool  on)
inlinenoexcept

Ping-pong for the insert-style processBlock(buffer): the first two channels feed their echoes to each other (left to right, right to left), with the same wiring as processPingPong() and the insert's blend, dry + (wet - dry) * mix. Further channels keep a plain delay. Off by default; needs two or more channels. Thread-safe.

Definition at line 373 of file Delay.h.

◆ setSmoother()

template<typename SampleType >
void dspark::Delay< SampleType >::setSmoother ( SmootherType  type)
inlinenoexcept

Sets the smoothing algorithm used for delay time changes.

Parameters
typeThe desired SmootherType.

Definition at line 174 of file Delay.h.

◆ setSmoothingTime()

template<typename SampleType >
void dspark::Delay< SampleType >::setSmoothingTime ( float  ms)
inlinenoexcept

Sets the smoothing transition time.

Parameters
msTime in milliseconds. Non-finite values are ignored.

Definition at line 184 of file Delay.h.

◆ setState()

template<typename SampleType >
bool dspark::Delay< SampleType >::setState ( const uint8_t *  data,
size_t  size 
)
inline

Restores parameters from a blob (tolerant; rejects foreign ids).

Definition at line 606 of file Delay.h.

Member Data Documentation

◆ kDefaultMaxDelaySeconds

template<typename SampleType >
constexpr double dspark::Delay< SampleType >::kDefaultMaxDelaySeconds = 4.0
staticconstexprprotected

Definition at line 628 of file Delay.h.

◆ kMaxChannels

template<typename SampleType >
constexpr int dspark::Delay< SampleType >::kMaxChannels = 16
staticconstexprprotected

Definition at line 627 of file Delay.h.


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