Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Optimize dBFS <-> amplitude functions
  • Loading branch information
LostRobotMusic authored Oct 8, 2024
commit 59217c562f1e1b74cfbfe483c4947a91cd3b4685
46 changes: 27 additions & 19 deletions include/lmms_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
LostRobotMusic marked this conversation as resolved.
Outdated
{
return expf(2.302585092994046f * x);
Comment thread
LostRobotMusic marked this conversation as resolved.
Outdated
}

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.
Expand All @@ -174,7 +199,7 @@ inline float safeAmpToDbfs(float amp)
{
Comment thread
LostRobotMusic marked this conversation as resolved.
return amp == 0.0f
? -INFINITY
: log10f( amp ) * 20.0f;
: ampToDbfs(amp);
}


Expand All @@ -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<class T, class F>
Expand Down