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.

90 lines
1.4KB

  1. #pragma once
  2. #include "math.hpp"
  3. namespace rack {
  4. struct RCFilter {
  5. float c = 0.0;
  6. float xstate[1] = {};
  7. float ystate[1] = {};
  8. // `r` is the ratio between the cutoff frequency and sample rate, i.e. r = f_c / f_s
  9. void setCutoff(float r) {
  10. c = 2.0 / r;
  11. }
  12. void process(float x) {
  13. float y = (x + xstate[0] - ystate[0] * (1 - c)) / (1 + c);
  14. xstate[0] = x;
  15. ystate[0] = y;
  16. }
  17. float lowpass() {
  18. return ystate[0];
  19. }
  20. float highpass() {
  21. return xstate[0] - ystate[0];
  22. }
  23. };
  24. struct PeakFilter {
  25. float state = 0.0;
  26. float c = 0.0;
  27. /** Rate is lambda / sampleRate */
  28. void setRate(float r) {
  29. c = 1.0 - r;
  30. }
  31. void process(float x) {
  32. if (x > state)
  33. state = x;
  34. state *= c;
  35. }
  36. float peak() {
  37. return state;
  38. }
  39. };
  40. struct SlewLimiter {
  41. float rise = 1.0;
  42. float fall = 1.0;
  43. float out = 0.0;
  44. float process(float in) {
  45. float delta = clampf(in - out, -fall, rise);
  46. out += delta;
  47. return out;
  48. }
  49. };
  50. struct SchmittTrigger {
  51. /** 0 unknown, 1 low, 2 high */
  52. int state = 0;
  53. float low = 0.0;
  54. float high = 1.0;
  55. void setThresholds(float low, float high) {
  56. this->low = low;
  57. this->high = high;
  58. }
  59. /** Returns true if triggered */
  60. bool process(float in) {
  61. bool triggered = false;
  62. if (in >= high) {
  63. if (state == 1)
  64. triggered = true;
  65. state = 2;
  66. }
  67. else if (in <= low) {
  68. state = 1;
  69. }
  70. return triggered;
  71. }
  72. void reset() {
  73. state = 0;
  74. }
  75. };
  76. } // namespace rack