-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathClamp.cpp
221 lines (192 loc) · 8.53 KB
/
Clamp.cpp
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
#include "plugin.hpp"
#include "barkComponents.hpp"
using namespace barkComponents;
struct Clamp : Module {
enum ParamIds {
MAX_PARAM,
CEILING_PARAM,
MIN_PARAM,
LINKMINMAX_PARAM,
_2BY_PARAM,
GAIN_PARAM,
ENUMS(ATTENUVERT_BUTTONS, 4),
NUM_PARAMS
};
enum InputIds {
INL_INPUT,
INR_INPUT,
NUM_INPUTS
};
enum OutputIds {
OUTL_OUTPUT,
OUTR_OUTPUT,
NUM_OUTPUTS
};
enum LightIds { NUM_LIGHTS };
dsp::ClockDivider step;
float volt1, volt2, prevMax = 0.f, prevMin = 0.f;
float prevVal1, prevVal2;
Clamp() {
config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS);
configParam(MAX_PARAM, -10.f, 10.f, 10.f, "Max", "v");
configParam(MIN_PARAM, -10.f, 10.f, -10.f, "Min", "v");
configParam(_2BY_PARAM, -2.f, 2.f, 1.f, "Multiplier");
/*
defualt 0dB (1.f) == -6dB
*/
configParam(GAIN_PARAM, 0.f, 4.f, 1.f, "Input Gain", "dB", -10, 40);
//Switch---
for (int i = 0; i < 4; i++) {
configSwitch(ATTENUVERT_BUTTONS + i, 0.f, 1.f, 0.f, "Snap to", { "On", "Off" });
}
configSwitch(LINKMINMAX_PARAM, 0.f, 1.f, 1.f, "Link", { "On", "Off" });
configSwitch(CEILING_PARAM, 0.f, 1.f, 0.f, "Celing", {"Off","-0.1dB"});
//Input---
configInput(INL_INPUT, "Left");
configInput(INR_INPUT, "Right");
//Output---
configOutput(OUTL_OUTPUT, "Left");
configOutput(OUTR_OUTPUT, "Right");
step.setDivision(32);
//Route Bypass---
configBypass(INL_INPUT, OUTL_OUTPUT);
configBypass(INR_INPUT, OUTR_OUTPUT);
}//config
void process(const ProcessArgs &args) override {
bool linkParams = params[LINKMINMAX_PARAM].getValue() < 1.f;
prevVal1 = params[MAX_PARAM].getValue();
prevVal2 = params[MIN_PARAM].getValue();
//display
volt1 = params[MAX_PARAM].getValue();
volt2 = params[MIN_PARAM].getValue();
//------- one knob controls other,
if (linkParams && prevVal1 != prevMax) {
params[MIN_PARAM].setValue(-prevVal1);
} else if (linkParams && prevVal2 != prevMin) {
params[MAX_PARAM].setValue(-prevVal2);
}
prevMax = prevVal1;
prevMin = prevVal2;
//pad 0.1dB
if (params[CEILING_PARAM].getValue() > 0.f) {
volt1 = 9.94f;
volt2 = -9.94f;
} else {
volt1 = params[MAX_PARAM].getValue();
volt2 = params[MIN_PARAM].getValue();
}
params[MAX_PARAM].setValue(volt1);
params[MIN_PARAM].setValue(volt2);
//--------------------
float gain = params[GAIN_PARAM].getValue(), attenuvert = params[_2BY_PARAM].getValue(), inL, inR;
//snap atten value to knob
params[ATTENUVERT_BUTTONS + 0].getValue() == 1.f ? params[_2BY_PARAM].setValue(-1.f) : params[_2BY_PARAM].setValue(params[_2BY_PARAM].getValue());
params[ATTENUVERT_BUTTONS + 1].getValue() == 1.f ? params[_2BY_PARAM].setValue(1.f) : params[_2BY_PARAM].setValue(params[_2BY_PARAM].getValue());
params[ATTENUVERT_BUTTONS + 2].getValue() == 1.f ? params[_2BY_PARAM].setValue(-2.f) : params[_2BY_PARAM].setValue(params[_2BY_PARAM].getValue());
params[ATTENUVERT_BUTTONS + 3].getValue() == 1.f ? params[_2BY_PARAM].setValue(2.f) : params[_2BY_PARAM].setValue(params[_2BY_PARAM].getValue());
//apply pre gain and attenuvert
inL = clamp(inputs[INL_INPUT].getVoltage() * gain * attenuvert, fmin(params[MIN_PARAM].getValue(), params[MAX_PARAM].getValue()),
fmax(params[MAX_PARAM].getValue(), params[MIN_PARAM].getValue()));
inR = clamp(inputs[INR_INPUT].getVoltage() * gain * attenuvert, fmin(params[MIN_PARAM].getValue(), params[MAX_PARAM].getValue()),
fmax(params[MAX_PARAM].getValue(), params[MIN_PARAM].getValue()));
///output---
//if not connected sets outputs to offsets
inputs[INL_INPUT].isConnected() == true ?
outputs[OUTL_OUTPUT].setVoltage(inL) : outputs[OUTL_OUTPUT].setVoltage(volt1);
inputs[INL_INPUT].isConnected() == true ?
outputs[OUTR_OUTPUT].setVoltage(inR) : outputs[OUTR_OUTPUT].setVoltage(volt2);
}//process
};
//----------------------------------- Volt Display -----------------------------------
struct voltDisplayWidget : TransparentWidget {
Clamp *module;
float* value;
//std::shared_ptr<Font> font;
std::string fontPath;
voltDisplayWidget() {
//font = FONT;
fontPath = asset::plugin(pluginInstance, FONT);
}
void draw(const DrawArgs &voltDisp) override {
std::shared_ptr<Font> font = APP->window->loadFont(fontPath);
NVGcolor backgroundColor = nvgRGB(26, 26, 36);
NVGcolor borderColor = nvgRGB(0, 0, 0);
NVGcolor gradStartCol = nvgRGBA(255, 255, 244, 17);
NVGcolor gradEndCol = nvgRGBA(0, 0, 0, 15);
NVGcolor textColor = nvgRGB(63, 154, 0);
nvgBeginPath(voltDisp.vg);
nvgRoundedRect(voltDisp.vg, 0.0, 0.0, box.size.x, box.size.y, 0.75);
nvgFillColor(voltDisp.vg, backgroundColor);
nvgFill(voltDisp.vg);
nvgStrokeWidth(voltDisp.vg, 0.75);
nvgStrokeColor(voltDisp.vg, borderColor);
nvgStroke(voltDisp.vg);
nvgTextAlign(voltDisp.vg, 1 << 1);
nvgFontSize(voltDisp.vg, FONT_SIZE);
if (font) {
nvgFontFaceId(voltDisp.vg, font->handle);
}
nvgTextLetterSpacing(voltDisp.vg, LETTER_SPACING);
char display_string[9];
sprintf(display_string, "%0.4f", *value);
Vec textPos = Vec(25.364f, TEXT_POS_Y); // box.size = Vec(50.728f, 13.152f);
nvgFillColor(voltDisp.vg, nvgTransRGBA(nvgRGB(0xdf, 0xd2, 0x2c), 16));
nvgText(voltDisp.vg, textPos.x, textPos.y, "$$$$", NULL);
nvgFillColor(voltDisp.vg, nvgTransRGBA(nvgRGB(0xda, 0xe9, 0x29), 11));
nvgText(voltDisp.vg, textPos.x, textPos.y, "##.##", NULL);
nvgFillColor(voltDisp.vg, textColor);
nvgText(voltDisp.vg, textPos.x, textPos.y, display_string, NULL);
//---------Gradient Screen
nvgRoundedRect(voltDisp.vg, 0.f, 0.f, box.size.x, box.size.y, .75f);
//(startX,startY)-(endX,endY) should be the reverse of inkscape coordinates
float gradHeight = 12.728f;
nvgFillPaint(voltDisp.vg, nvgLinearGradient(voltDisp.vg, 71.5f, gradHeight - 4.98f, 70.61f,
gradHeight - 10.11f, gradStartCol, gradEndCol));
nvgFill(voltDisp.vg);
}
};
//----------------------------------- Volt Display -----------------------------------
struct ClampWidget : ModuleWidget {
ClampWidget(Clamp *module) {
setModule(module);
setPanel(APP->window->loadSvg(asset::plugin(pluginInstance, "res/BarkClamp.svg")));
constexpr float portLX = 4.11f, portRX = 31.67f, inY = 187.78f, outY = 60.18f,
att1xPos[2] = { 4.77f, 44.48f }, att2xPos[2] = { 15.34f, 34.15f };
///Ports---
//Out---
addOutput(createOutput<BarkOutPort350>(Vec(portLX, rackY - outY), module, Clamp::OUTL_OUTPUT));
addOutput(createOutput<BarkOutPort350>(Vec(portRX, rackY - outY), module, Clamp::OUTR_OUTPUT));
//In---
addInput(createInput<BarkInPort350>(Vec(portLX, rackY - inY), module, Clamp::INL_INPUT));
addInput(createInput<BarkInPort350>(Vec(portRX, rackY - inY), module, Clamp::INR_INPUT));
//Knobs---
addParam(createParam<BarkKnob_40s>(Vec(14.677f, rackY - 333.8f), module, Clamp::MAX_PARAM));
addParam(createParam<BarkKnob_40s>(Vec(14.677f, rackY - 259.8f), module, Clamp::MIN_PARAM));
addParam(createParam<BarkKnob_20>(Vec(20.f, rackY - 155.16f), module, Clamp::_2BY_PARAM));
addParam(createParam<BarkKnob_40>(Vec(10.f, rackY - 119.14f), module, Clamp::GAIN_PARAM));
//Switch---
addParam(createParam<BarkSwitchSmallSide>(Vec(26.68f, rackY - 283.7f), module, Clamp::LINKMINMAX_PARAM));
//button---
addParam(createParam<BarkPushButton1>(Vec(2.42f, rackY - 334.44f), module, Clamp::CEILING_PARAM));
addParam(createParam<BarkPushButton1>(Vec(att1xPos[0], rackY - 146.75f), module, Clamp::ATTENUVERT_BUTTONS + 0));
addParam(createParam<BarkPushButton1>(Vec(att1xPos[1], rackY - 146.75f), module, Clamp::ATTENUVERT_BUTTONS + 1));
addParam(createParam<BarkPushButton1>(Vec(att2xPos[0], rackY - 132.07f), module, Clamp::ATTENUVERT_BUTTONS + 2));
addParam(createParam<BarkPushButton1>(Vec(att2xPos[1], rackY - 132.07f), module, Clamp::ATTENUVERT_BUTTONS + 3));
//screw---
//screw---
addChild(createWidget<RandomRotateScrew>(Vec(box.size.x - 12.3f, 2.7f))); //pos2
addChild(createWidget<RandomRotateScrew>(Vec(2.7, 367.7f))); //pos3
//Display---
if (module != NULL) {
voltDisplayWidget *maxVolt = createWidget<voltDisplayWidget>(Vec(4.61f, rackY - 355.65f));
maxVolt->box.size = Vec(50.728f, 13.152f);
maxVolt->value = &module->volt1;
addChild(maxVolt);
voltDisplayWidget *minVolt = createWidget<voltDisplayWidget>(Vec(4.61f, rackY - 210.51f));
minVolt->box.size = Vec(50.728f, 13.152f);
minVolt->value = &module->volt2;
addChild(minVolt);
}
}
};
Model *modelClamp = createModel<Clamp, ClampWidget>("Clamp");