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.

70 lines
1.2KB

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