-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteensypolysynth.ino
156 lines (140 loc) · 3.32 KB
/
teensypolysynth.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
#include <Audio.h>
#include "VAEngine.h"
const int WTCOUNT=128;
const int WTLEN=256;
float Waveforms[WTCOUNT*WTLEN];
VAEngine<16,256,256> vaEngine(&Waveforms[0]);
AudioOutputI2S i2s1;
AudioMixer4 mixer;
AudioConnection patchCord1(vaEngine, 0, mixer, 0);
AudioConnection patchCord2(mixer, 0, i2s1, 0);
AudioConnection patchCord3(mixer, 0, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1;
void initWaveForms()
{
for(int i=0;i<WTLEN;i++)
{
Waveforms[i] = ((sin(2.0 * (PI / (float)WTLEN) * i)));
}
for (int i = 0; i < 128; i++)
{
Waveforms[WTLEN+i] = ( (-1.0 + i * (1.0 / ((double)WTLEN / 2.0))));
}
for (int i = 128; i < 256; i++)
{
Waveforms[WTLEN+i] = ( (1.0 - i * (1.0 / ((double)WTLEN / 2.0))));
}
for (int i = 0; i < 256; i++)
{
Waveforms[2*WTLEN+i] = (i < (WTLEN / 2) ? 1.0 : -1.0);
}
for (int i = 0; i < 256; i++)
{
Waveforms[3*WTLEN+i] = ( (-1.0 + (2.0 / WTLEN) * i));
}
for (int i = 0; i < WTLEN; i++)
{
Waveforms[4*WTLEN+i] = -1.0+random(1UL<<31)* (2.0/(1UL<<31));
}
for (int w=5;w<WTCOUNT;w++)
{
for(int i=0;i<256;i++)
{
float f1 = (WTCOUNT-w)/120;
float f2 = ((WTCOUNT-w)%120)/110.0;
float f3 = ((WTCOUNT-w)%62)/60.0;
float f4 = ((WTCOUNT-w)%31)/31.0;
Waveforms[w*WTLEN+i] = ((f1*Waveforms[i]+f2*Waveforms[WTLEN+i]+f3*Waveforms[2*WTLEN+i]+f4*Waveforms[3*WTLEN+i])/4);
}
}
}
void OnNoteOn(byte channel, byte note, byte velocity)
{
vaEngine.handleNoteOn(channel,note,velocity);
}
void OnNoteOff(byte channel, byte note, byte velocity)
{
vaEngine.handleNoteOff(channel,note,velocity);
}
void OnControlChange(byte channel, byte control, byte value)
{
vaEngine.handleControlChange(channel, control, value);
}
void OnPitchChange(byte channel,int pitch)
{
vaEngine.handlePitchBend(channel,pitch+8192);
}
long lastrun=0;
void setup()
{
initWaveForms();
Serial.begin(115200);
Serial4.begin(115200);
delay(4000);
vaEngine.init(44100.0);
Serial.println("init done!");
AudioMemory(20);
sgtl5000_1.enable();
sgtl5000_1.volume(0.8);
mixer.gain(0, 0.7);
usbMIDI.setHandleNoteOn(OnNoteOn);
usbMIDI.setHandleNoteOff(OnNoteOff);
usbMIDI.setHandleControlChange(OnControlChange);
usbMIDI.setHandlePitchChange(OnPitchChange);
lastrun = millis();
ADSR adsr(44100.0);
for(int i=0;i<128;i++)
{
adsr.SetDecayMidi(i);
Serial.print(i);
Serial.print(":");
Serial.print(adsr.GetDecayCoef());
Serial.println();
}
Serial.println(1.43343434);
}
void loop()
{
usbMIDI.read();
serialReadHandler();
long now=millis();
if(now-lastrun>2000)
{
lastrun = now;
}
}
void serialReadHandler()
{
if(Serial4.available()>0)
{
Serial.println("data!");
}
while(Serial4.available()>=3)
{
byte b0 = Serial4.read();
byte b1 = Serial4.read();
byte b2 = Serial4.read();
byte channel = b0&0b1111;
byte message = b0&0b11110000;
Serial.print("CH:");
Serial.print(channel);
Serial.print(" MSG");
Serial.print(message,HEX);
Serial.print(" B1:");
Serial.print(b1,HEX);
Serial.print(" B2:");
Serial.println(b2,HEX);
if((b0&0b11110000)==0xB0)
{
OnControlChange(channel,b1,b2);
}
if((b0&0b11110000)==0x80)
{
OnNoteOff(channel,b1,b2);
}
if((b0&0b11110000)==0x90)
{
OnNoteOn(channel,b1,b2);
}
}
}