59 if (!(sampleRate > 0.0))
return;
61 sampleRate_ = sampleRate;
62 invSampleRate_ = 1.0 / sampleRate;
79 if (frequencyHz != frequencyHz)
return;
80 frequency_ = frequencyHz;
88 [[nodiscard]] T
getFrequency() const noexcept {
return frequency_; }
97 const T current = currentPhase();
101 phase_ = wrapUnit(phase_ + increment_);
118 const T current = currentPhase();
120 const double modulatedIncrement =
121 (
static_cast<double>(frequency_) +
static_cast<double>(fmHz)) * invSampleRate_;
123 phase_ = wrapUnit(phase_ + modulatedIncrement);
132 [[nodiscard]] T
getPhase() const noexcept {
return currentPhase(); }
140 phase_ = wrapUnit(
static_cast<double>(newPhase));
153 phase_ = wrapUnit(
static_cast<double>(fractionalOffset));
166 if (phase_ >=
static_cast<double>(threshold))
174 void reset(T startPhase = T(0)) noexcept
176 phase_ = wrapUnit(
static_cast<double>(startPhase));
183 [[nodiscard]] T
getIncrement() const noexcept {
return static_cast<T
>(increment_); }
189 void updateIncrement() noexcept
191 increment_ =
static_cast<double>(frequency_) * invSampleRate_;
199 [[nodiscard]] T currentPhase() const noexcept
201 const T v =
static_cast<T
>(phase_);
202 constexpr T belowOne = T(1) - std::numeric_limits<T>::epsilon() / T(2);
203 return (v >= T(1)) ? belowOne : v;
211 static double wrapUnit(
double p)
noexcept
213 if (p >= 0.0 && p < 1.0)
return p;
217 if (p != p)
return 0.0;
220 double wrapped = p - std::trunc(p);
221 if (wrapped < 0.0) wrapped += 1.0;
224 if (wrapped >= 1.0) wrapped -= 1.0;
228 double sampleRate_ = 48000.0;
229 double invSampleRate_ = 1.0 / 48000.0;
233 double increment_ = 0.0;
Describes the audio processing environment (sample rate, block size, channels).
Core mathematical utilities for digital signal processing.
Phase accumulator generating a [0, 1) ramp for oscillators and modulation.
T getFrequency() const noexcept
Returns the baseline frequency.
void prepare(const AudioSpec &spec) noexcept
Prepares from AudioSpec (unified API).
void reset(T startPhase=T(0)) noexcept
Resets the phasor to a specific initial state (default 0).
void setFrequency(T frequencyHz) noexcept
Sets the baseline oscillation frequency.
T advance() noexcept
Returns the phase for the current sample, then advances the accumulator (post-increment semantics).
void setPhase(T newPhase) noexcept
Forcibly overwrites the current phase.
T getIncrement() const noexcept
Returns the calculated phase increment per sample.
T getPhase() const noexcept
Returns the current phase without advancing the accumulator.
void prepare(double sampleRate) noexcept
Prepares the phasor for a given sample rate.
void softSync(T threshold=T(0.5)) noexcept
Soft syncs the oscillator conditionally.
T advanceWithFM(T fmHz) noexcept
Advances the phase with per-sample frequency modulation.
void hardSync(T fractionalOffset=T(0)) noexcept
Hard syncs the oscillator, optionally with a sub-sample offset.
Main namespace for the DSPark framework.
Describes the audio environment for a DSP processor.