diff --git a/include/dsp/digital.hpp b/include/dsp/digital.hpp index 4a90a5a5..ed6571af 100644 --- a/include/dsp/digital.hpp +++ b/include/dsp/digital.hpp @@ -6,7 +6,7 @@ namespace rack { -/** Turns high when value reaches 1, turns low when value reaches 0 */ +/** Turns HIGH when value reaches 1.f, turns LOW when value reaches 0.f. */ struct SchmittTrigger { // UNKNOWN is used to represent a stable state when the previous state is not yet set enum State { @@ -14,7 +14,14 @@ struct SchmittTrigger { LOW, HIGH }; - State state = UNKNOWN; + State state; + + SchmittTrigger() { + reset(); + } + void reset() { + state = UNKNOWN; + } /** Updates the state of the Schmitt Trigger given a value. Returns true if triggered, i.e. the value increases from 0 to 1. If different trigger thresholds are needed, use @@ -48,28 +55,39 @@ struct SchmittTrigger { bool isHigh() { return state == HIGH; } - void reset() { - state = UNKNOWN; - } }; /** When triggered, holds a high value for a specified time before going low again */ -struct PulseGenerator { - float time = 0.f; - float pulseTime = 0.f; +struct TriggerGenerator { + float time; + float triggerDuration; + + TriggerGenerator() { + reset(); + } + /** Immediately resets the state to LOW */ + void reset() { + time = 0.f; + triggerDuration = 0.f; + } + /** Advances the state by `deltaTime`. Returns whether the pulse is in the HIGH state. */ bool process(float deltaTime) { time += deltaTime; - return time < pulseTime; + return time < triggerDuration; } - void trigger(float pulseTime) { - // Keep the previous pulseTime if the existing pulse would be held longer than the currently requested one. - if (time + pulseTime >= this->pulseTime) { + /** Begins a trigger with the given `triggerDuration`. */ + void trigger(float triggerDuration) { + // Keep the previous triggerDuration if the existing pulse would be held longer than the currently requested one. + if (time + triggerDuration >= this->triggerDuration) { time = 0.f; - this->pulseTime = pulseTime; + this->triggerDuration = triggerDuration; } } }; +/** Deprecated name for TriggerGenerator */ +typedef TriggerGenerator PulseGenerator; + } // namespace rack