Use templates for common geometric constants (#7558)

* add templates for common geometric constants

* oops missed one

* LD_2PI -> LD_PI

i re-added the wrong constant ffs

* CamelCase names and also verify compilation without -DLMMS_MINIMAL

* C++20 stuff

Updated to account for `<numbers>` and C++20:
- Marked all `lmms_constants.h` constants with an exact equivalent in `<numbers>` as deprecated
- Removed all `lmms_constants.h` constants where no variant is currently in use
- Using `inline constexpr`
- Using `std::floating_point` concept instead of `typename`

* add lmms::numbers namespace

* Remove panning_constants.h

Moves the four constants in panning_constants.h into panning.h, then
removes panning.h.

* Use std::exp(n) instead of powf(numbers::e, n)

* Use C++ std math functions

Co-authored-by: Dalton Messmer <messmer.dalton@gmail.com>

* Use overloaded std math functions

An attempt to fix compiler warnings on some platforms

* Remove uses of __USE_XOPEN

And also update two functions I missed from the previous commit

* Missed a few

* Fix ANOTHER std math function use

Of course there's another one

---------

Co-authored-by: Dalton Messmer <messmer.dalton@gmail.com>
This commit is contained in:
Fawn
2025-02-03 09:52:13 -07:00
committed by GitHub
parent fc125bc7ba
commit 9aa937d391
33 changed files with 127 additions and 189 deletions

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -50,10 +50,7 @@ public:
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>; }
}
@@ -62,7 +59,7 @@ public:
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;
}

View File

@@ -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;

View File

@@ -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();
@@ -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 );
@@ -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 );
@@ -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();
@@ -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();
@@ -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 = std::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 )

View File

@@ -185,7 +185,7 @@ public :
{
// calc intermediate
float w0 = F_2PI * m_freq / m_sampleRate;
float w0 = numbers::tau_v<float> * m_freq / m_sampleRate;
float c = cosf( w0 );
float s = sinf( w0 );
float alpha = s / ( 2 * m_res );
@@ -228,7 +228,7 @@ public :
{
// calc intermediate
float w0 = F_2PI * m_freq / m_sampleRate;
float w0 = numbers::tau_v<float> * m_freq / m_sampleRate;
float c = cosf( w0 );
float s = sinf( w0 );
float alpha = s / ( 2 * m_res );
@@ -269,7 +269,7 @@ public:
void calcCoefficents() override
{
// calc intermediate
float w0 = F_2PI * m_freq / m_sampleRate;
float w0 = numbers::tau_v<float> * m_freq / m_sampleRate;
float c = cosf( w0 );
float s = sinf( w0 );
float A = pow( 10, m_gain * 0.025);
@@ -332,7 +332,7 @@ public :
{
// calc intermediate
float w0 = F_2PI * m_freq / m_sampleRate;
float w0 = numbers::tau_v<float> * m_freq / m_sampleRate;
float c = cosf( w0 );
float s = sinf( w0 );
float A = pow( 10, m_gain * 0.025);
@@ -369,7 +369,7 @@ public :
{
// calc intermediate
float w0 = F_2PI * m_freq / m_sampleRate;
float w0 = numbers::tau_v<float> * m_freq / m_sampleRate;
float c = cosf( w0 );
float s = sinf( w0 );
float A = pow( 10, m_gain * 0.025 );

View File

@@ -56,9 +56,9 @@ EqAnalyser::EqAnalyser() :
for (auto i = std::size_t{0}; i < FFT_BUFFER_SIZE; i++)
{
m_fftWindow[i] = (a0 - a1 * cos(2 * F_PI * i / ((float)FFT_BUFFER_SIZE - 1.0))
+ a2 * cos(4 * F_PI * i / ((float)FFT_BUFFER_SIZE - 1.0))
- a3 * cos(6 * F_PI * i / ((float)FFT_BUFFER_SIZE - 1.0)));
m_fftWindow[i] = (a0 - a1 * std::cos(2 * numbers::pi_v<float> * i / ((float)FFT_BUFFER_SIZE - 1.0))
+ a2 * std::cos(4 * numbers::pi_v<float> * i / ((float)FFT_BUFFER_SIZE - 1.0))
- a3 * std::cos(6 * numbers::pi_v<float> * i / ((float)FFT_BUFFER_SIZE - 1.0)));
}
clear();
}

View File

@@ -94,7 +94,7 @@ Effect::ProcessStatus FlangerEffect::processImpl(SampleFrame* buf, const fpp_t f
float amplitude = m_flangerControls.m_lfoAmountModel.value() * Engine::audioEngine()->outputSampleRate();
bool invertFeedback = m_flangerControls.m_invertFeedbackModel.value();
m_lfo->setFrequency( 1.0/m_flangerControls.m_lfoFrequencyModel.value() );
m_lfo->setOffset( m_flangerControls.m_lfoPhaseModel.value() / 180 * D_PI );
m_lfo->setOffset(m_flangerControls.m_lfoPhaseModel.value() / 180 * numbers::pi);
m_lDelay->setFeedback( m_flangerControls.m_feedbackModel.value() );
m_rDelay->setFeedback( m_flangerControls.m_feedbackModel.value() );
auto dryS = std::array<sample_t, 2>{};

View File

@@ -271,7 +271,7 @@ void GranularPitchShifterEffect::changeSampleRate()
m_grainCount = 0;
m_grains.reserve(8);// arbitrary
m_dcCoeff = std::exp(-2.0 * F_PI * DcRemovalHz / m_sampleRate);
m_dcCoeff = std::exp(-numbers::tau_v<float> * DcRemovalHz / m_sampleRate);
const double pitch = m_granularpitchshifterControls.m_pitchModel.value() * (1. / 12.);
const double pitchSpread = m_granularpitchshifterControls.m_pitchSpreadModel.value() * (1. / 24.);

View File

@@ -118,10 +118,10 @@ private:
void setCoefs(float sampleRate, float cutoff)
{
const float g = std::tan(F_PI * cutoff / sampleRate);
const float ginv = g / (1.f + g * (g + F_SQRT_2));
const float g = std::tan(numbers::pi_v<float> * cutoff / sampleRate);
const float ginv = g / (1.f + g * (g + numbers::sqrt2_v<float>));
m_g1 = ginv;
m_g2 = 2.f * (g + F_SQRT_2) * ginv;
m_g2 = 2.f * (g + numbers::sqrt2_v<float>) * ginv;
m_g3 = g * ginv;
m_g4 = 2.f * ginv;
}

View File

@@ -54,7 +54,7 @@ private:
inline void setFilterFreq( float fc, StereoOnePole & f )
{
const float b1 = expf( -2.0f * F_PI * fc );
const float b1 = std::exp(-numbers::tau_v<float> * fc);
f.setCoeffs( 1.0f - b1, b1 );
}

View File

@@ -413,7 +413,7 @@ struct sin_wave
static inline float process(float x)
{
x = positiveFraction(x);
return sinf(x * F_2PI);
return std::sin(x * numbers::tau_v<float>);
}
};
static freefunc1<float,sin_wave,true> sin_wave_func;
@@ -535,7 +535,7 @@ ExprFront::ExprFront(const char * expr, int last_func_samples)
m_data->m_expression_string = expr;
m_data->m_symbol_table.add_pi();
m_data->m_symbol_table.add_constant("e", F_E);
m_data->m_symbol_table.add_constant("e", numbers::e_v<float>);
m_data->m_symbol_table.add_constant("seed", SimpleRandom::generator() & max_float_integer_mask);

View File

@@ -252,14 +252,14 @@ void Xpressive::smooth(float smoothness,const graphModel * in,graphModel * out)
const int guass_size = (int)(smoothness * 5) | 1;
const int guass_center = guass_size/2;
const float delta = smoothness;
const float a= 1.0f / (sqrtf(2.0f * F_PI) * delta);
const float a = 1.0f / (std::sqrt(numbers::tau_v<float>) * delta);
auto const guassian = new float[guass_size];
float sum = 0.0f;
float temp = 0.0f;
for (int i = 0; i < guass_size; i++)
{
temp = (i - guass_center) / delta;
sum += guassian[i] = a * powf(F_E, -0.5f * temp * temp);
sum += guassian[i] = a * std::exp(-0.5f * temp * temp);
}
for (int i = 0; i < guass_size; i++)
{