-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKVerb.cpp
361 lines (303 loc) · 10.6 KB
/
KVerb.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
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
#include "daisysp.h"
#include "kxmx_bluemchen/src/kxmx_bluemchen.h"
#include <string.h>
using namespace kxmx;
using namespace daisy;
using namespace daisysp;
Bluemchen bluemchen;
static ReverbSc verb;
static DcBlock blk[2];
Parameter knob1;
Parameter knob2;
Parameter cv1;
Parameter cv2;
enum Params {
DRY,
WET,
LPF,
HPF,
FEED,
};
/* Store for CV and Knob values*/
float cv_values[4] = {0, 0, 0, 0};
// values for each parameter
float param_values[5] = {0, 0, 0, 0, 0};
/* value for the menu that is showing on screen
0 = main
1 = parameter
2 = mapping
*/
int currentMenu = 0;
/* value for the currently selected parameter
index of parameter_strings
*/
int currentParam = 0;
/* value for the currently selected mapping
index of mapping_strings
*/
int currentMapping = 0;
int mappingMenuSelection = 0;
bool editing = false;
// tracked whether the menu was already swapped with the current encoder press
bool menuSwapped = false;
/* variables for CV settings menu */
std::string parameter_strings[5] {"dry", "wet", "LPF", "HPF", "feed"};
std::string mapping_strings[5] {"bias", "Pot1", "Pot2", "CV1", "CV2"};
std::string sign_strings[3] {"-", "0", "+"};
std::string multiplier_strings[5] {"/4", "/2", "x1", "x2", "x4"};
float bias_limits[5][3] = {
// min, max, increment
{-1, 1, 0.1}, // dry
{-1, 1, 0.1}, // wet
{-1, 1, 0.1}, // LPF
{-1, 1, 0.1}, // HPF
{-1, 1, 0.1}, // feedback
};
float biases[5] = {0, 0, 0, 0, 0};
int mapping_indices[5][8] = {
// Pot1 sign, Pot1 multiplier, Pot2 sign, Pot2 multiplier, CV1 sign, CV1 multiplier, CV2 sign, CV2 multiplier
{1, 2, 1, 2, 1, 2, 1, 2}, // dry
{1, 2, 1, 2, 1, 2, 1, 2}, // wet
{1, 2, 1, 2, 1, 2, 1, 2}, // LPF
{1, 2, 1, 2, 1, 2, 1, 2}, // HPF
{1, 2, 1, 2, 1, 2, 1, 2}, // feedback
};
/* Value of the encoder */
int enc_val = 0;
void drawParamVisual(int index, int x, int y) {
bluemchen.display.DrawRect(x, y, x+std::min(int(param_values[index]*10), 10), y+8, true, true);
}
void MainMenu() {
bluemchen.display.SetCursor(0, 0);
std::string str = " KVERB";
char *cstr = &str[0];
bluemchen.display.WriteString(cstr, Font_6x8, true);
// draw up to 3 of the options, starting with the one before the current selection
int firstOptionToDraw = std::min(std::max(currentParam - 1, 0), 2);
for(int p = firstOptionToDraw; p < 5 && p-firstOptionToDraw < 4; p++){
if (p == currentParam) {
bluemchen.display.SetCursor(0, 8*(1+p-firstOptionToDraw));
str = ">";
bluemchen.display.WriteString(cstr, Font_6x8, true);
}
bluemchen.display.SetCursor(6, 8*(1+p-firstOptionToDraw));
str = parameter_strings[p];
bluemchen.display.WriteString(cstr, Font_6x8, true);
drawParamVisual(p, 36, 8*(1+p-firstOptionToDraw));
}
}
void ParameterMenu() {
bluemchen.display.SetCursor(0, 0);
std::string str = parameter_strings[currentParam];
char *cstr = &str[0];
bluemchen.display.WriteString(cstr, Font_6x8, true);
// draw up to 3 of the options, starting with the one before the current selection
int firstOptionToDraw = std::min(std::max(currentMapping - 1, 0), 2);
for(int p = firstOptionToDraw; p < 5 && p-firstOptionToDraw < 4; p++){
if (p == currentMapping) {
bluemchen.display.SetCursor(0, 8*(1+p-firstOptionToDraw));
str = ">";
bluemchen.display.WriteString(cstr, Font_6x8, true);
}
bluemchen.display.SetCursor(6, 8*(1+p-firstOptionToDraw));
str = mapping_strings[p];
bluemchen.display.WriteString(cstr, Font_6x8, true);
}
}
void MappingMenu() {
bluemchen.display.SetCursor(0, 0);
std::string str = parameter_strings[currentParam];
char *cstr = &str[0];
bluemchen.display.WriteString(cstr, Font_6x8, true);
bluemchen.display.SetCursor(6, 8);
str = mapping_strings[currentMapping];
bluemchen.display.WriteString(cstr, Font_6x8, true);
if (currentMapping == 0) {
// bias mapping
bluemchen.display.SetCursor(6, 16);
str = std::to_string(biases[currentParam]);
bluemchen.display.WriteString(cstr, Font_6x8, true);
}
else {
// CV or pot mapping
bluemchen.display.SetCursor(0, 16 + 8*mappingMenuSelection);
str = ">";
bluemchen.display.WriteString(cstr, Font_6x8, true);
bool inverted = editing && mappingMenuSelection == 0;
if (inverted) {
bluemchen.display.DrawRect(11, 16, 17, 22, true, true);
}
bluemchen.display.SetCursor(12, 16);
str = sign_strings[mapping_indices[currentParam][(currentMapping-1)*2]];
bluemchen.display.WriteString(cstr, Font_6x8, !inverted);
inverted = editing && mappingMenuSelection == 1;
if (inverted) {
bluemchen.display.DrawRect(11, 24, 17, 32, true, true);
}
bluemchen.display.SetCursor(12, 24);
str = multiplier_strings[mapping_indices[currentParam][(currentMapping-1)*2+1]];
bluemchen.display.WriteString(cstr, Font_6x8, !inverted);
}
}
void UpdateOled() {
bluemchen.display.Fill(false);
switch (currentMenu) {
case 0:
MainMenu();
break;
case 1:
ParameterMenu();
break;
case 2:
MappingMenu();
break;
}
bluemchen.display.Update();
}
void processEncoder() {
if (!menuSwapped && bluemchen.encoder.Pressed()) {
if (bluemchen.encoder.TimeHeldMs() > 500) {
// long press
currentMenu = std::max(currentMenu - 1, 0);
menuSwapped = true;
}
}
if (bluemchen.encoder.FallingEdge()) {
if (!menuSwapped) {
// short press
if (currentMenu == 2 && currentMapping != 0) {
editing = !editing;
}
else {
currentMenu = std::min(currentMenu + 1, 2);
}
}
menuSwapped = false;
}
switch (currentMenu) {
case 0:
// main menu
currentParam = std::min(std::max(int(currentParam+bluemchen.encoder.Increment()), 0), 4);
break;
case 1:
// parameter menu
currentMapping = std::min(std::max(int(currentMapping+bluemchen.encoder.Increment()), 0), 4);
break;
case 2:
// mapping menu
if (currentMapping == 0) {
biases[currentParam] = std::min(std::max(
biases[currentParam] + bias_limits[currentParam][2] * bluemchen.encoder.Increment(),
bias_limits[currentParam][0]),
bias_limits[currentParam][1]);
}
else {
if (editing) {
mapping_indices[currentParam][mappingMenuSelection+(currentMapping-1)*2] = std::min(std::max(
int(mapping_indices[currentParam][mappingMenuSelection+(currentMapping-1)*2] + bluemchen.encoder.Increment()),
0),
mappingMenuSelection == 0 ? 2 : 4);
}
else {
mappingMenuSelection = std::min(std::max(int(mappingMenuSelection + bluemchen.encoder.Increment()), 0), 1);
}
}
break;
}
}
float calculateMappingEffect(int controlIndex, int mappingIndex) {
switch (mappingIndex) {
// "/4", "/2", "x1", "x2", "x4"
case 0:
return cv_values[controlIndex] / 4.0f;
case 1:
return cv_values[controlIndex] / 2.0f;
case 2:
return cv_values[controlIndex];
case 3:
return cv_values[controlIndex] * 2.0f;
case 4:
return cv_values[controlIndex] * 4.0f;
}
return 0.0f;
}
void calculateValues() {
for (int p = 0; p < 5; p++) {
param_values[p] = biases[p];
for (int cv = 0; cv < 4; cv ++) {
switch (mapping_indices[p][cv*2]) {
case 0:
// negative mapping
param_values[p] -= calculateMappingEffect(cv, mapping_indices[p][cv*2+1]);
break;
case 1:
// no mapping
break;
case 2:
// positive mapping
param_values[p] += calculateMappingEffect(cv, mapping_indices[p][cv*2+1]);
break;
}
}
// clamp all values between 0 and 1
param_values[p] = std::min(std::max(param_values[p], 0.0f), 1.0f);
}
}
void UpdateControls() {
bluemchen.ProcessAllControls();
cv_values[0] = knob1.Process();
cv_values[1] = knob2.Process();
cv_values[2] = cv1.Process();
cv_values[3] = cv2.Process();
calculateValues();
processEncoder();
}
void AudioCallback(AudioHandle::InputBuffer in, AudioHandle::OutputBuffer out, size_t size) {
float dryL, dryR, wetL, wetR, sendL, sendR;
bluemchen.ProcessAnalogControls();
for(size_t i = 0; i < size; i++) {
verb.SetFeedback(param_values[FEED]);
// transform the LPF value from a 0-1 range to exponential hertz
float lpf_freq = param_values[LPF] * 100.0f;
lpf_freq = lpf_freq * lpf_freq * 2.0f;
verb.SetLpFreq(lpf_freq);
// Read Inputs (only stereo in are used)
dryL = in[0][i];
dryR = in[1][i];
// Send Signal to Reverb
sendL = dryL * param_values[WET];
sendR = dryR * param_values[WET];
verb.Process(sendL, sendR, &wetL, &wetR);
// Dc Block
wetL = blk[0].Process(wetL);
wetR = blk[1].Process(wetR);
// Out 1 and 2 are Mixed
out[0][i] = (dryL * param_values[DRY]) + wetL;
out[1][i] = (dryR * param_values[DRY]) + wetR;
// Out 3 and 4 are just wet
out[2][i] = wetL;
out[3][i] = wetR;
}
UpdateControls();
}
int main(void) {
float samplerate;
bluemchen.Init();
samplerate = bluemchen.AudioSampleRate();
verb.Init(samplerate);
verb.SetFeedback(param_values[FEED]);
verb.SetLpFreq(param_values[LPF]);
knob1.Init(bluemchen.controls[bluemchen.CTRL_1], 0.0f, 1.0f, Parameter::LINEAR);
knob2.Init(bluemchen.controls[bluemchen.CTRL_2], 0.0f, 1.0f, Parameter::LINEAR);
cv1.Init(bluemchen.controls[bluemchen.CTRL_3], -1.0f, 1.0f, Parameter::LINEAR);
cv2.Init(bluemchen.controls[bluemchen.CTRL_4], -1.0f, 1.0f, Parameter::LINEAR);
blk[0].Init(samplerate);
blk[1].Init(samplerate);
bluemchen.StartAdc();
bluemchen.StartAudio(AudioCallback);
while (1)
{
UpdateControls();
UpdateOled();
}
}