From ba4c64adeaa4fed8ac5d8bbd6496567422a0ca79 Mon Sep 17 00:00:00 2001 From: Paul Ferrand Date: Mon, 23 Dec 2019 18:32:37 +0100 Subject: [PATCH] Readability cleanup --- src/sfizz/MathHelpers.h | 73 ++++++++++++++++++++++++++--------------- src/sfizz/Voice.cpp | 17 +++------- 2 files changed, 51 insertions(+), 39 deletions(-) diff --git a/src/sfizz/MathHelpers.h b/src/sfizz/MathHelpers.h index 601152e7..23a01211 100644 --- a/src/sfizz/MathHelpers.h +++ b/src/sfizz/MathHelpers.h @@ -26,7 +26,7 @@ * @author Paul Ferrand (paul@ferrand.cc) * @brief Contains math helper functions and math constants * @version 0.1 - * @date 2019-11-23 + * @date 2019-11-23 */ #pragma once #include @@ -43,10 +43,10 @@ inline constexpr T min(T op1, T op2, T op3, T op4) { return std::min(op1, std::m /** * @brief Converts db values into power (applies 10**(in/10)) - * - * @tparam Type - * @param in - * @return Type + * + * @tparam Type + * @param in + * @return Type */ template inline constexpr Type db2pow(Type in) @@ -56,10 +56,10 @@ inline constexpr Type db2pow(Type in) /** * @brief Converts power values into dB (applies 10log10(in)) - * - * @tparam Type - * @param in - * @return Type + * + * @tparam Type + * @param in + * @return Type */ template inline constexpr Type pow2db(Type in) @@ -69,10 +69,10 @@ inline constexpr Type pow2db(Type in) /** * @brief Converts dB values to magnitude (applies 10**(in/20)) - * - * @tparam Type - * @param in - * @return constexpr Type + * + * @tparam Type + * @param in + * @return constexpr Type */ template inline constexpr Type db2mag(Type in) @@ -82,10 +82,10 @@ inline constexpr Type db2mag(Type in) /** * @brief Converts magnitude values into dB (applies 20log10(in)) - * - * @tparam Type - * @param in - * @return Type + * + * @tparam Type + * @param in + * @return Type */ template inline constexpr Type mag2db(Type in) @@ -95,9 +95,9 @@ inline constexpr Type mag2db(Type in) /** * @brief Global random singletons - * + * * TODO: could be moved into a singleton class holder - * + * */ namespace Random { static std::random_device randomDevice; @@ -106,9 +106,9 @@ namespace Random { /** * @brief Converts a midi note to a frequency value - * - * @param noteNumber - * @return float + * + * @param noteNumber + * @return float */ inline float midiNoteFrequency(const int noteNumber) { @@ -117,11 +117,11 @@ inline float midiNoteFrequency(const int noteNumber) /** * @brief Clamps a value between bounds, including the bounds! - * - * @tparam T - * @param v - * @param lo - * @param hi + * + * @tparam T + * @param v + * @param lo + * @param hi * @return T */ template @@ -131,6 +131,25 @@ constexpr T clamp( const T& v, const T& lo, const T& hi ) return (v < lo) ? lo : (hi < v) ? hi : v; } +template +constexpr void incrementAll(T& only) +{ + only++; +} + +template +constexpr void incrementAll(T& first, Args&... rest) +{ + first++; + incrementAll(rest...); +} + +template +constexpr ValueType linearInterpolation(ValueType left, ValueType right, ValueType leftCoeff, ValueType rightCoeff) +{ + return left * leftCoeff + right * rightCoeff; +} + template constexpr Type pi { 3.141592653589793238462643383279502884 }; template diff --git a/src/sfizz/Voice.cpp b/src/sfizz/Voice.cpp index d7800a8f..a7e4a15e 100644 --- a/src/sfizz/Voice.cpp +++ b/src/sfizz/Voice.cpp @@ -420,23 +420,16 @@ void sfz::Voice::fillWithData(AudioSpan buffer) noexcept auto leftSource = source.getChannel(0); if (source.getNumChannels() == 1) { while (ind < indices.end()) { - *left = leftSource[*ind] * (*leftCoeff) + leftSource[*ind + 1] * (*rightCoeff); - left++; - ind++; - leftCoeff++; - rightCoeff++; + *left = linearInterpolation(leftSource[*ind], leftSource[*ind + 1], *leftCoeff, *rightCoeff); + incrementAll(ind, left, leftCoeff, rightCoeff); } } else { auto right = buffer.getChannel(1); auto rightSource = source.getChannel(1); while (ind < indices.end()) { - *left = leftSource[*ind] * (*leftCoeff) + leftSource[*ind + 1] * (*rightCoeff); - *right = rightSource[*ind] * (*leftCoeff) + rightSource[*ind + 1] * (*rightCoeff); - left++; - right++; - ind++; - leftCoeff++; - rightCoeff++; + *left = linearInterpolation(leftSource[*ind], leftSource[*ind + 1], *leftCoeff, *rightCoeff); + *right = linearInterpolation(rightSource[*ind], rightSource[*ind + 1], *leftCoeff, *rightCoeff); + incrementAll(ind, left, right, leftCoeff, rightCoeff); } }