-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathesp32soundsynth.ino
1000 lines (903 loc) · 21.6 KB
/
esp32soundsynth.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
//#include <MIDI.h>
#include "WiFi.h"
#include <Arduino.h>
//#include <U8g2lib.h>
#include "LowPass.h"
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
#include "FileIO.h"
#include <HardwareSerial.h>
#include "SynthVoice.h"
#define ANALOG_IN 36
// specify the board to use for pinout
//#define GENERIC
//#define TTGO16MPROESP32OLED
//#define ESP32TTGO_T8_V1_7
#define ESP32TTGO_T2
#ifdef ESP32TTGO_T2
#include <TFT_eSPI.h>
#include <SPI.h>
#ifndef TFT_DISPOFF
#define TFT_DISPOFF 0x28
#endif
#ifndef TFT_SLPIN
#define TFT_SLPIN 0x10
#endif
#define TFT_MOSI 19
#define TFT_SCLK 18
#define TFT_CS 5
#define TFT_DC 16
#define TFT_RST 23
#define TFT_BL 4 // Display backlight control pin
#define ADC_EN 14
#define ADC_PIN 34
#define LED 21
#define MIDIRX 13
#define MIDITX 12
#endif
#ifdef ESP32TTGO_T8_V1_7
#define LED 21
#define MIDIRX 22
#define MIDITX 19
#endif
//#define IIC_1604_DISPLAY
#ifdef IIC_1604_DISPLAY
#define IIC_SDA_PIN 14
#define IIC_SCL_PIN 27
#include <LiquidCrystal_I2C.h>
int lcdColumns = 16;
int lcdRows = 2;
#endif
//#define ESP32DEVKIT1_DOIT
//#define ESP32DEVKIT1_DOIT
#ifdef ESP32TTGO_T8_V1_1
#define LED 2
#define MIDIRX 16
#define MIDITX 17
#endif
#ifdef ESP32DEVKIT1_DOIT
#define LED 2
#define MIDIRX 16
#define MIDITX 17
#endif
#ifdef TTGO16MPROESP32OLED
//#define NOMIDI
#define LED 2
#define MIDIRX 36
#define MIDITX 37
#endif
#ifdef GENERIC
#define LED 21
#define MIDIRX 22
#define MIDITX 19
#endif
#define RED_BUTTON 4
#define YELLOW_BUTTON 2
#define SAMPLE_RATE 8000
#define NUM_VOICES 4
#define NUM_DRUMS 0
#define WTLEN 256
#define MIDI_COMMAND 128
hw_timer_t * timer = NULL;
volatile long t = 0;
HardwareSerial console(0);
HardwareSerial hSerial(2);
#ifdef TTGO16MPROESP32OLED
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, 16,15,4);
#endif
#ifdef ESP32TTGO_T2
TFT_eSPI tft = TFT_eSPI(135, 240);
TFT_eSprite img = TFT_eSprite(&tft);
const int numlines=6;
char line[numlines][17];
volatile int currline=-1;
#endif
char buf0[17]= " \0";
char buf1[17]= " \0";
int8_t fp_sinWaveTable[WTLEN];
int8_t fp_sawWaveTable[WTLEN];
int8_t fp_triWaveTable[WTLEN];
int8_t fp_squWaveTable[WTLEN];
int8_t fp_plsWaveTable[WTLEN];
int8_t fp_rndWaveTable[WTLEN];
uint8_t dwfbuf_l[128];
uint8_t dwfbuf_r[128];
char line1[17];
char line2[17];
uint8_t dwfidx=0;
double f = 22.5;
int notes[] = {0,3,5,12,15,17,24,27,29};
int notelen = 9;
int noteidx = 0;
LowPass lowpass;
int voices_notes[NUM_VOICES];
int drums_notes[NUM_DRUMS];
enum controller_states{CS_OSC=0, CS_ENV,CS_AMP,CS_FIL};
int controller_state = CS_OSC;
uint8_t knob_values[4][8]; //first is state, second is knob;
int value_pickup[8] = {0,0,0,0,0,0,0,0};
uint8_t ffreq = 127;
uint8_t fq = 0;
unsigned long lastUpdateScreenTime;
void initFpSin()
{
for(int i=0;i<WTLEN;i++)
{
fp_sinWaveTable[i] = (int8_t)(((WTLEN-1)/2.0)*(sin(2*(PI/(float)WTLEN)*i)));
}
return;
}
void initFpTri()
{
for(int i=0;i<128;i++)
{
fp_triWaveTable[i] = (int8_t)(((WTLEN+1)/2.0)*(-1.0+i*(1.0/((double)WTLEN/2.0))));
}
for(int i=128;i<256;i++)
{
fp_triWaveTable[i] = (int8_t)(((WTLEN+1)/2.0)*(1.0 - i*(1.0/((double)WTLEN/2.0))));
}
}
void initFpSqu()
{
for(int i=0;i<256;i++)
{
fp_squWaveTable[i] = (i<(WTLEN/2)?127:-127);
}
}
void initFpSaw()
{
for(int i = 0;i<256;i++)
{
fp_sawWaveTable[i] = (int8_t)((WTLEN/2)*(-1.0 + (2.0/WTLEN)*i));
}
}
void initFpRnd()
{
for(int i=0;i<WTLEN;i++)
{
fp_rndWaveTable[i] = (int8_t)(random(0,255)-127);
}
}
void initFpPls()
{
for(int i=0;i<WTLEN;i++)
{
if(i ==WTLEN/4)
{
fp_plsWaveTable[i] = 127;
}
else if(i==WTLEN-(WTLEN/4))
{
fp_plsWaveTable[i] = -127;
}
else
{
fp_plsWaveTable[i] = 0;
}
}
}
double beatlen(double bpm)
{
return (double)60000.0/(bpm*4);
}
SynthVoice voices[NUM_VOICES];
SynthVoice drums[NUM_DRUMS];
volatile bool play = false;
volatile unsigned long t_start;
volatile unsigned long t_end;
volatile unsigned long t_diff;
volatile unsigned long t_counter = 0;
volatile double avg_time_micros = 0;
volatile int shift = 0;
void IRAM_ATTR onTimer() {
t_start = micros();
int64_t s = 0;
uint8_t data=0;
uint8_t datar = 0;
for(int i=0;i<NUM_VOICES;i++)
{
s = s + (int32_t)(voices[i].Process() + Num(127));
}
data = (s/(NUM_VOICES));
dwfbuf_l[dwfidx]=data;
//s = ((int32_t)nsinosc.Process())+128;
//s = s+(fpsinosc[i].Process())>>16;
//s = nsinosc.Process()>>10;
s = 0;
for(int i=0;i<NUM_DRUMS;i++)
{
s = s + (int32_t)(drums[i].Process() + Num(127));
}
datar = data;//(s/NUM_DRUMS);
dwfbuf_r[dwfidx]=datar;
dwfidx = (dwfidx+1)%128;
dacWrite(25, data);
dacWrite(26, datar);
t_end = micros();
t_diff = t_end-t_start;
t_counter++;
avg_time_micros = t_diff;
}
TaskHandle_t Task1;
void setup()
{
//formatFat();
WiFi.mode(WIFI_OFF);
btStop();
//lcd.write((const uint8_t*)"SYNTH (C)2019");
console.begin(57600,SERIAL_8N1);
//hSerial.setRxBufferSize(1);
#ifndef NOMIDI
hSerial.begin(31250,SERIAL_8N1,MIDIRX,MIDITX);
#endif
//hSerial.begin(115200);
pinMode(LED,OUTPUT);
digitalWrite(LED,HIGH);
delay(2000);
digitalWrite(LED,LOW);
for(int i=0;i<NUM_VOICES;i++)
{
voices_notes[i] = -1;
}
for(int i=0;i<NUM_DRUMS;i++)
{
drums_notes[i] = -1;
}
initFpSaw();
initFpSin();
initFpSqu();
initFpTri();
initFpRnd();
initFpPls();
strcpy(line1,"BOKONTEP (C)");
strcpy(line2,"ESP32SYNTH 2019");
for(int i =0;i<NUM_VOICES;i++)
{
voices[i] = SynthVoice(SAMPLE_RATE);
voices[i].AddOsc1SharedWaveTable(WTLEN,&fp_sinWaveTable[0]);
voices[i].AddOsc1SharedWaveTable(WTLEN,&fp_sawWaveTable[0]);
voices[i].AddOsc1SharedWaveTable(WTLEN,&fp_triWaveTable[0]);
voices[i].AddOsc1SharedWaveTable(WTLEN,&fp_squWaveTable[0]);
voices[i].AddOsc1SharedWaveTable(WTLEN,&fp_plsWaveTable[0]);
voices[i].AddOsc1SharedWaveTable(WTLEN,&fp_rndWaveTable[0]);
voices[i].SetOsc1ADSR(10,1,1.0,1000);
voices[i].AddOsc2SharedWaveTable(WTLEN,&fp_sinWaveTable[0]);
voices[i].AddOsc2SharedWaveTable(WTLEN,&fp_sawWaveTable[0]);
voices[i].AddOsc2SharedWaveTable(WTLEN,&fp_triWaveTable[0]);
voices[i].AddOsc2SharedWaveTable(WTLEN,&fp_squWaveTable[0]);
voices[i].AddOsc2SharedWaveTable(WTLEN,&fp_plsWaveTable[0]);
voices[i].AddOsc2SharedWaveTable(WTLEN,&fp_rndWaveTable[0]);
voices[i].SetOsc2ADSR(10,1,1.0,1000);
lastUpdateScreenTime=millis();
}
/*
for(int i=0;i<NUM_DRUMS;i++)
{
drums[i] = SynthVoice(SAMPLE_RATE);
drums[i].AddOsc1WaveTable(WTLEN,&fp_rndWaveTable[0]);
drums[i].SetOsc1ADSR(50,40,0.0,1);
drums[i].AddOsc2WaveTable(WTLEN,&fp_rndWaveTable[0]);
drums[i].SetOsc2ADSR(45,40,0.0,1);
}
*/
/* Use 1st timer of 4 */
/* 1 tick take 1/(80MHZ/80) = 1u16s so we set divider 80 and count up */
timer = timerBegin(0, 80, true);
/* Attach onTimer function to our timer */
timerAttachInterrupt(timer, &onTimer, true);
/* Set alarm to call onTimer function every second 1 tick is 1us
=> 1 second is 1000000us */
/* Repeat the alarm (third parameter) */
//(int32_t)nsinosc.Process()+(128)
timerAlarmWrite(timer, 1000000/SAMPLE_RATE, true);
/* Start an alarm */
timerAlarmEnable(timer);
//#ifdef TTGO16MPROESP32OLED
//Serial.println("stalnrt timer");
xTaskCreatePinnedToCore(displayData,"displayData",20000,NULL,1,&Task1,0);
//#endif
}
void printMidiMessage(uint8_t command, uint8_t data1, uint8_t data2)
{
unsigned long now = millis();
sprintf(buf0,"MIDI RX:%02X:%02X:%02X\0",command,data1,data2);
if(now-lastUpdateScreenTime<200)
{
return;
}
console.print("MIDI DATA:");
console.print(command);
console.print(":");
console.print(data1);
console.print(":");
console.println(data2);
console.flush();
}
void testChords()
{
if(t_counter%32000==0)
{
for(int i = 0;i<NUM_VOICES;i++)
{
console.print("Voice ");
console.print(i);
if(voices[i].IsPlaying())
{
voices[i].MidiNoteOff();
console.println("noteoff");
}
else
{
int root = random(40,60);
console.println("noteon");
{
noteidx=0;
}
voices[i].MidiNoteOn(notes[noteidx]+root,127);
noteidx++;
}
}
}
if(t_counter%8000==0)
{
console.println(avg_time_micros);
}
}
uint8_t commandByte;
uint8_t noteByte;
uint8_t velocityByte;
uint8_t noteOn = 144;
int serialData;
uint8_t command;
uint8_t channel;
int data1;
int data2;
enum midistate{WAIT_COMMAND,WAIT_DATA1,WAIT_DATA2};
bool firsttime = true;
midistate mstate=WAIT_COMMAND;
byte rotaries[4][8];
#ifdef ESP32TTGO_T2
void writeTFTLine(char* text)
{
currline++;
int newidx = (currline%numlines);
sprintf(&line[newidx][0],"%s",text);
}
#endif
void handleNoteOn(byte channel, byte note, byte velocity)
{
char buf[17];
bool found=false;
int maxnote = -1;
int maxnoteidx = -1;
digitalWrite(LED,HIGH);
sprintf(buf, "note:%03d vel:%03d",note, velocity);
strcpy(line1,buf);
strcpy(line2,"");
#ifdef ESP32TTGO_T2
writeTFTLine(buf);
#endif
if(channel !=10)
{
for(int i=0;i<NUM_VOICES;i++)
{
if(voices_notes[i]==-1)
{
voices_notes[i]=note;
voices[i].MidiNoteOn(note,velocity);
found = true;
return;
}
if(voices_notes[i]>maxnote)
{
maxnote = voices_notes[i];
maxnoteidx = i;
}
}
voices_notes[maxnoteidx]=note;
voices[maxnoteidx].MidiNoteOn(note,velocity);
}
else
{
for(int i=0;i<NUM_DRUMS;i++)
{
if(drums_notes[i]==-1)
{
drums_notes[i]=note;
drums[i].MidiNoteOn(note,velocity);
found = true;
return;
}
if(drums_notes[i]>maxnote)
{
maxnote = voices_notes[i];
maxnoteidx = i;
}
}
drums_notes[maxnoteidx]=note;
drums[maxnoteidx].MidiNoteOn(note,velocity);
}
}
void handleNoteOff(byte channel, byte note, byte velocity)
{
char buf[17];
sprintf(buf, "note:%03d vel:%03d",note, velocity);
strcpy(line1,buf);
strcpy(line2,"");
#ifdef ESP32TTGO_T2
writeTFTLine(buf);
#endif
if(channel!=10)
{
digitalWrite(LED,LOW);
for(int i=0;i<NUM_VOICES;i++)
{
if(voices_notes[i]==note)
{
voices_notes[i]=-1;
voices[i].MidiNoteOff();
//break;
}
}
}
else
{
digitalWrite(LED,LOW);
for(int i=0;i<NUM_DRUMS;i++)
{
if(drums_notes[i]==note)
{
drums_notes[i]=-1;
drums[i].MidiNoteOff();
//break;
}
}
}
}
void handlePitchBend(byte channel, byte bendlsb, byte bendmsb)
{
if(channel!=10)
{
uint16_t bend = bendmsb<<7 | bendlsb;
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].MidiBend(bend);
}
}
}
void handleCC(byte channel, byte cc, byte data, int* vpickup)
{
switch(cc)
{
case 1: //Modulation
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].MidiMod(data);
}
break;
case 2: //PWM
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].MidiPwm(data);
}
break;
case 64: //pedal osc1 waveform
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].MidiOsc1Wave(data);
}
break;
case 65: //portamento osc2 waveform
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].MidiOsc1Wave(data);
}
break;
case 91: //ROTARY 1 ON UMX490
handleRotaryData(0, controller_state,data,vpickup);
break;
case 93: //ROTARY 2 ON UMX490
handleRotaryData(1, controller_state,data,vpickup);
break;
case 74: //ROTARY 3 ON UMX490
handleRotaryData(2, controller_state,data,vpickup);
break;
case 71: //ROTARY 4 ON UMX490
handleRotaryData(3, controller_state,data,vpickup);
break;
case 73: //ROTARY 5 ON UMX490
handleRotaryData(4, controller_state,data,vpickup);
break;
case 75: //ROTARY 6 ON UMX490
handleRotaryData(5, controller_state,data,vpickup);
break;
case 72: //ROTARY 7 ON UMX490
handleRotaryData(6, controller_state,data,vpickup);
break;
case 10: //ROTARY 8 ON UMX490
handleRotaryData(7, controller_state,data,vpickup);
break;
case 97: //button 1 on UMX490
controller_state = CS_OSC;
for(int i=0;i<8;i++)
{
vpickup[i] = 1;
}
break;
case 96: //button 2 on UMX490
controller_state = CS_ENV;
for(int i=0;i<8;i++)
{
vpickup[i] = 1;
}
break;
case 66: //button 3 on UMX490
controller_state = CS_AMP;
for(int i=0;i<8;i++)
{
vpickup[i] = 1;
}
break;
case 67: //button 4 on UMX490
controller_state = CS_FIL;
for(int i=0;i<8;i++)
{
vpickup[i] = 1;
}
break;
}
}
void handleRotaryData(int rotary, int state, byte data, int* value_pickup)
{
int diff = knob_values[state][rotary]-data;
if((diff>3 || diff<-3) && value_pickup[rotary] == 1)
{
return;
}
else
{
value_pickup[rotary] = 0;
}
knob_values[state][rotary] = data;
switch(state)
{
case CS_OSC:
switch(rotary)
{
case 0:
for(int i=0;i<NUM_VOICES;i++)
{
int divisor = 127/voices[i].GetOsc1WaveTableCount();
voices[i].MidiOsc1Wave(data/divisor);
}
break;
case 1:
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].SetFmod1(data);
}
break;
case 2:
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].SetOsc1PhaseOffset(data);
}
break;
case 4:
for(int i=0;i<NUM_VOICES;i++)
{
int divisor = 127/voices[i].GetOsc2WaveTableCount();
voices[i].MidiOsc2Wave(data/divisor);
}
case 5:
{
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].SetFmod2(data);
}
}
case 6:
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].SetOsc2PhaseOffset(data);
}
break;
case 7:
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].SetFmod3(data);
}
break;
}
break;
case CS_ENV:
switch(rotary)
{
case 0:
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].SetOsc1ADSR(knob_values[CS_ENV][0],knob_values[CS_ENV][1],knob_values[CS_ENV][2],knob_values[CS_ENV][3]);
}
break;
case 1:
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].SetOsc1ADSR(knob_values[CS_ENV][0],knob_values[CS_ENV][1],knob_values[CS_ENV][2],knob_values[CS_ENV][3]);
}
break;
case 2:
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].SetOsc1ADSR(knob_values[CS_ENV][0],knob_values[CS_ENV][1],knob_values[CS_ENV][2],knob_values[CS_ENV][3]);
}
break;
case 3:
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].SetOsc1ADSR(knob_values[CS_ENV][0],knob_values[CS_ENV][1],knob_values[CS_ENV][2],knob_values[CS_ENV][3]);
}
break;
case 4:
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].SetOsc2ADSR(knob_values[CS_ENV][4],knob_values[CS_ENV][5],knob_values[CS_ENV][6],knob_values[CS_ENV][7]);
}
break;
case 5:
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].SetOsc2ADSR(knob_values[CS_ENV][4],knob_values[CS_ENV][5],knob_values[CS_ENV][6],knob_values[CS_ENV][7]);
}
break;
case 6:
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].SetOsc2ADSR(knob_values[CS_ENV][4],knob_values[CS_ENV][5],knob_values[CS_ENV][6],knob_values[CS_ENV][7]);
}
break;
case 7:
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].SetOsc2ADSR(knob_values[CS_ENV][4],knob_values[CS_ENV][5],knob_values[CS_ENV][6],knob_values[CS_ENV][7]);
}
break;
}
break;
case CS_AMP:
break;
case CS_FIL:
switch(rotary)
{
case 0: // freq
ffreq = data;
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].SetFilterParameters(ffreq, fq);
}
break;
case 1: // q
fq = data ;
for(int i=0;i<NUM_VOICES;i++)
{
voices[i].SetFilterParameters(ffreq, fq);
}
break;
}
break;
}
}
void displayData(void * parameter)
{
#ifdef TTGO16MPROESP32OLED
u8g2.begin();
while(true)
{
u8g2.clearBuffer(); // clear the internal memory
//u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
for (int i=0;i<127;i++)
{
u8g2.drawLine(i,(dwfbuf_l[i]>>2),i+1,(dwfbuf_l[i+1]>>2));
u8g2.drawLine(i,(dwfbuf_r[i]>>2),i+1,(dwfbuf_r[i+1]>>2));
}
//u8g2.setFont( u8g2_font_unifont_t_cyrillic);
//u8g2.drawExtUTF8(-8,10,0,NULL,line1);
u8g2.setFont( u8g2_font_unifont_t_greek);
u8g2.drawExtUTF8(-8,10,0,NULL,line1);
u8g2.drawExtUTF8(-8,21,0,NULL,line2);
u8g2.sendBuffer(); // transfer internal memory to the display
delay(10);
}
#endif
#ifdef IIC_1604_DISPLAY
LiquidCrystal_I2C lcd(0x27,20,2);
lcd.init(IIC_SDA_PIN,IIC_SCL_PIN);
lcd.backlight();
//const uint8_t* msg = (uint8_t*)("hello\n");
//lcd.write(msg);
lcd.setCursor(0,0);
lcd.print("ESP32SYNTH");
lcd.setCursor(0,1);
lcd.print("BOKONTEP2019");
delay(2000);
while(true)
{
lcd.setCursor(0,0);
lcd.print(buf0);
lcd.setCursor(0,1);
lcd.print(buf1);
delay(100);
}
#endif
#ifdef ESP32TTGO_T2
tft.init();
tft.setRotation(3);
tft.fillScreen(TFT_BLUE);
tft.setTextSize(2);
tft.setTextColor(TFT_WHITE);
tft.setCursor(0, 0);
img.setColorDepth(8);
img.createSprite(tft.width(),tft.height());
img.fillSprite(TFT_BLUE);
img.setTextSize(2);
writeTFTLine("ESP32SYNTH");
writeTFTLine("(C) BOKONTEP");
writeTFTLine("2020 ");
//pinMode(TFT_BL, OUTPUT);
while(true)
{
//sprintf(line1,"%s",buf0);
//sprintf(line2,"%s",buf1);
//img.fillScreen(TFT_BLUE);
img.fillSprite(TFT_BLUE);
img.setTextColor(TFT_WHITE);
if(currline<numlines)
{
for(int i=0;i<numlines;i++)
{
img.drawString(&line[i][0],0,i*15);
}
}
else
{
for(int i=0;i<numlines;i++)
{
int idx = (currline-i)%numlines;
img.drawString(&line[idx][0],0,(numlines-1-i)*15);
}
}
img.drawString(line1,0,106);
img.drawString(line2,0,121);
for (int i=0;i<127;i++)
{
img.drawLine(i*1.875,(dwfbuf_l[i])>>1,(i+1)*1.875,(dwfbuf_l[i+1])>>1,TFT_YELLOW);
//img.drawLine(i*1.8,(dwfbuf_r[i]>>1),i+1,(dwfbuf_r[i+1]>>1),TFT_RED);
}
img.pushSprite(0,0);
delay(10);
}
#endif
}
void loop()
{
//testChords();
#ifndef NOMIDI
scanMidi();
#endif
//displayData();
}
void scanMidi()
{
pinMode(LED,OUTPUT);
if(firsttime)
{
//Serial.write(144);
//Serial.write(59);
//Serial.write(120);
//Serial.flush();
firsttime = false;
}
switch(mstate)
{
case WAIT_COMMAND:
serialData = hSerial.read();
if(serialData>-1)
{
commandByte = serialData;
command = (commandByte>>4)&7;
channel = commandByte & 15;
//Serial.write(144);
//Serial.write(command);
//Serial.write(channel);
//Serial.flush();
switch(command)
{
case 0: //NOTE OFF
mstate = WAIT_DATA1;
break;
case 1: //NOTE ON
mstate = WAIT_DATA1;
break;
case 2: //AFTERTOUCH
break;
case 3: //CONTINUOUS CONTROLLER (CC)
mstate = WAIT_DATA1;
break;
case 4: // PATCH CHANGE
break;
case 5: // CHANNEL PRESSURE
break;
case 6: // PITCH BEND
mstate = WAIT_DATA1;
break;
case 7: // NON MUSICAL COMMANDS
break;
}
}
return;
case WAIT_DATA1:
serialData = hSerial.read();
if(serialData>-1)
{
data1 = serialData;
switch(command)
{
case 0:
mstate = WAIT_DATA2;
break;
case 1:
mstate = WAIT_DATA2;
break;
case 3:
mstate = WAIT_DATA2;
break;
case 6:
mstate = WAIT_DATA2;
break;
}
}
return;
case WAIT_DATA2:
serialData = hSerial.read();
if(serialData>-1)
{
data2 = serialData;
switch(command)
{
case 0:
printMidiMessage(command,data1,data2);
handleNoteOff(channel,data1,data2);
mstate = WAIT_COMMAND;
break;
case 1:
printMidiMessage(command,data1,data2);
if(data2==0)
{
handleNoteOff(channel,data1,data2);
}
else
{
handleNoteOn(channel,data1,data2);
}
mstate = WAIT_COMMAND;
break;
case 3:
handleCC(channel, data1, data2,&value_pickup[0]);
mstate = WAIT_COMMAND;
break;
case 6:
handlePitchBend(channel,data1,data2);
mstate = WAIT_COMMAND;
break;
}
}
return;
}
}
void loadParameters()
{
}