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.

74 lines
1.2KB

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