-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Use templates for common geometric constants #7558
Open
rubiefawn
wants to merge
7
commits into
LMMS:master
Choose a base branch
from
rubiefawn:template-literals
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+121
−154
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c8592ba
add templates for common geometric constants
rubiefawn fdc56b4
oops missed one
rubiefawn ae0dfc8
LD_2PI -> LD_PI
rubiefawn 447a157
CamelCase names and also verify compilation without -DLMMS_MINIMAL
rubiefawn 26e37f7
C++20 stuff
rubiefawn 3a61877
add lmms::numbers namespace
rubiefawn 0794170
Remove panning_constants.h
rubiefawn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 ); | ||||||
|
||||||
|
@@ -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; | ||||||
|
@@ -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; | ||||||
|
@@ -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 ) | ||||||
{ | ||||||
|
@@ -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); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Here too. |
||||||
|
||||||
return; | ||||||
} | ||||||
|
@@ -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)); | ||||||
|
@@ -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 ); | ||||||
|
||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,36 +25,54 @@ | |
#ifndef LMMS_CONSTANTS_H | ||
#define LMMS_CONSTANTS_H | ||
|
||
namespace lmms | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI, The C++ 20 PR was merged, so might as well make the switch now. |
||
// #include <numbers> | ||
// #include <concepts> | ||
|
||
namespace lmms::numbers | ||
{ | ||
|
||
//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>; | ||
|
||
} | ||
|
||
constexpr long double LD_PI = 3.14159265358979323846264338327950288419716939937510; | ||
constexpr long double LD_2PI = LD_PI * 2.0; | ||
constexpr long double LD_PI_2 = LD_PI * 0.5; | ||
constexpr long double LD_PI_R = 1.0 / LD_PI; | ||
constexpr long double LD_PI_SQR = LD_PI * LD_PI; | ||
constexpr long double LD_E = 2.71828182845904523536028747135266249775724709369995; | ||
constexpr long double LD_E_R = 1.0 / LD_E; | ||
constexpr long double LD_SQRT_2 = 1.41421356237309504880168872420969807856967187537695; | ||
|
||
constexpr double D_PI = (double) LD_PI; | ||
constexpr double D_2PI = (double) LD_2PI; | ||
constexpr double D_PI_2 = (double) LD_PI_2; | ||
constexpr double D_PI_R = (double) LD_PI_R; | ||
constexpr double D_PI_SQR = (double) LD_PI_SQR; | ||
constexpr double D_E = (double) LD_E; | ||
constexpr double D_E_R = (double) LD_E_R; | ||
constexpr double D_SQRT_2 = (double) LD_SQRT_2; | ||
|
||
constexpr float F_PI = (float) LD_PI; | ||
constexpr float F_2PI = (float) LD_2PI; | ||
constexpr float F_PI_2 = (float) LD_PI_2; | ||
constexpr float F_PI_R = (float) LD_PI_R; | ||
constexpr float F_PI_SQR = (float) LD_PI_SQR; | ||
constexpr float F_E = (float) LD_E; | ||
constexpr float F_E_R = (float) LD_E_R; | ||
constexpr float F_SQRT_2 = (float) LD_SQRT_2; | ||
namespace lmms | ||
{ | ||
|
||
constexpr float F_EPSILON = 1.0e-10f; // 10^-10 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One simplification.