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

Domain-neutral math: constants, clamp, interpolation. More...

Detailed Description

Domain-neutral math: constants, clamp, interpolation.

Functions

template<typename T >
constexpr T clamp (T x, T lo, T hi)
 x held to [lo, hi]; a NaN goes to lo.
 
template<typename T >
constexpr T clamp01 (T x)
 x held to [0, 1]; a NaN goes to 0.
 
bool is_finite (float x)
 std::isfinite, read from the exponent's bits.
 
float finite_or (float x, float fallback)
 x, or fallback when x is an infinity or a NaN.
 
constexpr float crossfade (float a, float b, float t)
 Linear crossfade between two values.
 
constexpr float smooth_step (float t)
 Hermite smooth step (3rd-order S-curve)
 
constexpr int32_t crossfade_i16 (int16_t a, int16_t b, uint16_t t)
 Integer crossfade for 16-bit values.
 
uint32_t magnitude_bits (float x)
 x's bits with the sign cleared.
 

Variables

constexpr float PI = 3.14159265358979323846f
 
constexpr float TWO_PI = 2.0f * PI
 
constexpr float HALF_PI = 0.5f * PI
 
constexpr float MS_PER_SECOND = 1000.0f
 
constexpr float SECONDS_PER_MS = 0.001f
 
constexpr float SECONDS_PER_MINUTE = 60.0f
 
constexpr uint16_t U16_MAX = 65535
 The full scale of a 16-bit unsigned blend factor.
 
constexpr uint32_t FLOAT_EXPONENT_BITS = 0x7F800000u
 A single's exponent field: all ones in infinities and NaNs.
 

Function Documentation

◆ clamp()

◆ clamp01()

template<typename T >
constexpr T sbl::math::clamp01 ( T  x)
inlineconstexpr

x held to [0, 1]; a NaN goes to 0.

Parameters
xValue to clamp
Returns
x, 0 or 1

Definition at line 35 of file clamp.hpp.

References clamp().

Referenced by sbl::dsp::Vco::knee_for(), sbl::control::ParamEcho< Target >::poll(), sbl::dsp::Vca::process(), sbl::dsp::DryWet::process(), and sbl::dsp::Vco::pulse_amount().

◆ is_finite()

bool sbl::math::is_finite ( float  x)
inline

std::isfinite, read from the exponent's bits.

Parameters
xAny float
Returns
false for an infinity or a NaN, true otherwise

Definition at line 27 of file finite.hpp.

References FLOAT_EXPONENT_BITS, and magnitude_bits().

Referenced by finite_or(), sbl::dsp::PhaseWalk::frame(), and sbl::dsp::PmInput::process().

◆ finite_or()

◆ crossfade()

constexpr float sbl::math::crossfade ( float  a,
float  b,
float  t 
)
inlineconstexpr

Linear crossfade between two values.

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

Definition at line 28 of file interpolate.hpp.

◆ smooth_step()

constexpr float sbl::math::smooth_step ( float  t)
inlineconstexpr

Hermite smooth step (3rd-order S-curve)

Maps [0, 1] to [0, 1] with zero derivative at the endpoints.

Parameters
tInput in [0, 1] (not clamped)
Returns
t^2 * (3 - 2t)

Definition at line 38 of file interpolate.hpp.

◆ crossfade_i16()

constexpr int32_t sbl::math::crossfade_i16 ( int16_t  a,
int16_t  b,
uint16_t  t 
)
inlineconstexpr

Integer crossfade for 16-bit values.

Parameters
aValue at t=0
bValue at t=1
tBlend factor [0, 65535] (0 = all a, 65535 = all b)
Returns
a + (b - a) * t / 65535, truncated toward zero

Definition at line 48 of file interpolate.hpp.

References U16_MAX.

◆ magnitude_bits()

uint32_t sbl::math::magnitude_bits ( float  x)
inline

x's bits with the sign cleared.

For finite a and b, magnitude_bits(a) < magnitude_bits(b) exactly when |a| < |b|; a non-finite value reads above every finite one, and both zeros read 0.

Parameters
xAny float, NaN and infinities included
Returns
The IEEE 754 bit pattern of x with bit 31 cleared

Definition at line 29 of file magnitude.hpp.

Referenced by sbl::dsp::SoftLimiter::acts_on(), and is_finite().

Variable Documentation

◆ PI

constexpr float sbl::math::PI = 3.14159265358979323846f
inlineconstexpr

◆ TWO_PI

constexpr float sbl::math::TWO_PI = 2.0f * PI
inlineconstexpr

2 pi

Definition at line 18 of file constants.hpp.

Referenced by sbl::dsp::OnePole::coefficient_for().

◆ HALF_PI

constexpr float sbl::math::HALF_PI = 0.5f * PI
inlineconstexpr

pi / 2

Definition at line 19 of file constants.hpp.

◆ MS_PER_SECOND

constexpr float sbl::math::MS_PER_SECOND = 1000.0f
inlineconstexpr

Milliseconds in a second

Definition at line 21 of file constants.hpp.

◆ SECONDS_PER_MS

constexpr float sbl::math::SECONDS_PER_MS = 0.001f
inlineconstexpr

Seconds in a millisecond

Definition at line 22 of file constants.hpp.

◆ SECONDS_PER_MINUTE

constexpr float sbl::math::SECONDS_PER_MINUTE = 60.0f
inlineconstexpr

Seconds in a minute

Definition at line 23 of file constants.hpp.

◆ U16_MAX

constexpr uint16_t sbl::math::U16_MAX = 65535
inlineconstexpr

The full scale of a 16-bit unsigned blend factor.

Definition at line 26 of file constants.hpp.

Referenced by crossfade_i16().

◆ FLOAT_EXPONENT_BITS

constexpr uint32_t sbl::math::FLOAT_EXPONENT_BITS = 0x7F800000u
inlineconstexpr

A single's exponent field: all ones in infinities and NaNs.

Definition at line 19 of file finite.hpp.

Referenced by is_finite().