-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathScaleCV.cpp
More file actions
184 lines (151 loc) · 5.87 KB
/
Copy pathScaleCV.cpp
File metadata and controls
184 lines (151 loc) · 5.87 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
#include "plugin.hpp"
#include "musiclib.hpp"
struct ScaleCV : Module {
enum ParamIds {
ROOT_PARAM,
MODE_PARAM,
NUM_PARAMS
};
enum InputIds {
ROOT_INPUT,
MODE_INPUT,
ENUMS(QUANTIZER_INPUTS, 4),
NUM_INPUTS
};
enum OutputIds {
POLY_OUTPUT,
ENUMS(QUANTIZER_OUTPUTS, 4),
NUM_OUTPUTS
};
enum LightIds {
NUM_LIGHTS
};
int root_semi = 0;
int mode = 0;
ScaleCV() {
config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS);
configParam(ROOT_PARAM, -4.0, 4.0, 0.0, "Root Note");
configParam(MODE_PARAM, -4.0, 4.0, -4.0, "Mode");
configInput(ROOT_INPUT, "1V/oct pitch");
configInput(MODE_INPUT, "Mode");
configOutput(POLY_OUTPUT, "Polyphonic");
}
void process(const ProcessArgs& args) override;
};
void ScaleCV::process(const ProcessArgs &args){
float value = params[ROOT_PARAM].getValue();
if(inputs[ROOT_INPUT].isConnected()){
value = inputs[ROOT_INPUT].getVoltage();
}
float mode_val = params[MODE_PARAM].getValue();
if(inputs[MODE_INPUT].isConnected()){
mode_val = inputs[MODE_INPUT].getVoltage();
}
mode_val = clamp(mode_val,-4.0f, 2.0f);
mode = (int)floor(mode_val + 4.0f);
//quantize root note
float octave = round(value);
float semi = voltage_to_note(value);
root_semi = voltage_to_note_int(value);
int root_note = (octave + 4) * 12 + (int)semi;
//Make the scale
struct scale s = get_scale(root_note, mode);
//Quantizers
for(int t=0; t<4; t++){
if(inputs[QUANTIZER_INPUTS + t].isConnected() && outputs[QUANTIZER_OUTPUTS + t].isConnected()){
int channel_count = inputs[QUANTIZER_INPUTS + t].getChannels();
outputs[QUANTIZER_OUTPUTS + t].setChannels(channel_count);
for(int c=0; c < channel_count; c++){
float in_v = inputs[QUANTIZER_INPUTS + t].getVoltage(c);
float in_octave = round(in_v) + 4;
float in_semi = voltage_to_note(in_v);
float lowest_dist = 12.0f;
float out_note = 0.0f;
for(int i=0; i<7; i++){
float note = (float)(s.notes[i] - ((octave + 4) * 12));
float dist = abs(note - in_semi);
if(dist < lowest_dist){
out_note = note;
lowest_dist = dist;
}
//check one octave down
note -= 12;
dist = abs(note - in_semi);
if(dist < lowest_dist){
out_note = note;
lowest_dist = dist;
}
}
out_note += in_octave * 12.0f;
outputs[QUANTIZER_OUTPUTS + t].setVoltage(note_to_voltage((int)out_note), c);
}
}
}
outputs[POLY_OUTPUT].setChannels(7);
for(int t=0; t<7; t++){
outputs[POLY_OUTPUT].setVoltage(note_to_voltage(s.notes[t]),t);
}
}
struct ScaleCVWidget : ModuleWidget {
struct ChordDisplayWidget : TransparentWidget {
ScaleCV* module;
char text[13] = "";
ChordDisplayWidget(Vec _pos, Vec _size, ScaleCV* _module) {
box.size = _size;
box.pos = _pos.minus(_size.div(2));
module = _module;
}
void drawLayer(const DrawArgs& args, int layer) override {
if (layer == 1) {
std::shared_ptr<Font> font = APP->window->loadFont(asset::plugin(pluginInstance, "res/fonts/PixelOperator.ttf"));
if(font){
NVGcolor textColor = prepareDisplay(args.vg, &box, 22);
nvgFontFaceId(args.vg, font->handle);
nvgTextLetterSpacing(args.vg, -1.5);
nvgTextAlign(args.vg, NVG_ALIGN_CENTER);
Vec textPos = Vec(box.size.x/2, 21.0f);
nvgFillColor(args.vg, textColor);
if (module != NULL){
get_scale_name(module->root_semi,module->mode,text);
}else{
snprintf(text, 13, " ");
}
nvgText(args.vg, textPos.x, textPos.y, text, NULL);
}
}
Widget::drawLayer(args, layer);
}
};
ScaleCVWidget(ScaleCV* module) {
setModule(module);
setPanel(APP->window->loadSvg(asset::plugin(pluginInstance, "res/ScaleCV.svg")));
addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, 0)));
addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, 0)));
addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
const int centerX = box.size.x / 2;
ChordDisplayWidget* display = new ChordDisplayWidget(Vec(centerX, 55), Vec(box.size.x - 5, 29), module);
addChild(display);
const int offsetXL = 40;
static const int offsetX = 28;
static const int posY = 190;
static const int spacingY2 = 32;
static const int posY1 = posY + spacingY2;
static const int posY2 = posY + (spacingY2 * 2);
static const int posY3 = posY + (spacingY2 * 3);
addParam(createParamCentered<Rogan2PWhite>(Vec(centerX,95), module, ScaleCV::ROOT_PARAM));
addInput(createInputCentered<PJ301MPort>(Vec(centerX - offsetXL, 95), module, ScaleCV::ROOT_INPUT));
addParam(createParamCentered<Rogan2PWhite>(Vec(centerX,140), module, ScaleCV::MODE_PARAM));
addInput(createInputCentered<PJ301MPort>(Vec(centerX - offsetXL, 140), module, ScaleCV::MODE_INPUT));
addInput(createInputCentered<PJ301MPort>(Vec(centerX - offsetX, posY), module, ScaleCV::QUANTIZER_INPUTS + 0));
addInput(createInputCentered<PJ301MPort>(Vec(centerX - offsetX, posY1), module, ScaleCV::QUANTIZER_INPUTS + 1));
addInput(createInputCentered<PJ301MPort>(Vec(centerX - offsetX, posY2), module, ScaleCV::QUANTIZER_INPUTS + 2));
addInput(createInputCentered<PJ301MPort>(Vec(centerX - offsetX, posY3), module, ScaleCV::QUANTIZER_INPUTS + 3));
addOutput(createOutputCentered<PJ301MPort>(Vec(centerX + offsetX, posY), module, ScaleCV::QUANTIZER_OUTPUTS + 0));
addOutput(createOutputCentered<PJ301MPort>(Vec(centerX + offsetX, posY1), module, ScaleCV::QUANTIZER_OUTPUTS + 1));
addOutput(createOutputCentered<PJ301MPort>(Vec(centerX + offsetX, posY2), module, ScaleCV::QUANTIZER_OUTPUTS + 2));
addOutput(createOutputCentered<PJ301MPort>(Vec(centerX + offsetX, posY3), module, ScaleCV::QUANTIZER_OUTPUTS + 3));
addOutput(createOutputCentered<PJ301MPort>(Vec(centerX, 332), module, ScaleCV::POLY_OUTPUT));
}
};
Model* modelScaleCV = createModel<ScaleCV, ScaleCVWidget>("ScaleCV");