DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
dspark::Phaser< T > Class Template Reference

Zero-latency, highly optimized allpass-based phaser. More...

#include <Phaser.h>

Collaboration diagram for dspark::Phaser< T >:

Classes

struct  FirstOrderAllpass
 

Public Member Functions

 ~Phaser ()=default
 
void prepare (const AudioSpec &spec)
 Prepares the phaser and allocates internal state blocks.
 
void processBlock (AudioBufferView< T > buffer) noexcept
 Processes an audio block in-place.
 
void reset () noexcept
 Clears internal DSP state and history buffers. Call this when transport stops or playback jumps.
 
void setRate (T hz) noexcept
 Sets the speed of the phaser sweep.
 
void setDepth (T amount) noexcept
 Sets how wide the frequency sweep is.
 
void setMix (T dryWet) noexcept
 Balances the unprocessed and processed signal.
 
void setStages (int count) noexcept
 Configures the intensity/color of the phaser via filter stages.
 
void setFeedback (T amount) noexcept
 Injects phase-shifted signal back into the input for resonance.
 
void setStereoSpread (T amount) noexcept
 Sets the stereo phase spread of the sweep LFO.
 
void setCenterFrequency (T hz) noexcept
 Defines the midpoint of the logarithmic frequency sweep.
 
void setFrequencyRange (T minHz, T maxHz) noexcept
 Explicitly defines the sweep boundaries.
 
void setLfoWaveform (typename Oscillator< T >::Waveform wf) noexcept
 Changes the geometric shape of the LFO modulation.
 
int getStages () const noexcept
 Retrieves the active stage count.
 
T getRate () const noexcept
 Retrieves the current sweep rate.
 
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).
 

Static Public Attributes

static constexpr int kMaxStages = 12
 
static constexpr double kSpreadGlideSeconds = 0.1
 Duration of the right-LFO phase glide after a spread change.
 

Protected Member Functions

void processChunk (AudioBufferView< T > buffer) noexcept
 One block of at most the prepared size.
 

Protected Attributes

AudioSpec spec_ {}
 
std::atomic< T > rate_ { T(0.5) }
 
std::atomic< T > depth_ { T(0.8) }
 
std::atomic< T > mix_ { T(0.5) }
 
std::atomic< T > feedback_ { T(0) }
 
std::atomic< T > minFreq_ { T(200) }
 
std::atomic< T > maxFreq_ { T(6000) }
 
std::atomic< int > numStages_ { 4 }
 
std::atomic< typename Oscillator< T >::Waveform > lfoWaveform_ { Oscillator<T>::Waveform::Sine }
 
std::atomic< T > stereoSpread_ { T(0) }
 
bool prepared_ { false }
 
T currentDepth_ { T(0.8) }
 
T currentFb_ { T(0) }
 
T smoothCoef_ { T(0.01) }
 
T lastSpread_ { T(-1) }
 
double spreadOffset_ { 0.0 }
 Current right-LFO phase offset (cycles).
 
double spreadTarget_ { 0.0 }
 Offset the glide lands on (cycles).
 
double spreadStep_ { 0.0 }
 Per-sample offset increment of the glide.
 
int spreadGlideLeft_ { 0 }
 Samples left in the glide (0 = settled).
 
int lastStages_ { kMaxStages }
 
std::array< FirstOrderAllpass, kMaxStages > stages_ {}
 
std::array< T, kMaxChannels > fbState_ {}
 
Oscillator< T > lfo_
 
Oscillator< T > lfoR_
 
DryWetMixer< T > mixer_
 

Static Protected Attributes

static constexpr int kMaxChannels = 16
 

Detailed Description

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

Zero-latency, highly optimized allpass-based phaser.

Employs parameter smoothing to prevent zipper noise and utilizes fast math approximations for real-time performance.

Template Parameters
TSample type (must satisfy FloatType concept, e.g., float or double).

Definition at line 68 of file Phaser.h.

Constructor & Destructor Documentation

◆ ~Phaser()

template<FloatType T>
dspark::Phaser< T >::~Phaser ( )
default

Member Function Documentation

◆ getRate()

template<FloatType T>
T dspark::Phaser< T >::getRate ( ) const
inlinenoexcept

Retrieves the current sweep rate.

Returns
Rate in Hz.

Definition at line 437 of file Phaser.h.

◆ getStages()

template<FloatType T>
int dspark::Phaser< T >::getStages ( ) const
inlinenoexcept

Retrieves the active stage count.

Returns
Number of stages.

Definition at line 434 of file Phaser.h.

◆ getState()

template<FloatType T>
std::vector< uint8_t > dspark::Phaser< T >::getState ( ) const
inline

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

Definition at line 441 of file Phaser.h.

◆ prepare()

template<FloatType T>
void dspark::Phaser< T >::prepare ( const AudioSpec &  spec)
inline

Prepares the phaser and allocates internal state blocks.

An invalid spec (non-positive or non-finite fields) is a no-op that keeps the previous state.

Parameters
specHardware audio specifications (sample rate, block size).

Definition at line 88 of file Phaser.h.

◆ processBlock()

template<FloatType T>
void dspark::Phaser< T >::processBlock ( AudioBufferView< T >  buffer)
inlinenoexcept

Processes an audio block in-place.

Any length works: a block longer than the prepared maximum runs in prepared-size slices (the dry/wet mixer holds one prepared block).

Parameters
bufferThe AudioBufferView containing channels to process.

Definition at line 121 of file Phaser.h.

◆ processChunk()

template<FloatType T>
void dspark::Phaser< T >::processChunk ( AudioBufferView< T >  buffer)
inlineprotectednoexcept

One block of at most the prepared size.

Definition at line 135 of file Phaser.h.

◆ reset()

template<FloatType T>
void dspark::Phaser< T >::reset ( )
inlinenoexcept

Clears internal DSP state and history buffers. Call this when transport stops or playback jumps.

Definition at line 284 of file Phaser.h.

◆ setCenterFrequency()

template<FloatType T>
void dspark::Phaser< T >::setCenterFrequency ( T  hz)
inlinenoexcept

Defines the midpoint of the logarithmic frequency sweep.

Keeps the current sweep RATIO and recenters it geometrically. Callable before prepare() (a 20 kHz ceiling stands in for Nyquist until the rate is known; the per-sample cutoff clamp covers later rate changes anyway).

Parameters
hzCenter frequency in Hertz (non-positive or non-finite values are ignored).

Definition at line 390 of file Phaser.h.

◆ setDepth()

template<FloatType T>
void dspark::Phaser< T >::setDepth ( T  amount)
inlinenoexcept

Sets how wide the frequency sweep is.

Parameters
amountRange: [0.0 (static), 1.0 (full spectrum)]. Non-finite values are ignored.

Definition at line 322 of file Phaser.h.

◆ setFeedback()

template<FloatType T>
void dspark::Phaser< T >::setFeedback ( T  amount)
inlinenoexcept

Injects phase-shifted signal back into the input for resonance.

Parameters
amountFeedback gain. Range: [-0.99, 0.99]. Negative values alter the vocal formant shape. Non-finite values are ignored.

Definition at line 355 of file Phaser.h.

◆ setFrequencyRange()

template<FloatType T>
void dspark::Phaser< T >::setFrequencyRange ( T  minHz,
T  maxHz 
)
inlinenoexcept

Explicitly defines the sweep boundaries.

Parameters
minHzLower limit of the allpass cutoff sweep.
maxHzUpper limit of the allpass cutoff sweep. Non-finite values are ignored.

Definition at line 411 of file Phaser.h.

◆ setLfoWaveform()

template<FloatType T>
void dspark::Phaser< T >::setLfoWaveform ( typename Oscillator< T >::Waveform  wf)
inlinenoexcept

Changes the geometric shape of the LFO modulation.

Parameters
wfTarget waveform (e.g., Sine, Triangle; wild enum values clamp).

Definition at line 425 of file Phaser.h.

◆ setMix()

template<FloatType T>
void dspark::Phaser< T >::setMix ( T  dryWet)
inlinenoexcept

Balances the unprocessed and processed signal.

Parameters
dryWetRange: [0.0 (fully dry), 1.0 (fully wet)]. Non-finite values are ignored.

Definition at line 333 of file Phaser.h.

◆ setRate()

template<FloatType T>
void dspark::Phaser< T >::setRate ( T  hz)
inlinenoexcept

Sets the speed of the phaser sweep.

Parameters
hzLFO frequency in Hertz. Range: [0.01, 20.0]. Non-finite values are ignored.

Definition at line 311 of file Phaser.h.

◆ setStages()

template<FloatType T>
void dspark::Phaser< T >::setStages ( int  count)
inlinenoexcept

Configures the intensity/color of the phaser via filter stages.

Parameters
countNumber of allpass stages. Range: [1, 12]. Evens (2,4,6) are standard.

Definition at line 345 of file Phaser.h.

◆ setState()

template<FloatType T>
bool dspark::Phaser< T >::setState ( const uint8_t *  data,
size_t  size 
)
inline

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

Definition at line 460 of file Phaser.h.

◆ setStereoSpread()

template<FloatType T>
void dspark::Phaser< T >::setStereoSpread ( T  amount)
inlinenoexcept

Sets the stereo phase spread of the sweep LFO.

Offsets the right channel's LFO phase against the left, like classic hardware stereo phasers. 0 = mono-coherent sweep (both channels move together), 1 = fully opposed (180-degree) sweep. A change while running glides the offset over kSpreadGlideSeconds (0.1 s), so the sweep never jumps; after prepare() or reset() the first block starts on the set spread.

Parameters
amountSpread amount [0.0, 1.0]. Applied on the audio thread. Non-finite values are ignored.

Definition at line 374 of file Phaser.h.

Member Data Documentation

◆ currentDepth_

template<FloatType T>
T dspark::Phaser< T >::currentDepth_ { T(0.8) }
protected

Definition at line 495 of file Phaser.h.

◆ currentFb_

template<FloatType T>
T dspark::Phaser< T >::currentFb_ { T(0) }
protected

Definition at line 496 of file Phaser.h.

◆ depth_

template<FloatType T>
std::atomic<T> dspark::Phaser< T >::depth_ { T(0.8) }
protected

Definition at line 484 of file Phaser.h.

◆ fbState_

template<FloatType T>
std::array<T, kMaxChannels> dspark::Phaser< T >::fbState_ {}
protected

Definition at line 513 of file Phaser.h.

◆ feedback_

template<FloatType T>
std::atomic<T> dspark::Phaser< T >::feedback_ { T(0) }
protected

Definition at line 486 of file Phaser.h.

◆ kMaxChannels

template<FloatType T>
constexpr int dspark::Phaser< T >::kMaxChannels = 16
staticconstexprprotected

Definition at line 478 of file Phaser.h.

◆ kMaxStages

template<FloatType T>
constexpr int dspark::Phaser< T >::kMaxStages = 12
staticconstexpr

Definition at line 73 of file Phaser.h.

◆ kSpreadGlideSeconds

template<FloatType T>
constexpr double dspark::Phaser< T >::kSpreadGlideSeconds = 0.1
staticconstexpr

Duration of the right-LFO phase glide after a spread change.

Definition at line 76 of file Phaser.h.

◆ lastSpread_

template<FloatType T>
T dspark::Phaser< T >::lastSpread_ { T(-1) }
protected

Definition at line 498 of file Phaser.h.

◆ lastStages_

template<FloatType T>
int dspark::Phaser< T >::lastStages_ { kMaxStages }
protected

Definition at line 503 of file Phaser.h.

◆ lfo_

template<FloatType T>
Oscillator<T> dspark::Phaser< T >::lfo_
protected

Definition at line 514 of file Phaser.h.

◆ lfoR_

template<FloatType T>
Oscillator<T> dspark::Phaser< T >::lfoR_
protected

Definition at line 515 of file Phaser.h.

◆ lfoWaveform_

template<FloatType T>
std::atomic<typename Oscillator<T>::Waveform> dspark::Phaser< T >::lfoWaveform_ { Oscillator<T>::Waveform::Sine }
protected

Definition at line 490 of file Phaser.h.

◆ maxFreq_

template<FloatType T>
std::atomic<T> dspark::Phaser< T >::maxFreq_ { T(6000) }
protected

Definition at line 488 of file Phaser.h.

◆ minFreq_

template<FloatType T>
std::atomic<T> dspark::Phaser< T >::minFreq_ { T(200) }
protected

Definition at line 487 of file Phaser.h.

◆ mix_

template<FloatType T>
std::atomic<T> dspark::Phaser< T >::mix_ { T(0.5) }
protected

Definition at line 485 of file Phaser.h.

◆ mixer_

template<FloatType T>
DryWetMixer<T> dspark::Phaser< T >::mixer_
protected

Definition at line 516 of file Phaser.h.

◆ numStages_

template<FloatType T>
std::atomic<int> dspark::Phaser< T >::numStages_ { 4 }
protected

Definition at line 489 of file Phaser.h.

◆ prepared_

template<FloatType T>
bool dspark::Phaser< T >::prepared_ { false }
protected

Definition at line 494 of file Phaser.h.

◆ rate_

template<FloatType T>
std::atomic<T> dspark::Phaser< T >::rate_ { T(0.5) }
protected

Definition at line 483 of file Phaser.h.

◆ smoothCoef_

template<FloatType T>
T dspark::Phaser< T >::smoothCoef_ { T(0.01) }
protected

Definition at line 497 of file Phaser.h.

◆ spec_

template<FloatType T>
AudioSpec dspark::Phaser< T >::spec_ {}
protected

Definition at line 480 of file Phaser.h.

◆ spreadGlideLeft_

template<FloatType T>
int dspark::Phaser< T >::spreadGlideLeft_ { 0 }
protected

Samples left in the glide (0 = settled).

Definition at line 502 of file Phaser.h.

◆ spreadOffset_

template<FloatType T>
double dspark::Phaser< T >::spreadOffset_ { 0.0 }
protected

Current right-LFO phase offset (cycles).

Definition at line 499 of file Phaser.h.

◆ spreadStep_

template<FloatType T>
double dspark::Phaser< T >::spreadStep_ { 0.0 }
protected

Per-sample offset increment of the glide.

Definition at line 501 of file Phaser.h.

◆ spreadTarget_

template<FloatType T>
double dspark::Phaser< T >::spreadTarget_ { 0.0 }
protected

Offset the glide lands on (cycles).

Definition at line 500 of file Phaser.h.

◆ stages_

template<FloatType T>
std::array<FirstOrderAllpass, kMaxStages> dspark::Phaser< T >::stages_ {}
protected

Definition at line 512 of file Phaser.h.

◆ stereoSpread_

template<FloatType T>
std::atomic<T> dspark::Phaser< T >::stereoSpread_ { T(0) }
protected

Definition at line 491 of file Phaser.h.


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