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.

29 lines
832B

  1. #pragma once
  2. #include <rack.hpp>
  3. /** When triggered, holds a high value for a specified time before going low again */
  4. struct PulseGenerator_4 {
  5. simd::float_4 remaining = 0.f;
  6. /** Immediately disables the pulse */
  7. void reset() {
  8. remaining = 0.f;
  9. }
  10. /** Advances the state by `deltaTime`. Returns whether the pulse is in the HIGH state. */
  11. simd::float_4 process(float deltaTime) {
  12. simd::float_4 mask = (remaining > 0.f);
  13. remaining -= ifelse(mask, deltaTime, 0.f);
  14. return ifelse(mask, simd::float_4::mask(), 0.f);
  15. }
  16. /** Begins a trigger with the given `duration`. */
  17. void trigger(simd::float_4 mask, float duration = 1e-3f) {
  18. // Keep the previous pulse if the existing pulse will be held longer than the currently requested one.
  19. remaining = ifelse(mask & (duration > remaining), duration, remaining);
  20. }
  21. };