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.

141 lines
2.3KB

  1. #pragma once
  2. #include <dsp/common.hpp>
  3. namespace rack {
  4. namespace dsp {
  5. /** Detects when a boolean changes from false to true */
  6. struct BooleanTrigger {
  7. bool state = true;
  8. void reset() {
  9. state = true;
  10. }
  11. bool process(bool state) {
  12. bool triggered = (state && !this->state);
  13. this->state = state;
  14. return triggered;
  15. }
  16. };
  17. /** Turns HIGH when value reaches 1.f, turns LOW when value reaches 0.f. */
  18. struct SchmittTrigger {
  19. bool state = true;
  20. void reset() {
  21. state = true;
  22. }
  23. /** Updates the state of the Schmitt Trigger given a value.
  24. Returns true if triggered, i.e. the value increases from 0 to 1.
  25. If different trigger thresholds are needed, use
  26. process(rescale(in, low, high, 0.f, 1.f))
  27. for example.
  28. */
  29. bool process(float in) {
  30. if (state) {
  31. // HIGH to LOW
  32. if (in <= 0.f) {
  33. state = false;
  34. }
  35. }
  36. else {
  37. // LOW to HIGH
  38. if (in >= 1.f) {
  39. state = true;
  40. return true;
  41. }
  42. }
  43. return false;
  44. }
  45. bool isHigh() {
  46. return state;
  47. }
  48. };
  49. /** When triggered, holds a high value for a specified time before going low again */
  50. struct PulseGenerator {
  51. float remaining = 0.f;
  52. /** Immediately disables the pulse */
  53. void reset() {
  54. remaining = 0.f;
  55. }
  56. /** Advances the state by `deltaTime`. Returns whether the pulse is in the HIGH state. */
  57. bool process(float deltaTime) {
  58. if (remaining > 0.f) {
  59. remaining -= deltaTime;
  60. return true;
  61. }
  62. return false;
  63. }
  64. /** Begins a trigger with the given `duration`. */
  65. void trigger(float duration = 1e-3f) {
  66. // Keep the previous pulse if the existing pulse will be held longer than the currently requested one.
  67. if (duration > remaining) {
  68. remaining = duration;
  69. }
  70. }
  71. };
  72. struct Timer {
  73. float time = 0.f;
  74. void reset() {
  75. time = 0.f;
  76. }
  77. /** Returns the time since last reset or initialization. */
  78. float process(float deltaTime) {
  79. time += deltaTime;
  80. return time;
  81. }
  82. };
  83. struct ClockDivider {
  84. uint32_t clock = 0;
  85. uint32_t division = 1;
  86. void reset() {
  87. clock = 0;
  88. }
  89. void setDivision(uint32_t division) {
  90. this->division = division;
  91. }
  92. uint32_t getDivision() {
  93. return division;
  94. }
  95. uint32_t getClock() {
  96. return clock;
  97. }
  98. /** Returns true when the clock reaches `division` and resets. */
  99. bool process() {
  100. clock++;
  101. if (clock >= division) {
  102. clock = 0;
  103. return true;
  104. }
  105. return false;
  106. }
  107. };
  108. } // namespace dsp
  109. } // namespace rack