Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize dBFS/amplitude conversion functions #7535

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
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
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)
LostRobotMusic marked this conversation as resolved.
Show resolved Hide resolved
{
return expf(2.302585092994046f * x);
LostRobotMusic marked this conversation as resolved.
Show resolved Hide resolved
}

static inline float fastLog10f(float x)
{
return logf(x) * 0.4342944819032518f;
LostRobotMusic marked this conversation as resolved.
Show resolved Hide resolved
}

//! @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)
{
LostRobotMusic marked this conversation as resolved.
Show resolved Hide 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
Loading