36 T(0.99999997408724855), T(-0.16666646026660671), T(0.0083328727116396326),
37 T(-0.00019799239565814083), T(2.5871610835732768e-6)};
45template <FloatType T>
inline constexpr T
pi = std::numbers::pi_v<T>;
48template <FloatType T>
inline constexpr T
twoPi = T(2) * std::numbers::pi_v<T>;
51template <FloatType T>
inline constexpr T
invTwoPi = T(1) / twoPi<T>;
54template <FloatType T>
inline constexpr T
halfPi = std::numbers::pi_v<T> / T(2);
57template <FloatType T>
inline constexpr T
sqrt2 = std::numbers::sqrt2_v<T>;
60template <FloatType T>
inline constexpr T
invSqrt2 = T(1) / std::numbers::sqrt2_v<T>;
74[[nodiscard]]
inline T
decibelsToGain(T dB, T minusInfinityDb = T(-100)) noexcept
78 return dB <= minusInfinityDb ? T(0) : std::exp(dB * T(0.11512925464970228420));
89[[nodiscard]]
inline T
gainToDecibels(T gain, T minusInfinityDb = T(-100)) noexcept
93 return gain > T(0) ? std::max(minusInfinityDb, T(8.6858896380650365530) * std::log(gain))
113template <FloatType T>
114[[nodiscard]]
inline T
mapRange(T value, T inMin, T inMax, T outMin, T outMax)
noexcept
116 if (inMin == inMax)
return outMin;
117 return outMin + (outMax - outMin) * ((value - inMin) / (inMax - inMin));
133template <FloatType T>
134[[nodiscard]]
inline T
moveTowards(T from, T to, T maxDelta)
noexcept
138 const T delta = to - from;
139 return std::abs(delta) <= maxDelta ? to : from + std::clamp(delta, -maxDelta, maxDelta);
160template <FloatType T>
165 x = std::clamp(x, T(-3), T(3));
167 const auto x2 = x * x;
168 const auto x4 = x2 * x2;
170 return x * (T(945) + T(105) * x2 + x4) / (T(945) + T(420) * x2 + T(15) * x4);
182template <FloatType T>
188 constexpr T kLog2Of10 =
189 static_cast<T
>(std::numbers::ln10_v<long double> / std::numbers::ln2_v<long double>);
190 return std::exp2(x * kLog2Of10);
201template <FloatType T>
204 return std::exp2(x * std::numbers::log2e_v<T>);
221template <FloatType T>
224 constexpr T limit = T(1.520);
225 if (std::abs(x) > limit)
return std::tan(x);
227 const T x4 = x2 * x2;
229 return x * (T(945) - T(105) * x2 + x4) / (T(945) - T(420) * x2 + T(15) * x4);
247template <FloatType T>
252 const T k = std::floor(x * invTwoPi<T> + T(0.5));
254 x -= k * T(0.0019353071795864769253);
258 if (x > halfPi<T>) x = pi<T> - x;
259 else if (x < -halfPi<T>) x = -pi<T> - x;
265 constexpr auto c = detail::fastSinPolynomial<T>;
266 return x * (c[0] + x2 * (c[1] + x2 * (c[2] + x2 * (c[3] + x2 * c[4]))));
275template <FloatType T>
292template <FloatType T>
296 T m = std::frexp(x, &e);
298 if (m < T(0.70710678118654752440)) { m *= T(2); --e; }
302 const T s = (m - T(1)) / (m + T(1));
304 const T lnm = T(2) * s * (T(1)
307 + s2 * T(1.0 / 7.0))));
309 return lnm +
static_cast<T
>(e) * std::numbers::ln2_v<T>;
325template <FloatType T>
329 T wrapped = phase - twoPi<T> * std::floor(phase * invTwoPi<T>);
336 if (wrapped < T(0)) wrapped += twoPi<T>;
337 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 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 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 half a...
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 Pade rational function.
constexpr T invTwoPi
1 / (2 * Pi) (0.15915...). Useful for fast phase divisions.
T fastExp(T x) noexcept
Fast approximation of e^x via std::exp2 (~2x faster than std::exp on MSVC).
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).