VCV Rack API v2
Loading...
Searching...
No Matches
digital.hpp
Go to the documentation of this file.
1#pragma once
2#include <dsp/common.hpp>
3
4
5namespace rack {
6namespace dsp {
7
8
11 enum State : uint8_t {
15 };
16 union {
19 bool state;
20 };
21
22 void reset() {
24 }
25
27 bool process(bool in) {
28 bool triggered = (s == LOW) && in;
29 s = in ? HIGH : LOW;
30 return triggered;
31 }
32
33 enum Event {
34 NONE = 0,
36 UNTRIGGERED = -1
37 };
41 Event event = NONE;
42 if (s == LOW && in) {
43 event = TRIGGERED;
44 }
45 else if (s == HIGH && !in) {
46 event = UNTRIGGERED;
47 }
48 s = in ? HIGH : LOW;
49 return event;
50 }
51
52 bool isHigh() {
53 return s == HIGH;
54 }
55};
56
57
60template <typename T = float>
64 reset();
65 }
66 void reset() {
67 state = T::mask();
68 }
69 T process(T in, T lowThreshold = 0.f, T highThreshold = 1.f) {
70 T on = (in >= highThreshold);
71 T off = (in <= lowThreshold);
72 T triggered = ~state & on;
73 state = on | (state & ~off);
74 return triggered;
75 }
76 T isHigh() {
77 return state;
78 }
79};
80
81
82template <>
83struct TSchmittTrigger<float> {
84 enum State : uint8_t {
87 UNINITIALIZED
88 };
89 union {
90 State s = UNINITIALIZED;
92 bool state;
93 };
94
95 void reset() {
96 s = UNINITIALIZED;
97 }
98
107 bool process(float in, float lowThreshold = 0.f, float highThreshold = 1.f) {
108 if (s == LOW && in >= highThreshold) {
109 // LOW to HIGH
110 s = HIGH;
111 return true;
112 }
113 else if (s == HIGH && in <= lowThreshold) {
114 // HIGH to LOW
115 s = LOW;
116 }
117 else if (s == UNINITIALIZED && in >= highThreshold) {
118 // UNINITIALIZED to HIGH
119 s = HIGH;
120 }
121 else if (s == UNINITIALIZED && in <= lowThreshold) {
122 // UNINITIALIZED to LOW
123 s = LOW;
124 }
125 return false;
126 }
127
128 enum Event {
129 NONE = 0,
130 TRIGGERED = 1,
131 UNTRIGGERED = -1
132 };
135 Event processEvent(float in, float lowThreshold = 0.f, float highThreshold = 1.f) {
136 Event event = NONE;
137 if (s == LOW && in >= highThreshold) {
138 // LOW to HIGH
139 s = HIGH;
140 event = TRIGGERED;
141 }
142 else if (s == HIGH && in <= lowThreshold) {
143 // HIGH to LOW
144 s = LOW;
145 event = UNTRIGGERED;
146 }
147 else if (s == UNINITIALIZED && in >= highThreshold) {
148 // UNINITIALIZED to HIGH
149 s = HIGH;
150 }
151 else if (s == UNINITIALIZED && in <= lowThreshold) {
152 // UNINITIALIZED to LOW
153 s = LOW;
154 }
155 return event;
156 }
157
158 bool isHigh() {
159 return s == HIGH;
160 }
161};
162
164
165
168 float remaining = 0.f;
169
171 void reset() {
172 remaining = 0.f;
173 }
174
176 bool process(float deltaTime) {
177 if (remaining > 0.f) {
178 remaining -= deltaTime;
179 return true;
180 }
181 return false;
182 }
183
184 bool isHigh() {
185 return (remaining > 0.f);
186 }
187
189 void trigger(float duration = 1e-3f) {
190 // Keep the previous pulse if the existing pulse will be held longer than the currently requested one.
191 if (duration > remaining) {
192 remaining = duration;
193 }
194 }
195};
196
197
199template <typename T = float>
200struct TTimer {
201 T time = 0.f;
202
203 void reset() {
204 time = 0.f;
205 }
206
208 T process(T deltaTime) {
209 time += deltaTime;
210 return time;
211 }
212
214 return time;
215 }
216};
217
219
220
229 uint32_t clock = 0;
230 uint32_t division = 1;
231
232 void reset() {
233 clock = 0;
234 }
235
236 void setDivision(uint32_t division) {
237 this->division = division;
238 }
239
240 uint32_t getDivision() {
241 return division;
242 }
243
244 uint32_t getClock() {
245 return clock;
246 }
247
249 bool process() {
250 clock++;
251 if (clock >= division) {
252 clock = 0;
253 return true;
254 }
255 return false;
256 }
257};
258
259
260} // namespace dsp
261} // namespace rack
TTimer Timer
Definition digital.hpp:218
TSchmittTrigger SchmittTrigger
Definition digital.hpp:163
Root namespace for the Rack API.
Definition AudioDisplay.hpp:9
Detects when a boolean changes from false to true.
Definition digital.hpp:10
bool state
Deprecated.
Definition digital.hpp:19
void reset()
Definition digital.hpp:22
bool process(bool in)
Returns whether the input changed from false to true.
Definition digital.hpp:27
bool isHigh()
Definition digital.hpp:52
State s
Definition digital.hpp:17
Event
Definition digital.hpp:33
@ UNTRIGGERED
Definition digital.hpp:36
@ TRIGGERED
Definition digital.hpp:35
@ NONE
Definition digital.hpp:34
State
Definition digital.hpp:11
@ HIGH
Definition digital.hpp:13
@ UNINITIALIZED
Definition digital.hpp:14
@ LOW
Definition digital.hpp:12
Event processEvent(bool in)
Returns TRIGGERED if the input changed from false to true, and UNTRIGGERED if the input changed from ...
Definition digital.hpp:40
Counts calls to process(), returning true every division calls.
Definition digital.hpp:228
bool process()
Returns true when the clock reaches division and resets.
Definition digital.hpp:249
uint32_t getDivision()
Definition digital.hpp:240
uint32_t getClock()
Definition digital.hpp:244
void setDivision(uint32_t division)
Definition digital.hpp:236
uint32_t clock
Definition digital.hpp:229
void reset()
Definition digital.hpp:232
uint32_t division
Definition digital.hpp:230
When triggered, holds a high value for a specified time before going low again.
Definition digital.hpp:167
bool isHigh()
Definition digital.hpp:184
void reset()
Immediately disables the pulse.
Definition digital.hpp:171
float remaining
Definition digital.hpp:168
void trigger(float duration=1e-3f)
Begins a trigger with the given duration.
Definition digital.hpp:189
bool process(float deltaTime)
Advances the state by deltaTime.
Definition digital.hpp:176
State
Definition digital.hpp:84
@ LOW
Definition digital.hpp:85
@ HIGH
Definition digital.hpp:86
bool isHigh()
Definition digital.hpp:158
void reset()
Definition digital.hpp:95
Event processEvent(float in, float lowThreshold=0.f, float highThreshold=1.f)
Returns TRIGGERED if the input reached highThreshold, and UNTRIGGERED if the input reached lowThresho...
Definition digital.hpp:135
bool state
Deprecated.
Definition digital.hpp:92
Event
Definition digital.hpp:128
bool process(float in, float lowThreshold=0.f, float highThreshold=1.f)
Updates the state of the Schmitt Trigger given a value.
Definition digital.hpp:107
Turns HIGH when value reaches a threshold (default 0.f), turns LOW when value reaches a threshold (de...
Definition digital.hpp:61
T isHigh()
Definition digital.hpp:76
void reset()
Definition digital.hpp:66
T state
Definition digital.hpp:62
TSchmittTrigger()
Definition digital.hpp:63
T process(T in, T lowThreshold=0.f, T highThreshold=1.f)
Definition digital.hpp:69
Accumulates a timer when process() is called.
Definition digital.hpp:200
T time
Definition digital.hpp:201
void reset()
Definition digital.hpp:203
T getTime()
Definition digital.hpp:213
T process(T deltaTime)
Returns the time since last reset or initialization.
Definition digital.hpp:208