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

The BLEP oscillator core: one phase, three waveform parts, band-limited edges. More...

Detailed Description

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

The BLEP oscillator core: one phase, three waveform parts, band-limited edges.

One phase accumulator drives a saw with a movable knee, a pulse of variable width and a triangle with a variable peak position. The naive output is the weighted sum of the three; every step and corner in it is corrected at its sub-frame time through a BlepCorrector over the Kernel, so the output trails the naive waveform by LATENCY frames and a harmonic at f is scaled by the kernel's response. The kernel is a compile-time choice (dsp/math/polyblep.hpp): the default is math::Sinc16; math::Bspline4 and math::Linear2 never overshoot.

The core is below the signal transport: its inputs are natural units, set by the composition that owns it, which carries the ports and does the one conversion. Frequency is cycles per frame, so the core holds no sample rate.

Inputs and their handling, per frame:

  • Frequency is clamped to [0, MAX_FREQUENCY], a quarter cycle per frame (fs/4). At zero the phase holds. A non-finite value counts as zero.
  • Pulse width, saw knee and triangle peak are fractions of a cycle. Each frame they are kept at least one frame's travel away from the wrap, in [frequency, 1 - frequency], so a frame holds at most one event per part besides the wrap and the event times stay exact. The margin is for the event bookkeeping, not for band-limiting; it narrows the pulse's reach at high frequency (a quarter to three quarters at the cap). The knee and the peak also stay in [MIN_BREAKPOINT, 1 - MIN_BREAKPOINT], which keeps the slopes finite.
  • The saw knee is phase distortion: the phase runs to the knee over the first half of the saw's range and from it over the second, so the saw is a two-slope ramp with a corner at the knee and a second corner at the wrap. At 0.5 it is the plain saw. Its mean is 0.5 - knee; the core removes no DC.
  • A pulse-width change is a moving threshold: the edge it moves is band-limited at its new time and nothing is ramped.
  • Blend weights, the saw knee and the triangle peak change the naive value directly, so they ramp linearly over RAMP_FRAMES frames from the frame after the call, carried across process() calls.
  • A frequency change takes effect at the next frame. The slope change it gives the saw and triangle is not corrected.

Output is nominally [-1, 1] per part. With Bspline4 or Linear2, kernels that never overshoot, the peak cannot exceed the sum of the weights' magnitudes; with Sinc16 a step overshoots by 8.2 % of its height (polyblep.hpp). The pulse is low before its edge and high after it, as in Plaits, so it shares the saw's drop at the wrap and a saw-to-pulse blend morphs without a dip. A pulse with its edge at w carries a mean of 1 - 2w; 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 75 of file blep_osc.hpp.

#include <blep_osc.hpp>

Public Member Functions

 BlepOsc ()
 A saw at zero frequency until set; blend, knee and peak changes ramp over RAMP_FRAMES.
 
void set_frequency (float cycles_per_frame)
 Set the frequency. Takes effect at the next frame.
 
void set_pulse_width (float fraction_of_cycle)
 Set where the pulse's rising edge falls in the cycle. Takes effect next frame as a moving edge.
 
void set_saw_knee (float fraction_of_cycle)
 Set where the saw's phase bends. Ramps over RAMP_FRAMES.
 
void set_triangle_peak (float fraction_of_cycle)
 Set where the triangle peaks within the cycle. Ramps over RAMP_FRAMES.
 
void set_blend (float saw, float pulse, float triangle)
 Set the weights of the three parts in the sum. Ramp over RAMP_FRAMES.
 
float process ()
 One frame.
 
template<class... Ports>
void process (float *out, uint16_t frames, Ports... ports)
 A buffer of frames, the ports in PORTS' order, any trailing ones left off.
 
void reset ()
 Phase to zero, ramps to their targets, corrections dropped.
 
float phase () const
 The phase after the last frame, in [0, 1).
 
float frequency () const
 The frequency, in cycles per frame.
 

Static Public Attributes

static constexpr float MAX_FREQUENCY = 0.25f
 
static constexpr float MIN_BREAKPOINT = 1.0f / 32.0f
 
static constexpr uint16_t RAMP_FRAMES = 64
 
static constexpr uint8_t LATENCY = Kernel::LATENCY
 
static constexpr transport::Port PORTS []
 The ports, in the order the buffer call takes them; each sets the quantity of the setter of its name.
 

Constructor & Destructor Documentation

◆ BlepOsc()

template<class Kernel = math::Sinc16>
sbl::dsp::BlepOsc< Kernel >::BlepOsc ( )
inline

A saw at zero frequency until set; blend, knee and peak changes ramp over RAMP_FRAMES.

Definition at line 83 of file blep_osc.hpp.

References sbl::dsp::BlepOsc< Kernel >::RAMP_FRAMES.

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

Member Function Documentation

◆ set_frequency()

template<class Kernel = math::Sinc16>
void sbl::dsp::BlepOsc< Kernel >::set_frequency ( float  cycles_per_frame)
inline

Set the frequency. Takes effect at the next frame.

Parameters
cycles_per_frameFrequency in cycles per frame, clamped to [0, MAX_FREQUENCY]; non-finite counts as zero
Context
Same thread as process(); real-time safe.

Definition at line 95 of file blep_osc.hpp.

References sbl::math::clamp(), sbl::math::finite_or(), and sbl::dsp::BlepOsc< Kernel >::MAX_FREQUENCY.

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

◆ set_pulse_width()

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

Set where the pulse's rising edge falls in the cycle. Takes effect next frame as a moving edge.

Parameters
fraction_of_cycleEdge position, clamped to [0, 1]: low before it, high from it to the wrap; non-finite counts as 0.5
Context
Same thread as process(); real-time safe.

Definition at line 106 of file blep_osc.hpp.

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

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

◆ set_saw_knee()

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

Set where the saw's phase bends. Ramps over RAMP_FRAMES.

Parameters
fraction_of_cycleKnee, clamped to [MIN_BREAKPOINT, 1 - MIN_BREAKPOINT]; 0.5 is the plain saw; non-finite counts as 0.5
Context
Same thread as process(); real-time safe.

Definition at line 117 of file blep_osc.hpp.

References sbl::math::clamp(), sbl::math::finite_or(), sbl::dsp::BlepOsc< Kernel >::MIN_BREAKPOINT, and sbl::dsp::Ramp::set().

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

◆ set_triangle_peak()

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

Set where the triangle peaks within the cycle. Ramps over RAMP_FRAMES.

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

Definition at line 128 of file blep_osc.hpp.

References sbl::math::clamp(), sbl::math::finite_or(), sbl::dsp::BlepOsc< Kernel >::MIN_BREAKPOINT, and sbl::dsp::Ramp::set().

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

◆ set_blend()

template<class Kernel = math::Sinc16>
void sbl::dsp::BlepOsc< Kernel >::set_blend ( float  saw,
float  pulse,
float  triangle 
)
inline

Set the weights of the three parts in the sum. Ramp over RAMP_FRAMES.

Parameters
sawWeight of the saw; non-finite counts as zero
pulseWeight of the pulse; non-finite counts as zero
triangleWeight of the triangle; non-finite counts as zero
Context
Same thread as process(); real-time safe.

Definition at line 141 of file blep_osc.hpp.

References sbl::math::finite_or(), and sbl::dsp::Ramp::set().

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

◆ process() [1/2]

template<class Kernel = math::Sinc16>
float sbl::dsp::BlepOsc< Kernel >::process ( )
inline

One frame.

Returns
The corrected sample LATENCY frames back
Context
Audio thread; real-time safe.

Definition at line 153 of file blep_osc.hpp.

References sbl::math::clamp(), sbl::dsp::BlepOsc< Kernel >::MIN_BREAKPOINT, sbl::dsp::math::polyblep_time(), and sbl::dsp::Ramp::step().

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

◆ process() [2/2]

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

A buffer of frames, the ports in PORTS' order, any trailing ones left off.

Frequency and pulse width as blocks are set each frame: the audio-rate FM and PWM paths. As scalars they are set once, before the block's first frame, and take effect as their setters say. The knee, the blend weights and the triangle peak are scalars and ramp over RAMP_FRAMES. A port left off holds its last value.

Template Parameters
PortsA block or an arithmetic scalar for frequency and pulse_width; scalars for the rest
Parameters
[out]outOutput buffer, frames long
framesFrame count
portsfrequency in cycles per frame, pulse_width, saw_knee, saw, pulse, triangle, triangle_peak, as their setters take them
Context
Audio thread; real-time safe.

Definition at line 242 of file blep_osc.hpp.

References sbl::dsp::BlepOsc< Kernel >::BlepOsc(), sbl::math::finite_or(), sbl::transport::fits_v, sbl::dsp::BlepOsc< Kernel >::frequency(), sbl::dsp::BlepOsc< Kernel >::process(), sbl::dsp::Ramp::set(), sbl::dsp::BlepOsc< Kernel >::set_frequency(), sbl::dsp::BlepOsc< Kernel >::set_pulse_width(), sbl::dsp::BlepOsc< Kernel >::set_saw_knee(), and sbl::dsp::BlepOsc< Kernel >::set_triangle_peak().

◆ reset()

template<class Kernel = math::Sinc16>
void sbl::dsp::BlepOsc< Kernel >::reset ( )
inline

Phase to zero, ramps to their targets, corrections dropped.

Definition at line 286 of file blep_osc.hpp.

References sbl::dsp::Ramp::jump(), sbl::dsp::Ramp::target(), and sbl::dsp::Ramp::value().

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

◆ phase()

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

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

Definition at line 300 of file blep_osc.hpp.

◆ frequency()

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

The frequency, in cycles per frame.

Definition at line 302 of file blep_osc.hpp.

Referenced by sbl::dsp::Vco::frequency(), and sbl::dsp::BlepOsc< Kernel >::process().

Member Data Documentation

◆ MAX_FREQUENCY

template<class Kernel = math::Sinc16>
constexpr float sbl::dsp::BlepOsc< Kernel >::MAX_FREQUENCY = 0.25f
staticconstexpr

cycles per frame: fs/4

Definition at line 77 of file blep_osc.hpp.

Referenced by sbl::dsp::BlepOsc< Kernel >::set_frequency().

◆ MIN_BREAKPOINT

template<class Kernel = math::Sinc16>
constexpr float sbl::dsp::BlepOsc< Kernel >::MIN_BREAKPOINT = 1.0f / 32.0f
staticconstexpr

the knee's and the peak's distance from either end

Definition at line 78 of file blep_osc.hpp.

Referenced by sbl::dsp::BlepOsc< Kernel >::process(), sbl::dsp::BlepOsc< Kernel >::set_saw_knee(), and sbl::dsp::BlepOsc< Kernel >::set_triangle_peak().

◆ RAMP_FRAMES

template<class Kernel = math::Sinc16>
constexpr uint16_t sbl::dsp::BlepOsc< Kernel >::RAMP_FRAMES = 64
staticconstexpr

blend and peak changes ramp over this many frames

Definition at line 79 of file blep_osc.hpp.

Referenced by sbl::dsp::BlepOsc< Kernel >::BlepOsc().

◆ LATENCY

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

frames the output trails the naive waveform

Definition at line 80 of file blep_osc.hpp.

◆ PORTS

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

The ports, in the order the buffer call takes them; each sets the quantity of the setter of its name.

Definition at line 216 of file blep_osc.hpp.


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