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.

digital.hpp 2.1KB

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