-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathturnt.cpp
More file actions
472 lines (427 loc) · 14.8 KB
/
Copy pathturnt.cpp
File metadata and controls
472 lines (427 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#include "plugin.hpp"
#include "inc/ResizableRingBuffer.hpp"
#include "widgets/Scope.hpp"
#include "widgets/ScopeData.hpp"
#include "widgets/TabDisplay.cpp"
#include "widgets/PanelBackground.hpp"
#include "widgets/InverterWidget.hpp"
struct Turnt : Module
{
enum ParamId
{
MODE_PARAM,
ZERO_PARAM,
PROB_PARAM,
PARAMS_LEN
};
enum InputId
{
SOURCE_INPUT,
ZERO_INPUT,
PROB_INPUT,
INPUTS_LEN
};
enum OutputId
{
TRIG_OUTPUT,
OUTPUTS_LEN
};
enum LightId
{
LIGHTS_LEN
};
struct Point
{
float min = 0.f;
float max = 0.f;
};
Turnt()
{
config(PARAMS_LEN, INPUTS_LEN, OUTPUTS_LEN, LIGHTS_LEN);
configSwitch(MODE_PARAM, 0.f, 2.f, 0.f, "mode",
{"direction", "through zero", "both"});
configParam(ZERO_PARAM, -10.f, 10.f, 0.f, "zero threshold", "V");
configParam(PROB_PARAM, 0.f, 1.f, 1.f, "probability", "%", 0.f, 100.f);
configInput(SOURCE_INPUT, "source");
configInput(ZERO_INPUT, "zero threshold");
getInputInfo(ZERO_INPUT)->description = "expects -10V to 10V signal";
configInput(PROB_INPUT, "probability");
getInputInfo(PROB_INPUT)->description = "expects 0V to 10V signal";
configOutput(TRIG_OUTPUT, "trigger");
scope_data.backgroundColor = nvgRGB(0x1e, 0x1e, 0x1e);
scope_data.wavePrimaryColor = nvgRGB(0xff, 0xff, 0xff);
scope_data.gridColor = nvgRGB(0x50, 0x57, 0x5c);
scope_data.triggerColor = nvgRGB(0x8f, 0x3b, 0x3b);
for (int ch = 0; ch < MAX_POLY; ch++)
{
scope_data.buffer[ch].resize(256);
for (int i = 0; i < scope_data.buffer[ch].size; i++)
{
scope_data.buffer[ch].add(std::make_pair(0.f, false));
}
}
if (use_global_contrast[TURNT])
{
module_contrast[TURNT] = global_contrast;
}
}
ScopeData scope_data;
float samples[MAX_POLY][3] = {0.f, 0.f, 0.f};
bool freeze_when_idle = false;
bool triggered[MAX_POLY] = {false};
int trigger_mode = {0};
bool gate_high[MAX_POLY] = {false};
dsp::PulseGenerator pulse[MAX_POLY];
int buffer_index[MAX_POLY] = {0};
int frame_index[MAX_POLY] = {0};
void process(const ProcessArgs &args) override
{
int mode = params[MODE_PARAM].getValue();
float zero = params[ZERO_PARAM].getValue();
scope_data.zeroThreshold[scope_data.activeChannel] = zero;
float prob = params[PROB_PARAM].getValue();
if (inputs[ZERO_INPUT].isConnected())
{
zero *= inputs[ZERO_INPUT].getVoltage() / 10.f;
}
if (inputs[PROB_INPUT].isConnected())
{
prob *= inputs[PROB_INPUT].getVoltage() / 10.f;
}
int channels = std::max(1, inputs[SOURCE_INPUT].getChannels());
outputs[TRIG_OUTPUT].setChannels(channels);
if (!inputs[SOURCE_INPUT].isConnected())
{
if (freeze_when_idle)
{
return;
}
for (int ch = 0; ch < MAX_POLY; ch++)
{
for (int i = 0; i < scope_data.buffer[ch].size; i++)
{
scope_data.buffer[ch].get(i).operator=(std::make_pair(0.f, false));
}
}
}
for (int ch = 0; ch < channels; ch++)
{
auto in = inputs[SOURCE_INPUT].getVoltage(ch);
if (in != samples[ch][0])
{
samples[ch][2] = samples[ch][1];
samples[ch][1] = samples[ch][0];
samples[ch][0] = in;
auto v1 = samples[ch][0];
auto v2 = samples[ch][1];
auto d1 = v2 - v1;
auto v3 = samples[ch][2];
auto d2 = v3 - v2;
bool trig = false;
float r = random::uniform();
switch (mode)
{
case 0:
trig =
((d1 > 0.f && d2 < 0.f) || (d1 < 0.f && d2 > 0.f))
? (r < prob)
: false;
break;
case 1:
trig =
((v1 > zero && v2 <= zero) || (v1 < zero && v2 >= zero))
? (r < prob)
: false;
break;
case 2:
trig =
((d1 > 0.f && d2 < 0.f) || (d1 < 0.f && d2 > 0.f) || (v1 > zero && v2 <= zero) || (v1 < zero && v2 >= zero))
? (r < prob)
: false;
break;
}
if (trig)
{
triggered[ch] = true;
switch (trigger_mode)
{
case 0:
pulse[ch].trigger(1e-3f);
break;
case 1:
gate_high[ch] = !gate_high[ch];
break;
}
}
}
if (trigger_mode == 1)
{
outputs[TRIG_OUTPUT].setVoltage(gate_high[ch] ? 10.f : 0.f, ch);
triggered[ch] = gate_high[ch] || triggered[ch];
}
else
{
auto pulseValue = pulse[ch].process(args.sampleTime);
outputs[TRIG_OUTPUT].setVoltage(pulseValue ? 10.f : 0.f, ch);
triggered[ch] = bool(pulseValue) || triggered[ch];
}
if (buffer_index[ch] < scope_data.buffer[ch].size)
{
float dt = dsp::approxExp2_taylor5(
-(-std::log2(scope_data.timeScale))) /
scope_data.buffer[ch].size;
int frame_count = (int)std::ceil(dt * args.sampleRate);
if (++frame_index[ch] >= frame_count)
{
frame_index[ch] = 0;
scope_data.buffer[ch].add(std::make_pair(in, triggered[ch]));
triggered[ch] = false;
buffer_index[ch]++;
}
}
else
{
buffer_index[ch] = 0;
frame_index[ch] = 0;
}
}
}
void onReset() override
{
for (int i = 0; i < MAX_POLY; i++)
{
pulse[i].reset();
gate_high[i] = false;
}
}
void dataFromJson(json_t *rootJ) override
{
json_t *modeJ = json_object_get(rootJ, "trigger mode");
if (modeJ)
{
trigger_mode = json_integer_value(modeJ);
}
json_t *freezeJ = json_object_get(rootJ, "freeze when idle");
if (freezeJ)
{
freeze_when_idle = json_boolean_value(freezeJ);
}
}
json_t *dataToJson() override
{
json_t *rootJ = json_object();
json_object_set_new(rootJ, "trigger mode",
json_integer(trigger_mode));
json_object_set_new(rootJ, "freeze on disconnect",
json_boolean(freeze_when_idle));
return rootJ;
}
};
struct TurntWidget : ModuleWidget
{
TabDisplay *topTabDisplay = new TabDisplay();
TabDisplay *bottomTabDisplay = new TabDisplay();
PanelBackground *panelBackground = new PanelBackground();
SvgPanel *svgPanel;
Inverter *inverter = new Inverter();
TurntWidget(Turnt *module)
{
setModule(module);
svgPanel = createPanel(asset::plugin(pluginInstance, "res/turnt.svg"));
setPanel(svgPanel);
panelBackground->box.size = svgPanel->box.size;
svgPanel->fb->addChildBottom(panelBackground);
inverter->box.pos = Vec(0.f, 0.f);
inverter->box.size = Vec(box.size.x, box.size.y);
addChild(inverter);
float y_start = RACK_GRID_WIDTH * 4;
float dy = RACK_GRID_WIDTH;
float x = box.size.x / 2;
float y = y_start;
addInput(createInputCentered<BitPort>(Vec(x, y + dy), module,
Turnt::SOURCE_INPUT));
y += dy * 3;
x -= RACK_GRID_WIDTH * 1.5;
addInput(createInputCentered<BitPort>(Vec(x - dy * 1.25, y + dy / 2), module,
Turnt::ZERO_INPUT));
y += dy * 2;
addParam(createParamCentered<SmallBitKnob>(Vec(x + dy / 2, y - dy - dy / 2), module,
Turnt::ZERO_PARAM));
y -= dy * 2;
x += RACK_GRID_WIDTH * 3;
addInput(createInputCentered<BitPort>(Vec(x + dy * 1.25, y + dy / 2), module,
Turnt::PROB_INPUT));
y += dy * 2;
addParam(createParamCentered<SmallBitKnob>(Vec(x - dy / 2, y - dy - dy / 2), module,
Turnt::PROB_PARAM));
y += dy * 2;
x -= RACK_GRID_WIDTH * 1.5;
addParam(createParamCentered<CKSSThreeHorizontal>(Vec(x, y - dy / 2), module,
Turnt::MODE_PARAM));
y += dy * 2;
addOutput(createOutputCentered<BitPort>(Vec(x, y), module,
Turnt::TRIG_OUTPUT));
y += dy * 2.f;
auto scopeData = module ? &module->scope_data : nullptr;
auto scope = new Scope(scopeData);
scope->box.pos = Vec(1.f, y + 5.f);
scope->box.size = Vec(box.size.x - 2.f, 95.f);
topTabDisplay->box.pos = Vec(scope->box.pos.x, scope->box.pos.y - 9.f);
topTabDisplay->box.size = Vec(scope->box.size.x, 10.f);
bottomTabDisplay->box.pos = Vec(scope->box.pos.x, scope->box.pos.y + scope->box.size.y - 1.f);
bottomTabDisplay->box.size = Vec(scope->box.size.x, 10.f);
for (int i = 0; i < 8; i++)
{
topTabDisplay->addTab(std::to_string(i + 1), [this, scopeData, i]()
{
if (topTabDisplay->tabAvailable[i] == false ||
(topTabDisplay->tabAvailable[i] == true &&
topTabDisplay->selectedTab == i)) {
return;
}
topTabDisplay->selectedTab = i;
bottomTabDisplay->selectedTab = -1;
scopeData->activeChannel = i; });
}
for (int i = 8; i < 16; i++)
{
bottomTabDisplay->addTab(std::to_string(i + 1), [this, scopeData, i]()
{
if (bottomTabDisplay->tabAvailable[i - 8] == false ||
(bottomTabDisplay->tabAvailable[i - 8] == true &&
bottomTabDisplay->selectedTab == i - 8)) {
return;
}
topTabDisplay->selectedTab = -1;
bottomTabDisplay->selectedTab = i - 8;
scopeData->activeChannel = i; });
}
topTabDisplay->selectedTab = 0;
bottomTabDisplay->selectedTab = -1;
addChild(topTabDisplay);
addChild(bottomTabDisplay);
addChild(scope);
}
void step() override
{
if (module)
{
int numChannels = module->inputs[Turnt::SOURCE_INPUT].getChannels();
for (int i = 0; i < 8; i++)
{
if (i < numChannels)
{
topTabDisplay->tabAvailable[i] = true;
}
else
{
topTabDisplay->tabAvailable[i] = false;
}
}
for (int i = 8; i < 16; i++)
{
if (i < numChannels)
{
bottomTabDisplay->tabAvailable[i - 8] = true;
}
else
{
bottomTabDisplay->tabAvailable[i - 8] = false;
}
}
Turnt *turntModule = dynamic_cast<Turnt *>(this->module);
if (!turntModule)
return;
if (use_global_contrast[TURNT])
{
module_contrast[TURNT] = global_contrast;
}
if (module_contrast[TURNT] != panelBackground->contrast)
{
panelBackground->contrast = module_contrast[TURNT];
if (panelBackground->contrast < 0.4f)
{
panelBackground->invert(true);
inverter->invert = true;
}
else
{
panelBackground->invert(false);
inverter->invert = false;
}
svgPanel->fb->dirty = true;
}
}
ModuleWidget::step();
}
void drawLayer(const DrawArgs &args, int layer) override
{
ModuleWidget::drawLayer(args, layer);
}
void draw(const DrawArgs &args) override
{
ModuleWidget::draw(args);
}
void appendContextMenu(Menu *menu) override
{
Turnt *module = dynamic_cast<Turnt *>(this->module);
assert(module);
menu->addChild(new MenuSeparator());
menu->addChild(createSubmenuItem("contrast", "", [=](Menu *menu)
{
Menu* contrastMenu = new Menu();
ContrastSlider *contrastSlider = new ContrastSlider(&(module_contrast[TURNT]));
contrastSlider->box.size.x = 200.f;
GlobalOption *globalOption = new GlobalOption(&(use_global_contrast[TURNT]));
contrastMenu->addChild(globalOption);
contrastMenu->addChild(new MenuSeparator());
contrastMenu->addChild(contrastSlider);
contrastMenu->addChild(createMenuItem("set global contrast", "",
[]() {
global_contrast = module_contrast[TURNT];
use_global_contrast[TURNT] = true;
}));
menu->addChild(contrastMenu); }));
menu->addChild(createCheckMenuItem("freeze when idle", "", [=]()
{ return module->freeze_when_idle; }, [=]()
{ module->freeze_when_idle = !module->freeze_when_idle; }));
menu->addChild(createSubmenuItem("trigger mode", "", [=](Menu *menu)
{
Menu* trigMenu = new Menu();
trigMenu->addChild(createCheckMenuItem(
"trigger", "",
[=]() { return module->trigger_mode == 0; },
[=]() { module->trigger_mode = 0; }));
trigMenu->addChild(createCheckMenuItem(
"latch", "",
[=]() { return module->trigger_mode == 1; },
[=]() { module->trigger_mode = 1; }));
menu->addChild(trigMenu); }));
menu->addChild(createSubmenuItem("scope mode", "", [=](Menu *menu)
{
Menu* scopeMenu = new Menu();
scopeMenu->addChild(createCheckMenuItem(
"bipolar", "",
[=]() { return module->scope_data.scopeMode[module->scope_data.activeChannel] == 0; },
[=]() { module->scope_data.scopeMode[module->scope_data.activeChannel] = 0; }));
scopeMenu->addChild(createCheckMenuItem(
"unipolar", "",
[=]() { return module->scope_data.scopeMode[module->scope_data.activeChannel] == 1; },
[=]() { module->scope_data.scopeMode[module->scope_data.activeChannel] = 1; }));
menu->addChild(scopeMenu); }));
menu->addChild(createSubmenuItem("time scale", "", [=](Menu *menu)
{
Menu* divMenu = new Menu();
divMenu->addChild(createCheckMenuItem("low", "",
[=]() { return module->scope_data.buffer[module->scope_data.activeChannel].size == 64; },
[=]() { module->scope_data.buffer[module->scope_data.activeChannel].resize(64); }));
divMenu->addChild(createCheckMenuItem("medium", "",
[=]() { return module->scope_data.buffer[module->scope_data.activeChannel].size == 256; },
[=]() { module->scope_data.buffer[module->scope_data.activeChannel].resize(256); }));
divMenu->addChild(createCheckMenuItem("high", "",
[=]() { return module->scope_data.buffer[module->scope_data.activeChannel].size == 2048; },
[=]() { module->scope_data.buffer[module->scope_data.activeChannel].resize(2048); }));
menu->addChild(divMenu); }));
}
};
Model *modelTurnt = createModel<Turnt, TurntWidget>("turnt");