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.

33 lines
1012B

  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 = simd::float_4::zero();
  6. /** Immediately disables the pulse */
  7. void reset() {
  8. remaining = simd::float_4::zero();
  9. }
  10. /** Advances the state by `deltaTime`. Returns whether the pulse is in the HIGH state. */
  11. inline simd::float_4 process(float deltaTime) {
  12. simd::float_4 mask = (remaining > simd::float_4::zero());
  13. remaining -= ifelse(mask, simd::float_4(deltaTime), simd::float_4::zero());
  14. return ifelse(mask, simd::float_4::mask(), simd::float_4::zero());
  15. }
  16. /** Begins a trigger with the given `duration`. */
  17. inline 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. simd::float_4 duration_4 = simd::float_4(duration);
  20. remaining = ifelse(mask & (duration_4 > remaining), duration_4, remaining);
  21. }
  22. };