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.

61 lines
1.1KB

  1. #pragma once
  2. #include "math.hpp"
  3. namespace rack {
  4. namespace dsp {
  5. static const float FREQ_C4 = 261.6256f;
  6. static const float FREQ_A4 = 440.0000f;
  7. /** The normalized sinc function. https://en.wikipedia.org/wiki/Sinc_function */
  8. inline float sinc(float x) {
  9. if (x == 0.f)
  10. return 1.f;
  11. x *= M_PI;
  12. return std::sin(x) / x;
  13. }
  14. inline float quadraticBipolar(float x) {
  15. float x2 = x*x;
  16. return (x >= 0.f) ? x2 : -x2;
  17. }
  18. inline float cubic(float x) {
  19. return x*x*x;
  20. }
  21. inline float quarticBipolar(float x) {
  22. float y = x*x*x*x;
  23. return (x >= 0.f) ? y : -y;
  24. }
  25. inline float quintic(float x) {
  26. // optimal with --fast-math
  27. return x*x*x*x*x;
  28. }
  29. inline float sqrtBipolar(float x) {
  30. return (x >= 0.f) ? std::sqrt(x) : -std::sqrt(-x);
  31. }
  32. /** This is pretty much a scaled sinh */
  33. inline float exponentialBipolar(float b, float x) {
  34. const float a = b - 1.f / b;
  35. return (std::pow(b, x) - std::pow(b, -x)) / a;
  36. }
  37. inline float amplitudeToDb(float amp) {
  38. return std::log10(amp) * 20.f;
  39. }
  40. inline float dbToAmplitude(float db) {
  41. return std::pow(10.f, db / 20.f);
  42. }
  43. } // namespace dsp
  44. } // namespace rack