36template <FloatType T>
inline constexpr T
pi = std::numbers::pi_v<T>;
39template <FloatType T>
inline constexpr T
twoPi = T(2) * std::numbers::pi_v<T>;
42template <FloatType T>
inline constexpr T
invTwoPi = T(1) / twoPi<T>;
45template <FloatType T>
inline constexpr T
halfPi = std::numbers::pi_v<T> / T(2);
48template <FloatType T>
inline constexpr T
sqrt2 = std::numbers::sqrt2_v<T>;
51template <FloatType T>
inline constexpr T
invSqrt2 = T(1) / std::numbers::sqrt2_v<T>;
65[[nodiscard]]
inline T
decibelsToGain(T dB, T minusInfinityDb = T(-100)) noexcept
67 return dB <= minusInfinityDb ? T(0) : std::pow(T(10), dB / T(20));
78[[nodiscard]]
inline T
gainToDecibels(T gain, T minusInfinityDb = T(-100)) noexcept
80 return gain > T(0) ? std::max(minusInfinityDb, T(20) * std::log10(gain))
100template <FloatType T>
101[[nodiscard]]
inline T
mapRange(T value, T inMin, T inMax, T outMin, T outMax)
noexcept
103 if (inMin == inMax)
return outMin;
104 return outMin + (outMax - outMin) * ((value - inMin) / (inMax - inMin));
125template <FloatType T>
130 x = std::clamp(x, T(-3), T(3));
132 const auto x2 = x * x;
133 const auto x4 = x2 * x2;
135 return x * (T(945) + T(105) * x2 + x4) / (T(945) + T(420) * x2 + T(15) * x4);
147template <FloatType T>
153 constexpr T kLog2Of10 =
154 static_cast<T
>(std::numbers::ln10_v<long double> / std::numbers::ln2_v<long double>);
155 return std::exp2(x * kLog2Of10);
166template <FloatType T>
169 return std::exp2(x * std::numbers::log2e_v<T>);
186template <FloatType T>
189 constexpr T limit = T(1.520);
190 if (std::abs(x) > limit)
return std::tan(x);
192 const T x4 = x2 * x2;
194 return x * (T(945) - T(105) * x2 + x4) / (T(945) - T(420) * x2 + T(15) * x4);
212template <FloatType T>
217 const T k = std::floor(x * invTwoPi<T> + T(0.5));
219 x -= k * T(0.0019353071795864769253);
223 if (x > halfPi<T>) x = pi<T> - x;
224 else if (x < -halfPi<T>) x = -pi<T> - x;
230 return x * (T(0.9999999995)
231 + x2 * (T(-0.1666666580)
232 + x2 * (T(0.0083333075)
233 + x2 * (T(-0.0001984090)
234 + x2 * T(0.0000027526)))));
243template <FloatType T>
260template <FloatType T>
264 T m = std::frexp(x, &e);
266 if (m < T(0.70710678118654752440)) { m *= T(2); --e; }
270 const T s = (m - T(1)) / (m + T(1));
272 const T lnm = T(2) * s * (T(1)
275 + s2 * T(1.0 / 7.0))));
277 return lnm +
static_cast<T
>(e) * std::numbers::ln2_v<T>;
293template <FloatType T>
297 T wrapped = phase - twoPi<T> * std::floor(phase * invTwoPi<T>);
304 if (wrapped < T(0)) wrapped += twoPi<T>;
305 if (wrapped >= twoPi<T>) wrapped -= twoPi<T>;
Constrains a type to IEEE floating-point (float or double).
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 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 (float error is ~7e-6 here: half an ulp m...
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).