From 542bd28a283fe0c9886c16fd5c00b7cf63ccc2f6 Mon Sep 17 00:00:00 2001 From: Paul Fd Date: Tue, 10 Mar 2020 14:12:46 +0100 Subject: [PATCH] Use @falktx's way for the math constants --- src/sfizz/MathHelpers.h | 29 +++++++++++++---------------- src/sfizz/SIMDHelpers.h | 2 +- src/sfizz/Voice.cpp | 6 +++--- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/sfizz/MathHelpers.h b/src/sfizz/MathHelpers.h index a8c97be6..ac755a11 100644 --- a/src/sfizz/MathHelpers.h +++ b/src/sfizz/MathHelpers.h @@ -124,9 +124,7 @@ inline float midiNoteFrequency(const int noteNumber) template constexpr T clamp( T v, T lo, T hi ) { - v = min(v, hi); - v = max(v, lo); - return v; + return max(min(v, hi), lo); } template @@ -148,19 +146,18 @@ constexpr ValueType linearInterpolation(ValueType left, ValueType right, ValueTy return left * leftCoeff + right * rightCoeff; } -constexpr double dPi { 3.141592653589793238462643383279502884}; -constexpr double dTwoPi { dPi * 2 }; -constexpr double dPiTwo { dPi / 2 }; -constexpr double dPiFour { dPi / 4 }; -constexpr double dSqrtTwo { 1.414213562373095048801688724209698078569671875376948073176 }; -constexpr double dSqrtTwoInv { 0.707106781186547524400844362104849039284835937688474036588 }; - -constexpr float fPi { 3.141592653589793238462643383279502884}; -constexpr float fTwoPi { fPi * 2 }; -constexpr float fPiTwo { fPi / 2 }; -constexpr float fPiFour { fPi / 4 }; -constexpr float fSqrtTwo { 1.414213562373095048801688724209698078569671875376948073176 }; -constexpr float fSqrtTwoInv { 0.707106781186547524400844362104849039284835937688474036588 }; +template +constexpr Type pi() { return static_cast(3.141592653589793238462643383279502884); }; +template +constexpr Type twoPi() { return pi() * 2; }; +template +constexpr Type piTwo() { return pi() / 2; }; +template +constexpr Type piFour() { return pi() / 4; }; +template +constexpr Type sqrtTwo() { return static_cast(1.414213562373095048801688724209698078569671875376948073176); }; +template +constexpr Type sqrtTwoInv() { return static_cast(0.707106781186547524400844362104849039284835937688474036588); }; /** @brief A fraction which is parameterized by integer type diff --git a/src/sfizz/SIMDHelpers.h b/src/sfizz/SIMDHelpers.h index 253c125a..00761dae 100644 --- a/src/sfizz/SIMDHelpers.h +++ b/src/sfizz/SIMDHelpers.h @@ -748,7 +748,7 @@ namespace _internals { int i = 0; for (; i < panSize; ++i) - pan[i] = std::cos(i * (dPiTwo / (panSize - 1))); + pan[i] = std::cos(i * (twoPi() / (panSize - 1))); for (; i < static_cast(pan.size()); ++i) pan[i] = pan[panSize - 1]; diff --git a/src/sfizz/Voice.cpp b/src/sfizz/Voice.cpp index d74c8964..f92ec40b 100644 --- a/src/sfizz/Voice.cpp +++ b/src/sfizz/Voice.cpp @@ -479,7 +479,7 @@ void sfz::Voice::fillWithGenerator(AudioSpan buffer) noexcept auto bends = tempSpan2.first(buffer.getNumFrames()); auto phases = tempSpan2.first(buffer.getNumFrames()); - const float step = baseFrequency * fTwoPi / sampleRate; + const float step = baseFrequency * twoPi() / sampleRate; fill(jumps, step); if (region->bendStep > 1) @@ -496,8 +496,8 @@ void sfz::Voice::fillWithGenerator(AudioSpan buffer) noexcept copy(leftSpan, rightSpan); // Wrap the phase so we don't loose too much precision on longer notes - const auto numTwoPiWraps = static_cast(phase / fTwoPi); - phase -= fTwoPi * static_cast(numTwoPiWraps); + const auto numTwoPiWraps = static_cast(phase / twoPi()); + phase -= twoPi() * static_cast(numTwoPiWraps); } }