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

Phase accumulator generating a [0, 1) ramp for oscillators and modulation. More...

#include <Phasor.h>

Public Member Functions

void prepare (double sampleRate) noexcept
 Prepares the phasor for a given sample rate.
 
void prepare (const AudioSpec &spec) noexcept
 Prepares from AudioSpec (unified API).
 
void setFrequency (T frequencyHz) noexcept
 Sets the baseline oscillation frequency.
 
getFrequency () const noexcept
 Returns the baseline frequency.
 
advance () noexcept
 Returns the phase for the current sample, then advances the accumulator (post-increment semantics).
 
advanceWithFM (T fmHz) noexcept
 Advances the phase with per-sample frequency modulation.
 
getPhase () const noexcept
 Returns the current phase without advancing the accumulator.
 
void setPhase (T newPhase) noexcept
 Forcibly overwrites the current phase.
 
void hardSync (T fractionalOffset=T(0)) noexcept
 Hard syncs the oscillator, optionally with a sub-sample offset.
 
void softSync (T threshold=T(0.5)) noexcept
 Soft syncs the oscillator conditionally.
 
void reset (T startPhase=T(0)) noexcept
 Resets the phasor to a specific initial state (default 0).
 
getIncrement () const noexcept
 Returns the calculated phase increment per sample.
 

Detailed Description

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

Phase accumulator generating a [0, 1) ramp for oscillators and modulation.

Scalar recursive accumulator with a fast in-range wrap (one compare in the common case) that also handles arbitrarily large excursions (extreme FM, frequencies far above the sample rate) without unbounded loops. Supports fractional phase offsets on hard sync for PolyBLEP/MinBLEP-style anti-aliased oscillators.

Note
This class is not internally thread-safe. Parameter updates (e.g., setFrequency) should be synchronized or smoothed by the caller prior to block processing.
Template Parameters
TSample type (must satisfy FloatType concept).

Definition at line 49 of file Phasor.h.

Member Function Documentation

◆ advance()

template<FloatType T>
T dspark::Phasor< T >::advance ( )
inlinenoexcept

Returns the phase for the current sample, then advances the accumulator (post-increment semantics).

Returns
Current phase value in [0, 1).

Definition at line 95 of file Phasor.h.

◆ advanceWithFM()

template<FloatType T>
T dspark::Phasor< T >::advanceWithFM ( fmHz)
inlinenoexcept

Advances the phase with per-sample frequency modulation.

The instantaneous increment is computed in double, and the wrap safely handles arbitrarily large FM indices without unbounded loops. A NaN excursion cannot poison the accumulator permanently (the wrap resets it to 0), but the caller should keep fmHz finite.

Parameters
fmHzInstantaneous frequency deviation in Hz.
Returns
Current phase value in [0, 1).

Definition at line 116 of file Phasor.h.

◆ getFrequency()

template<FloatType T>
T dspark::Phasor< T >::getFrequency ( ) const
inlinenoexcept

Returns the baseline frequency.

Returns
Current frequency in Hz.

Definition at line 88 of file Phasor.h.

◆ getIncrement()

template<FloatType T>
T dspark::Phasor< T >::getIncrement ( ) const
inlinenoexcept

Returns the calculated phase increment per sample.

Returns
Increment value.

Definition at line 183 of file Phasor.h.

◆ getPhase()

template<FloatType T>
T dspark::Phasor< T >::getPhase ( ) const
inlinenoexcept

Returns the current phase without advancing the accumulator.

Returns
Phase value in [0, 1).

Definition at line 132 of file Phasor.h.

◆ hardSync()

template<FloatType T>
void dspark::Phasor< T >::hardSync ( fractionalOffset = T(0))
inlinenoexcept

Hard syncs the oscillator, optionally with a sub-sample offset.

For analog modeling, the fractionalOffset is crucial for anti-aliasing techniques (like PolyBLEP) to account for inter-sample sync events.

Parameters
fractionalOffsetSub-sample phase offset (default: 0).

Definition at line 151 of file Phasor.h.

◆ prepare() [1/2]

template<FloatType T>
void dspark::Phasor< T >::prepare ( const AudioSpec spec)
inlinenoexcept

Prepares from AudioSpec (unified API).

Parameters
specFramework audio specification struct.

Definition at line 70 of file Phasor.h.

◆ prepare() [2/2]

template<FloatType T>
void dspark::Phasor< T >::prepare ( double  sampleRate)
inlinenoexcept

Prepares the phasor for a given sample rate.

Parameters
sampleRateSample rate in Hz. Must be > 0 (invalid values, including NaN, are ignored).

Definition at line 57 of file Phasor.h.

◆ reset()

template<FloatType T>
void dspark::Phasor< T >::reset ( startPhase = T(0))
inlinenoexcept

Resets the phasor to a specific initial state (default 0).

Parameters
startPhaseInitial phase value (wrapped to [0, 1)).

Definition at line 174 of file Phasor.h.

◆ setFrequency()

template<FloatType T>
void dspark::Phasor< T >::setFrequency ( frequencyHz)
inlinenoexcept

Sets the baseline oscillation frequency.

Parameters
frequencyHzFrequency in Hz. Negative values produce a descending ramp. NaN is ignored (keeps the previous frequency).

Definition at line 77 of file Phasor.h.

◆ setPhase()

template<FloatType T>
void dspark::Phasor< T >::setPhase ( newPhase)
inlinenoexcept

Forcibly overwrites the current phase.

Parameters
newPhaseAny phase value (safely wrapped to [0, 1); NaN resets to 0).

Definition at line 138 of file Phasor.h.

◆ softSync()

template<FloatType T>
void dspark::Phasor< T >::softSync ( threshold = T(0.5))
inlinenoexcept

Soft syncs the oscillator conditionally.

Standard implementation: resets the phase only if the current phase is beyond a specified threshold (typically the second half of the cycle).

Parameters
thresholdPhase threshold required to allow synchronization.

Definition at line 164 of file Phasor.h.


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