Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
sbl::dsp::PhaseAccumulator Class Reference

A phase advanced by a signed step each frame, held as a 32-bit count of the cycle. More...

Detailed Description

A phase advanced by a signed step each frame, held as a 32-bit count of the cycle.

The phase is a 32-bit unsigned count, 2^32 counts to the cycle, and each frame adds the step's count to it. Integer addition is exact and wraps by itself, so the phase accumulates no rounding error: the one error is the step's rounding to the nearest count, a frequency resolution of fs / 2^32, about 11 uHz at 48 kHz, which does not grow with time. A float accumulator rounds every add instead.

The interface is float, in natural units: a step in cycles per frame in; the phase in [0, 1), and the step as it is taken, out. The count stays inside. Below the signal transport: the component that owns an accumulator converts its ports to the step.

The step is signed, so the phase runs forward, backward and through zero, as PhasePulse reads it. A signed count tells the direction only for steps under half a cycle a frame, so the step is clamped to MAX_STEP either way; a pitch alone stays far under it. A non-finite step counts as no motion. The phase out is the count's top 24 bits, exact in a float and never 1.

const float step = acc.set_step(frequency); // when the frequency changes
const float phase = acc.advance(); // each frame
core.process(phase, step, offset);
A phase advanced by a signed step each frame, held as a 32-bit count of the cycle.
float advance()
One frame: advance by the step.
float set_step(float cycles_per_frame)
Set the step each later advance() takes.
float step() const
The step each advance takes, in cycles per frame.
float phase() const
The phase after the last advance, in [0, 1).

Definition at line 42 of file phase_accumulator.hpp.

#include <phase_accumulator.hpp>

Public Member Functions

float set_step (float cycles_per_frame)
 Set the step each later advance() takes.
 
float advance ()
 One frame: advance by the step.
 
float phase () const
 The phase after the last advance, in [0, 1).
 
float step () const
 The step each advance takes, in cycles per frame.
 
void reset (float phase=0.0f)
 Move to a phase. The step stays.
 

Static Public Attributes

static constexpr float MAX_STEP = 0.49999997f
 

Member Function Documentation

◆ set_step()

float sbl::dsp::PhaseAccumulator::set_step ( float  cycles_per_frame)
inline

Set the step each later advance() takes.

Parameters
cycles_per_frameStep in cycles per frame, signed; clamped to MAX_STEP either way; non-finite counts as 0
Returns
The step as it will be taken, rounded to the nearest count
Context
Same thread as advance(); real-time safe.

Definition at line 53 of file phase_accumulator.hpp.

References step().

Referenced by sbl::dsp::PmPulse::process().

◆ advance()

float sbl::dsp::PhaseAccumulator::advance ( )
inline

One frame: advance by the step.

Returns
The new phase, in [0, 1)
Context
Audio thread; real-time safe.

Definition at line 64 of file phase_accumulator.hpp.

References phase().

Referenced by sbl::dsp::PmPulse::process().

◆ phase()

float sbl::dsp::PhaseAccumulator::phase ( ) const
inline

The phase after the last advance, in [0, 1).

Definition at line 70 of file phase_accumulator.hpp.

Referenced by advance(), sbl::dsp::PmPulse::phase(), and reset().

◆ step()

float sbl::dsp::PhaseAccumulator::step ( ) const
inline

The step each advance takes, in cycles per frame.

Definition at line 73 of file phase_accumulator.hpp.

Referenced by set_step().

◆ reset()

void sbl::dsp::PhaseAccumulator::reset ( float  phase = 0.0f)
inline

Move to a phase. The step stays.

Parameters
phasePhase in cycles, wrapped into [0, 1); non-finite counts as 0

Definition at line 80 of file phase_accumulator.hpp.

References phase().

Referenced by sbl::dsp::PmPulse::reset().

Member Data Documentation

◆ MAX_STEP

constexpr float sbl::dsp::PhaseAccumulator::MAX_STEP = 0.49999997f
staticconstexpr

cycles per frame either way: the float just under 1/2

Definition at line 44 of file phase_accumulator.hpp.


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