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

A band-limited slope shape driven by a signed phase, from a falling saw through a triangle to a rising saw. More...

Detailed Description

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

A band-limited slope shape driven by a signed phase, from a falling saw through a triangle to a rising saw.

The shape rises from -1 to +1 over w of the cycle, on [rise, fall), and falls from +1 to -1 over the rest. Its corners sit where PhasePulse's edges sit for the same width and anchor: the trough at PhasePulse::rise(w, a), the peak at PhasePulse::fall(w, a). Width 0.5 is a triangle; toward 0 the shape is a falling saw, toward 1 a rising saw. The anchor means what it means for the pulse: which corner holds as width changes, and how far a change dw moves the fundamental's phase, -a dw/2 of a cycle.

The shape's slope is the pulse's, less its mean, times K = 2 / (w (1 - w)), so each harmonic is K / (2 pi k) times PhasePulse's and a quarter cycle behind it. Its mean is 0 at every width; it carries no DC. Width 1 - w is the shape at w inverted and shifted, so mirrored widths in a pair keep the pulse's relation.

The core owns no phase accumulator and walks each frame as PhasePulse does, through a PhaseWalk: the driver gives the phase, in [0, 1), the signed step that led there, in cycles, and a phase offset at the end of each of SUBSTEPS parts of the frame, and the core reads the phase plus the offset. Forward, backward and through zero, across the wrap, with the offset as phase modulation; every corner crossed in the frame is corrected at its own time through a BlepCorrector over the Kernel, so the output trails the naive shape by LATENCY frames.

A corner's correction is a slope change: K times the phase's speed relative to the corner, in cycles per frame, K taken at the width at the crossing. A trough is a trough in either direction of travel, so the sign is the corner's, not the direction's: +K |speed| at the trough, -K |speed| at the peak. Under phase modulation the speed is each sub-step's. Each correction is a change from the slope before it, so the first frame after a reset starts the count: the shape's slope at the starting phase times the first speed, corrected at the reset's instant. Without it a start on the short segment, or on its trough, leaves the next corner unpaired.

The phase is linear within each sub-step, and the shape's slope is not zero, so where the phase's speed changes between sub-steps or frames the shape's slope changes by its slope times the change: 2 / w on the rising segment. Those changes are not corrected, as BlepOsc leaves a frequency change's. Under phase modulation they leave aliasing that grows with the depth, the pitch and the narrowness of the short segment.

Width and anchor take effect across the next frame, moving linearly, as PhasePulse's do, and nothing is ramped, so a pulse and a saw given the same width and anchor stay in step. A corner a change moves past the phase is corrected like any other crossing. Unlike the pulse's level, the shape's value also changes along a segment when width changes, and the slope changes where that motion starts and stops are not corrected. Near the floor the value at a phase goes as 1 / w, so a large step of width within one frame is no longer a straight segment between corners, and its correction overshoots. Ramped over 48 frames, as a driver's ramp moves it, the peak is no higher than the static one (test_phase_saw.hpp). A driver ramps width, and sets it before reset() for the first frame to start from it.

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 (PhaseWalk).
  • 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 [WIDTH_FLOOR, 1 - WIDTH_FLOOR]; non-finite counts as 0.5. Toward 0 and 1 the slope of the short segment grows as 1/w, and with it the naive value's sensitivity to the phase's rounding: against the same shape and corrections worked in double, the error stays under 2.4e-4 at the floor (test_phase_saw.hpp), and grows as 1/w below it. Under phase modulation the slope changes on the short segment are not corrected and also grow as 1/w: with a sine at the pitch half a cycle deep at 1.1 kHz the output stays within the naive range at width 0.001 and under 1.4 at the floor (test_phase_saw.hpp).
  • 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 the corners ring, and the peak grows as the short segment shortens toward a step, whose overshoot is 8.2 % of its height (polyblep.hpp). From 110 Hz to 4.4 kHz the peak stays at or under 1.165 at every width (test_phase_saw.hpp).

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 107 of file phase_saw.hpp.

#include <phase_saw.hpp>

Public Member Functions

void set_width (float fraction_of_cycle)
 Set the rising segment's share of the cycle. Takes effect next frame.
 
void set_anchor (float anchor)
 Set where width acts. Takes effect next frame.
 
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, as PhasePulse's.
 
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 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 trough, where the shape starts to rise, as PhasePulse::rise().
 
static constexpr float fall (float width, float anchor)
 The peak, where the shape starts to fall, as PhasePulse::fall().
 
static float shape (float phase, float rise, float fall, float width)
 The naive shape at a phase.
 

Static Public Attributes

static constexpr float MAX_STEP = PhaseWalk::MAX_STEP
 
static constexpr float WIDTH_FLOOR = 0.0005f
 
static constexpr uint8_t LATENCY = Kernel::LATENCY
 
static constexpr transport::Port PORTS []
 The ports, in the order the buffer call takes them, as PhasePulse's.
 

Member Function Documentation

◆ set_width()

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

Set the rising segment's share of the cycle. Takes effect next frame.

Parameters
fraction_of_cycleWidth, clamped to [WIDTH_FLOOR, 1 - WIDTH_FLOOR]; non-finite counts as 0.5
Context
Same thread as process(); real-time safe.

Definition at line 119 of file phase_saw.hpp.

References sbl::math::clamp(), sbl::math::finite_or(), and sbl::dsp::PhaseSaw< Kernel >::WIDTH_FLOOR.

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

◆ set_anchor()

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

Set where width acts. Takes effect next frame.

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

Definition at line 131 of file phase_saw.hpp.

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

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

◆ process() [1/4]

template<class Kernel = math::Sinc16>
float sbl::dsp::PhaseSaw< 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 145 of file phase_saw.hpp.

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

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

◆ process() [2/4]

template<class Kernel = math::Sinc16>
template<uint8_t SUBSTEPS>
float sbl::dsp::PhaseSaw< 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, as PhasePulse'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.

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 165 of file phase_saw.hpp.

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

◆ process() [3/4]

template<class Kernel = math::Sinc16>
float sbl::dsp::PhaseSaw< 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 202 of file phase_saw.hpp.

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

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

◆ process() [4/4]

template<class Kernel = math::Sinc16>
template<class... Ports>
void sbl::dsp::PhaseSaw< 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.

As PhasePulse's: width and anchor as blocks are set each frame, as scalars once before the first frame; left off, they hold. The offset 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 227 of file phase_saw.hpp.

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

◆ reset()

template<class Kernel = math::Sinc16>
void sbl::dsp::PhaseSaw< 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 265 of file phase_saw.hpp.

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

◆ phase()

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

◆ offset()

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

The offset the core read last frame, in cycles.

Definition at line 276 of file phase_saw.hpp.

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

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

◆ width()

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

◆ anchor()

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

◆ rise()

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

The trough, where the shape starts to rise, as PhasePulse::rise().

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

Definition at line 289 of file phase_saw.hpp.

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

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

◆ fall()

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

The peak, where the shape starts to fall, as PhasePulse::fall().

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

Definition at line 298 of file phase_saw.hpp.

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

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

◆ shape()

template<class Kernel = math::Sinc16>
static float sbl::dsp::PhaseSaw< Kernel >::shape ( float  phase,
float  rise,
float  fall,
float  width 
)
inlinestatic

The naive shape at a phase.

Parameters
phasePhase, in [0, 1)
riseThe trough, from rise()
fallThe peak, from fall()
widthThe width the corners were taken at
Returns
The shape's value, in [-1, 1]

Definition at line 309 of file phase_saw.hpp.

References sbl::dsp::PhaseSaw< Kernel >::fall(), sbl::dsp::PhaseSaw< Kernel >::phase(), sbl::dsp::PhaseSaw< Kernel >::rise(), and sbl::dsp::PhaseSaw< Kernel >::width().

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

Member Data Documentation

◆ MAX_STEP

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

cycles per frame, either direction

Definition at line 109 of file phase_saw.hpp.

◆ WIDTH_FLOOR

template<class Kernel = math::Sinc16>
constexpr float sbl::dsp::PhaseSaw< Kernel >::WIDTH_FLOOR = 0.0005f
staticconstexpr

the narrowest segment, a two-thousandth of the cycle

Definition at line 110 of file phase_saw.hpp.

Referenced by sbl::dsp::PhaseSaw< Kernel >::set_width().

◆ LATENCY

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

frames the output trails the naive shape

Definition at line 111 of file phase_saw.hpp.

◆ PORTS

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

The ports, in the order the buffer call takes them, as PhasePulse's.

Definition at line 205 of file phase_saw.hpp.


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