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.

56 lines
955B

  1. #pragma once
  2. #include "util/math.hpp"
  3. namespace rack {
  4. /** The normalized sinc function. https://en.wikipedia.org/wiki/Sinc_function */
  5. inline float sinc(float x) {
  6. if (x == 0.f)
  7. return 1.f;
  8. x *= M_PI;
  9. return sinf(x) / x;
  10. }
  11. inline float quadraticBipolar(float x) {
  12. float x2 = x*x;
  13. return (x >= 0.f) ? x2 : -x2;
  14. }
  15. inline float cubic(float x) {
  16. return x*x*x;
  17. }
  18. inline float quarticBipolar(float x) {
  19. float y = x*x*x*x;
  20. return (x >= 0.f) ? y : -y;
  21. }
  22. inline float quintic(float x) {
  23. // optimal with --fast-math
  24. return x*x*x*x*x;
  25. }
  26. inline float sqrtBipolar(float x) {
  27. return (x >= 0.f) ? sqrtf(x) : -sqrtf(-x);
  28. }
  29. /** This is pretty much a scaled sinh */
  30. inline float exponentialBipolar(float b, float x) {
  31. const float a = b - 1.f / b;
  32. return (powf(b, x) - powf(b, -x)) / a;
  33. }
  34. inline float gainToDb(float gain) {
  35. return log10f(gain) * 20.f;
  36. }
  37. inline float dbToGain(float db) {
  38. return powf(10.f, db / 20.f);
  39. }
  40. } // namespace rack