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.

98 lines
1.7KB

  1. #pragma once
  2. #include <math.hpp>
  3. #include <simd/functions.hpp>
  4. namespace rack {
  5. /** Digital signal processing routines
  6. */
  7. namespace dsp {
  8. // Constants
  9. static const float FREQ_C4 = 261.6256f;
  10. static const float FREQ_A4 = 440.0000f;
  11. // Mathematical functions
  12. /** The normalized sinc function
  13. See https://en.wikipedia.org/wiki/Sinc_function
  14. */
  15. inline float sinc(float x) {
  16. if (x == 0.f)
  17. return 1.f;
  18. x *= M_PI;
  19. return std::sin(x) / x;
  20. }
  21. template <typename T>
  22. T sinc(T x) {
  23. T zeromask = (x == 0.f);
  24. x *= M_PI;
  25. x = simd::sin(x) / x;
  26. return simd::ifelse(zeromask, 1.f, x);
  27. }
  28. // Conversion functions
  29. template <typename T>
  30. T amplitudeToDb(T amp) {
  31. return simd::log10(amp) * 20;
  32. }
  33. template <typename T>
  34. T dbToAmplitude(T db) {
  35. return std::pow(10, db / 20);
  36. }
  37. // Functions for parameter scaling
  38. template <typename T>
  39. T quadraticBipolar(T x) {
  40. return simd::sgn(x) * (x*x);
  41. }
  42. template <typename T>
  43. T cubic(T x) {
  44. return x*x*x;
  45. }
  46. template <typename T>
  47. T quarticBipolar(T x) {
  48. return simd::sgn(x) * (x*x*x*x);
  49. }
  50. template <typename T>
  51. T quintic(T x) {
  52. // optimal with -fassociative-math
  53. return x*x*x*x*x;
  54. }
  55. template <typename T>
  56. T sqrtBipolar(T x) {
  57. return simd::sgn(x) * simd::sqrt(x);
  58. }
  59. /** This is pretty much a scaled sinh.
  60. Slow. Not recommended for parameter scaling.
  61. */
  62. template <typename T>
  63. T exponentialBipolar(T b, T x) {
  64. T a = b - 1.f / b;
  65. return (std::pow(b, x) - std::pow(b, -x)) / a;
  66. }
  67. /** Useful for storing arrays of samples in ring buffers and casting them to `float*` to be used by interleaved processors, like SampleRateConverter */
  68. template <size_t CHANNELS, typename T = float>
  69. struct Frame {
  70. T samples[CHANNELS];
  71. };
  72. } // namespace dsp
  73. } // namespace rack