-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmars.cpp
662 lines (517 loc) · 20 KB
/
mars.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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
#include "daisy_petal.h"
#include "daisysp.h"
#include "funbox.h"
#include <RTNeural/RTNeural.h> // NOTE: Need to use older version of RTNeural, same as GuitarML/Seed
#include "delayline_2tap.h"
#include "ImpulseResponse/ImpulseResponse.h"
#include "expressionHandler.h"
// Model Weights (edit this file to add model weights trained with Colab script)
// The models must be GRU (gated recurrent unit) with hidden size = 9, snapshot models (not condidtioned on a parameter)
#include "all_model_data_gru9_4count.h"
#include "ImpulseResponse/ir_data.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, Mix, filter, delayTime, delayFdbk, expression;
bool bypass;
int modelInSize;
unsigned int modelIndex;
bool pswitch1[2], pswitch2[2], pswitch3[2], pdip[4];
int switch1[2], switch2[2], switch3[2], dip[4];
float nnLevelAdjust;
int indexMod;
float knobValues[6]; // Moved to global
int toggleValues[3];
bool dipValues[4];
float dryMix, wetMix;
Led led1, led2;
float mix_effects;
float vfilter;
Tone tone; // Low Pass
ATone toneHP; // High Pass
Balance bal; // Balance for volume correction in filtering
// Impulse Response
ImpulseResponse mIR;
int m_currentIRindex;
// Expression
ExpressionHandler expHandler;
bool expression_pressed;
// Delay Max Definitions (Assumes 48kHz samplerate)
#define MAX_DELAY static_cast<size_t>(48000.0f * 2.f)
DelayLine2Tap<float, MAX_DELAY> DSY_SDRAM_BSS delayLine;
// Delay with dotted eighth and triplett options
struct delay
{
DelayLine2Tap<float, MAX_DELAY> *del;
float currentDelay;
float delayTarget;
float feedback = 0.0;
float active = false;
float level = 1.0; // Level multiplier of output
bool secondTapOn = false;
float Process(float in)
{
//set delay times
fonepole(currentDelay, delayTarget, .0002f);
del->SetDelay(currentDelay);
float read = del->Read();
float secondTap = 0.0;
if (secondTapOn) {
secondTap = del->ReadSecondTap();
}
if (active) {
del->Write((feedback * read) + in);
} else {
del->Write(feedback * read); // if not active, don't write any new sound to buffer
}
return (read + secondTap) * level;
}
};
delay delay1;
// Neural Network Model
// Currently only using snapshot models, they tend to sound better and
// we can use input level as gain.
RTNeural::ModelT<float, 1, 1,
RTNeural::GRULayerT<float, 1, 9>,
RTNeural::DenseT<float, 9, 1>> model;
// Notes: With default settings, GRU 10 is max size currently able to run on Daisy Seed
// - Parameterized 1-knob GRU 10 is max, GRU 8 with effects is max
// - Parameterized 2-knob/3-knob at GRU 8 is max
// - With multi effect (reverb, etc.) added GRU 9 is recommended to allow room for processing of other effects
// - These models should be trained using 48kHz audio data, since Daisy uses 48kHz by default.
// Models trained with other samplerates, or running Daisy at a different samplerate will sound different.
//TODO for preset, currently the pedal freezes if the preset hasnt been saved to QPSI before on the Daisy Seed, fix
//Setting Struct containing parameters we want to save to flash
// Using the persistent storage example found on the Daisy Forum:
// https://forum.electro-smith.com/t/saving-values-to-flash-memory-using-persistentstorage-class-on-daisy-pod/4306
struct Settings {
float knobs[6];
int toggles[3];
bool dips[4];
//Overloading the != operator
//This is necessary as this operator is used in the PersistentStorage source code
bool operator!=(const Settings& a) const {
return !(a.knobs[0]==knobs[0] && a.knobs[1]==knobs[1] && a.knobs[2]==knobs[2] && a.knobs[3]==knobs[3] && a.knobs[4]==knobs[4] && a.knobs[5]==knobs[5] && a.toggles[0]==toggles[0] && a.toggles[1]==toggles[1] && a.toggles[2]==toggles[2] && a.dips[0]==dips[0] && a.dips[1]==dips[1] && a.dips[2]==dips[2] && a.dips[3]==dips[3]);
//return !(a.p1==p1 && a.p2==p2 && a.p3==p3 && a.p4==p4 && a.p5==p5 && a.p6==p6);
}
};
//Persistent Storage Declaration. Using type Settings and passed the devices qspi handle
PersistentStorage<Settings> SavedSettings(hw.seed.qspi);
bool use_preset = false;
bool trigger_save = false;
int blink = 100;
bool save_check = false;
bool update_switches = false;
void Load() {
//Reference to local copy of settings stored in flash
Settings &LocalSettings = SavedSettings.GetSettings();
knobValues[0] = LocalSettings.knobs[0];
knobValues[1] = LocalSettings.knobs[1];
knobValues[2] = LocalSettings.knobs[2];
knobValues[3] = LocalSettings.knobs[3];
knobValues[4] = LocalSettings.knobs[4];
knobValues[5] = LocalSettings.knobs[5];
toggleValues[0] = LocalSettings.toggles[0];
toggleValues[1] = LocalSettings.toggles[1];
toggleValues[2] = LocalSettings.toggles[2];
dipValues[0] = LocalSettings.dips[0];
dipValues[1] = LocalSettings.dips[1];
dipValues[2] = LocalSettings.dips[2];
dipValues[3] = LocalSettings.dips[3];
use_preset = true;
}
void Save() {
//Reference to local copy of settings stored in flash
Settings &LocalSettings = SavedSettings.GetSettings();
LocalSettings.knobs[0] = knobValues[0];
LocalSettings.knobs[1] = knobValues[1];
LocalSettings.knobs[2] = knobValues[2];
LocalSettings.knobs[3] = knobValues[3];
LocalSettings.knobs[4] = knobValues[4];
LocalSettings.knobs[5] = knobValues[5];
LocalSettings.toggles[0] = toggleValues[0];
LocalSettings.toggles[1] = toggleValues[1];
LocalSettings.toggles[2] = toggleValues[2];
LocalSettings.dips[0] = dipValues[0];
LocalSettings.dips[1] = dipValues[1];
LocalSettings.dips[2] = dipValues[2];
LocalSettings.dips[3] = dipValues[3];
trigger_save = true;
}
void updateSwitch1()
{
//int modelIndex = 0;
/*
if (pswitch1[0] == true) {
modelIndex = 1;
} else if (pswitch1[1] == true) {
modelIndex = 3;
} else {
modelIndex = 2;
}
*/
int modelIndex = toggleValues[0] + 1;
auto& gru = (model).template get<0>();
auto& dense = (model).template get<1>();
modelInSize = 1;
gru.setWVals(model_collection[modelIndex].rec_weight_ih_l0);
gru.setUVals(model_collection[modelIndex].rec_weight_hh_l0);
gru.setBVals(model_collection[modelIndex].rec_bias);
dense.setWeights(model_collection[modelIndex].lin_weight);
dense.setBias(model_collection[modelIndex].lin_bias.data());
model.reset();
nnLevelAdjust = model_collection[modelIndex].levelAdjust;
}
void updateSwitch2()
{
int irIndex = toggleValues[1];
mIR.Init(ir_collection[irIndex]); // ir_data is from ir_data.h
}
void updateSwitch3()
{
if (toggleValues[2] == 0) {
delay1.secondTapOn = false;
} else if (toggleValues[2] == 2) {
delay1.secondTapOn = true;
delay1.del->set2ndTapFraction(0.6666667); // triplett
} else {
delay1.secondTapOn = true;
delay1.del->set2ndTapFraction(0.75); // dotted eighth
}
}
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) {
// Don't ever, for any reason, do anything to anyone for any reason ever, no matter what, no matter where, or who, or who you are with, or where you are going, or where you've been... ever, for any reason whatsoever...
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);
}
// Save Preset - Either raise the hold time for save check, or instruct user to hold left then right, let go right then left for Set Expression mode
if(hw.switches[Funbox::FOOTSWITCH_2].TimeHeldMs() >= 700 && !save_check && !expression_pressed && hw.switches[Funbox::FOOTSWITCH_1].TimeHeldMs() <= 50) // TODO Check that this logic keeps peset separate from expression
{
Save();
save_check = true;
}
// Load Preset
if(hw.switches[Funbox::FOOTSWITCH_2].FallingEdge() && !expression_pressed)
{
if (save_check) {
save_check = false;
} else {
use_preset = !use_preset;
if (use_preset) {
Load();
} else {
update_switches = true; // Need to update switches based on current switch position after turning off preset
}
led2.Set(use_preset ? 1.0f : 0.0f);
}
// Need to update switches based on preset
updateSwitch1();
updateSwitch2();
updateSwitch3();
}
// Handle blink for saving a preset
if (blink < 100) {
blink += 1;
led2.Set(1.0f);
} else {
if (!expHandler.isExpressionSetMode())
led2.Set(use_preset ? 1.0f : 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) { // 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;
}
updateSwitch1();
}
// 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 || update_switches) {
if (pswitch2[0] == true) {
toggleValues[1] = 0;
} else if (pswitch2[1] == true) {
toggleValues[1] = 2;
} else {
toggleValues[1] = 1;
}
updateSwitch2();
}
// 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 || update_switches) {
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 || update_switches) {
for (int i=0; i<4; i++) {
dipValues[i] = pdip[i]; // TODO Check logic here
}
}
update_switches = false; // only update once after turning off preset
}
// 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();
float input_arr[1] = { 0.0 }; // Neural Net Input
float delay_out;
// Knob and Expression Processing ////////////////////
float newExpressionValues[6];
if (!use_preset) { // TODO Do I want to lock out the knobs when using a preset?
knobValues[0] = Gain.Process();
knobValues[1] = Mix.Process();
knobValues[2] = Level.Process();
knobValues[3] = filter.Process();
knobValues[4] = delayTime.Process();
knobValues[5] = delayFdbk.Process();
}
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 vmix = newExpressionValues[1];
float vlevel = newExpressionValues[2];
float vfilter = newExpressionValues[3];
float vdelayTime = newExpressionValues[4];
float vdelayFdbk = newExpressionValues[5];
//float vlevel = vexpression; // TESTING
// Mix and tone control
// Set Filter Controls
if (vfilter <= 0.5) {
float filter_value = (vfilter * 39800.0f) + 100.0f;
tone.SetFreq(filter_value);
} else {
float filter_value = (vfilter - 0.5) * 800.0f + 40.0f;
toneHP.SetFreq(filter_value);
}
// Calculate mix parameters
// A cheap mostly energy constant crossfade from SignalSmith Blog
// https://signalsmith-audio.co.uk/writing/2021/cheap-energy-crossfade/
float x2 = 1.0 - vmix;
float A = vmix*x2;
float B = A * (1.0 + 1.4186 * A);
float C = B + vmix;
float D = B + x2;
wetMix = C * C;
dryMix = D * D;
// DELAY //
if (vdelayTime < 0.01) { // if knob < 1%, set delay to inactive
delay1.active = false;
} else {
delay1.active = true;
}
// From 0 to 75% knob is 0 to 1 second, 75% to 100% knob is 1 to 2 seconds (for more control over 1 second range)
if (vdelayTime <= 0.75) {
delay1.delayTarget = 2400 + vdelayTime * 60800; // in samples 50ms to 1 second range // Note: changing delay time with heavy reverb creates a cool modulation effect
} else {
delay1.delayTarget = 48000 + (vdelayTime - 0.75) * 192000; // 1 second to 2 second range
}
delay1.feedback = vdelayFdbk;
// Order of effects is:
// Gain -> Neural Model -> Tone -> Delay -> IR ->
//
for(size_t i = 0; i < size; i++)
{
// Process your signal here
if(bypass)
{
// Could do a trails mode here
out[0][i] = in[0][i];
out[1][i] = in[0][i];
}
else
{
float ampOut = 0.0;
input_arr[0] = in[0][i] * vgain;
// Process Neural Net Model //
if (dipValues[0]) { // Enable/Disable neural model
ampOut = model.forward (input_arr) + input_arr[0];
ampOut *= nnLevelAdjust;
} else {
ampOut = input_arr[0];
}
// Process Tone
float filter_in = ampOut;
float filter_out;
float balanced_out;
if (vfilter <= 0.5) {
filter_out = tone.Process(filter_in);
balanced_out = bal.Process(filter_out, filter_in);
} else {
filter_out = toneHP.Process(filter_in);
balanced_out = bal.Process(filter_out, filter_in);
}
delay_out = delay1.Process(balanced_out); // Moved delay prior to IR
// IMPULSE RESPONSE //
float impulse_out = 0.0;
if (dipValues[1]) // If IR is enabled by dip switch
{
impulse_out = mIR.Process(balanced_out * dryMix + delay_out * wetMix) * 0.2;
} else {
impulse_out = balanced_out * dryMix + delay_out * wetMix;
}
// Process Delay
float output = impulse_out * vlevel * 0.4; // 0.4 for level adjust
out[0][i] = output;
out[1][i] = output;
}
}
}
int main(void)
{
float samplerate;
hw.Init();
samplerate = hw.AudioSampleRate();
setupWeights();
hw.SetAudioBlockSize(48); // Up to 256 reduces stuttering when using 2 tap delay (due to processing)
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; // TODO I think by setting all these to true, will force loading the correct model/ir on booting the pedal - verify
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;
Gain.Init(hw.knob[Funbox::KNOB_1], 0.1f, 2.5f, Parameter::LINEAR);
Mix.Init(hw.knob[Funbox::KNOB_2], 0.0f, 1.0f, Parameter::LINEAR);
Level.Init(hw.knob[Funbox::KNOB_3], 0.0f, 1.0f, Parameter::LINEAR); // lower range for quieter level
filter.Init(hw.knob[Funbox::KNOB_4], 0.0f, 1.0f, Parameter::CUBE);
delayTime.Init(hw.knob[Funbox::KNOB_5], 0.0f, 1.0f, Parameter::LINEAR);
delayFdbk.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;
indexMod = 0;
// Initialize & set params for mixers
mix_effects = 0.5;
tone.Init(samplerate);
toneHP.Init(samplerate);
bal.Init(samplerate);
vfilter = 0.5;
delayLine.Init();
delay1.del = &delayLine;
delay1.delayTarget = 2400; // in samples
delay1.feedback = 0.0;
delay1.active = true;
// Expression
expHandler.Init(6);
expression_pressed = false;
// 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();
//Initilize the PersistentStorage Object with default values.
//Defaults will be the first values stored in flash when the device is first turned on. They can also be restored at a later date using the RestoreDefaults method
Settings DefaultSettings = {0.0f, 0.0f};
SavedSettings.Init(DefaultSettings);
hw.StartAdc();
hw.StartAudio(AudioCallback);
while(1)
{
if(trigger_save) {
SavedSettings.Save(); // Writing locally stored settings to the external flash
trigger_save = false;
blink = 0;
}
System::Delay(100);
}
}