From 59217c562f1e1b74cfbfe483c4947a91cd3b4685 Mon Sep 17 00:00:00 2001 From: Lost Robot <34612565+LostRobotMusic@users.noreply.github.com> Date: Mon, 7 Oct 2024 23:49:49 -0700 Subject: [PATCH 1/4] Optimize dBFS <-> amplitude functions --- include/lmms_math.h | 46 ++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/include/lmms_math.h b/include/lmms_math.h index 72800838818..75f63a3e408 100644 --- a/include/lmms_math.h +++ b/include/lmms_math.h @@ -164,7 +164,32 @@ inline float linearToLogScale(float min, float max, float value) return std::isnan( result ) ? 0 : result; } +static inline float fastPow10f(float x) +{ + return expf(2.302585092994046f * x); +} + +static inline float fastLog10f(float x) +{ + return logf(x) * 0.4342944819032518f; +} +//! @brief Converts linear amplitude (>0-1.0) to dBFS scale. +//! @param amp Linear amplitude, where 1.0 = 0dBFS. ** Must be larger than zero! ** +//! @return Amplitude in dBFS. +static inline float ampToDbfs(float amp) +{ + return fastLog10f(amp) * 20.0f; +} + + +//! @brief Converts dBFS-scale to linear amplitude with 0dBFS = 1.0 +//! @param dbfs The dBFS value to convert. ** Must be a real number - not inf/nan! ** +//! @return Linear amplitude +static inline float dbfsToAmp(float dbfs) +{ + return fastPow10f(dbfs * 0.05f); +} //! @brief Converts linear amplitude (0-1.0) to dBFS scale. Handles zeroes as -inf. @@ -174,7 +199,7 @@ inline float safeAmpToDbfs(float amp) { return amp == 0.0f ? -INFINITY - : log10f( amp ) * 20.0f; + : ampToDbfs(amp); } @@ -185,27 +210,10 @@ inline float safeDbfsToAmp(float dbfs) { return std::isinf( dbfs ) ? 0.0f - : std::pow(10.f, dbfs * 0.05f ); + : dbfsToAmp(dbfs); } -//! @brief Converts linear amplitude (>0-1.0) to dBFS scale. -//! @param amp Linear amplitude, where 1.0 = 0dBFS. ** Must be larger than zero! ** -//! @return Amplitude in dBFS. -inline float ampToDbfs(float amp) -{ - return log10f(amp) * 20.0f; -} - - -//! @brief Converts dBFS-scale to linear amplitude with 0dBFS = 1.0 -//! @param dbfs The dBFS value to convert. ** Must be a real number - not inf/nan! ** -//! @return Linear amplitude -inline float dbfsToAmp(float dbfs) -{ - return std::pow(10.f, dbfs * 0.05f); -} - //! Returns the linear interpolation of the two values template From fa357f6909ab67fbd34e4126f51bfe22a48abc21 Mon Sep 17 00:00:00 2001 From: Lost Robot <34612565+LostRobotMusic@users.noreply.github.com> Date: Mon, 7 Oct 2024 23:55:58 -0700 Subject: [PATCH 2/4] fix mixed tabs and spaces --- include/lmms_math.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/lmms_math.h b/include/lmms_math.h index 75f63a3e408..e4e2efeff93 100644 --- a/include/lmms_math.h +++ b/include/lmms_math.h @@ -171,7 +171,7 @@ static inline float fastPow10f(float x) static inline float fastLog10f(float x) { - return logf(x) * 0.4342944819032518f; + return logf(x) * 0.4342944819032518f; } //! @brief Converts linear amplitude (>0-1.0) to dBFS scale. From 73fb99aa1678081c17c0882554d61eefdbf6ddab Mon Sep 17 00:00:00 2001 From: Lost Robot <34612565+LostRobotMusic@users.noreply.github.com> Date: Tue, 8 Oct 2024 06:42:14 -0700 Subject: [PATCH 3/4] style --- include/lmms_math.h | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/include/lmms_math.h b/include/lmms_math.h index e4e2efeff93..75a14d7092d 100644 --- a/include/lmms_math.h +++ b/include/lmms_math.h @@ -164,12 +164,12 @@ inline float linearToLogScale(float min, float max, float value) return std::isnan( result ) ? 0 : result; } -static inline float fastPow10f(float x) +inline float fastPow10f(float x) { return expf(2.302585092994046f * x); } -static inline float fastLog10f(float x) +inline float fastLog10f(float x) { return logf(x) * 0.4342944819032518f; } @@ -177,7 +177,7 @@ static inline float fastLog10f(float x) //! @brief Converts linear amplitude (>0-1.0) to dBFS scale. //! @param amp Linear amplitude, where 1.0 = 0dBFS. ** Must be larger than zero! ** //! @return Amplitude in dBFS. -static inline float ampToDbfs(float amp) +inline float ampToDbfs(float amp) { return fastLog10f(amp) * 20.0f; } @@ -186,7 +186,7 @@ static inline float ampToDbfs(float amp) //! @brief Converts dBFS-scale to linear amplitude with 0dBFS = 1.0 //! @param dbfs The dBFS value to convert. ** Must be a real number - not inf/nan! ** //! @return Linear amplitude -static inline float dbfsToAmp(float dbfs) +inline float dbfsToAmp(float dbfs) { return fastPow10f(dbfs * 0.05f); } @@ -197,9 +197,7 @@ static inline float dbfsToAmp(float dbfs) //! @return Amplitude in dBFS. -inf for 0 amplitude. inline float safeAmpToDbfs(float amp) { - return amp == 0.0f - ? -INFINITY - : ampToDbfs(amp); + return amp == 0.0f ? -INFINITY : ampToDbfs(amp); } @@ -208,9 +206,7 @@ inline float safeAmpToDbfs(float amp) //! @return Linear amplitude inline float safeDbfsToAmp(float dbfs) { - return std::isinf( dbfs ) - ? 0.0f - : dbfsToAmp(dbfs); + return std::isinf(dbfs) ? 0.0f : dbfsToAmp(dbfs); } From 50843069adcb058adfe36c84dead7e3b129cc205 Mon Sep 17 00:00:00 2001 From: Lost Robot <34612565+LostRobotMusic@users.noreply.github.com> Date: Tue, 8 Oct 2024 12:03:20 -0700 Subject: [PATCH 4/4] use std namespace --- include/lmms_math.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/lmms_math.h b/include/lmms_math.h index 75a14d7092d..bdadd7ba0c4 100644 --- a/include/lmms_math.h +++ b/include/lmms_math.h @@ -166,12 +166,12 @@ inline float linearToLogScale(float min, float max, float value) inline float fastPow10f(float x) { - return expf(2.302585092994046f * x); + return std::exp(2.302585092994046f * x); } inline float fastLog10f(float x) { - return logf(x) * 0.4342944819032518f; + return std::log(x) * 0.4342944819032518f; } //! @brief Converts linear amplitude (>0-1.0) to dBFS scale.