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.

87 lines
2.6KB

  1. #include <assert.h>
  2. #include <memory>
  3. #include "AudioMath.h"
  4. #include "LookupTable.h"
  5. #include "ObjectCache.h"
  6. const double AudioMath::Pi = 3.1415926535897932384626433832795028841971;
  7. const double AudioMath::Pi_2 = 1.5707963267948966192313216916397514420986;
  8. const double AudioMath::Ln2 = 0.693147180559945309417;
  9. const double AudioMath::Ln10 = 2.30258509299404568402;
  10. const double AudioMath::E = 2.71828182845904523536;
  11. std::function<double(double)> AudioMath::makeFunc_Sin()
  12. {
  13. return [](double x) {
  14. return std::sin(x * 2 * Pi);
  15. };
  16. }
  17. std::function<double(double)> AudioMath::makeFunc_Exp(double xMin, double xMax, double yMin, double yMax)
  18. {
  19. const double a = (std::log(yMax) - log(yMin)) / (xMax - xMin);
  20. const double b = log(yMin) - a * xMin;
  21. return [a, b](double d) {
  22. return std::exp(a * d + b);
  23. };
  24. }
  25. std::function<double(double)> AudioMath::makeFunc_AudioTaper(double dbAtten)
  26. {
  27. assert(dbAtten < 0);
  28. const double gainAtQuarter = gainFromDb(dbAtten);
  29. std::function<double(double)> linearFunc;
  30. std::function<double(double)> expFunc;
  31. {
  32. // for linear part at bottom
  33. const double x0 = 0;
  34. const double x1 = .25;
  35. const double y0 = 0;
  36. const double y1 = gainAtQuarter;
  37. const double a = (y1 - y0) / (x1 - x0);
  38. const double b = y0 - a * x0;
  39. linearFunc = [a, b](double d) {
  40. return a * d + b;
  41. };
  42. }
  43. {
  44. // for exp part on top
  45. const double xMin = .25;
  46. const double yMin = gainAtQuarter;
  47. const double xMax = 1;
  48. const double yMax = 1;
  49. expFunc = makeFunc_Exp(xMin, xMax, yMin, yMax);
  50. }
  51. return [linearFunc, expFunc](double d) {
  52. return (d <= .25) ? linearFunc(d) : expFunc(d);
  53. };
  54. }
  55. AudioMath::ScaleFun<float> AudioMath::makeBipolarAudioScaler(float y0, float y1)
  56. {
  57. // Use a cached singleton for the lookup table - don't need to have unique copies
  58. std::shared_ptr<LookupTableParams<float>> lookup = ObjectCache<float>::getBipolarAudioTaper();
  59. const float x0 = -5;
  60. const float x1 = 5;
  61. const float a = (y1 - y0) / (x1 - x0);
  62. const float b = y0 - a * x0;
  63. // Notice how lambda captures the smart pointer. Now
  64. // lambda owns a reference to it.
  65. return [a, b, lookup](float cv, float knob, float trim) {
  66. auto mappedTrim = LookupTable<float>::lookup(*lookup, trim);
  67. float x = cv * mappedTrim + knob;
  68. x = std::max<float>(-5.0f, x);
  69. x = std::min(5.0f, x);
  70. return a * x + b;
  71. };
  72. }
  73. // declare some test variables here
  74. int _numLookupParams = 0;
  75. int _numBiquads = 0;