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::PhasePulse< Kernel > Class Template Reference

A band-limited pulse driven by a signed phase, its width placed by a bipolar anchor. More...

Detailed Description

template<class Kernel = math::Sinc16>
class sbl::dsp::PhasePulse< Kernel >

A band-limited pulse driven by a signed phase, its width placed by a bipolar anchor.

The pulse is high for w of the cycle, on [c - w/2, c + w/2) with its center at c = 1/2 + a (1 - w)/2, and low elsewhere. The anchor a, in [-1, 1], says which edges a change of width moves: at 0 the pulse is centered on phase 1/2 and both edges move; at +1 its fall sits at the wrap and only the rise moves, as in BlepOsc's pulse; at -1 its rise sits at the wrap and only the fall moves. A change dw in width moves the center by -a dw/2 of a cycle, and the fundamental's phase with it, so the anchor sets how far PWM moves the pitch. Both edges stay in [0, 1] at every width and anchor. Every edge is corrected at its sub-frame time through a BlepCorrector over the Kernel, so the output trails the naive waveform by LATENCY frames, as in BlepOsc.

The core owns no phase accumulator. Each frame its driver gives it the phase, in [0, 1), and the signed step that led there in cycles; one accumulator can drive several cores, each reading it plus its own offset. The step is signed: the phase may run forward, backward and through zero. The core keeps the previous frame's phase, and the step says how many wraps lie between the two, round(previous + step - phase). The phases are the segment's endpoints, so the crossings always agree with the naive samples, whatever rounding the driver's accumulator did.

Crossings are found on the unwrapped segment against each edge and its images one cycle up and down, in either direction and across the wrap, and every image crossed in the frame is corrected at its own time. The frame's segments and their crossings are a PhaseWalk. An edge at the wrap is corrected like any other, and width therefore needs no margin for the bookkeeping and reaches [0, 1] at every step: at 0 and at 1 the edges coincide and their corrections cancel. A change of width or anchor moves the edges across the next frame; nothing is ramped.

Each frame may carry a phase offset, in cycles: the core reads the phase plus the offset, and its step is the given step plus the offset's change since the previous frame. A moving offset is phase modulation, and the edges it moves are corrected like any other motion; a constant offset is a time shift. Each core keeps its own previous offset, so cores reading one accumulator can each carry their own.

A frame call can also report the edges it corrected, each at its time within the frame and with the level's change, and level() gives the naive level at the frame's end. Together they are the naive pulse as a level that steps at exact times, with none of the corrector's latency: what a modulator that listens to the pulse within the same frames needs.

The core is below the signal transport. Its inputs are natural units, set by the driver that owns it.

Inputs and their handling, per frame:

  • The step, the offset's change included, is clamped to [-MAX_STEP, MAX_STEP], one cycle per frame. With edge jumps of up to one cycle, a frame holds at most three crossings per edge. Past the clamp the frame's two phases stay exact and the segment between them loses whole cycles, whose crossings go uncorrected. Band-limiting degrades well before the clamp; a driver for pitch alone caps its step lower, as BlepOsc caps at fs/4.
  • Phase is linear within a frame: an edge is timed on the segment between the frame's two phases, so a pulse born and closed within one frame is not seen.
  • A phase outside [0, 1) is wrapped into it. A non-finite phase or step counts as no motion: the phase holds. A non-finite offset holds the previous one.
  • Width is clamped to [0, 1]; non-finite counts as 0.5.
  • The anchor is clamped to [-1, 1]; non-finite counts as 0.

With Bspline4 or Linear2, kernels that never overshoot, output is in [-1, 1]. With Sinc16 a step overshoots by 8.2 % of its height, so the pulse reaches 1.16 (polyblep.hpp). A pulse of width w carries a mean of 2w - 1; the core removes no DC.

Output does not depend on how the stream is split into calls: all state advances per frame.

Template Parameters
KernelThe BLEP kernel; math::Sinc16 by default

Definition at line 97 of file phase_pulse.hpp.

#include <phase_pulse.hpp>

Public Member Functions

void set_width (float fraction_of_cycle)
 Set the pulse width. Takes effect next frame as moving edges.
 
void set_anchor (float anchor)
 Set where width acts. Takes effect next frame as moving edges.
 
float process (float phase, float step, float offset)
 One frame: the corrected sample LATENCY frames back.
 
template<uint8_t SUBSTEPS>
float process (float phase, float step, const float(&offsets)[SUBSTEPS])
 One frame in SUBSTEPS equal parts, the offset given at the end of each.
 
template<uint8_t SUBSTEPS, class OnEdge >
float process (float phase, float step, const float(&offsets)[SUBSTEPS], OnEdge &&on_edge)
 The frame call above, reporting each edge the phase crossed.
 
float process (float phase, float step)
 One frame with no offset: an offset carried before moves back to zero, as phase modulation.
 
template<class... Ports>
void process (float *out, uint16_t frames, Ports... ports)
 A buffer of frames: phase and step per frame, then width, anchor and offset, which may be left off.
 
void reset (float phase=0.0f, float offset=0.0f)
 Move to a phase plus an offset; width and anchor to their targets, corrections dropped.
 
float phase () const
 The phase the core read last frame, the offset included.
 
float level () const
 The naive level at the end of the last frame, +1 or -1: the pulse before its corrections, with no latency.
 
float offset () const
 The offset the core read last frame, in cycles.
 
float width () const
 The width set for the next frame.
 
float anchor () const
 The anchor set for the next frame.
 

Static Public Member Functions

static constexpr float rise (float width, float anchor)
 The phase where the pulse rises.
 
static constexpr float fall (float width, float anchor)
 The phase where the pulse falls.
 
static float advance (float phase, float step)
 Advance a phase by a signed step and wrap it into [0, 1)
 

Static Public Attributes

static constexpr float MAX_STEP = PhaseWalk::MAX_STEP
 
static constexpr uint8_t LATENCY = Kernel::LATENCY
 
static constexpr transport::Port PORTS []
 The ports, in the order the buffer call takes them; width and anchor set what their setters set.
 

Member Function Documentation

◆ set_width()

template<class Kernel = math::Sinc16>
void sbl::dsp::PhasePulse< Kernel >::set_width ( float  fraction_of_cycle)
inline

Set the pulse width. Takes effect next frame as moving edges.

Parameters
fraction_of_cycleWidth as a fraction of the cycle, clamped to [0, 1]; non-finite counts as 0.5
Context
Same thread as process(); real-time safe.

Definition at line 108 of file phase_pulse.hpp.

References sbl::math::clamp(), and sbl::math::finite_or().

Referenced by sbl::dsp::PhasePulse< Kernel >::process(), and sbl::dsp::PmPulse::process().

◆ set_anchor()

template<class Kernel = math::Sinc16>
void sbl::dsp::PhasePulse< Kernel >::set_anchor ( float  anchor)
inline

Set where width acts. Takes effect next frame as moving edges.

Parameters
anchorClamped to [-1, 1]: -1 holds the rise at the wrap, 0 the center at 1/2, +1 the fall at the wrap; non-finite counts as 0
Context
Same thread as process(); real-time safe.

Definition at line 119 of file phase_pulse.hpp.

References sbl::dsp::PhasePulse< Kernel >::anchor(), sbl::math::clamp(), and sbl::math::finite_or().

Referenced by sbl::dsp::PhasePulse< Kernel >::process(), and sbl::dsp::PmPulse::process().

◆ process() [1/5]

template<class Kernel = math::Sinc16>
float sbl::dsp::PhasePulse< Kernel >::process ( float  phase,
float  step,
float  offset 
)
inline

One frame: the corrected sample LATENCY frames back.

Parameters
phaseThe frame's phase, in [0, 1)
stepThe signed step from the previous frame's phase, in cycles
offsetThe frame's phase offset, in cycles; its change since the previous frame adds to the step
Returns
The corrected sample LATENCY frames back
Context
Audio thread; real-time safe.

Definition at line 133 of file phase_pulse.hpp.

References sbl::dsp::PhasePulse< Kernel >::offset(), sbl::dsp::PhasePulse< Kernel >::phase(), and sbl::dsp::PhasePulse< Kernel >::process().

Referenced by sbl::dsp::PhasePulse< Kernel >::process(), sbl::dsp::PhasePulse< Kernel >::process(), and sbl::dsp::PhasePulse< Kernel >::process().

◆ process() [2/5]

template<class Kernel = math::Sinc16>
template<uint8_t SUBSTEPS>
float sbl::dsp::PhasePulse< Kernel >::process ( float  phase,
float  step,
const float(&)  offsets[SUBSTEPS] 
)
inline

One frame in SUBSTEPS equal parts, the offset given at the end of each.

Through the frame the phase moves linearly by step to phase, plus the offset at the end of each part; offsets[SUBSTEPS - 1] is the frame's. Each part is a segment of its own, so an offset that curves within the frame is followed SUBSTEPS times more closely than by one segment. Width and anchor move linearly across the frame. One part is the one-segment call above.

Template Parameters
SUBSTEPSParts per frame, at least 1
Parameters
phaseThe frame's phase, in [0, 1)
stepThe signed step from the previous frame's phase, in cycles
offsetsThe phase offset at the end of each part, in cycles
Returns
The corrected sample LATENCY frames back
Context
Audio thread; real-time safe.

Definition at line 156 of file phase_pulse.hpp.

References sbl::dsp::PhasePulse< Kernel >::phase(), and sbl::dsp::PhasePulse< Kernel >::process().

◆ process() [3/5]

template<class Kernel = math::Sinc16>
template<uint8_t SUBSTEPS, class OnEdge >
float sbl::dsp::PhasePulse< Kernel >::process ( float  phase,
float  step,
const float(&)  offsets[SUBSTEPS],
OnEdge &&  on_edge 
)
inline

The frame call above, reporting each edge the phase crossed.

The edges are those the corrector is given, so over a frame their heights sum to the change in level(). Parts come in time order; within a part the rising edge's crossings come before the falling edge's, whatever their times.

Template Parameters
SUBSTEPSParts per frame, at least 1
OnEdgeA callable taking (float t, float height)
Parameters
phaseThe frame's phase, in [0, 1)
stepThe signed step from the previous frame's phase, in cycles
offsetsThe phase offset at the end of each part, in cycles
on_edgeCalled once per crossing as on_edge(t, height): t the time from the crossing to the frame's end, in frames, in [0, 1]; height the naive level's change, +2 or -2
Returns
The corrected sample LATENCY frames back
Context
Audio thread; real-time safe.

Definition at line 179 of file phase_pulse.hpp.

References sbl::dsp::PhasePulse< Kernel >::fall(), sbl::dsp::PhaseWalk::frame(), sbl::dsp::PhaseWalk::Segment::last, sbl::dsp::PhasePulse< Kernel >::phase(), sbl::dsp::PhaseWalk::phase(), sbl::dsp::PhasePulse< Kernel >::rise(), and sbl::dsp::PhaseWalk::Segment::through.

◆ process() [4/5]

template<class Kernel = math::Sinc16>
float sbl::dsp::PhasePulse< Kernel >::process ( float  phase,
float  step 
)
inline

One frame with no offset: an offset carried before moves back to zero, as phase modulation.

Parameters
phaseThe frame's phase, in [0, 1)
stepThe signed step from the previous frame's phase, in cycles
Returns
The corrected sample LATENCY frames back
Context
Audio thread; real-time safe.

Definition at line 210 of file phase_pulse.hpp.

References sbl::dsp::PhasePulse< Kernel >::phase(), and sbl::dsp::PhasePulse< Kernel >::process().

Referenced by sbl::dsp::PhasePulse< Kernel >::process().

◆ process() [5/5]

template<class Kernel = math::Sinc16>
template<class... Ports>
void sbl::dsp::PhasePulse< Kernel >::process ( float *  out,
uint16_t  frames,
Ports...  ports 
)
inline

A buffer of frames: phase and step per frame, then width, anchor and offset, which may be left off.

Width and anchor as blocks are set each frame; as scalars they are set once, before the block's first frame. Either way they take effect as moving edges, as their setters say. Left off, they hold. The offset, in cycles, is per frame as a block and constant as a scalar; left off, it is zero.

Template Parameters
PortsBlocks for phase and step, then a block or an arithmetic scalar for each of the rest
Parameters
[out]outOutput buffer, frames long
framesFrame count
portsphase in [0, 1) and step in cycles per frame, then width, anchor and offset in cycles
Context
Audio thread; real-time safe.

Definition at line 237 of file phase_pulse.hpp.

References sbl::dsp::PhasePulse< Kernel >::anchor(), sbl::transport::fits_v, sbl::dsp::PhasePulse< Kernel >::offset(), sbl::dsp::PhasePulse< Kernel >::phase(), sbl::dsp::PhasePulse< Kernel >::process(), sbl::dsp::PhasePulse< Kernel >::set_anchor(), sbl::dsp::PhasePulse< Kernel >::set_width(), and sbl::dsp::PhasePulse< Kernel >::width().

◆ reset()

template<class Kernel = math::Sinc16>
void sbl::dsp::PhasePulse< Kernel >::reset ( float  phase = 0.0f,
float  offset = 0.0f 
)
inline

Move to a phase plus an offset; width and anchor to their targets, corrections dropped.

Parameters
phasePhase in cycles; a non-finite phase counts as 0
offsetOffset in cycles, kept as the last frame's; a non-finite offset counts as 0

Definition at line 275 of file phase_pulse.hpp.

References sbl::dsp::PhasePulse< Kernel >::fall(), sbl::dsp::PhasePulse< Kernel >::offset(), sbl::dsp::PhasePulse< Kernel >::phase(), sbl::dsp::PhaseWalk::phase(), sbl::dsp::PhaseWalk::reset(), and sbl::dsp::PhasePulse< Kernel >::rise().

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

◆ phase()

◆ level()

template<class Kernel = math::Sinc16>
float sbl::dsp::PhasePulse< Kernel >::level ( ) const
inline

The naive level at the end of the last frame, +1 or -1: the pulse before its corrections, with no latency.

Definition at line 287 of file phase_pulse.hpp.

◆ offset()

template<class Kernel = math::Sinc16>
float sbl::dsp::PhasePulse< Kernel >::offset ( ) const
inline

The offset the core read last frame, in cycles.

Definition at line 289 of file phase_pulse.hpp.

References sbl::dsp::PhaseWalk::offset().

Referenced by sbl::dsp::PhasePulse< Kernel >::process(), sbl::dsp::PhasePulse< Kernel >::process(), and sbl::dsp::PhasePulse< Kernel >::reset().

◆ width()

template<class Kernel = math::Sinc16>
float sbl::dsp::PhasePulse< Kernel >::width ( ) const
inline

◆ anchor()

template<class Kernel = math::Sinc16>
float sbl::dsp::PhasePulse< Kernel >::anchor ( ) const
inline

◆ rise()

template<class Kernel = math::Sinc16>
static constexpr float sbl::dsp::PhasePulse< Kernel >::rise ( float  width,
float  anchor 
)
inlinestaticconstexpr

The phase where the pulse rises.

Parameters
widthWidth, in [0, 1]
anchorAnchor, in [-1, 1]
Returns
(1 - w)(1 + a)/2, in [0, 1]

Definition at line 302 of file phase_pulse.hpp.

References sbl::dsp::PhasePulse< Kernel >::anchor(), and sbl::dsp::PhasePulse< Kernel >::width().

Referenced by sbl::dsp::PhasePulse< Kernel >::process(), and sbl::dsp::PhasePulse< Kernel >::reset().

◆ fall()

template<class Kernel = math::Sinc16>
static constexpr float sbl::dsp::PhasePulse< Kernel >::fall ( float  width,
float  anchor 
)
inlinestaticconstexpr

The phase where the pulse falls.

Parameters
widthWidth, in [0, 1]
anchorAnchor, in [-1, 1]
Returns
1 - (1 - w)(1 - a)/2, in [0, 1]

Definition at line 311 of file phase_pulse.hpp.

References sbl::dsp::PhasePulse< Kernel >::anchor(), and sbl::dsp::PhasePulse< Kernel >::width().

Referenced by sbl::dsp::PhasePulse< Kernel >::process(), and sbl::dsp::PhasePulse< Kernel >::reset().

◆ advance()

template<class Kernel = math::Sinc16>
static float sbl::dsp::PhasePulse< Kernel >::advance ( float  phase,
float  step 
)
inlinestatic

Advance a phase by a signed step and wrap it into [0, 1)

For a driver's accumulator. A small negative step from just above zero would round to 1.0f on adding a cycle; the result is kept below 1.

Parameters
phasePhase, in cycles
stepSigned step, in cycles
Returns
The wrapped phase, in [0, 1)

Definition at line 323 of file phase_pulse.hpp.

References sbl::dsp::PhasePulse< Kernel >::phase(), and sbl::dsp::PhaseWalk::wrap().

Member Data Documentation

◆ MAX_STEP

template<class Kernel = math::Sinc16>
constexpr float sbl::dsp::PhasePulse< Kernel >::MAX_STEP = PhaseWalk::MAX_STEP
staticconstexpr

cycles per frame, either direction

Definition at line 99 of file phase_pulse.hpp.

◆ LATENCY

template<class Kernel = math::Sinc16>
constexpr uint8_t sbl::dsp::PhasePulse< Kernel >::LATENCY = Kernel::LATENCY
staticconstexpr

frames the output trails the naive pulse

Definition at line 100 of file phase_pulse.hpp.

◆ PORTS

template<class Kernel = math::Sinc16>
constexpr transport::Port sbl::dsp::PhasePulse< Kernel >::PORTS[]
staticconstexpr
Initial value:

The ports, in the order the buffer call takes them; width and anchor set what their setters set.

Definition at line 213 of file phase_pulse.hpp.


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