-
Notifications
You must be signed in to change notification settings - Fork 14
/
esp32_midi_sampler.ino
1039 lines (877 loc) · 24.9 KB
/
esp32_midi_sampler.ino
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2023 Marcel Licence
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
* der GNU General Public License, wie von der Free Software Foundation,
* Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
* veröffentlichten Version, weiter verteilen und/oder modifizieren.
*
* Dieses Programm wird in der Hoffnung bereitgestellt, dass es nützlich sein wird, jedoch
* OHNE JEDE GEWÄHR,; sogar ohne die implizite
* Gewähr der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
* Siehe die GNU General Public License für weitere Einzelheiten.
*
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
* Programm erhalten haben. Wenn nicht, siehe <https://www.gnu.org/licenses/>.
*/
/*
* this file should be opened with arduino, this is the main project file
*
* shown in: https://youtu.be/7uSobNW7_A4
*
* Some features listet here:
* - loading/saving from/to sd
* - add patch parameters saving
* - buffered audio processing -> higher polyphony
* - sd or littleFs usage (selectable)
* - global pitchbend and modulation
* - vu meter (using neopixel lib)
* - adsr vca control
* - file system selection -> littleFs support
* - master reverb effect
* - precise pitching of samples (just using simple float didn't work well)
* - sound removal support -> defrag
* - normalize record
* - patch selection
* - save with automatic increment of number in filename
* - display / vt100 terminal support
* - support of ST7735 160x80 compatible display -> update of Adafruit-ST7735-Library required
* - allows using the AS5600 for scratching
*
* Author: Marcel Licence
*/
#ifdef __CDT_PARSER__
#include <cdt.h>
#endif
/*
*
/$$$$$$$$ /$$ /$$ /$$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$
| $$_____/ | $$ | $$ | $$__ $$ /$$__ $$| $$__ $$ /$$__ $$| $$$ /$$$| $$| $$
| $$ /$$$$$$$ /$$$$$$ | $$$$$$$ | $$ /$$$$$$ | $$ \ $$| $$ \__/| $$ \ $$| $$ \ $$| $$$$ /$$$$| $$| $$
| $$$$$ | $$__ $$ |____ $$| $$__ $$| $$ /$$__ $$ | $$$$$$$/| $$$$$$ | $$$$$$$/| $$$$$$$$| $$ $$/$$ $$| $$| $$
| $$__/ | $$ \ $$ /$$$$$$$| $$ \ $$| $$| $$$$$$$$ | $$____/ \____ $$| $$__ $$| $$__ $$| $$ $$$| $$|__/|__/
| $$ | $$ | $$ /$$__ $$| $$ | $$| $$| $$_____/ | $$ /$$ \ $$| $$ \ $$| $$ | $$| $$\ $ | $$
| $$$$$$$$| $$ | $$| $$$$$$$| $$$$$$$/| $$| $$$$$$$ | $$ | $$$$$$/| $$ | $$| $$ | $$| $$ \/ | $$ /$$ /$$
|________/|__/ |__/ \_______/|_______/ |__/ \_______/ |__/ \______/ |__/ |__/|__/ |__/|__/ |__/|__/|__/
*/
/*
* todos:
* - better sound morphing - in progress
* - fix of auto gain - improved
* - add tremolo
* - modulation after time
* - harmonics?
* - ignore other tags in wav files
*
* done:
* - add wav saving to sd
* - add wav loading from sd
* - add patch parameter saving
* - buffered audio processing poly from 6 to 14
* - sd or littleFs usage
* - pitchbend and modulation
* - vu meter neopixel
* - adsr control
* - file system selection -> littleFs support
* - add reverb effect
* - increase of precision required of sample pitch
* - sound removal support -> defrag
* - normalize record
* - patch selection
* - save with automatic increment
* - display
* - simple scratching
*/
#include "config.h"
#include <Arduino.h>
#include <WiFi.h>
#ifdef AS5600_ENABLED
#include <Wire.h>
#endif
/* requires the ML_SynthTools library: https://github.com/marcel-licence/ML_SynthTools */
#include <ml_arp.h>
#include <ml_reverb.h>
#include <ml_midi_ctrl.h>
#include <ml_delay.h>
#ifdef OLED_OSC_DISP_ENABLED
#include <ml_scope.h>
#endif
#include <ml_types.h>
#define ML_SYNTH_INLINE_DECLARATION
#include <ml_inline.h>
#undef ML_SYNTH_INLINE_DECLARATION
/*
* use this to activate auto loading
*/
#define AUTO_LOAD_PATCHES_FROM_LITTLEFS
//#define AUTO_LOAD_PATCHES_FROM_SD_MMC
/*
* you can activate this but be careful
* this will pass trough the mic signal to output during record
* this may cause heavy feedbacks!
*/
//#define SAMPLER_PASS_TROUGH_DURING_RECORD
/*
* Chip is ESP32D0WDQ5 (revision 1)
* Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
* ref.: https://www.makerfabs.com/desfile/files/ESP32-A1S%20Product%20Specification.pdf
* Board: ESP32 Dev Module
* Flash Size: 32Mbit -> 4MB
* RAM internal: 520KB SRAM
* PSRAM: 4M (set to enabled!!!)
*
*/
/* to avoid the high click when turning on the microphone */
static float click_supp_gain = 0.0f;
float *vuInL;
float *vuInR;
float *vuOutL;
float *vuOutR;
extern uint32_t sampleRecordCount;
/* this application starts here */
void setup()
{
// put your setup code here, to run once:
delay(500);
Serial.begin(SERIAL_BAUDRATE);
Serial.println();
#ifdef AS5600_ENABLED
// digitalWrite(TFT_CS, HIGH);
// pinMode(TFT_CS, OUTPUT);
Wire.setClock(I2C_SPEED);
#endif
click_supp_gain = 0.0f;
#ifdef BLINK_LED_PIN
Blink_Setup();
#endif
Status_Setup();
Audio_Setup();
#ifdef AS5600_ENABLED
Wire.begin(I2C_SDA, I2C_SCL);
Wire.setClock(I2C_SPEED);
AS5600_Setup();
#endif
#ifdef ESP32_AUDIO_KIT
button_setup();
#endif
Sine_Init();
/*
* setup midi module / rx port
*/
Midi_Setup();
#ifdef ARP_MODULE_ENABLED
Arp_Init(24 * 4); /* slowest tempo one step per bar */
#endif
{
static int16_t *storage = NULL;
uint32_t storageLen = 0;
#ifndef SAMPLER_USE_HEAP_ONLY
psramInit();
Serial.printf("Total PSRAM: %d\n", ESP.getPsramSize());
Serial.printf("Free PSRAM: %d\n", ESP.getFreePsram());
if (ESP.getPsramSize() == 0)
{
Serial.printf("PSRAM is not available! Please ensure PSRAM is enabled and also available on your ESP32");
while (true)
{
delay(1);
}
}
if (ESP.getFreePsram() == 0)
{
Serial.printf("PSRAM has no free Memory! Please ensure PSRAM is enabled and also available on your ESP32");
while (true)
{
delay(1);
}
}
storageLen = ESP.getFreePsram() / sizeof(int16_t);
uint32_t storageBytes = storageLen * sizeof(int16_t);
Serial.printf("Try to allocate %d bytes\n", storageBytes);
storage = (int16_t *)ps_calloc(storageLen, sizeof(int16_t));
if (storage == NULL)
{
Serial.printf("Not able to allocate the complete PSRAM buffer!\nNow trying to reduce the allocation buffer size");
/*
* for some reason using a newer ESP32 board library you cannot allocate the complete PSRAM memory
* this loop will decrease the buffer size and repeat the allocation step again until it is successful
*/
while ((storage == NULL))
{
storageLen -= 1;
storageBytes = storageLen * sizeof(int16_t);
storage = (int16_t *)ps_calloc(storageLen, sizeof(int16_t));
}
}
Serial.printf("Total PSRAM: %d\n", ESP.getPsramSize());
Serial.printf("Free PSRAM: %d\n", ESP.getFreePsram());
#else
storageLen = SAMPLER_USE_HEAP_ONLY;
uint32_t storageBytes = storageLen * sizeof(int16_t);
Serial.printf("Try to allocate %d bytes from heap\n", storageBytes);
storage = (int16_t *)calloc(storageLen, sizeof(int16_t));
#endif
if (storage == NULL)
{
Serial.printf("Memory couldn't be allocated as sample storage!\n");
#ifdef ESP32
Serial.printf("ESP.getFreeHeap() %d\n", ESP.getFreeHeap());
Serial.printf("ESP.getMinFreeHeap() %d\n", ESP.getMinFreeHeap());
Serial.printf("ESP.getHeapSize() %d\n", ESP.getHeapSize());
Serial.printf("ESP.getMaxAllocHeap() %d\n", ESP.getMaxAllocHeap());
Serial.printf("Total heap: %d\n", ESP.getHeapSize());
Serial.printf("Free heap: %d\n", ESP.getFreeHeap());
/* PSRAM will be fully used by the looper */
Serial.printf("Total PSRAM: %d\n", ESP.getPsramSize());
Serial.printf("Free PSRAM: %d\n", ESP.getFreePsram());
#endif
while (true)
{
delay(1);
}
}
Serial.printf("Allocated %d bytes\n", storageBytes);
Serial.printf("storageLen: %d\n", storageLen);
Serial.printf("storageLen: %0.2fs\n", ((float)storageLen) / ((float)SAMPLE_RATE));
Sampler_Init(storage, storageLen);
}
/*
* Initialize reverb
* The buffer shall be static to ensure that
* the memory will be exclusive available for the reverb module
*/
static float revBuffer[REV_BUFF_SIZE];
Reverb_Setup(revBuffer);
#ifdef MAX_DELAY
/*
* Prepare a buffer which can be used for the delay
*/
static int16_t *delBuffer1 = (int16_t *)malloc(sizeof(int16_t) * MAX_DELAY);
static int16_t *delBuffer2 = (int16_t *)malloc(sizeof(int16_t) * MAX_DELAY);
Delay_Init2(delBuffer1, delBuffer2, MAX_DELAY);
if ((delBuffer1 == NULL) || (delBuffer2 == NULL))
{
/*
* we will end up here if the requested buffer is too big
* the malloc memory is limited and you should keep it small as possible
*/
Serial.printf("Memory for delay couldn't be initialized!\n");
while (true)
{
delay(1);
}
}
Serial.printf("delay samples: %d\n", MAX_DELAY);
Serial.printf("Max delay time: %0.2fs\n", ((float)MAX_DELAY) / ((float)SAMPLE_RATE));
#endif
#ifdef ESP32
Serial.printf("ESP.getFreeHeap() %d\n", ESP.getFreeHeap());
Serial.printf("ESP.getMinFreeHeap() %d\n", ESP.getMinFreeHeap());
Serial.printf("ESP.getHeapSize() %d\n", ESP.getHeapSize());
Serial.printf("ESP.getMaxAllocHeap() %d\n", ESP.getMaxAllocHeap());
Serial.printf("Total heap: %d\n", ESP.getHeapSize());
Serial.printf("Free heap: %d\n", ESP.getFreeHeap());
/* PSRAM will be fully used by the looper */
Serial.printf("Total PSRAM: %d\n", ESP.getPsramSize());
Serial.printf("Free PSRAM: %d\n", ESP.getFreePsram());
#endif
vuInL = VuMeterMatrix_GetPtr(0);
vuInR = VuMeterMatrix_GetPtr(1);
vuOutL = VuMeterMatrix_GetPtr(6);
vuOutR = VuMeterMatrix_GetPtr(7);
#ifdef AUTO_LOAD_PATCHES_FROM_LITTLEFS
/* finally we can preload some data if available */
PatchManager_SetDestination(0, 1);
Sampler_LoadPatchFile("/samples/pet_bottle.wav");
Sampler_LoadPatchFile("/samples/sine_plug.wav");
#endif
/* select sd card */
#ifdef AUTO_LOAD_PATCHES_FROM_SD_MMC
PatchManager_SetDestination(1, 1);
Sampler_LoadPatchFile("/samples/PlateDeep.wav");
Sampler_LoadPatchFile("/samples/76_Pure.wav");
#endif
Serial.printf("Firmware started successfully\n");
/* use this to easily test the output */
#ifdef NOTE_ON_AFTER_SETUP
Sampler_NoteOn(0, 64, 1);
#endif
#ifdef MIDI_STREAM_PLAYER_ENABLED
MidiStreamPlayer_Init();
//char midiFile[] = "/esp32_sampler_demo.mid";
char midiFile[] = "/esp32_sampler_demo2.mid";
MidiStreamPlayer_PlayMidiFile_fromLittleFS(midiFile, 2);
#endif
#ifdef AS5600_ENABLED
/* starts first loaded sample and activates this for scratching */
Sampler_SetScratchSample(0, 1);
#endif
#ifdef ESP32
Core0TaskInit();
#else
#error only supported by ESP32 platform
#endif
}
#ifdef ESP32
/*
* Core 0
*/
/* this is used to add a task to core 0 */
TaskHandle_t Core0TaskHnd;
inline
void Core0TaskInit()
{
/* we need a second task for the terminal output */
xTaskCreatePinnedToCore(Core0Task, "CoreTask0", 8000, NULL, 999, &Core0TaskHnd, 0);
}
inline
void Core0TaskSetup()
{
/*
* init your stuff for core0 here
*/
#ifdef OLED_OSC_DISP_ENABLED
ScopeOled_Setup();
#endif
#ifdef DISPLAY_160x80_ENABLED
Display_Setup();
#ifdef SCREEN_ENABLED
Screen_Setup();
#endif
App_SetBrightness(0, 0.25);
#endif
VuMeterMatrix_Init();
}
inline
void Core0TaskLoop()
{
Status_Process();
#ifndef AS5600_ENABLED /* does not work together */
VuMeterMatrix_Display();
#endif
#ifdef SCREEN_ENABLED
Screen_Loop();
#endif
#ifdef DISPLAY_160x80_ENABLED
Display_Draw();
#endif
#ifdef OLED_OSC_DISP_ENABLED
ScopeOled_Process();
#endif
}
void Core0Task(void *parameter)
{
Core0TaskSetup();
while (true)
{
Core0TaskLoop();
/* this seems necessary to trigger the watchdog */
delay(1);
yield();
}
}
#endif /* ESP32 */
static uint32_t midiSyncCount = 0;
void Midi_SyncRecvd()
{
midiSyncCount += 1;
}
void Synth_RealTimeMsg(uint8_t msg)
{
#ifndef MIDI_SYNC_MASTER
switch (msg)
{
case 0xfa: /* start */
Arp_Reset();
break;
case 0xf8: /* Timing Clock */
Midi_SyncRecvd();
break;
}
#endif
}
#ifdef MIDI_SYNC_MASTER
#define MIDI_PPQ 24
#define SAMPLES_PER_MIN (SAMPLE_RATE*60)
static float midi_tempo = 120.0f;
void MidiSyncMasterLoop(void)
{
static float midiDiv = 0;
midiDiv += SAMPLE_BUFFER_SIZE;
if (midiDiv >= (SAMPLES_PER_MIN) / (MIDI_PPQ * midi_tempo))
{
midiDiv -= (SAMPLES_PER_MIN) / (MIDI_PPQ * midi_tempo);
Midi_SyncRecvd();
}
}
void Synth_SetMidiMasterTempo(uint8_t unused, float val)
{
midi_tempo = 60.0f + val * (240.0f - 60.0f);
}
#endif
void Synth_SongPosition(uint16_t pos)
{
Serial.printf("Songpos: %d\n", pos);
if (pos == 0)
{
Arp_Reset();
}
}
void Synth_SongPosReset(uint8_t unused, float var)
{
if (var > 0)
{
Synth_SongPosition(0);
}
}
float master_output_gain = 1.0f;
/* little enum to make switching more clear */
enum acSource
{
acSrcLine,
acSrcMic
};
/* line in is used by default, so it should not be changed here */
enum acSource selSource = acSrcLine;
/* be carefull when calling this function, microphones can cause very bad feedback!!! */
void App_ToggleSource(uint8_t channel, float value)
{
if (value > 0)
{
switch (selSource)
{
case acSrcLine:
click_supp_gain = 0.0f;
#ifdef AC101_ENABLED
ac101_setSourceMic();
#endif
selSource = acSrcMic;
Status_TestMsg("Input: Microphone");
break;
case acSrcMic:
click_supp_gain = 0.0f;
#ifdef AC101_ENABLED
ac101_setSourceLine();
#endif
selSource = acSrcLine;
Status_TestMsg("Input: LineIn");
break;
}
}
}
void App_SetOutputLevel(uint8_t not_used, float value)
{
master_output_gain = value;
}
/*
* this should avoid having a constant offset on our signal
* I am not sure if that is required, but in case it can avoid early clipping
*/
static float fl_offset = 0.0f;
static float fr_offset = 0.0f;
static float fl_sample[SAMPLE_BUFFER_SIZE];
static float fr_sample[SAMPLE_BUFFER_SIZE];
#ifndef absf
#define absf(a) ((a>=0.0f)?(a):(-a))
#endif
/*
* the main audio task
*/
inline void audio_task()
{
memset(fl_sample, 0, sizeof(fl_sample));
memset(fr_sample, 0, sizeof(fr_sample));
Audio_Input(fl_sample, fr_sample);
for (int n = 0; n < SAMPLE_BUFFER_SIZE; n++)
{
/*
* this avoids the high peak coming over the mic input when switching to it
*/
fl_sample[n] *= click_supp_gain;
fr_sample[n] *= click_supp_gain;
if (click_supp_gain < 1.0f)
{
click_supp_gain += 0.00001f;
}
else
{
click_supp_gain = 1.0f;
}
/* make it a bit quieter */
fl_sample[n] *= 0.5f;
fr_sample[n] *= 0.5f;
/*
* this removes dc from signal
*/
fl_offset = fl_offset * 0.99 + fl_sample[n] * 0.01;
fr_offset = fr_offset * 0.99 + fr_sample[n] * 0.01;
fl_sample[n] -= fl_offset;
fr_sample[n] -= fr_offset;
/*
* put vu values to vu meters
*/
*vuInL = max(*vuInL, absf(fl_sample[n]));
*vuInR = max(*vuInR, absf(fr_sample[n]));
}
/*
* main loop core
*/
Sampler_Process(fl_sample, fr_sample, SAMPLE_BUFFER_SIZE);
#ifdef MAX_DELAY
/*
* process delay line
*/
Delay_Process_Buff2(fl_sample, fr_sample, SAMPLE_BUFFER_SIZE);
#endif
/*
* add some mono reverb
*/
Reverb_Process(fl_sample, SAMPLE_BUFFER_SIZE);
memcpy(fr_sample, fl_sample, sizeof(fr_sample));
/*
* apply master output gain
*/
for (int n = 0; n < SAMPLE_BUFFER_SIZE; n++)
{
/* apply master_output_gain */
fl_sample[n] *= master_output_gain;
fr_sample[n] *= master_output_gain;
/* output signal to vu meter */
*vuOutL = max(*vuOutL, absf(fl_sample[n]));
*vuOutR = max(*vuOutR, absf(fr_sample[n]));
}
Audio_Output(fl_sample, fr_sample);
#ifdef OLED_OSC_DISP_ENABLED
ScopeOled_AddSamples(fl_sample, fr_sample, SAMPLE_BUFFER_SIZE);
#endif
Status_Process_Sample(SAMPLE_BUFFER_SIZE);
}
/*
* this function will be called once a second
* call can be delayed when one operation needs more time (> 1/44100s)
*/
void loop_1Hz(void)
{
#ifdef BLINK_LED_PIN
Blink_Process();
#endif
#if 0 /* use this line to to show analog button values to setup their values */
printf("ADC: %d\n", analogRead(36));
#endif
}
void loop_100Hz(void)
{
static uint32_t loop_cnt;
loop_cnt += 1;
if ((loop_cnt) >= 100)
{
loop_cnt = 0;
loop_1Hz();
}
/*
* Put your functions here which should be called 100 times a second
*/
#ifdef ESP32_AUDIO_KIT
button_loop();
#endif
}
#ifdef AS5600_ENABLED
inline
void loop_4th()
{
#ifdef AS5600_ENABLED
#ifdef DISPLAY_160x80_ENABLED
if (Display_Busy() == false)
#endif
{
AS5600_Loop();
Sampler_SetPitchAbs(AS5600_GetPitch(4));
}
#endif
}
#endif
/*
* this is the main loop
*/
void loop()
{
audio_task(); /* audio tasks blocks for one sample -> 1/44100s */
#ifdef AS5600_ENABLED
static uint32_t prec4 = 0;
prec4++;
if ((prec4 % 4) == 0)
{
loop_4th();
}
#endif
VuMeterMatrix_Process(); /* calculates slow falling bars */
static uint32_t loop_cnt;
loop_cnt += SAMPLE_BUFFER_SIZE;
if ((loop_cnt) >= (SAMPLE_RATE / 100))
{
loop_cnt = 0;
loop_100Hz();
}
/*
* doing midi only 64 times per sample cycle
*/
Midi_Process();
#ifdef MIDI_STREAM_PLAYER_ENABLED
MidiStreamPlayer_Tick(SAMPLE_BUFFER_SIZE);
#endif
}
uint8_t baseKey = 64 + 12 + 7; // c
#ifdef KEYS_TO_MIDI_NOTES
void App_Key1Down()
{
Sampler_NoteOn(1, baseKey + 0, 1);
}
void App_Key2Down()
{
Sampler_NoteOn(1, baseKey + 2, 1);
}
void App_Key3Down()
{
Sampler_NoteOn(1, baseKey + 4, 1);
}
void App_Key4Down()
{
Sampler_NoteOn(1, baseKey + 5, 1);
}
void App_Key5Down()
{
Sampler_NoteOn(1, baseKey + 7, 1);
}
void App_Key6Down()
{
Sampler_NoteOn(1, baseKey + 9, 1);
}
void App_Key1Up()
{
Sampler_NoteOff(1, baseKey + 0);
}
void App_Key2Up()
{
Sampler_NoteOff(1, baseKey + 2);
}
void App_Key3Up()
{
Sampler_NoteOff(1, baseKey + 4);
}
void App_Key4Up()
{
Sampler_NoteOff(1, baseKey + 5);
}
void App_Key5Up()
{
Sampler_NoteOff(1, baseKey + 7);
}
void App_Key6Up()
{
Sampler_NoteOff(1, baseKey + 9);
}
#else
enum keyMode_e
{
keyMode_playbackChrom,
keyMode_record,
keyMode_playbackSample,
keyMode_storage
};
enum keyMode_e currentKeyMode = keyMode_playbackChrom;
#ifdef AS5600_ENABLED
uint8_t scratchNote = 0;
void App_ButtonCbPlaySample(uint8_t key, uint8_t down)
{
if (down > 0)
{
switch (key)
{
case 0:
scratchNote++;
Sampler_SetScratchSample(scratchNote, 1);
break;
case 1:
if (scratchNote > 0)
{
scratchNote--;
}
Sampler_SetScratchSample(scratchNote, 1);
break;
}
}
switch (key)
{
case 2:
delay(250); /* to avoid recording the noise of the button */
Sampler_RecordWait(0, down);
break;
case 3:
//Sampler_Record(0, down);
if (down > 0)
{
AS5600_SetPitchOffset(1.0f);
}
else
{
AS5600_SetPitchOffset(-100.0f);
}
break;
}
}
#else
uint8_t lastCh = 1;
void App_ButtonCb(uint8_t key, uint8_t down)
{
if ((key == 0) && (down > 0))
{
switch (currentKeyMode)
{
case keyMode_storage:
currentKeyMode = keyMode_record;
#ifdef DISPLAY_160x80_ENABLED
Display_SetHeader("Record");
#endif
Status_LogMessage("Record");
Status_SetKeyText(0, "1:select");
Status_SetKeyText(1, "2:rWait");
Status_SetKeyText(2, "3:record");
Status_SetKeyText(3, "4:littFs");
Status_SetKeyText(4, "5:sdMmc");
Status_SetKeyText(5, "6:save");
break;
case keyMode_record:
currentKeyMode = keyMode_playbackSample;
#ifdef DISPLAY_160x80_ENABLED
Display_SetHeader("Sample playback");
#endif
Status_LogMessage("Sample playback");
Status_SetKeyText(0, "1:select");
Status_SetKeyText(1, "2:smpl1");
Status_SetKeyText(2, "3:smpl2");
Status_SetKeyText(3, "4:smpl3");
Status_SetKeyText(4, "5:smpl4");
Status_SetKeyText(5, "6:smpl5");
break;
case keyMode_playbackSample:
currentKeyMode = keyMode_playbackChrom;
#ifdef DISPLAY_160x80_ENABLED
Display_SetHeader("Chromatic playback");
#endif
Status_LogMessage("Chromatic playback");
Status_SetKeyText(0, "1:select");
Status_SetKeyText(1, "2:c");
Status_SetKeyText(2, "3:c#");
Status_SetKeyText(3, "4:d");
Status_SetKeyText(4, "5:d#");
Status_SetKeyText(5, "6:e");
break;
case keyMode_playbackChrom:
currentKeyMode = keyMode_storage;
#ifdef DISPLAY_160x80_ENABLED
Display_SetHeader("Sample loader");
#endif
Status_LogMessage("Sample loader");
Status_SetKeyText(0, "1:select");
Status_SetKeyText(1, "2:next");
Status_SetKeyText(2, "3:prev");
Status_SetKeyText(3, "4:littFs");
Status_SetKeyText(4, "5:sdMmc");
Status_SetKeyText(5, "6:load");
break;
}
}
else
{
switch (currentKeyMode)
{
case keyMode_playbackChrom:
if (down > 0)
{
Sampler_NoteOn(lastCh, baseKey + key, 1);
}
else
{
Sampler_NoteOff(lastCh, baseKey + key);
}
break;
case keyMode_record:
switch (key)
{
case 1:
delay(250); /* to avoid recording the noise of the button */
Sampler_RecordWait(0, down);
break;
case 2:
Sampler_Record(0, down);
break;
case 3:
PatchManager_SetDestination(0, 0);
break;
case 4:
PatchManager_SetDestination(0, 1);
break;
case 5:
Sampler_SavePatch(0, down);
break;
}
break;
case keyMode_playbackSample:
if (down > 0)
{
Sampler_NoteOn(0, key - 1, 1);
lastCh = key;
}
else
{
Sampler_NoteOff(0, key - 1);
}