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.

68 lines
1.0KB

  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. void setRiseFall(float _rise, float _fall) {
  45. rise = _rise;
  46. fall = _fall;
  47. }
  48. float process(float in) {
  49. float delta = clampf(in - out, -fall, rise);
  50. out += delta;
  51. return out;
  52. }
  53. };
  54. } // namespace rack