-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlicker.cpp
More file actions
89 lines (76 loc) · 3.32 KB
/
Flicker.cpp
File metadata and controls
89 lines (76 loc) · 3.32 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
#include "Flicker.h"
#include "IPlug_include_in_plug_src.h"
#include "IControls.h"
#include <iostream>
#include <algorithm>
#include <chrono>
using namespace std::chrono;
Flicker::Flicker(const InstanceInfo& info)
: Plugin(info, MakeConfig(kNumParams, kNumPresets))
{
long long ms = duration_cast< milliseconds >(
system_clock::now().time_since_epoch()
).count();
srand((unsigned int) ms);
flickerCountdown = 0;
GetParam(kFlicker)->InitDouble("Flicker", 0., 0., 100.0, 0.01, "%");
GetParam(kFlickerOn)->InitBool("Light On", true);
GetParam(kFlickerLength)->InitMilliseconds("Length", 1., 1., 1000.);
#if IPLUG_EDITOR // http://bit.ly/2S64BDd
mMakeGraphicsFunc = [&]() {
return MakeGraphics(*this, PLUG_WIDTH, PLUG_HEIGHT, PLUG_FPS, GetScaleForScreen(PLUG_WIDTH, PLUG_HEIGHT));
};
mLayoutFunc = [&](IGraphics* pGraphics) {
const IBitmap switchBitmap = pGraphics->LoadBitmap((PNGSWITCH_FN), 2, true);
pGraphics->AttachCornerResizer(EUIResizerMode::Scale, false);
pGraphics->AttachPanelBackground(COLOR_GRAY);
pGraphics->LoadFont("Roboto-Regular", ROBOTO_FN);
const IRECT b = pGraphics->GetBounds();
pGraphics->AttachControl(new IBitmapControl(0, 0, pGraphics->LoadBitmap(OFF_FN, 1)));
pGraphics->AttachControl(new IBitmapControl(0, 0, pGraphics->LoadBitmap(ON_FN, 1)), kInFlicker);
pGraphics->AttachControl(new IVKnobControl(b.GetCentredInside(100).GetVShifted(-200).GetHShifted(250), kFlickerLength));
pGraphics->AttachControl(new IVKnobControl(b.GetCentredInside(100).GetVShifted(-90).GetHShifted(250), kFlicker));
pGraphics->AttachControl(new ITextControl( b.GetCentredInside(100).GetVShifted(-240).GetHShifted(350), "Flicker On/Off", IText(18, COLOR_WHITE)));
pGraphics->AttachControl(new IBSwitchControl(b.GetCentredInside(100).GetVShifted(-200).GetHShifted(350), switchBitmap, kFlickerOn));
};
#endif
}
#if IPLUG_EDITOR
void Flicker::HideOnLightbulb(bool hide)
{
if (GetUI()->GetControlWithTag(kInFlicker)->As<IControl>()) {
GetUI()->GetControlWithTag(kInFlicker)->As<IControl>()->Hide(hide);
}
}
#endif
#if IPLUG_DSP
void Flicker::ProcessBlock(sample** inputs, sample** outputs, int nFrames)
{
const int randNum = rand() % 10000;
const int sampleRate = GetSampleRate();
// std::cout << std::to_string(randNum) << "\n";
// std::cout << sampleRate << " " << nFrames << "\n";
// std::cout << flickerTime << " " << flickerEndTime << "\n";
const double flicker = GetParam(kFlicker)->Value() * 100;
// scale milliseconds to samples
const int flickerLength = std::round(sampleRate * GetParam(kFlickerLength)->Value() / 1000.);
// if the light is on, a flicker turns the light off. if the light is off, the flicker turns the light on
const bool lightOn = GetParam(kFlickerOn)->Value();
const bool notInFlicker = flickerCountdown <= 0;
const bool playSound = (notInFlicker && lightOn) || (!notInFlicker && !lightOn);
if (notInFlicker && randNum < flicker) {
flickerCountdown = flickerLength;
}
if (GetUI()) {
HideOnLightbulb(!playSound);
}
const int nChans = NOutChansConnected();
for (int s = 0; s < nFrames; s++) {
for (int c = 0; c < nChans; c++) {
outputs[c][s] = playSound ? inputs[c][s] : 0;
}
}
// subtract number of samples from countdown
flickerCountdown = std::max((long long) 0, flickerCountdown - nFrames);
}
#endif