|
@@ -6,35 +6,39 @@ |
|
|
namespace rack { |
|
|
namespace rack { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Turns high when value reaches the high threshold, turns low when value reaches the low threshold */ |
|
|
|
|
|
|
|
|
/** Turns high when value reaches 1, turns low when value reaches 0 */ |
|
|
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 {UNKNOWN, LOW, HIGH} state = UNKNOWN; |
|
|
|
|
|
float low = 0.0; |
|
|
|
|
|
float high = 1.0; |
|
|
|
|
|
void setThresholds(float low, float high) { |
|
|
|
|
|
this->low = low; |
|
|
|
|
|
this->high = high; |
|
|
|
|
|
} |
|
|
|
|
|
/** Returns true if triggered */ |
|
|
|
|
|
|
|
|
enum State { |
|
|
|
|
|
UNKNOWN, |
|
|
|
|
|
LOW, |
|
|
|
|
|
HIGH |
|
|
|
|
|
}; |
|
|
|
|
|
State 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 |
|
|
|
|
|
process(rescale(in, low, high, 0.f, 1.f)) |
|
|
|
|
|
for example. |
|
|
|
|
|
*/ |
|
|
bool process(float in) { |
|
|
bool process(float in) { |
|
|
switch (state) { |
|
|
switch (state) { |
|
|
case LOW: |
|
|
case LOW: |
|
|
if (in >= high) { |
|
|
|
|
|
|
|
|
if (in >= 1.f) { |
|
|
state = HIGH; |
|
|
state = HIGH; |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
break; |
|
|
break; |
|
|
case HIGH: |
|
|
case HIGH: |
|
|
if (in <= low) { |
|
|
|
|
|
|
|
|
if (in <= 0.f) { |
|
|
state = LOW; |
|
|
state = LOW; |
|
|
} |
|
|
} |
|
|
break; |
|
|
break; |
|
|
default: |
|
|
default: |
|
|
if (in >= high) { |
|
|
|
|
|
|
|
|
if (in >= 1.f) { |
|
|
state = HIGH; |
|
|
state = HIGH; |
|
|
} |
|
|
} |
|
|
else if (in <= low) { |
|
|
|
|
|
|
|
|
else if (in <= 0.f) { |
|
|
state = LOW; |
|
|
state = LOW; |
|
|
} |
|
|
} |
|
|
break; |
|
|
break; |
|
@@ -52,8 +56,8 @@ struct SchmittTrigger { |
|
|
|
|
|
|
|
|
/** 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 { |
|
|
struct PulseGenerator { |
|
|
float time = 0.0; |
|
|
|
|
|
float pulseTime = 0.0; |
|
|
|
|
|
|
|
|
float time = 0.f; |
|
|
|
|
|
float pulseTime = 0.f; |
|
|
bool process(float deltaTime) { |
|
|
bool process(float deltaTime) { |
|
|
time += deltaTime; |
|
|
time += deltaTime; |
|
|
return time < pulseTime; |
|
|
return time < pulseTime; |
|
@@ -61,7 +65,7 @@ struct PulseGenerator { |
|
|
void trigger(float pulseTime) { |
|
|
void trigger(float pulseTime) { |
|
|
// Keep the previous pulseTime if the existing pulse would be held longer than the currently requested one. |
|
|
// Keep the previous pulseTime if the existing pulse would be held longer than the currently requested one. |
|
|
if (time + pulseTime >= this->pulseTime) { |
|
|
if (time + pulseTime >= this->pulseTime) { |
|
|
time = 0.0; |
|
|
|
|
|
|
|
|
time = 0.f; |
|
|
this->pulseTime = pulseTime; |
|
|
this->pulseTime = pulseTime; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|