-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmercury.cpp
578 lines (471 loc) · 17.6 KB
/
mercury.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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#include "daisy_petal.h"
#include "daisysp.h"
#include "funbox.h"
#include <RTNeural/RTNeural.h>
#include <q/fx/biquad.hpp>
#include "wavenet/wavenet_model.hpp"
#include "expressionHandler.h"
// Model Weights (edit this file to add model weights trained with Colab script)
#include "model_data_nam.h"
using namespace daisy;
using namespace daisysp;
using namespace funbox; // This is important for mapping the correct controls to the Daisy Seed on Funbox PCB
// Declare a local daisy_petal for hardware access
DaisyPetal hw;
Parameter gain, level, presence, bass, mid, treble, expression;
bool bypass;
int modelIndex = 0;
int m_currentModelindex = -1;
bool pswitch1[2], pswitch2[2], pswitch3[2], pdip[4];
int switch1[2], switch2[2], switch3[2], dip[4];
float nnLevelAdjust;
bool eqOn = true;
float knobValues[6]; // Moved to global
int toggleValues[3];
bool dipValues[4];
// Midi
bool midi_control[6]; // just knobs for now
float pknobValues[6]; // Used for Midi control logic
float dryMix, wetMix;
bool silence_output = false;
float setPopReduce, popReduce;
Led led1, led2;
// Expression
ExpressionHandler expHandler;
bool expression_pressed;
struct NAMMathsProvider
{
#if RTNEURAL_USE_EIGEN
template <typename Matrix>
static auto tanh (const Matrix& x)
{
// See: math_approx::tanh<3>
const auto x_poly = x.array() * (1.0f + 0.183428244899f * x.array().square());
return x_poly.array() * (x_poly.array().square() + 1.0f).array().rsqrt();
//return x.array().tanh(); // Tried using Eigen's built in tanh(), also works, failed on the same larger models as above custom tanh
}
#elif RTNEURAL_USE_XSIMD
template <typename T>
static T tanh (const T& x)
{
return math_approx::tanh<3> (x);
}
#endif
};
constexpr uint8_t NUM_FILTERS_NAM = 4;
const float minGain = -10.f;
const float maxGain = 10.f;
const float centerFrequencyNam[NUM_FILTERS_NAM] = {180.f, 1200.f, 4000.f, 8000.f}; // Experiment with these freqs and q values
const float q_nam[NUM_FILTERS_NAM] = {0.7f, 0.6f, 0.5f, 0.5f};
cycfi::q::peaking filter_nam[NUM_FILTERS_NAM] = {{0, centerFrequencyNam[0], 48000, q_nam[0]}, {0, centerFrequencyNam[1], 48000, q_nam[1]}, {0, centerFrequencyNam[2], 48000, q_nam[2]}, {0, centerFrequencyNam[3], 48000, q_nam[3]}};
// NOTE NAM "Pico" (unnoficial model type)
// This is the same as the Nano models, except the number of channels on the first layer is 2 instead of 4 (and the input size on the 2nd layer is 2 instead of 4 also)
using Dilations = wavenet::Dilations<1, 2, 4, 8, 16, 32, 64>;
using Dilations2 = wavenet::Dilations<128, 256, 512, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512>;
wavenet::Wavenet_Model<float,
1,
wavenet::Layer_Array<float, 1, 1, 2, 2, 3, Dilations, false, NAMMathsProvider>,
wavenet::Layer_Array<float, 2, 1, 1, 2, 3, Dilations2, true, NAMMathsProvider>>
rtneural_wavenet;
bool knobMoved(float old_value, float new_value)
{
float tolerance = 0.005;
if (new_value > (old_value + tolerance) || new_value < (old_value - tolerance)) {
return true;
} else {
return false;
}
}
void SelectModel()
{
if (m_currentModelindex != modelIndex) {
rtneural_wavenet.load_weights (model_collection_nam[modelIndex].weights);
static constexpr size_t N = 1; // number of samples sent through model at once
rtneural_wavenet.prepare (N); // This is needed, including this allowed the led to come on before freezing
rtneural_wavenet.prewarm(); // Note: looks like this just sends some 0's through the model
m_currentModelindex = modelIndex;
}
}
void updateSwitch1or2()
{
if (toggleValues[0] == 0) { // low gain models
if (toggleValues[1] == 0) {
modelIndex = 0;
nnLevelAdjust = 1.3;
} else if (toggleValues[1] == 1) {
modelIndex = 1;
nnLevelAdjust = 1.6;
} else {
modelIndex = 2;
nnLevelAdjust = 1.1;
}
} else if (toggleValues[0] == 1) { // med gain models
if (toggleValues[1] == 0) {
modelIndex = 3;
nnLevelAdjust = 1.0;
} else if (toggleValues[1] == 1) {
modelIndex = 4;
nnLevelAdjust = 1.0;
} else {
modelIndex = 5;
nnLevelAdjust = 0.7;
}
} else { // high gain models
if (toggleValues[1] == 0) {
modelIndex = 6;
nnLevelAdjust = 0.9;
} else if (toggleValues[1] == 1) {
modelIndex = 7;
nnLevelAdjust = 0.9;
} else {
modelIndex = 8;
nnLevelAdjust = 0.9;
}
}
silence_output = true;
popReduce = 1.0;
setPopReduce = 0.0;
//SelectModel();
}
void updateSwitch3()
{
if (toggleValues[2] == 0) {
} else if (toggleValues[2] == 2) {
} else {
}
}
void UpdateButtons()
{
// (De-)Activate bypass and toggle LED when left footswitch is let go, or enable/disable amp if held for greater than 1 second //
// Can only disable/enable amp when not in bypass mode
if(hw.switches[Funbox::FOOTSWITCH_1].FallingEdge())
{
if (!expression_pressed) {
bypass = !bypass;
led1.Set(bypass ? 0.0f : 1.0f);
}
expression_pressed = false;
}
// Toggle Expression mode by holding down both footswitches for half a second
if(hw.switches[Funbox::FOOTSWITCH_1].TimeHeldMs() >= 500 && hw.switches[Funbox::FOOTSWITCH_2].TimeHeldMs() >= 500 && !expression_pressed ) {
expHandler.ToggleExpressionSetMode();
if (expHandler.isExpressionSetMode()) {
led1.Set(expHandler.returnLed1Brightness()); // Dim LEDs in expression set mode
led2.Set(expHandler.returnLed2Brightness()); // Dim LEDs in expression set mode
} else {
led1.Set(bypass ? 0.0f : 1.0f);
led2.Set(0.0f);
}
expression_pressed = true; // Keeps it from switching over and over while held
}
// Clear Expression settings by holding down both footswitches for 2 seconds
if(hw.switches[Funbox::FOOTSWITCH_1].TimeHeldMs() >= 2000 && hw.switches[Funbox::FOOTSWITCH_2].TimeHeldMs() >= 2000) {
expHandler.Reset();
led1.Set(bypass ? 0.0f : 1.0f);
led2.Set(0.0f);
}
led1.Update();
led2.Update();
}
void UpdateSwitches()
{
// Detect any changes in switch positions
// 3-way Switch 1
bool changed1 = false;
for(int i=0; i<2; i++) {
if (hw.switches[switch1[i]].Pressed() != pswitch1[i]) {
pswitch1[i] = hw.switches[switch1[i]].Pressed();
changed1 = true;
}
}
if (changed1) { // update_switches is for turning off preset
if (pswitch1[0] == true) {
toggleValues[0] = 0;
} else if (pswitch1[1] == true) {
toggleValues[0] = 2;
} else {
toggleValues[0] = 1;
}
updateSwitch1or2();
}
// 3-way Switch 2
bool changed2 = false;
for(int i=0; i<2; i++) {
if (hw.switches[switch2[i]].Pressed() != pswitch2[i]) {
pswitch2[i] = hw.switches[switch2[i]].Pressed();
changed2 = true;
}
}
if (changed2) {
if (pswitch2[0] == true) {
toggleValues[1] = 0;
} else if (pswitch2[1] == true) {
toggleValues[1] = 2;
} else {
toggleValues[1] = 1;
}
updateSwitch1or2();
}
// 3-way Switch 3
bool changed3 = false;
for(int i=0; i<2; i++) {
if (hw.switches[switch3[i]].Pressed() != pswitch3[i]) {
pswitch3[i] = hw.switches[switch3[i]].Pressed();
changed3 = true;
}
}
if (changed3) {
if (pswitch3[0] == true) {
toggleValues[2] = 0;
} else if (pswitch3[1] == true) {
toggleValues[2] = 2;
} else {
toggleValues[2] = 1;
}
updateSwitch3();
}
// Dip switches
bool changed4 = false;
for(int i=0; i<4; i++) {
if (hw.switches[dip[i]].Pressed() != pdip[i]) {
pdip[i] = hw.switches[dip[i]].Pressed();
dipValues[i] = pdip[i]; // TODO Look into consolidating logic for dipValues, pdip, etc (this is for preset saving)
bool changed4 = true;
// Action for dipswitches handled in audio callback
}
}
// Update if preset turned off
if (changed4) {
for (int i=0; i<4; i++) {
dipValues[i] = pdip[i]; // TODO Check logic here
}
}
}
// This runs at a fixed rate, to prepare audio samples
static void AudioCallback(AudioHandle::InputBuffer in,
AudioHandle::OutputBuffer out,
size_t size)
{
//hw.ProcessAllControls();
hw.ProcessAnalogControls();
hw.ProcessDigitalControls();
UpdateButtons();
UpdateSwitches();
// Knob and Expression Processing ////////////////////
float newExpressionValues[6];
// Knob 1
if (!midi_control[0]) // If not under midi control, use knob ADC
pknobValues[0] = knobValues[0] = gain.Process();
else if (knobMoved(pknobValues[0], gain.Process())) // If midi controlled, watch for knob movement to end Midi control
midi_control[0] = false;
// Knob 2
if (!midi_control[1]) // If not under midi control, use knob ADC
pknobValues[1] = knobValues[1] = level.Process();
else if (knobMoved(pknobValues[1], level.Process())) // If midi controlled, watch for knob movement to end Midi control
midi_control[1] = false;
// Knob 3
if (!midi_control[2]) // If not under midi control, use knob ADC
pknobValues[2] = knobValues[2] = presence.Process();
else if (knobMoved(pknobValues[2], presence.Process())) // If midi controlled, watch for knob movement to end Midi control
midi_control[2] = false;
// Knob 4
if (!midi_control[3]) // If not under midi control, use knob ADC
pknobValues[3] = knobValues[3] = bass.Process();
else if (knobMoved(pknobValues[3], bass.Process())) // If midi controlled, watch for knob movement to end Midi control
midi_control[3] = false;
// Knob 5
if (!midi_control[4]) // If not under midi control, use knob ADC
pknobValues[4] = knobValues[4] = mid.Process();
else if (knobMoved(pknobValues[4], mid.Process())) // If midi controlled, watch for knob movement to end Midi control
midi_control[4] = false;
// Knob 6
if (!midi_control[5]) // If not under midi control, use knob ADC
pknobValues[5] = knobValues[5] = treble.Process();
else if (knobMoved(pknobValues[5], treble.Process())) // If midi controlled, watch for knob movement to end Midi control
midi_control[5] = false;
float vexpression = expression.Process(); // 0 is heel (up), 1 is toe (down)
expHandler.Process(vexpression, knobValues, newExpressionValues);
// If in expression set mode, set LEDS accordingly
if (expHandler.isExpressionSetMode()) {
led1.Set(expHandler.returnLed1Brightness());
led2.Set(expHandler.returnLed2Brightness());
}
float vgain = newExpressionValues[0];
float vlevel = newExpressionValues[1];
float vpresence = newExpressionValues[2] * 20.0 - 10.0; // Make eq control range from -10 to +10 dB
float vbass = newExpressionValues[3] * 20.0 - 10.0;
float vmid = newExpressionValues[4] * 20.0 - 10.0;
float vtreble = newExpressionValues[5] * 20.0 - 10.0;
// Order of effects is:
// Gain -> Neural Model -> Tone ->
//
// Bass, Mid, Treble, Presence
filter_nam[0].config(vbass, centerFrequencyNam[0], 48000, q_nam[0]);
filter_nam[1].config(vmid, centerFrequencyNam[1], 48000, q_nam[1]);
filter_nam[2].config(vtreble, centerFrequencyNam[2], 48000, q_nam[2]);
filter_nam[3].config(vpresence, centerFrequencyNam[3], 48000, q_nam[3]);
float input_arr[1] = { 0.0 }; // Neural Net Input
for(size_t i = 0; i < size; i++)
{
// Process your signal here
if(bypass)
{
out[0][i] = in[0][i];
out[1][i] = in[0][i];
}
else
{
float ampOut = 0.0;
input_arr[0] = in[0][i] * vgain;
if (silence_output) { // Don't process while switching models, else bad sound
fonepole(popReduce, setPopReduce, .0002f);
if (popReduce < 0.0003 && setPopReduce == 0.0) {
SelectModel();
setPopReduce = 1.0;
}
if (popReduce > 0.99 && setPopReduce == 1.0) {
popReduce = 1.0;
silence_output = false;
}
}
if (setPopReduce == 1.0) // If the model is finished changing, process neural net
ampOut = rtneural_wavenet.forward (input_arr[0]) * 0.4 * nnLevelAdjust; // TODO Try try sending a block at a time, possible speed improvement
// Apply 4 band EQ
if (eqOn) {
for (uint8_t i = 0; i < NUM_FILTERS_NAM; i++) {
ampOut = filter_nam[i](ampOut);
}
}
out[0][i] = out[1][i] = ampOut * vlevel * popReduce;
}
}
}
// Typical Switch case for Message Type.
void HandleMidiMessage(MidiEvent m)
{
switch(m.type)
{
case NoteOn:
{
//led2.Set(1.0); // TODO Simple test to see if midi note is detected
//led2.Update();
NoteOnEvent p = m.AsNoteOn();
// This is to avoid Max/MSP Note outs for now..
if(m.data[1] != 0)
{
p = m.AsNoteOn();
// Do stuff with the midi Note/Velocity info here
//osc.SetFreq(mtof(p.note));
//osc.SetAmp((p.velocity / 127.0f));
}
}
break;
case ControlChange:
{
ControlChangeEvent p = m.AsControlChange();
switch(p.control_number)
{
case 14:
midi_control[0] = true;
knobValues[0] = ((float)p.value / 127.0f);
break;
case 15:
midi_control[1] = true;
knobValues[1] = ((float)p.value / 127.0f);
break;
case 16:
midi_control[2] = true;
knobValues[2] = ((float)p.value / 127.0f);
break;
case 17:
midi_control[3] = true;
knobValues[3] = ((float)p.value / 127.0f);
break;
case 18:
midi_control[4] = true;
knobValues[4] = ((float)p.value / 127.0f);
break;
case 19:
midi_control[5] = true;
knobValues[5] = ((float)p.value / 127.0f);
break;
default: break;
}
break;
}
default: break;
}
}
int main(void)
{
float samplerate;
hw.Init();
samplerate = hw.AudioSampleRate();
setupWeightsNam();
hw.SetAudioBlockSize(48);
switch1[0]= Funbox::SWITCH_1_LEFT;
switch1[1]= Funbox::SWITCH_1_RIGHT;
switch2[0]= Funbox::SWITCH_2_LEFT;
switch2[1]= Funbox::SWITCH_2_RIGHT;
switch3[0]= Funbox::SWITCH_3_LEFT;
switch3[1]= Funbox::SWITCH_3_RIGHT;
dip[0]= Funbox::SWITCH_DIP_1;
dip[1]= Funbox::SWITCH_DIP_2;
dip[2]= Funbox::SWITCH_DIP_3;
dip[3]= Funbox::SWITCH_DIP_4;
pswitch1[0]= true;
pswitch1[1]= true;
pswitch2[0]= true;
pswitch2[1]= true;
pswitch3[0]= true;
pswitch3[1]= true;
pdip[0]= true;
pdip[1]= true;
pdip[2]= true;
pdip[3]= true;
setupWeightsNam(); // in the model data nam .h file
//updateSwitch1or2();
SelectModel();
setPopReduce = 1.0;
popReduce = 1.0;
filter_nam[0].config(0.0, centerFrequencyNam[0], samplerate, q_nam[0]);
filter_nam[1].config(0.0, centerFrequencyNam[1], samplerate, q_nam[1]);
filter_nam[2].config(0.0, centerFrequencyNam[2], samplerate, q_nam[2]);
gain.Init(hw.knob[Funbox::KNOB_1], 0.1f, 2.5f, Parameter::LINEAR);
level.Init(hw.knob[Funbox::KNOB_2], 0.0f, 1.0f, Parameter::LINEAR);
presence.Init(hw.knob[Funbox::KNOB_3], 0.0f, 1.0f, Parameter::LINEAR);
bass.Init(hw.knob[Funbox::KNOB_4], 0.0f, 1.0f, Parameter::LINEAR);
mid.Init(hw.knob[Funbox::KNOB_5], 0.0f, 1.0f, Parameter::LINEAR);
treble.Init(hw.knob[Funbox::KNOB_6], 0.0f, 1.0f, Parameter::LINEAR);
expression.Init(hw.expression, 0.0f, 1.0f, Parameter::LINEAR); // TODO Make sure this is the correct way to reference expression
// Initialize the correct model
modelIndex = 0;
nnLevelAdjust = 1.0; // TODO Use level adjust to get model volumes even
// Expression
expHandler.Init(6);
expression_pressed = false;
// Midi
for( int i = 0; i < 6; ++i )
midi_control[i] = false; // Is this needed? or does it default to false
// index for midi_control: 0-5 knobs
// Init the LEDs and set activate bypass
led1.Init(hw.seed.GetPin(Funbox::LED_1),false);
led1.Update();
bypass = true;
led2.Init(hw.seed.GetPin(Funbox::LED_2),false);
led2.Update();
hw.InitMidi();
hw.midi.StartReceive();
hw.StartAdc();
hw.StartAudio(AudioCallback);
while(1)
{
hw.midi.Listen();
// Handle MIDI Events
while(hw.midi.HasEvents())
{
HandleMidiMessage(hw.midi.PopEvent());
}
System::Delay(100);
}
}