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.

130 lines
3.4KB

  1. //
  2. // DSPUtilities.cpp
  3. //
  4. //
  5. #include "DSPUtilities.h"
  6. #ifndef DSP_UTILITIES
  7. #define DSP_UTILITIES
  8. #include <cmath>
  9. namespace rack_plugin_RJModules {
  10. //==============================================================================
  11. // Calculates the frequency of a given pitch (MIDI) value.
  12. double pitchToFreq(double pitch)
  13. {
  14. return pow(2, (pitch - 69) / 12) * 440;
  15. }
  16. //==============================================================================
  17. // Calculates the pitch (MIDI) of a given frequency value
  18. double freqToPitch(double freq)
  19. {
  20. return 69 + 12 * log2(freq / 440);
  21. }
  22. //==============================================================================
  23. /**
  24. Takes a value as input and clips it according to the min and max values.
  25. Returns the input if (minValue <= in <= maxValue).
  26. If (in < minValue), then return minValue.
  27. If (in > maxValue), then return maxValue.
  28. */
  29. double clipMinMax(double in, double minValue, double maxValue)
  30. {
  31. if (in < minValue)
  32. return minValue;
  33. else if (in > maxValue)
  34. return maxValue;
  35. else
  36. return in;
  37. }
  38. //==============================================================================
  39. /**
  40. Takes a value as input and clips it according to the min value.
  41. Returns the input if (minValue <= in).
  42. If (in < minValue), then return minValue.
  43. */
  44. double clipMin(double in, double minValue)
  45. {
  46. if (in < minValue)
  47. return minValue;
  48. else
  49. return in;
  50. }
  51. //==============================================================================
  52. /**
  53. Crossfades linearly between two values (in0, in1). The value returned is
  54. determined by the value of the xFadeCtrl argument.
  55. xFadeCtrl Range: 0->1
  56. - xFadeCtrl = 0 (only in0 comes through)
  57. - xFadeCtrl = 0.5 (equal mix of in0 and in1)
  58. - xfadeCtrl = 1 (only in1 comes through)
  59. */
  60. double xFadeLin(double xFadeCtrl, double in0, double in1)
  61. {
  62. // Clip the xFade parameter to only have range of 0->1
  63. xFadeCtrl = clipMinMax(xFadeCtrl, 0.0, 1.0);
  64. // Perform crossfading and return the value
  65. return (in0 * (1.0 - xFadeCtrl) + in1 * xFadeCtrl);
  66. }
  67. //==============================================================================
  68. /**
  69. Parabolic Controller Shaper:
  70. "Bends" the controller curve torwards the X or Y axis.
  71. input range: (-1..0..1) maps to output range: (-1..0..1).
  72. bend range: (-1..0..1)
  73. - bend = -1 (max bend towards X axis)
  74. - bend = 0 (don't bend)
  75. - bend = 1 (max bend towards Y axis)
  76. */
  77. double parCtrlShaper(double input, double bend)
  78. {
  79. // clip input and bend because the shaper only works in that range.
  80. input = clipMinMax(input, -1.0, 1.0);
  81. bend = clipMinMax(bend, -1.0, 1.0);
  82. return input * ((bend + 1) - fabs(input) * bend);
  83. }
  84. //==============================================================================
  85. /**
  86. Normalizes a range of values to the range 0->1.
  87. (start/end should probably be the range of a parameter)
  88. - input: the value to be normalized
  89. - start: the start of the input's range
  90. - end: the end of the input's range
  91. Note: (start < end) and (start > end) are both valid.
  92. */
  93. double normalizeRange(double input, double start, double end)
  94. {
  95. return (input - start) / (end - start);
  96. }
  97. double resonanceToQ(double resonance)
  98. {
  99. return 1.0 / (2.0 * (1.0 - resonance));
  100. }
  101. //==============================================================================
  102. } // namespace rack_plugin_RJModules
  103. #endif // DSP_UTILITIES