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.

143 lines
2.4KB

  1. #pragma once
  2. #include "dsp/common.hpp"
  3. namespace rack {
  4. namespace dsp {
  5. struct RCFilter {
  6. float c = 0.f;
  7. float xstate[1] = {};
  8. float ystate[1] = {};
  9. // `r` is the ratio between the cutoff frequency and sample rate, i.e. r = f_c / f_s
  10. void setCutoff(float r) {
  11. c = 2.f / r;
  12. }
  13. void process(float x) {
  14. float y = (x + xstate[0] - ystate[0] * (1 - c)) / (1 + c);
  15. xstate[0] = x;
  16. ystate[0] = y;
  17. }
  18. float lowpass() {
  19. return ystate[0];
  20. }
  21. float highpass() {
  22. return xstate[0] - ystate[0];
  23. }
  24. };
  25. struct PeakFilter {
  26. float state = 0.f;
  27. float c = 0.f;
  28. /** Rate is lambda / sampleRate */
  29. void setRate(float r) {
  30. c = 1.f - r;
  31. }
  32. void process(float x) {
  33. if (x > state)
  34. state = x;
  35. state *= c;
  36. }
  37. float peak() {
  38. return state;
  39. }
  40. };
  41. struct SlewLimiter {
  42. float rise = 0.f;
  43. float fall = 0.f;
  44. float out = NAN;
  45. float process(float deltaTime, float in) {
  46. if (std::isnan(out)) {
  47. out = in;
  48. }
  49. else if (out < in) {
  50. float y = out + rise * deltaTime;
  51. out = std::fmin(y, in);
  52. }
  53. else if (out > in) {
  54. float y = out - fall * deltaTime;
  55. out = std::fmax(y, in);
  56. }
  57. return out;
  58. }
  59. DEPRECATED float process(float in) {
  60. return process(1.f, in);
  61. }
  62. DEPRECATED void setRiseFall(float rise, float fall) {
  63. this->rise = rise;
  64. this->fall = fall;
  65. }
  66. };
  67. struct ExponentialSlewLimiter {
  68. float riseLambda = 0.f;
  69. float fallLambda = 0.f;
  70. float out = NAN;
  71. float process(float deltaTime, float in) {
  72. if (std::isnan(out)) {
  73. out = in;
  74. }
  75. else if (out < in) {
  76. float y = out + (in - out) * riseLambda * deltaTime;
  77. out = (out == y) ? in : y;
  78. }
  79. else if (out > in) {
  80. float y = out + (in - out) * fallLambda * deltaTime;
  81. out = (out == y) ? in : y;
  82. }
  83. return out;
  84. }
  85. DEPRECATED float process(float in) {
  86. return process(1.f, in);
  87. }
  88. };
  89. /** Applies exponential smoothing to a signal with the ODE
  90. \f$ \frac{dy}{dt} = x \lambda \f$.
  91. */
  92. struct ExponentialFilter {
  93. float out;
  94. float lambda = 0.f;
  95. ExponentialFilter() {
  96. reset();
  97. }
  98. void reset() {
  99. out = NAN;
  100. }
  101. float process(float deltaTime, float in) {
  102. if (std::isnan(out)) {
  103. out = in;
  104. }
  105. else {
  106. float y = out + (in - out) * lambda * deltaTime;
  107. // If no change was detected, assume float granularity is too small and snap output to input
  108. if (out == y)
  109. out = in;
  110. else
  111. out = y;
  112. }
  113. return out;
  114. }
  115. DEPRECATED float process(float in) {
  116. return process(1.f, in);
  117. }
  118. };
  119. } // namespace dsp
  120. } // namespace rack