-
Notifications
You must be signed in to change notification settings - Fork 3
/
midi_handles.ino
209 lines (172 loc) · 4.14 KB
/
midi_handles.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
#include "midi_handles.h"
/*********************
NOTE ON
********************/
void HandleNoteOn(byte channel, byte note, byte velocity)
{
//Serial.println(note);
byte min_rank = 255;
int empty_arg = -1;
for (byte i = 0; i < POLYPHONY; i++) //take a non playing oscil
{
if (!envelope[i].playing() && oscil_rank[i] < min_rank)
{
empty_arg = i;
min_rank = oscil_rank[i];
//break;
}
}
if (empty_arg == -1) //kill a oscil in release phase
{
min_rank = 255;
for (byte i = 0; i < POLYPHONY; i++)
{
if (oscil_state[i] == 0 && oscil_rank[i] < min_rank)
{
empty_arg = i;
min_rank = oscil_rank[i];
}
}
}
if (empty_arg != -1) //an empty oscil has been found
{
notes[empty_arg] = note;
set_freq(empty_arg);
//digitalWrite(LED, HIGH);
envelope[empty_arg].setADLevels(velocity, velocity);
//envelope[empty_arg].setADLevels(velocity << 1, velocity);
envelope[empty_arg].noteOn();
oscil_state[empty_arg] = 1;
byte max_rank = 0;
for (byte i = 0; i < POLYPHONY; i++)
{
if (oscil_rank[i] > max_rank) max_rank = oscil_rank[i];
}
oscil_rank[empty_arg] = max_rank + 1;
}
else // no empty oscil, kill one in sustain mode! (the oldest)
{
min_rank = 255;
byte min_rank_arg = 0;
for (byte i = 0; i < POLYPHONY; i++)
{
if (oscil_rank[i] < min_rank)
{
min_rank = oscil_rank[i];
min_rank_arg = i;
}
}
//Serial.println(min_rank_arg);
notes[min_rank_arg] = note;
envelope[min_rank_arg].setADLevels(velocity, velocity);
set_freq(min_rank_arg);
envelope[min_rank_arg].noteOn();
oscil_state[min_rank_arg] = 1;
//for (byte i = 0; i < POLYPHONY; i++) oscil_rank[i] -= min_rank;
byte max_rank = 0;
for (byte i = 0; i < POLYPHONY; i++)
{
if (oscil_rank[i] > max_rank) max_rank = oscil_rank[i];
}
oscil_rank[min_rank_arg] = max_rank + 1;
//Serial.println(max_rank);
}
// shift all oscill
min_rank = 255;
for (byte i = 0; i < POLYPHONY; i++)
{
if (oscil_rank[i] < min_rank)
{
empty_arg = i;
min_rank = oscil_rank[i];
}
}
if (min_rank != 0) for (byte i = 0; i < POLYPHONY; i++) oscil_rank[i] -= min_rank;
}
/**********************
NOTE OFF
*********************/
void HandleNoteOff(byte channel, byte note, byte velocity)
{
byte to_kill = 255;
byte min_rank = 255;
for (byte i = 0; i < POLYPHONY; i++)
{
if (note == notes[i] && oscil_state[i] == 1 && oscil_rank[i] < min_rank )
{
min_rank = oscil_rank[i];
to_kill = i;
}
}
if (to_kill != 255)
{
if (!sustain)
{
envelope[to_kill].noteOff();
oscil_state[to_kill] = 0;
}
else oscil_state[to_kill] = 2;
//envelope[0].noteOff();
for (byte i = 0; i < POLYPHONY; i++)
{
if (oscil_rank[i] > oscil_rank[to_kill]) oscil_rank[i] -= 1;
}
}
}
/*************************
CONTROL CHANGE
************************/
void HandleControlChange(byte channel, byte control, byte val)
{
switch (control)
{
case 64: // sustain
{
if (val == 0)
{
sustain = false;
for (byte i = 0; i < POLYPHONY; i++)
{
if (oscil_state[i] == 2)
{
envelope[i].noteOff();
oscil_state[i] = 0;
}
}
}
else
{
sustain = true;
}
break;
}
case 1: //modulation
{
#ifndef VIBRATO
for (byte i = 0; i < POLYPHONY; i++) LFO[i].setFreq_Q16n16((Q16n16) (val << 14 ));
#else
LFO[0].setFreq_Q16n16((Q16n16) (val << 13 ));
if (val == 0 && mod) mod = false;
else if (val != 0 && !mod) mod = true;
#endif
}
}
}
/**********************
PITCHBEND
* ***************/
void HandlePitchBend(byte channel, int bend)
{
pitchbend = bend;
for (byte i = 0; i < POLYPHONY; i++)
{
if (envelope[i].playing()) set_freq(i, false);
}
}
/*********************
AFTERTOUCH
* *******************/
void HandleAfterTouchChannel(byte channel, byte after)
{
aftertouch = after;
}