diff options
author | Chris Robinson <[email protected]> | 2022-01-27 01:38:39 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2022-01-27 01:38:39 -0800 |
commit | 1bbea9cd3060ef65a2623f156b4f12ebf62c52fe (patch) | |
tree | 005d29f38edcd1b985e52fc49b4e58fb0ba1721e | |
parent | 423888b8162f9d78ec4f1b00b8368f3ba22eedd6 (diff) |
Start and use a standard-like numbers header
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | alc/effects/convolution.cpp | 6 | ||||
-rw-r--r-- | alc/effects/reverb.cpp | 17 | ||||
-rw-r--r-- | common/alcomplex.cpp | 4 | ||||
-rw-r--r-- | common/alnumbers.h | 32 | ||||
-rw-r--r-- | core/mixer.cpp | 10 |
6 files changed, 52 insertions, 18 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index ddc37e53..9e0662b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -626,6 +626,7 @@ set(COMMON_OBJS common/alfstream.h common/almalloc.cpp common/almalloc.h + common/alnumbers.h common/alnumeric.h common/aloptional.h common/alspan.h diff --git a/alc/effects/convolution.cpp b/alc/effects/convolution.cpp index dbbca143..10e3dd9d 100644 --- a/alc/effects/convolution.cpp +++ b/alc/effects/convolution.cpp @@ -20,6 +20,7 @@ #include "albyte.h" #include "alcomplex.h" #include "almalloc.h" +#include "alnumbers.h" #include "alnumeric.h" #include "alspan.h" #include "base.h" @@ -464,9 +465,10 @@ void ConvolutionState::update(const ContextBase *context, const EffectSlot *slot { auto ScaleAzimuthFront = [](float azimuth, float scale) -> float { + constexpr float half_pi{al::numbers::pi_v<float>*0.5f}; const float abs_azi{std::fabs(azimuth)}; - if(!(abs_azi >= al::MathDefs<float>::Pi()*0.5f)) - return std::copysign(minf(abs_azi*scale, al::MathDefs<float>::Pi()*0.5f), azimuth); + if(!(abs_azi >= half_pi)) + return std::copysign(minf(abs_azi*scale, half_pi), azimuth); return azimuth; }; diff --git a/alc/effects/reverb.cpp b/alc/effects/reverb.cpp index 4fbfefd1..379cc1fb 100644 --- a/alc/effects/reverb.cpp +++ b/alc/effects/reverb.cpp @@ -30,6 +30,7 @@ #include "alc/effects/base.h" #include "almalloc.h" +#include "alnumbers.h" #include "alnumeric.h" #include "alspan.h" #include "core/ambidefs.h" @@ -745,7 +746,7 @@ inline float CalcDensityGain(const float a) inline void CalcMatrixCoeffs(const float diffusion, float *x, float *y) { /* The matrix is of order 4, so n is sqrt(4 - 1). */ - constexpr float n{1.73205080756887719318f/*std::sqrt(3.0f)*/}; + constexpr float n{al::numbers::sqrt3_v<float>}; const float t{diffusion * std::atan(n)}; /* Calculate the first mixing matrix coefficient. */ @@ -947,8 +948,6 @@ void ReverbState::updateDelayLine(const float earlyDelay, const float lateDelay, */ alu::Matrix GetTransformFromVector(const float *vec) { - constexpr float sqrt3{1.73205080756887719318f}; - /* Normalize the panning vector according to the N3D scale, which has an * extra sqrt(3) term on the directional components. Converting from OpenAL * to B-Format also requires negating X (ACN 1) and Z (ACN 3). Note however @@ -960,9 +959,9 @@ alu::Matrix GetTransformFromVector(const float *vec) float mag{std::sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2])}; if(mag > 1.0f) { - norm[0] = vec[0] / mag * -sqrt3; - norm[1] = vec[1] / mag * sqrt3; - norm[2] = vec[2] / mag * sqrt3; + norm[0] = vec[0] / mag * -al::numbers::sqrt3_v<float>; + norm[1] = vec[1] / mag * al::numbers::sqrt3_v<float>; + norm[2] = vec[2] / mag * al::numbers::sqrt3_v<float>; mag = 1.0f; } else @@ -971,9 +970,9 @@ alu::Matrix GetTransformFromVector(const float *vec) * term. There's no need to renormalize the magnitude since it would * just be reapplied in the matrix. */ - norm[0] = vec[0] * -sqrt3; - norm[1] = vec[1] * sqrt3; - norm[2] = vec[2] * sqrt3; + norm[0] = vec[0] * -al::numbers::sqrt3_v<float>; + norm[1] = vec[1] * al::numbers::sqrt3_v<float>; + norm[2] = vec[2] * al::numbers::sqrt3_v<float>; } return alu::Matrix{ diff --git a/common/alcomplex.cpp b/common/alcomplex.cpp index 5cb35f38..6d2b18c1 100644 --- a/common/alcomplex.cpp +++ b/common/alcomplex.cpp @@ -10,8 +10,8 @@ #include <utility> #include "albit.h" +#include "alnumbers.h" #include "alnumeric.h" -#include "math_defs.h" #include "opthelpers.h" @@ -123,7 +123,7 @@ void complex_fft(const al::span<std::complex<double>> buffer, const double sign) size_t step2{1u}; for(size_t i{0};i < log2_size;++i) { - const double arg{al::MathDefs<double>::Pi() / static_cast<double>(step2)}; + const double arg{al::numbers::pi / static_cast<double>(step2)}; const std::complex<double> w{std::cos(arg), std::sin(arg)*sign}; std::complex<double> u{1.0, 0.0}; diff --git a/common/alnumbers.h b/common/alnumbers.h new file mode 100644 index 00000000..98994b44 --- /dev/null +++ b/common/alnumbers.h @@ -0,0 +1,32 @@ +#ifndef COMMON_ALNUMBERS_H +#define COMMON_ALNUMBERS_H + +#include <utility> + +namespace al { + +namespace numbers { + +namespace detail_ { + template<typename T> + using as_fp = std::enable_if_t<std::is_floating_point<T>::value, T>; +} // detail_ + +template<typename T> +static constexpr auto pi_v = detail_::as_fp<T>(3.141592653589793238462643383279502884L); + +template<typename T> +static constexpr auto inv_pi_v = detail_::as_fp<T>(0.318309886183790671537767526745028724L); + +template<typename T> +static constexpr auto sqrt3_v = detail_::as_fp<T>(1.732050807568877293527446341505872367L); + +static constexpr auto pi = pi_v<double>; +static constexpr auto inv_pi = inv_pi_v<double>; +static constexpr auto sqrt3 = sqrt3_v<double>; + +} // namespace numbers + +} // namespace al + +#endif /* COMMON_ALNUMBERS_H */ diff --git a/core/mixer.cpp b/core/mixer.cpp index 71e48fe3..4618406b 100644 --- a/core/mixer.cpp +++ b/core/mixer.cpp @@ -5,9 +5,9 @@ #include <cmath> +#include "alnumbers.h" #include "devformat.h" #include "device.h" -#include "math_defs.h" #include "mixer/defs.h" struct CTag; @@ -24,9 +24,9 @@ std::array<float,MaxAmbiChannels> CalcAmbiCoeffs(const float y, const float z, c /* Zeroth-order */ coeffs[0] = 1.0f; /* ACN 0 = 1 */ /* First-order */ - coeffs[1] = 1.732050808f * y; /* ACN 1 = sqrt(3) * Y */ - coeffs[2] = 1.732050808f * z; /* ACN 2 = sqrt(3) * Z */ - coeffs[3] = 1.732050808f * x; /* ACN 3 = sqrt(3) * X */ + coeffs[1] = al::numbers::sqrt3_v<float> * y; /* ACN 1 = sqrt(3) * Y */ + coeffs[2] = al::numbers::sqrt3_v<float> * z; /* ACN 2 = sqrt(3) * Z */ + coeffs[3] = al::numbers::sqrt3_v<float> * x; /* ACN 3 = sqrt(3) * X */ /* Second-order */ const float xx{x*x}, yy{y*y}, zz{z*z}, xy{x*y}, yz{y*z}, xz{x*z}; coeffs[4] = 3.872983346f * xy; /* ACN 4 = sqrt(15) * X * Y */ @@ -81,7 +81,7 @@ std::array<float,MaxAmbiChannels> CalcAmbiCoeffs(const float y, const float z, c */ const float ca{std::cos(spread * 0.5f)}; /* Increase the source volume by up to +3dB for a full spread. */ - const float scale{std::sqrt(1.0f + spread/al::MathDefs<float>::Tau())}; + const float scale{std::sqrt(1.0f + al::numbers::inv_pi_v<float>/2.0f*spread)}; const float ZH0_norm{scale}; const float ZH1_norm{scale * 0.5f * (ca+1.f)}; |