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.

filter.hpp 949B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. } // namespace rack