Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
3.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. namespace dsp
  21. {
  22. double SpecialFunctions::besselI0 (double x) noexcept
  23. {
  24. auto ax = std::abs (x);
  25. if (ax < 3.75)
  26. {
  27. auto y = x / 3.75;
  28. y *= y;
  29. return 1.0 + y * (3.5156229 + y * (3.0899424 + y * (1.2067492
  30. + y * (0.2659732 + y * (0.360768e-1 + y * 0.45813e-2)))));
  31. }
  32. auto y = 3.75 / ax;
  33. return (std::exp (ax) / std::sqrt (ax))
  34. * (0.39894228 + y * (0.1328592e-1 + y * (0.225319e-2 + y * (-0.157565e-2 + y * (0.916281e-2
  35. + y * (-0.2057706e-1 + y * (0.2635537e-1 + y * (-0.1647633e-1 + y * 0.392377e-2))))))));
  36. }
  37. void SpecialFunctions::ellipticIntegralK (double k, double& K, double& Kp) noexcept
  38. {
  39. constexpr int M = 4;
  40. K = MathConstants<double>::halfPi;
  41. auto lastK = k;
  42. for (int i = 0; i < M; ++i)
  43. {
  44. lastK = std::pow (lastK / (1 + std::sqrt (1 - std::pow (lastK, 2.0))), 2.0);
  45. K *= 1 + lastK;
  46. }
  47. Kp = MathConstants<double>::halfPi;
  48. auto last = std::sqrt (1 - k * k);
  49. for (int i = 0; i < M; ++i)
  50. {
  51. last = std::pow (last / (1.0 + std::sqrt (1.0 - std::pow (last, 2.0))), 2.0);
  52. Kp *= 1 + last;
  53. }
  54. }
  55. Complex<double> SpecialFunctions::cde (Complex<double> u, double k) noexcept
  56. {
  57. constexpr int M = 4;
  58. double ke[M + 1];
  59. double* kei = ke;
  60. *kei = k;
  61. for (int i = 0; i < M; ++i)
  62. {
  63. auto next = std::pow (*kei / (1.0 + std::sqrt (1.0 - std::pow (*kei, 2.0))), 2.0);
  64. *++kei = next;
  65. }
  66. // NB: the spurious cast to double here is a workaround for a very odd link-time failure
  67. std::complex<double> last = std::cos (u * (double) MathConstants<double>::halfPi);
  68. for (int i = M - 1; i >= 0; --i)
  69. last = (1.0 + ke[i + 1]) / (1.0 / last + ke[i + 1] * last);
  70. return last;
  71. }
  72. Complex<double> SpecialFunctions::sne (Complex<double> u, double k) noexcept
  73. {
  74. constexpr int M = 4;
  75. double ke[M + 1];
  76. double* kei = ke;
  77. *kei = k;
  78. for (int i = 0; i < M; ++i)
  79. {
  80. auto next = std::pow (*kei / (1 + std::sqrt (1 - std::pow (*kei, 2.0))), 2.0);
  81. *++kei = next;
  82. }
  83. // NB: the spurious cast to double here is a workaround for a very odd link-time failure
  84. std::complex<double> last = std::sin (u * (double) MathConstants<double>::halfPi);
  85. for (int i = M - 1; i >= 0; --i)
  86. last = (1.0 + ke[i + 1]) / (1.0 / last + ke[i + 1] * last);
  87. return last;
  88. }
  89. Complex<double> SpecialFunctions::asne (Complex<double> w, double k) noexcept
  90. {
  91. constexpr int M = 4;
  92. double ke[M + 1];
  93. double* kei = ke;
  94. *kei = k;
  95. for (int i = 0; i < M; ++i)
  96. {
  97. auto next = std::pow (*kei / (1.0 + std::sqrt (1.0 - std::pow (*kei, 2.0))), 2.0);
  98. *++kei = next;
  99. }
  100. std::complex<double> last = w;
  101. for (int i = 1; i <= M; ++i)
  102. last = 2.0 * last / ((1.0 + ke[i]) * (1.0 + std::sqrt (1.0 - std::pow (ke[i - 1] * last, 2.0))));
  103. return 2.0 / MathConstants<double>::pi * std::asin (last);
  104. }
  105. } // namespace dsp
  106. } // namespace juce