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::math Namespace Reference

Math for the audio stack. More...

Detailed Description

Math for the audio stack.

Namespaces

namespace  sinc16
 Generated residual polynomials of the Sinc16 BLEP kernel.
 

Classes

struct  BlepResidual
 A kernel's corrections for the N frames it touches, oldest first. More...
 
struct  Bspline4
 The cubic B-spline over four frames, two before the event and two after. More...
 
struct  Linear2
 The triangular kernel over two frames, one before the event and one after: Plaits' polyBLEP. More...
 
struct  Sinc16
 A Kaiser-windowed sinc over sixteen frames, eight before the event and eight after. More...
 

Functions

float crossfade_equal_power (float a, float b, float t)
 Equal-power crossfade between two values.
 
void crossfade_gains_equal_power (float t, float end_zone, float &gain_a, float &gain_b)
 Equal-power gains for a crossfade with a zone at each end.
 
void crossfade (const float *a, const float *b, float *out, float mix, uint16_t frames)
 Linear crossfade over a buffer with constant mix.
 
void crossfade_equal_power (const float *a, const float *b, float *out, float mix, uint16_t frames)
 Equal-power crossfade over a buffer with constant mix.
 
float exp_mod (float base, float mod, float depth, float lo=DEFAULT_LO_HZ, float hi=DEFAULT_HI_HZ)
 Apply exponential modulation to a base frequency (scalar).
 
void exp_mod (const float *mod, float *freq_out, uint16_t frames, float base, float depth, float lo=DEFAULT_LO_HZ, float hi=DEFAULT_HI_HZ)
 Apply exponential modulation to a buffer.
 
float fast_sinf (float x)
 Fast sine approximation for x in [0, pi/2].
 
float fast_tan_pif (float f)
 Fast tan(pi f) for normalized frequency f in [0, 0.497].
 
float fast_exp2f (float x)
 Fast 2^x approximation for exponential modulation.
 
float fast_tanhf (float x)
 Fast tanh approximation using a [3,2] Pade approximant.
 
constexpr float one_minus_exp_neg (float x)
 1 - e^(-x) by inverse Taylor, with no expf
 
float db_to_gain (float db)
 Amplitude gain for a level in dB: 10^(db / 20).
 
float gain_to_db (float gain)
 Level in dB for an amplitude gain.
 
float level_gain (float level, float range_db)
 The level taper: a level in [0, 1] to a gain.
 
float ratio_from_octaves (float octaves)
 The frequency ratio of an interval: 2^octaves.
 
float octaves_from_ratio (float ratio)
 The interval of a frequency ratio, in octaves: log2(ratio).
 
float frequency (float reference, float octaves)
 A reference frequency moved by an interval in octaves.
 
constexpr float polyblep_time (float before, float after, float threshold_before, float threshold_after)
 Sub-frame time at which a linear segment crossed a moving threshold.
 
constexpr float polyblep_time (float before, float after, float threshold)
 Sub-frame time at which a linear segment crossed a fixed threshold.
 

Variables

constexpr float DEFAULT_LO_HZ = 20.0f
 
constexpr float DEFAULT_HI_HZ = 20000.0f
 
constexpr float FAST_TAN_PIF_MAX_F = 0.497f
 
constexpr float EXP2_MAX_ARG = 16.0f
 
constexpr int32_t FLOAT_MANTISSA_BITS = 23
 
constexpr float TANH_SATURATE_X = 3.0f
 
constexpr float DB_PER_DOUBLING = 6.0206f
 
constexpr float POLYBLEP_T_MAX = 0.99999994f
 The largest float below 1, the upper bound of a sub-frame time.
 

Function Documentation

◆ crossfade_equal_power() [1/2]

float sbl::dsp::math::crossfade_equal_power ( float  a,
float  b,
float  t 
)
inline

Equal-power crossfade between two values.

Maintains constant power across the fade (no dip at t=0.5). Returns a*sqrt(1-t) + b*sqrt(t), with sqrtf.

Parameters
aValue at t=0 (dry)
bValue at t=1 (wet)
tBlend factor in [0, 1] (not clamped)
Returns
a*sqrt(1-t) + b*sqrt(t)

Definition at line 33 of file crossfade.hpp.

◆ crossfade_gains_equal_power()

void sbl::dsp::math::crossfade_gains_equal_power ( float  t,
float  end_zone,
float &  gain_a,
float &  gain_b 
)
inline

Equal-power gains for a crossfade with a zone at each end.

The gains are the cosine and sine of a quarter turn, so their squares sum to 1. Within end_zone of either end of t's travel the nearer side's gain is exactly 1 and the other's exactly 0, so a control resting near an end, or a CV with a small offset, leaves none of the other side in.

Parameters
tBlend factor in [0, 1]; the scaled position is clamped, and a NaN gives the t=0 side
end_zoneThe share of t's travel at each end held at one side, in [0, 0.5)
[out]gain_aGain of the side at t=0, in [0, 1]
[out]gain_bGain of the side at t=1, in [0, 1]

Definition at line 48 of file crossfade.hpp.

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

◆ crossfade()

void sbl::dsp::math::crossfade ( const float *  a,
const float *  b,
float *  out,
float  mix,
uint16_t  frames 
)
inline

Linear crossfade over a buffer with constant mix.

out[i] = a[i] * (1-mix) + b[i] * mix. In-place safe (out can alias a or b).

Parameters
aBuffer at mix=0, frames long
bBuffer at mix=1, frames long
[out]outOutput buffer, frames long
mixBlend factor in [0, 1] (not clamped)
framesFrame count
Context
Audio thread; real-time safe.

Definition at line 69 of file crossfade.hpp.

◆ crossfade_equal_power() [2/2]

void sbl::dsp::math::crossfade_equal_power ( const float *  a,
const float *  b,
float *  out,
float  mix,
uint16_t  frames 
)
inline

Equal-power crossfade over a buffer with constant mix.

out[i] = a[i] * sqrt(1-mix) + b[i] * sqrt(mix). Gains computed once. In-place safe (out can alias a or b).

Parameters
aBuffer at mix=0, frames long
bBuffer at mix=1, frames long
[out]outOutput buffer, frames long
mixBlend factor in [0, 1] (not clamped)
framesFrame count
Context
Audio thread; real-time safe.

Definition at line 89 of file crossfade.hpp.

◆ exp_mod() [1/2]

float sbl::dsp::math::exp_mod ( float  base,
float  mod,
float  depth,
float  lo = DEFAULT_LO_HZ,
float  hi = DEFAULT_HI_HZ 
)
inline

Apply exponential modulation to a base frequency (scalar).

Converts a normalized modulation signal to a frequency via freq = base * 2^(mod * depth).

At depth = 2.0 and mod = 1.0, output is base * 4 (2 octaves up). At depth = 2.0 and mod = -1.0, output is base / 4 (2 octaves down). Equal steps in mod give equal intervals at any base frequency.

Parameters
baseCenter frequency in Hz (e.g., filter cutoff)
modNormalized modulation value, typically [-1.0, 1.0]
depthModulation depth in octaves (e.g., 2.0 = +/-2 octaves)
loMinimum output frequency in Hz (default 20)
hiMaximum output frequency in Hz (default 20000)
Returns
Modulated frequency in Hz, clamped to [lo, hi]; a NaN input is not caught and returns NaN

Definition at line 49 of file exp_mod.hpp.

References fast_exp2f().

Referenced by exp_mod().

◆ exp_mod() [2/2]

void sbl::dsp::math::exp_mod ( const float *  mod,
float *  freq_out,
uint16_t  frames,
float  base,
float  depth,
float  lo = DEFAULT_LO_HZ,
float  hi = DEFAULT_HI_HZ 
)
inline

Apply exponential modulation to a buffer.

Writes the scalar exp_mod() of each frame's mod to freq_out[].

Parameters
modNormalized modulation signal buffer, typically [-1.0, 1.0]
[out]freq_outOutput frequency buffer (Hz)
framesFrame count
baseCenter frequency in Hz
depthModulation depth in octaves
loMinimum output frequency in Hz (default 20)
hiMaximum output frequency in Hz (default 20000)
Context
Audio thread; real-time safe.

Definition at line 74 of file exp_mod.hpp.

References exp_mod().

◆ fast_sinf()

float sbl::dsp::math::fast_sinf ( float  x)
inline

Fast sine approximation for x in [0, pi/2].

Uses the 5th-order Taylor series x - x^3/6 + x^5/120.

Absolute error, which grows with x:

x = 0.0 -> error 0
x = 0.589 -> error 4.9e-6
x = pi/2 -> error 0.0045 (the maximum in the domain)
Parameters
xInput angle in radians, in [0, pi/2] (not checked)
Returns
Approximation of sin(x)

Definition at line 45 of file fast_math.hpp.

◆ fast_tan_pif()

float sbl::dsp::math::fast_tan_pif ( float  f)
inline

Fast tan(pi f) for normalized frequency f in [0, 0.497].

[5,4] Pade approximant of tan(x) evaluated at x = pi f:

tan(x) ~= x (945 - 105 x^2 + x^4) / (945 - 420 x^2 + 15 x^4)

Unlike a polynomial, the rational function models the pole at f = 0.5 (Nyquist): its denominator goes to zero near x = pi/2.

Relative error against tan(pi f), which grows with f:

f = 0.10 (4.8 kHz at 48 kHz) -> error 1.1e-5%
f = 0.35 (16.8 kHz) -> error 6.8e-5%
f = 0.45 (21.6 kHz) -> error 0.0025%
f = 0.497 (23.9 kHz) -> error 0.115%
Parameters
fNormalized frequency (freq_hz / sample_rate), in [0, 0.497] (not checked)
Returns
Approximation of tan(pi f)

Definition at line 73 of file fast_math.hpp.

References sbl::math::PI.

Referenced by sbl::dsp::Svf::integrator_gain_for().

◆ fast_exp2f()

float sbl::dsp::math::fast_exp2f ( float  x)
inline

Fast 2^x approximation for exponential modulation.

Decomposes x into integer and fractional parts. The integer part is applied by shifting the IEEE 754 exponent field (exact). The fractional part uses a 4th-order polynomial.

Accuracy against std::exp2f:

|x| <= 16 -> max relative error 0.092%, just below each whole x
x > 16 -> clamped to 16 (returns 65536)
x < -16 -> returns 0
Parameters
xExponent, e.g. +/-2.0 for +/-2 octaves of modulation
Returns
Approximation of 2^x

Definition at line 101 of file fast_math.hpp.

References EXP2_MAX_ARG, and FLOAT_MANTISSA_BITS.

Referenced by exp_mod().

◆ fast_tanhf()

float sbl::dsp::math::fast_tanhf ( float  x)
inline

Fast tanh approximation using a [3,2] Pade approximant.

tanh(x) ~= x (27 + x^2) / (27 + 9 x^2) for |x| <= 3. Inputs outside +/-3 return +/-1, and a NaN returns 0. Max relative error 2.6%, near |x| = 1.45. SoftLimiter bends its excess over a knee with it.

Parameters
xInput value, any range
Returns
Approximation of tanh(x), in [-1, 1]

Definition at line 145 of file fast_math.hpp.

References TANH_SATURATE_X.

Referenced by sbl::dsp::SoftLimiter::limit().

◆ one_minus_exp_neg()

constexpr float sbl::dsp::math::one_minus_exp_neg ( float  x)
inlineconstexpr

1 - e^(-x) by inverse Taylor, with no expf

Gives a one-pole coefficient from a time constant or a cutoff.

Parameters
xArgument, at or above 0
Returns
1 - 1 / (1 + x + x^2/2 + x^3/6), in [0, 1)

Definition at line 168 of file fast_math.hpp.

◆ db_to_gain()

float sbl::dsp::math::db_to_gain ( float  db)
inline

Amplitude gain for a level in dB: 10^(db / 20).

Parameters
dbLevel in dB
Returns
Linear amplitude gain

Definition at line 26 of file gain.hpp.

References DB_PER_DOUBLING.

Referenced by level_gain().

◆ gain_to_db()

float sbl::dsp::math::gain_to_db ( float  gain)
inline

Level in dB for an amplitude gain.

Parameters
gainLinear amplitude gain
Returns
20 log10(gain) in dB; -inf for a gain of 0 or less

Definition at line 34 of file gain.hpp.

◆ level_gain()

float sbl::dsp::math::level_gain ( float  level,
float  range_db 
)
inline

The level taper: a level in [0, 1] to a gain.

Exponential over range_db, shifted so 0 is silence and 1 is unity:

g(l) = (10^(-r (1 - l) / 20) - 10^(-r / 20)) / (1 - 10^(-r / 20)), r = range_db

Halfway is close to -r/2 dB. A range of 0 or less is the linear taper.

Parameters
levelLevel in [0, 1], clamped; a non-finite level counts as 0
range_dbRange of the taper in dB; 0 or less selects the linear taper
Returns
Linear amplitude gain in [0, 1]

Definition at line 49 of file gain.hpp.

References db_to_gain().

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

◆ ratio_from_octaves()

float sbl::dsp::math::ratio_from_octaves ( float  octaves)
inline

The frequency ratio of an interval: 2^octaves.

Parameters
octavesInterval in octaves
Returns
Frequency ratio, above 0 for a finite input

Definition at line 35 of file pitch.hpp.

◆ octaves_from_ratio()

float sbl::dsp::math::octaves_from_ratio ( float  ratio)
inline

The interval of a frequency ratio, in octaves: log2(ratio).

Parameters
ratioFrequency ratio, above 0
Returns
Interval in octaves

Definition at line 43 of file pitch.hpp.

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

◆ frequency()

float sbl::dsp::math::frequency ( float  reference,
float  octaves 
)
inline

A reference frequency moved by an interval in octaves.

Parameters
referenceReference frequency, in Hz or cycles per frame
octavesInterval in octaves
Returns
reference * 2^octaves, in the reference's unit

Definition at line 52 of file pitch.hpp.

Referenced by sbl::dsp::pm_frequency().

◆ polyblep_time() [1/2]

constexpr float sbl::dsp::math::polyblep_time ( float  before,
float  after,
float  threshold_before,
float  threshold_after 
)
constexpr

Sub-frame time at which a linear segment crossed a moving threshold.

A value went from before to after over one frame while the threshold went from threshold_before to threshold_after; the caller knows the segment crossed. Returns the fraction of the frame elapsed since the crossing, read at after: t = (after - th_after) / ((after - before) - (th_after - th_before)). A moving threshold is a pulse edge under PWM.

Parameters
beforeValue at the frame before
afterValue at the frame after
threshold_beforeThreshold at the frame before
threshold_afterThreshold at the frame after
Returns
Fraction of a frame since the crossing, clamped to [0, POLYBLEP_T_MAX]; 0 when the value and the threshold moved by the same amount, and NaN for a NaN input

Definition at line 242 of file polyblep.hpp.

References POLYBLEP_T_MAX.

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

◆ polyblep_time() [2/2]

constexpr float sbl::dsp::math::polyblep_time ( float  before,
float  after,
float  threshold 
)
constexpr

Sub-frame time at which a linear segment crossed a fixed threshold.

For a phase wrap, pass the phase before the wrap less one cycle as before, the wrapped phase as after and 0 as the threshold.

Parameters
beforeValue at the frame before
afterValue at the frame after
thresholdThreshold for both frames
Returns
Fraction of a frame since the crossing, in [0, POLYBLEP_T_MAX]; 0 when the value did not move

Definition at line 263 of file polyblep.hpp.

References polyblep_time().

Variable Documentation

◆ DEFAULT_LO_HZ

constexpr float sbl::dsp::math::DEFAULT_LO_HZ = 20.0f
inlineconstexpr

Default low output bound, in Hz

Definition at line 28 of file exp_mod.hpp.

◆ DEFAULT_HI_HZ

constexpr float sbl::dsp::math::DEFAULT_HI_HZ = 20000.0f
inlineconstexpr

Default high output bound, in Hz

Definition at line 29 of file exp_mod.hpp.

◆ FAST_TAN_PIF_MAX_F

constexpr float sbl::dsp::math::FAST_TAN_PIF_MAX_F = 0.497f
inlineconstexpr

fast_tan_pif is valid below this normalized frequency

Definition at line 25 of file fast_math.hpp.

◆ EXP2_MAX_ARG

constexpr float sbl::dsp::math::EXP2_MAX_ARG = 16.0f
inlineconstexpr

fast_exp2f clamps |x| here

Definition at line 26 of file fast_math.hpp.

Referenced by fast_exp2f().

◆ FLOAT_MANTISSA_BITS

constexpr int32_t sbl::dsp::math::FLOAT_MANTISSA_BITS = 23
inlineconstexpr

Mantissa width of an IEEE 754 single

Definition at line 27 of file fast_math.hpp.

Referenced by fast_exp2f().

◆ TANH_SATURATE_X

constexpr float sbl::dsp::math::TANH_SATURATE_X = 3.0f
inlineconstexpr

fast_tanhf returns +/-1 beyond this

Definition at line 28 of file fast_math.hpp.

Referenced by fast_tanhf().

◆ DB_PER_DOUBLING

constexpr float sbl::dsp::math::DB_PER_DOUBLING = 6.0206f
inlineconstexpr

20 log10(2), in dB

Definition at line 18 of file gain.hpp.

Referenced by db_to_gain().

◆ POLYBLEP_T_MAX

constexpr float sbl::dsp::math::POLYBLEP_T_MAX = 0.99999994f
inlineconstexpr

The largest float below 1, the upper bound of a sub-frame time.

Definition at line 41 of file polyblep.hpp.

Referenced by polyblep_time().