Browse Source

Add ::reset() to TriggerGenerator

tags/v0.6.2b
Andrew Belt 6 years ago
parent
commit
1405fbea4f
1 changed files with 31 additions and 13 deletions
  1. +31
    -13
      include/dsp/digital.hpp

+ 31
- 13
include/dsp/digital.hpp View File

@@ -6,7 +6,7 @@
namespace rack { 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 { struct SchmittTrigger {
// UNKNOWN is used to represent a stable state when the previous state is not yet set // UNKNOWN is used to represent a stable state when the previous state is not yet set
enum State { enum State {
@@ -14,7 +14,14 @@ struct SchmittTrigger {
LOW, LOW,
HIGH HIGH
}; };
State state = UNKNOWN;
State state;

SchmittTrigger() {
reset();
}
void reset() {
state = UNKNOWN;
}
/** Updates the state of the Schmitt Trigger given a value. /** Updates the state of the Schmitt Trigger given a value.
Returns true if triggered, i.e. the value increases from 0 to 1. Returns true if triggered, i.e. the value increases from 0 to 1.
If different trigger thresholds are needed, use If different trigger thresholds are needed, use
@@ -48,28 +55,39 @@ struct SchmittTrigger {
bool isHigh() { bool isHigh() {
return state == HIGH; return state == HIGH;
} }
void reset() {
state = UNKNOWN;
}
}; };




/** When triggered, holds a high value for a specified time before going low again */ /** 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) { bool process(float deltaTime) {
time += 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; time = 0.f;
this->pulseTime = pulseTime;
this->triggerDuration = triggerDuration;
} }
} }
}; };


/** Deprecated name for TriggerGenerator */
typedef TriggerGenerator PulseGenerator;



} // namespace rack } // namespace rack

Loading…
Cancel
Save