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. static const float FREQ_SEMITONE = 1.0594630943592953f;
  12. // Mathematical functions
  13. /** The normalized sinc function
  14. See https://en.wikipedia.org/wiki/Sinc_function
  15. */
  16. inline float sinc(float x) {
  17. if (x == 0.f)
  18. return 1.f;
  19. x *= M_PI;
  20. return std::sin(x) / x;
  21. }
  22. template <typename T>
  23. T sinc(T x) {
  24. T zeromask = (x == 0.f);
  25. x *= M_PI;
  26. x = simd::sin(x) / x;
  27. return simd::ifelse(zeromask, 1.f, x);
  28. }
  29. // Conversion functions
  30. template <typename T>
  31. T amplitudeToDb(T amp) {
  32. return simd::log10(amp) * 20;
  33. }
  34. template <typename T>
  35. T dbToAmplitude(T db) {
  36. return std::pow(10, db / 20);
  37. }
  38. // Functions for parameter scaling
  39. template <typename T>
  40. T quadraticBipolar(T x) {
  41. return simd::sgn(x) * (x*x);
  42. }
  43. template <typename T>
  44. T cubic(T x) {
  45. return x*x*x;
  46. }
  47. template <typename T>
  48. T quarticBipolar(T x) {
  49. return simd::sgn(x) * (x*x*x*x);
  50. }
  51. template <typename T>
  52. T quintic(T x) {
  53. // optimal with -fassociative-math
  54. return x*x*x*x*x;
  55. }
  56. template <typename T>
  57. T sqrtBipolar(T x) {
  58. return simd::sgn(x) * simd::sqrt(x);
  59. }
  60. /** This is pretty much a scaled sinh.
  61. Slow. Not recommended for parameter scaling.
  62. */
  63. template <typename T>
  64. T exponentialBipolar(T b, T x) {
  65. return (simd::pow(b, x) - simd::pow(b, -x)) / (b - 1.f / b);
  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