From 1a5c085aa2bb15d46d3bf07a86da73193f538e86 Mon Sep 17 00:00:00 2001 From: Vesa Date: Fri, 26 Dec 2014 19:31:16 +0200 Subject: [PATCH 1/2] Faster dbv/linear conversions, separate safe functions for when the extra safety (0/inf handling) is needed --- include/lmms_math.h | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/include/lmms_math.h b/include/lmms_math.h index 7cde29153..4ab9b3471 100644 --- a/include/lmms_math.h +++ b/include/lmms_math.h @@ -239,10 +239,10 @@ static inline float linearToLogScale( float min, float max, float value ) -//! @brief Converts linear amplitude (0-1.0) to dBV scale. +//! @brief Converts linear amplitude (0-1.0) to dBV scale. Handles zeroes as -inf. //! @param amp Linear amplitude, where 1.0 = 0dBV. //! @return Amplitude in dBV. -inf for 0 amplitude. -static inline float ampToDbv( float amp ) +static inline float safeAmpToDbv( float amp ) { return amp == 0.0f ? -INFINITY @@ -250,10 +250,10 @@ static inline float ampToDbv( float amp ) } -//! @brief Converts dBV-scale to linear amplitude with 0dBV = 1.0 +//! @brief Converts dBV-scale to linear amplitude with 0dBV = 1.0. Handles infinity as zero. //! @param dbv The dBV value to convert: all infinites are treated as -inf and result in 0 //! @return Linear amplitude -static inline float dbvToAmp( float dbv ) +static inline float safeDbvToAmp( float dbv ) { return isinff( dbv ) ? 0.0f @@ -261,6 +261,24 @@ static inline float dbvToAmp( float dbv ) } +//! @brief Converts linear amplitude (>0-1.0) to dBV scale. +//! @param amp Linear amplitude, where 1.0 = 0dBV. ** Must be larger than zero! ** +//! @return Amplitude in dBV. +static inline float ampToDbv( float amp ) +{ + return log10f( amp ) * 20.0f; +} + + +//! @brief Converts dBV-scale to linear amplitude with 0dBV = 1.0 +//! @param dbv The dBV value to convert. ** Must be a real number - not inf/nan! ** +//! @return Linear amplitude +static inline float dbvToAmp( float dbv ) +{ + return exp10f( dbv * 0.05f ); +} + + //! returns 1.0f if val >= 0.0f, -1.0 else static inline float sign( float val ) From 44a2455429bf7fe03a09b15f54ba03df9716a2a4 Mon Sep 17 00:00:00 2001 From: Vesa Date: Fri, 26 Dec 2014 19:33:48 +0200 Subject: [PATCH 2/2] Correct type in macros --- 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 4ab9b3471..d1a9dcc79 100644 --- a/include/lmms_math.h +++ b/include/lmms_math.h @@ -48,10 +48,10 @@ using namespace std; #define _isinff(x) isinf(x) #endif #ifndef exp10 -#define exp10(x) pow( 10, x ) +#define exp10(x) pow( 10.0, x ) #endif #ifndef exp10f -#define exp10f(x) powf( 10, x ) +#define exp10f(x) powf( 10.0f, x ) #endif #endif