39 T(0.99999997408724855), T(-0.16666646026660671), T(0.0083328727116396326),
40 T(-0.00019799239565814083), T(2.5871610835732768e-6)};
48template <FloatType T>
inline constexpr T
pi = std::numbers::pi_v<T>;
51template <FloatType T>
inline constexpr T
twoPi = T(2) * std::numbers::pi_v<T>;
54template <FloatType T>
inline constexpr T
invTwoPi = T(1) / twoPi<T>;
57template <FloatType T>
inline constexpr T
halfPi = std::numbers::pi_v<T> / T(2);
60template <FloatType T>
inline constexpr T
sqrt2 = std::numbers::sqrt2_v<T>;
63template <FloatType T>
inline constexpr T
invSqrt2 = T(1) / std::numbers::sqrt2_v<T>;
77[[nodiscard]]
inline T
decibelsToGain(T dB, T minusInfinityDb = T(-100)) noexcept
81 return dB <= minusInfinityDb ? T(0) : std::exp(dB * T(0.11512925464970228420));
92[[nodiscard]]
inline T
gainToDecibels(T gain, T minusInfinityDb = T(-100)) noexcept
96 return gain > T(0) ? std::max(minusInfinityDb, T(8.6858896380650365530) * std::log(gain))
116template <FloatType T>
117[[nodiscard]]
inline T
mapRange(T value, T inMin, T inMax, T outMin, T outMax)
noexcept
119 if (inMin == inMax)
return outMin;
120 return outMin + (outMax - outMin) * ((value - inMin) / (inMax - inMin));
136template <FloatType T>
137[[nodiscard]]
inline T
moveTowards(T from, T to, T maxDelta)
noexcept
141 const T delta = to - from;
142 return std::abs(delta) <= maxDelta ? to : from + std::clamp(delta, -maxDelta, maxDelta);
168template <FloatType T>
173 x = std::clamp(x, T(-3), T(3));
175 const auto x2 = x * x;
176 const auto x4 = x2 * x2;
178 return x * (T(945) + T(105) * x2 + x4) / (T(945) + T(420) * x2 + T(15) * x4);
196template <FloatType T>
202 constexpr T kLog2Of10 =
203 static_cast<T
>(std::numbers::ln10_v<long double> / std::numbers::ln2_v<long double>);
204 return std::exp2(x * kLog2Of10);
219template <FloatType T>
222 return std::exp2(x * std::numbers::log2e_v<T>);
241template <FloatType T>
244 const T v = std::min(std::max(x, T(0)), T(4));
246 const T even = T(120) + T(12) * v2;
247 const T odd = v * (T(60) + v2);
248 return (even - odd) / (even + odd);
265 const double half = 0.5 * (ll - rr);
266 return std::max(0.0, 0.5 * (ll + rr) + std::sqrt(half * half + lr * lr));
284template <FloatType T>
287 const double pl =
static_cast<double>(levelL) *
static_cast<double>(levelL);
288 const double pr =
static_cast<double>(levelR) *
static_cast<double>(levelR);
289 const double cross = std::sqrt(std::clamp(rho2, 0.0, 1.0) * pl * pr);
309template <FloatType T>
312 constexpr T limit = T(1.520);
313 if (std::abs(x) > limit)
return std::tan(x);
315 const T x4 = x2 * x2;
317 return x * (T(945) - T(105) * x2 + x4) / (T(945) - T(420) * x2 + T(15) * x4);
339template <FloatType T>
344 const T k = std::floor(x * invTwoPi<T> + T(0.5));
346 x -= k * T(0.0019353071795864769253);
350 if (x > halfPi<T>) x = pi<T> - x;
351 else if (x < -halfPi<T>) x = -pi<T> - x;
357 constexpr auto c = detail::fastSinPolynomial<T>;
358 return x * (c[0] + x2 * (c[1] + x2 * (c[2] + x2 * (c[3] + x2 * c[4]))));
368template <FloatType T>
392template <FloatType T>
397 if constexpr (std::is_same_v<T, float>)
399 const std::uint32_t bits = std::bit_cast<std::uint32_t>(x);
400 if (bits - 0x00800000u < 0x7F000000u)
402 e =
static_cast<int>(bits >> 23) - 126;
403 m = std::bit_cast<float>((bits & 0x007FFFFFu) | 0x3F000000u);
405 else m = std::frexp(x, &e);
407 else if constexpr (std::is_same_v<T, double>)
409 const std::uint64_t bits = std::bit_cast<std::uint64_t>(x);
410 if (bits - 0x0010000000000000ull < 0x7FE0000000000000ull)
412 e =
static_cast<int>(bits >> 52) - 1022;
413 m = std::bit_cast<double>((bits & 0x000FFFFFFFFFFFFFull) | 0x3FE0000000000000ull);
415 else m = std::frexp(x, &e);
419 m = std::frexp(x, &e);
422 if (m < T(0.70710678118654752440)) { m *= T(2); --e; }
426 const T s = (m - T(1)) / (m + T(1));
428 const T lnm = T(2) * s * (T(1)
431 + s2 * T(1.0 / 7.0))));
433 return lnm +
static_cast<T
>(e) * std::numbers::ln2_v<T>;
455template <FloatType T>
459 T wrapped = phase - twoPi<T> * std::floor(phase * invTwoPi<T>);
466 if (wrapped < T(0)) wrapped += twoPi<T>;
467 if (wrapped >= twoPi<T>) wrapped -= twoPi<T>;
Constrains a type to IEEE floating-point (float or double).
constexpr std::array< T, 5 > fastSinPolynomial
Main namespace for the DSPark framework.
constexpr T invSqrt2
1 / square root of 2 (0.70710...). Butterworth Q factor.
T fastTan(T x) noexcept
Fast approximation of tan(x) using a Pade [5,4] rational approximant.
T moveTowards(T from, T to, T maxDelta) noexcept
Moves a value toward a target by at most a given distance.
T dominantPairLevel(T levelL, T levelR, double rho2) noexcept
Level of the dominant component of a stereo pair from the levels of its two channels and their square...
T fastPow10(T x) noexcept
Fast approximation of 10^x using exp2.
T decibelsToGain(T dB, T minusInfinityDb=T(-100)) noexcept
Converts a value in decibels to linear gain.
T fastDecay(T x) noexcept
e^(-x) for x >= 0 by a rational approximation: the coefficient of a one-pole whose time changes every...
double dominantPairPower(double ll, double rr, double lr) noexcept
Power of the dominant component of a stereo pair: the larger eigenvalue of the pair's covariance [ll ...
T mapRange(T value, T inMin, T inMax, T outMin, T outMax) noexcept
Maps a value from one range to another (linear interpolation).
constexpr T sqrt2
Square root of 2 (1.41421...).
constexpr T pi
Pi (3.14159...) for the given floating-point type.
T fastSin(T x) noexcept
Fast sine approximation (degree-9 odd minimax polynomial).
T fastCos(T x) noexcept
Fast cosine approximation. See fastSin() for accuracy notes (the pi/2 offset costs float about one ul...
T fastLog(T x) noexcept
Fast natural logarithm approximation.
T gainToDecibels(T gain, T minusInfinityDb=T(-100)) noexcept
Converts a linear gain value to decibels.
T fastTanh(T x) noexcept
Fast tanh approximation using a Pade [5,4] rational function.
constexpr T invTwoPi
1 / (2 * Pi) (0.15915...). Useful for fast phase divisions.
T fastExp(T x) noexcept
e^x via std::exp2, using the identity e^x = 2^(x * log2(e)).
constexpr T halfPi
Pi / 2 (1.57079...). Quarter period; sin/cos phase offset.
constexpr T twoPi
2 * Pi (6.28318...).
T wrapPhase(T phase) noexcept
Normalises a phase value to the range [0, 2*pi).