DSPark 1.9.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...
 
enum class  Interpolation { Hermite , Sinc }
 Fractional read kernel. More...
 
enum class  Character { Clean , Tape , Bucket }
 Colour of the line (see the file comment). 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 setInterpolation (Interpolation mode) noexcept
 Selects the fractional read kernel. Thread-safe.
 
Interpolation getInterpolation () const noexcept
 Returns the fractional read kernel.
 
void setCharacter (Character c) noexcept
 Selects the character. Thread-safe; out-of-range values are clamped.
 
Character getCharacter () const noexcept
 
void setWowFlutter (SampleType amount) noexcept
 Wow and flutter of the tape character.
 
SampleType getWowFlutter () const noexcept
 
void setCharacterNoise (SampleType dbfs) noexcept
 Hiss of the tape and bucket characters.
 
SampleType getCharacterNoise () const noexcept
 
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 (the stored form). Non-finite values are ignored.
 
void setDelaySeconds (SampleType secs) noexcept
 Sets the delay time in seconds.
 
SampleType getDelayMs () const noexcept
 Returns the delay time in milliseconds.
 
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 81 of file Delay.h.

Member Enumeration Documentation

◆ Character

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

Colour of the line (see the file comment).

Enumerator
Clean 

An interpolated delay line (default).

Tape 

A tape loop: speed transport, wow and flutter, head loss, hiss.

Bucket 

A bucket-brigade device of 4096 stages.

Definition at line 136 of file Delay.h.

◆ 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 91 of file Delay.h.

◆ Interpolation

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

Fractional read kernel.

Enumerator
Hermite 

4-point cubic (default). Error -24 dB at 10 kHz on a moving read.

Sinc 

32-tap Kaiser sinc: below -96 dB to 15 kHz at 44.1/48 kHz, about 5x the cost of a Hermite read; the shortest delay becomes 17 samples instead of 3.

Definition at line 114 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 84 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 836 of file Delay.h.

◆ getCharacter()

template<typename SampleType >
Character dspark::Delay< SampleType >::getCharacter ( ) const
inlinenoexcept
Returns
The character.

Definition at line 150 of file Delay.h.

◆ getCharacterNoise()

template<typename SampleType >
SampleType dspark::Delay< SampleType >::getCharacterNoise ( ) const
inlinenoexcept
Returns
The hiss level of the characters in dBFS.

Definition at line 175 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 385 of file Delay.h.

◆ getDelayMs()

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

Returns the delay time in milliseconds.

Definition at line 377 of file Delay.h.

◆ getFeedbackMode()

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

Returns the active feedback mode.

Definition at line 108 of file Delay.h.

◆ getInterpolation()

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

Returns the fractional read kernel.

Definition at line 130 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 545 of file Delay.h.

◆ getMaxDelaySamples()

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

Returns maximum capacity in samples.

Definition at line 752 of file Delay.h.

◆ getMix()

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

Returns the insert dry/wet blend.

Definition at line 529 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 542 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 756 of file Delay.h.

◆ getWetView()

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

Exposes the internal wet buffer view.

Definition at line 749 of file Delay.h.

◆ getWowFlutter()

template<typename SampleType >
SampleType dspark::Delay< SampleType >::getWowFlutter ( ) const
inlinenoexcept
Returns
The wow and flutter amount.

Definition at line 162 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 716 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 183 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 196 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 273 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 454 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 560 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 598 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 660 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 425 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 647 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 631 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 637 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 281 of file Delay.h.

◆ setCharacter()

template<typename SampleType >
void dspark::Delay< SampleType >::setCharacter ( Character  c)
inlinenoexcept

Selects the character. Thread-safe; out-of-range values are clamped.

Definition at line 144 of file Delay.h.

◆ setCharacterNoise()

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

Hiss of the tape and bucket characters.

Parameters
dbfsRMS level in dBFS, clamped to [-140, -40]; -120 or less is silent. Non-finite values are ignored.

Definition at line 169 of file Delay.h.

◆ setDelayMs()

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

Sets the delay time in milliseconds (the stored form). Non-finite values are ignored.

Definition at line 363 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 339 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 374 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 393 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 410 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 401 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 102 of file Delay.h.

◆ setInterpolation()

template<typename SampleType >
void dspark::Delay< SampleType >::setInterpolation ( Interpolation  mode)
inlinenoexcept

Selects the fractional read kernel. Thread-safe.

Definition at line 123 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 522 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 539 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 314 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 324 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 779 of file Delay.h.

◆ setWowFlutter()

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

Wow and flutter of the tape character.

Parameters
amount0 (a steady transport) to 1, clamped; non-finite values are ignored.

Definition at line 156 of file Delay.h.

Member Data Documentation

◆ kDefaultMaxDelaySeconds

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

Definition at line 810 of file Delay.h.

◆ kMaxChannels

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

Definition at line 809 of file Delay.h.


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