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.

172 lines
5.0KB

  1. #pragma once
  2. #include <cmath>
  3. #include <functional>
  4. #include <algorithm>
  5. class AudioMath
  6. {
  7. public:
  8. AudioMath() = delete; // we are only static
  9. static const double Pi;
  10. static const double Pi_2; // Pi / 2
  11. static const double Ln2;
  12. static const double Ln10;
  13. static const double E;
  14. static bool closeTo(double x, double y, double tolerance)
  15. {
  16. const bool ret = std::abs(x - y) < tolerance;
  17. return ret;
  18. }
  19. static double db(double g)
  20. {
  21. return 20 * log(g) / Ln10;
  22. }
  23. static double cents(double f1, double f2)
  24. {
  25. return 1200 * std::log2(f1 / f2);
  26. }
  27. static double acents(double f1, double f2)
  28. {
  29. return std::abs(cents(f1, f2));
  30. }
  31. static double gainFromDb(double db)
  32. {
  33. return std::exp(Ln10 * db / 20.0);
  34. }
  35. /**
  36. * Returns a function that generates one period of sin for x = {0..1}.
  37. * Range (output) is -1 to 1.
  38. *
  39. * All makeFunc_xxx functions return functions that are not optimized.
  40. * They do not use lookup tables.
  41. */
  42. static std::function<double(double)> makeFunc_Sin();
  43. /*
  44. * Returns a function that generates an exponential defined by two points
  45. * At input = xMin, output will be yMin.
  46. * At input = xMax, output will be yMax.
  47. */
  48. static std::function<double(double)> makeFunc_Exp(double xMin, double xMax, double yMin, double yMax);
  49. /**
  50. * Returns a function for an "audio taper" attenuator gain.
  51. * function is pure exponential for x > .25, linear for x < .25
  52. * At input 1, output is 1
  53. * At input .25, output is the gain corresponding to adAtten
  54. * At input 0, the output is zero.
  55. */
  56. static std::function<double(double)> makeFunc_AudioTaper(double dbAtten);
  57. /**
  58. * ScaleFun is a function the combines CV, knob, and trim into a voltage.
  59. * Typically a ScaleFun is like an "attenuverter", where the trim input
  60. * is the attenuverter.
  61. */
  62. template <typename T>
  63. using ScaleFun = std::function<T(T cv, T knob, T trim)>;
  64. /**
  65. * Create a ScaleFun with the following properties:
  66. * 1) The values are combined with the typical formula: x = cv * trim + knob;
  67. * 2) x is clipped between -5 and 5
  68. * 3) range is then interpolated between y0, and y1.
  69. *
  70. * This particular function is used when knobs are -5..5,
  71. * and CV range is -5..5.
  72. *
  73. *
  74. * Can easily be used to add, clip and scale just a knob an a CV by
  75. * passing 1 for the trim param.
  76. */
  77. template <typename T>
  78. static ScaleFun<T> makeLinearScaler(T y0, T y1)
  79. {
  80. const T x0 = -5;
  81. const T x1 = 5;
  82. const T a = (y1 - y0) / (x1 - x0);
  83. const T b = y0 - a * x0;
  84. return [a, b](T cv, T knob, T trim) {
  85. T x = cv * trim + knob;
  86. x = std::max<T>(-5.0f, x);
  87. x = std::min(5.0f, x);
  88. return a * x + b;
  89. };
  90. }
  91. /**
  92. * Generates a scale function for an audio taper attenuverter.
  93. * Details the same as makeLinearScaler except that the CV
  94. * scaling will be exponential for most values, becoming
  95. * linear near zero.
  96. *
  97. * Note that the cv and knob will have linear taper, only the
  98. * attenuverter is audio taper.
  99. *
  100. * Implemented with a cached lookup table -
  101. * only the final scaling is done at run time
  102. */
  103. static ScaleFun<float> makeScalerWithBipolarAudioTrim(float y0, float y1);
  104. /**
  105. * SimpleScaleFun is a function the combines CV, and knob, into a voltage.
  106. * Usually in a synth module that has knob and cv, but no trim.
  107. *
  108. * cv and knob are -5 to +5. They are summed, and the sum is limited to 5, -5.
  109. * Then the sum gets an audio taper
  110. */
  111. template <typename T>
  112. using SimpleScaleFun = std::function<T(T cv, T knob)>;
  113. /**
  114. * maps +/- 5 volts from cv and knob to y0, y1
  115. * with an audio taper.
  116. */
  117. static SimpleScaleFun<float> makeSimpleScalerAudioTaper(float y0, float y1);
  118. template <typename T>
  119. static std::pair<T, T> getMinMax(const T* data, int numSamples)
  120. {
  121. T min = 1, max = -1;
  122. for (int i = 0; i < numSamples; ++i) {
  123. const T x = data[i];
  124. min = std::min(min, x);
  125. max = std::max(max, x);
  126. }
  127. return std::pair<T, T>(min, max);
  128. }
  129. /**
  130. * A random number generator function. uniform random 0..1
  131. */
  132. using RandomUniformFunc = std::function<float(void)>;
  133. static RandomUniformFunc random();
  134. /**
  135. * Folds numbers between +1 and -1
  136. */
  137. static inline float fold(float x)
  138. {
  139. float fold;
  140. const float bias = (x < 0) ? -1.f : 1.f;
  141. int phase = int((x + bias) / 2.f);
  142. bool isEven = !(phase & 1);
  143. // printf(" wrap(%f) phase=%d, isEven=%d", x, phase, isEven);
  144. if (isEven) {
  145. fold = x - 2.f * phase;
  146. } else {
  147. fold = -x + 2.f * phase;
  148. }
  149. // printf(" y=%f\n", wrap);
  150. return fold;
  151. }
  152. };