Skip to content

Commit

Permalink
add lmms::numbers namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiefawn committed Nov 27, 2024
1 parent 0b0afb2 commit dff4d78
Show file tree
Hide file tree
Showing 26 changed files with 115 additions and 122 deletions.
23 changes: 11 additions & 12 deletions include/BasicFilters.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,20 @@ class LinkwitzRiley
inline void setCoeffs( float freq )
{
// wc
const double wc = D_2PI * freq;
const double wc = numbers::tau * freq;
const double wc2 = wc * wc;
const double wc3 = wc2 * wc;
m_wc4 = wc2 * wc2;

// k
const double k = wc / tan( D_PI * freq / m_sampleRate );
const double k = wc / tan(numbers::pi * freq / m_sampleRate);
const double k2 = k * k;
const double k3 = k2 * k;
m_k4 = k2 * k2;

// a
static const double sqrt2 = sqrt( 2.0 );
const double sq_tmp1 = sqrt2 * wc3 * k;
const double sq_tmp2 = sqrt2 * wc * k3;
const double sq_tmp1 = numbers::sqrt2 * wc3 * k;
const double sq_tmp2 = numbers::sqrt2 * wc * k3;

m_a = 1.0 / ( 4.0 * wc2 * k2 + 2.0 * sq_tmp1 + m_k4 + 2.0 * sq_tmp2 + m_wc4 );

Expand Down Expand Up @@ -718,7 +717,7 @@ class BasicFilters
{
_freq = std::clamp(_freq, 50.0f, 20000.0f);
const float sr = m_sampleRatio * 0.25f;
const float f = 1.0f / ( _freq * F_2PI );
const float f = 1.0f / (_freq * numbers::tau_v<float>);

m_rca = 1.0f - sr / ( f + sr );
m_rcb = 1.0f - m_rca;
Expand Down Expand Up @@ -751,8 +750,8 @@ class BasicFilters
const float fract = vowelf - vowel;

// interpolate between formant frequencies
const float f0 = 1.0f / ( linearInterpolate( _f[vowel+0][0], _f[vowel+1][0], fract ) * F_2PI );
const float f1 = 1.0f / ( linearInterpolate( _f[vowel+0][1], _f[vowel+1][1], fract ) * F_2PI );
const float f0 = 1.0f / (linearInterpolate(_f[vowel+0][0], _f[vowel+1][0], fract) * numbers::tau_v<float>);
const float f1 = 1.0f / (linearInterpolate(_f[vowel+0][1], _f[vowel+1][1], fract) * numbers::tau_v<float>);

// samplerate coeff: depends on oversampling
const float sr = m_type == FilterType::FastFormant ? m_sampleRatio : m_sampleRatio * 0.25f;
Expand All @@ -774,7 +773,7 @@ class BasicFilters
// (Empirical tunning)
m_p = ( 3.6f - 3.2f * f ) * f;
m_k = 2.0f * m_p - 1;
m_r = _q * powf( F_E, ( 1 - m_p ) * 1.386249f );
m_r = _q * powf(numbers::e_v<float>, (1 - m_p) * 1.386249f);

if( m_doubleFilter )
{
Expand All @@ -791,7 +790,7 @@ class BasicFilters

m_p = ( 3.6f - 3.2f * f ) * f;
m_k = 2.0f * m_p - 1.0f;
m_r = _q * 0.1f * powf( F_E, ( 1 - m_p ) * 1.386249f );
m_r = _q * 0.1f * powf(numbers::e_v<float>, (1 - m_p) * 1.386249f);

return;
}
Expand All @@ -801,7 +800,7 @@ class BasicFilters
m_type == FilterType::Highpass_SV ||
m_type == FilterType::Notch_SV )
{
const float f = sinf(std::max(minFreq(), _freq) * m_sampleRatio * F_PI);
const float f = sinf(std::max(minFreq(), _freq) * m_sampleRatio * numbers::pi_v<float>);
m_svf1 = std::min(f, 0.825f);
m_svf2 = std::min(f * 2.0f, 0.825f);
m_svq = std::max(0.0001f, 2.0f - (_q * 0.1995f));
Expand All @@ -810,7 +809,7 @@ class BasicFilters

// other filters
_freq = std::clamp(_freq, minFreq(), 20000.0f);
const float omega = F_2PI * _freq * m_sampleRatio;
const float omega = numbers::tau_v<float> * _freq * m_sampleRatio;
const float tsin = sinf( omega ) * 0.5f;
const float tcos = cosf( omega );

Expand Down
2 changes: 1 addition & 1 deletion include/DspEffectLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ namespace lmms::DspEffectLibrary

void nextSample( sample_t& inLeft, sample_t& inRight )
{
const float toRad = F_PI / 180;
const float toRad = numbers::pi_v<float> / 180;
const sample_t tmp = inLeft;
inLeft += inRight * sinf( m_wideCoeff * ( .5 * toRad ) );
inRight -= tmp * sinf( m_wideCoeff * ( .5 * toRad ) );
Expand Down
2 changes: 1 addition & 1 deletion include/Oscillator.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class LMMS_EXPORT Oscillator
// now follow the wave-shape-routines...
static inline sample_t sinSample( const float _sample )
{
return sinf( _sample * F_2PI );
return sinf(_sample * numbers::tau_v<float>);
}

static inline sample_t triangleSample( const float _sample )
Expand Down
9 changes: 3 additions & 6 deletions include/QuadratureLfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class QuadratureLfo
QuadratureLfo( int sampleRate ) :
m_frequency(0),
m_phase(0),
m_offset(D_PI / 2)
m_offset(numbers::pi_half)
{
setSampleRate(sampleRate);
}
Expand All @@ -64,7 +64,7 @@ class QuadratureLfo
inline void setSampleRate ( int samplerate )
{
m_samplerate = samplerate;
m_twoPiOverSr = F_2PI / samplerate;
m_twoPiOverSr = numbers::tau_v<float> / samplerate;
m_increment = m_frequency * m_twoPiOverSr;
}

Expand All @@ -81,10 +81,7 @@ class QuadratureLfo
*r = sinf( m_phase + m_offset );
m_phase += m_increment;

while (m_phase >= D_2PI)
{
m_phase -= D_2PI;
}
while (m_phase >= numbers::tau) { m_phase -= numbers::tau; }
}

private:
Expand Down
2 changes: 1 addition & 1 deletion include/interpolation.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ inline float cubicInterpolate( float v0, float v1, float v2, float v3, float x )

inline float cosinusInterpolate( float v0, float v1, float x )
{
const float f = ( 1.0f - cosf( x * F_PI ) ) * 0.5f;
const float f = (1.0f - cosf(x * numbers::pi_v<float>)) * 0.5f;
return f * (v1 - v0) + v0;
}

Expand Down
82 changes: 44 additions & 38 deletions include/lmms_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,48 +25,54 @@
#ifndef LMMS_CONSTANTS_H
#define LMMS_CONSTANTS_H

#include <numbers>
#include <concepts>
// #include <numbers>
// #include <concepts>

namespace lmms
namespace lmms::numbers
{

template<std::floating_point T> inline constexpr T Tau = T(std::numbers::pi_v<T> * 2.0);
template<std::floating_point T> inline constexpr T HalfPi = T(std::numbers::pi_v<T> / 2.0);
template<std::floating_point T> inline constexpr T PiSquared = T(std::numbers::pi_v<T> * std::numbers::pi_v<T>);

inline constexpr long double LD_2PI = Tau<long double>;
inline constexpr double D_2PI = Tau<double>;
inline constexpr float F_2PI = Tau<float>;
inline constexpr float F_PI_2 = HalfPi<float>;
inline constexpr float F_PI_SQR = PiSquared<float>;

[[deprecated("use std::numbers::pi_v<long double> instead")]]
inline constexpr long double LD_PI = std::numbers::pi_v<long double>;

[[deprecated("use std::numbers::pi_v<double> instead")]]
inline constexpr double D_PI = std::numbers::pi_v<double>;

[[deprecated("use std::numbers::pi_v<float> instead")]]
inline constexpr float F_PI = std::numbers::pi_v<float>;

[[deprecated("use std::numbers::e_v<long double> instead")]]
inline constexpr long double LD_E = std::numbers::e_v<long double>;
//TODO C++20: Use std::floating_point instead of typename
//TODO C++20: Use std::numbers::pi_v<T> instead of literal value
template<typename T>
inline constexpr T pi_v = T(3.14159265358979323846264338327950288419716939937510);
inline constexpr double pi = pi_v<double>;

//TODO C++20: Use std::floating_point instead of typename
template<typename T>
inline constexpr T tau_v = T(pi_v<T> * 2.0);
inline constexpr double tau = tau_v<double>;

//TODO C++20: Use std::floating_point instead of typename
template<typename T>
inline constexpr T pi_half_v = T(pi_v<T> / 2.0);
inline constexpr double pi_half = pi_half_v<double>;

//TODO C++20: Use std::floating_point instead of typename
template<typename T>
inline constexpr T pi_sqr_v = T(pi_v<T> * pi_v<T>);
inline constexpr double pi_sqr = pi_sqr_v<double>;

//TODO C++20: Use std::floating_point instead of typename
//TODO C++20: Use std::numbers::e_v<T> instead of literal value
template<typename T>
inline constexpr T e_v = T(2.71828182845904523536028747135266249775724709369995);
inline constexpr double e = e_v<double>;

//TODO C++20: Use std::floating_point instead of typename
template<typename T>
inline constexpr T inv_e_v = T(1.0 / e_v<T>);
inline constexpr double inv_e = e_v<double>;

//TODO C++20: Use std::floating_point instead of typename
//TODO C++20: Use std::numbers::sqrt2_v<T> instead of literal value
template<typename T>
inline constexpr T sqrt2_v = T(1.41421356237309504880168872420969807856967187537695);
inline constexpr double sqrt2 = sqrt2_v<double>;

}

[[deprecated("use std::numbers::e_v<double> instead")]]
inline constexpr double D_E = std::numbers::e_v<double>;

[[deprecated("use std::numbers::e_v<float> instead")]]
inline constexpr float F_E = std::numbers::e_v<float>;

[[deprecated("use std::numbers::sqrt2_v<long double> instead")]]
inline constexpr long double LD_SQRT_2 = std::numbers::sqrt2_v<long double>;

[[deprecated("use std::numbers::sqrt2_v<double> instead")]]
inline constexpr double D_SQRT_2 = std::numbers::sqrt2_v<double>;

[[deprecated("use std::numbers::sqrt2_v<float> instead")]]
inline constexpr float F_SQRT_2 = std::numbers::sqrt2_v<float>;
namespace lmms
{

constexpr float F_EPSILON = 1.0e-10f; // 10^-10

Expand Down
9 changes: 4 additions & 5 deletions include/lmms_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,27 +140,26 @@ inline float logToLinearScale(float min, float max, float value)
{
const float mmax = std::max(std::abs(min), std::abs(max));
const float val = value * ( max - min ) + min;
float result = signedPowf( val / mmax, F_E ) * mmax;
float result = signedPowf(val / mmax, numbers::e_v<float>) * mmax;
return std::isnan( result ) ? 0 : result;
}
float result = powf( value, F_E ) * ( max - min ) + min;
float result = powf(value, numbers::e_v<float>) * (max - min) + min;
return std::isnan( result ) ? 0 : result;
}


//! @brief Scales value from logarithmic to linear. Value should be in min-max range.
inline float linearToLogScale(float min, float max, float value)
{
static const float EXP = 1.0f / F_E;
const float valueLimited = std::clamp(value, min, max);
const float val = ( valueLimited - min ) / ( max - min );
if( min < 0 )
{
const float mmax = std::max(std::abs(min), std::abs(max));
float result = signedPowf( valueLimited / mmax, EXP ) * mmax;
float result = signedPowf(valueLimited / mmax, numbers::inv_e_v<float>) * mmax;
return std::isnan( result ) ? 0 : result;
}
float result = powf( val, EXP ) * ( max - min ) + min;
float result = powf(val, numbers::inv_e_v<float>) * (max - min) + min;
return std::isnan( result ) ? 0 : result;
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/Compressor/Compressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ void CompressorEffect::calcTiltCoeffs()
m_lgain = exp(g1 / amp) - 1;
m_hgain = exp(g2 / amp) - 1;

const float omega = 2 * F_PI * m_compressorControls.m_tiltFreqModel.value();
const float omega = numbers::tau_v<float> * m_compressorControls.m_tiltFreqModel.value();
const float n = 1 / (m_sampleRate * 3 + omega);
m_a0 = 2 * omega * n;
m_b1 = (m_sampleRate * 3 - omega) * n;
Expand Down
2 changes: 1 addition & 1 deletion plugins/Delay/Lfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace lmms
Lfo::Lfo( int samplerate )
{
m_samplerate = samplerate;
m_twoPiOverSr = F_2PI / samplerate;
m_twoPiOverSr = numbers::tau_v<float> / samplerate;
}


Expand Down
7 changes: 2 additions & 5 deletions plugins/Delay/Lfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ class Lfo
m_frequency = frequency;
m_increment = m_frequency * m_twoPiOverSr;

if( m_phase >= F_2PI )
{
m_phase -= F_2PI;
}
if (m_phase >= numbers::tau_v<float>) { m_phase -= numbers::tau_v<float>; }
}


Expand All @@ -62,7 +59,7 @@ class Lfo
inline void setSampleRate ( int samplerate )
{
m_samplerate = samplerate;
m_twoPiOverSr = F_2PI / samplerate;
m_twoPiOverSr = numbers::tau_v<float> / samplerate;
m_increment = m_frequency * m_twoPiOverSr;
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/Dispersion/Dispersion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Effect::ProcessStatus DispersionEffect::processImpl(SampleFrame* buf, const fpp_
const bool dc = m_dispersionControls.m_dcModel.value();

// All-pass coefficient calculation
const float w0 = (F_2PI / m_sampleRate) * freq;
const float w0 = (numbers::tau_v<float> / m_sampleRate) * freq;
const float a0 = 1 + (std::sin(w0) / (reso * 2.f));
float apCoeff1 = (1 - (a0 - 1)) / a0;
float apCoeff2 = (-2 * std::cos(w0)) / a0;
Expand Down
21 changes: 7 additions & 14 deletions plugins/Eq/EqCurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ bool EqHandle::mousePressed() const
float EqHandle::getPeakCurve( float x )
{
double freqZ = xPixelToFreq( EqHandle::x(), m_width );
const int SR = Engine::audioEngine()->outputSampleRate();
double w0 = 2 * LD_PI * freqZ / SR ;
double w0 = numbers::tau * freqZ / Engine::audioEngine()->outputSampleRate();
double c = cosf( w0 );
double s = sinf( w0 );
double Q = getResonance();
Expand Down Expand Up @@ -238,8 +237,7 @@ float EqHandle::getPeakCurve( float x )
float EqHandle::getHighShelfCurve( float x )
{
double freqZ = xPixelToFreq( EqHandle::x(), m_width );
const int SR = Engine::audioEngine()->outputSampleRate();
double w0 = 2 * LD_PI * freqZ / SR;
double w0 = numbers::tau * freqZ / Engine::audioEngine()->outputSampleRate();
double c = cosf( w0 );
double s = sinf( w0 );
double A = pow( 10, yPixelToGain( EqHandle::y(), m_heigth, m_pixelsPerUnitHeight ) * 0.025 );
Expand Down Expand Up @@ -274,8 +272,7 @@ float EqHandle::getHighShelfCurve( float x )
float EqHandle::getLowShelfCurve( float x )
{
double freqZ = xPixelToFreq( EqHandle::x(), m_width );
const int SR = Engine::audioEngine()->outputSampleRate();
double w0 = 2 * LD_PI * freqZ / SR ;
double w0 = numbers::tau * freqZ / Engine::audioEngine()->outputSampleRate();
double c = cosf( w0 );
double s = sinf( w0 );
double A = pow( 10, yPixelToGain( EqHandle::y(), m_heigth, m_pixelsPerUnitHeight ) / 40 );
Expand Down Expand Up @@ -310,8 +307,7 @@ float EqHandle::getLowShelfCurve( float x )
float EqHandle::getLowCutCurve( float x )
{
double freqZ = xPixelToFreq( EqHandle::x(), m_width );
const int SR = Engine::audioEngine()->outputSampleRate();
double w0 = 2 * LD_PI * freqZ / SR ;
double w0 = numbers::tau * freqZ / Engine::audioEngine()->outputSampleRate();
double c = cosf( w0 );
double s = sinf( w0 );
double resonance = getResonance();
Expand Down Expand Up @@ -353,8 +349,7 @@ float EqHandle::getLowCutCurve( float x )
float EqHandle::getHighCutCurve( float x )
{
double freqZ = xPixelToFreq( EqHandle::x(), m_width );
const int SR = Engine::audioEngine()->outputSampleRate();
double w0 = 2 * LD_PI * freqZ / SR ;
double w0 = numbers::tau * freqZ / Engine::audioEngine()->outputSampleRate();
double c = cosf( w0 );
double s = sinf( w0 );
double resonance = getResonance();
Expand Down Expand Up @@ -527,10 +522,8 @@ void EqHandle::setlp48()

double EqHandle::calculateGain(const double freq, const double a1, const double a2, const double b0, const double b1, const double b2 )
{
const int SR = Engine::audioEngine()->outputSampleRate();

const double w = 2 * LD_PI * freq / SR ;
const double PHI = pow( sin( w / 2 ), 2 ) * 4;
const double w = sin(numbers::pi * freq / Engine::audioEngine()->outputSampleRate());
const double PHI = w * w * 4;

double gain = 10 * log10( pow( b0 + b1 + b2 , 2 ) + ( b0 * b2 * PHI - ( b1 * ( b0 + b2 )
+ 4 * b0 * b2 ) ) * PHI ) - 10 * log10( pow( 1 + a1 + a2, 2 )
Expand Down
Loading

0 comments on commit dff4d78

Please sign in to comment.