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
185 void trigger(float duration = 1e-3f) {
186 // Keep the previous pulse if the existing pulse will be held longer than the currently requested one.
187 if (duration > remaining) {
188 remaining = duration;
189 }
190 }
191};
192
193
195template <typename T = float>
196struct TTimer {
197 T time = 0.f;
198
199 void reset() {
200 time = 0.f;
201 }
202
204 T process(T deltaTime) {
205 time += deltaTime;
206 return time;
207 }
208
210 return time;
211 }
212};
213
215
216
225 uint32_t clock = 0;
226 uint32_t division = 1;
227
228 void reset() {
229 clock = 0;
230 }
231
232 void setDivision(uint32_t division) {
233 this->division = division;
234 }
235
236 uint32_t getDivision() {
237 return division;
238 }
239
240 uint32_t getClock() {
241 return clock;
242 }
243
245 bool process() {
246 clock++;
247 if (clock >= division) {
248 clock = 0;
249 return true;
250 }
251 return false;
252 }
253};
254
255
256} // namespace dsp
257} // namespace rack
TTimer Timer
Definition digital.hpp:214
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:224
bool process()
Returns true when the clock reaches division and resets.
Definition digital.hpp:245
uint32_t getDivision()
Definition digital.hpp:236
uint32_t getClock()
Definition digital.hpp:240
void setDivision(uint32_t division)
Definition digital.hpp:232
uint32_t clock
Definition digital.hpp:225
void reset()
Definition digital.hpp:228
uint32_t division
Definition digital.hpp:226
When triggered, holds a high value for a specified time before going low again.
Definition digital.hpp:167
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:185
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:196
T time
Definition digital.hpp:197
void reset()
Definition digital.hpp:199
T getTime()
Definition digital.hpp:209
T process(T deltaTime)
Returns the time since last reset or initialization.
Definition digital.hpp:204