-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMIDI.ino
376 lines (303 loc) · 9.78 KB
/
MIDI.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
// op qr st uv wx yz
unsigned char incomingByte,note;
boolean ignore, comandOff,slave,cc; //,midiNoteOn noteOn,
unsigned char state=0;
unsigned char channel=0;
int clockCounter;
#define MIDI_CHANNEL 1023
#define POLYPHONY NUMBER_OF_VOICES
unsigned char notesInBuffer=ZERO;
boolean thereIsNoteToPlay;
unsigned char sound, activeSound;
#define BUFFER_SIZE 16
unsigned char midiBuffer[BUFFER_SIZE];
#define EMPTY 255
unsigned char midiVelocity;
unsigned char fromBuffer;
unsigned char instantLoop;
boolean sustain;
boolean legato;
unsigned char channelSide=0;
unsigned char sideNote=1;
unsigned char sideDecay;
//int pitchBendNow;
int sampleRateNow;
unsigned char setting;
int attackInterval, releaseInterval;
//long legatoPosition;
PROGMEM prog_uint16_t noteSampleRateTable[49]={/*0-C*/
2772,2929,3103,3281,3500,3679,3910,4146,4392,4660,4924,5231,5528,5863,6221,6579,6960,7355,7784,8278,8786,9333,9847,10420,11023,11662,12402,13119,13898,14706,15606,16491,17550,18555,19677,20857,22050,23420,24807,26197,27815,29480,29480,29480,29480,29480,29480,29480,/*48-C*/29480};
void shiftBufferLeft(unsigned char from){
for(int i=from;i<notesInBuffer;i++){
midiBuffer[i]=midiBuffer[i+1];
}
}
void shiftBufferRight(){
for(int i=notesInBuffer;i>ZERO;i--){
midiBuffer[i]=midiBuffer[i-1];
}
}
boolean isThereNoteToPlay(){
return thereIsNoteToPlay;
}
unsigned char noteToPlay(){
thereIsNoteToPlay=false;
return midiBuffer[fromBuffer];
}
void putNoteIn(unsigned char note){
if(note<6) hw.freezeAllKnobs();
if(notesInBuffer==BUFFER_SIZE-1) removeNote(midiBuffer[BUFFER_SIZE-1]);
removeNote(note); // check if the note is already in the buffer if yes remove it
if(notesInBuffer<BUFFER_SIZE){ //put the note to the first position
if(notesInBuffer>ZERO){
shiftBufferRight();
}
midiBuffer[ZERO]=note; // put the last note to the first place
notesInBuffer++;
thereIsNoteToPlay=true;
fromBuffer=ZERO;
}
if(thereIsNoteToPlay) {
if(legato && note>=23 && note<66 && notesInBuffer>1) sound=note,sampleRateNow=(pgm_read_word_near(noteSampleRateTable+sound-23)),wave.setSampleRate(sampleRateNow);
else playSound(midiBuffer[ZERO]);
}
// hw.freezAllKnobs();
}
void clearBuffer(){
for(int i=ZERO;i<BUFFER_SIZE;i++) midiBuffer[i]=EMPTY;
notesInBuffer=0;
}
boolean removeNote(unsigned char note){
if(notesInBuffer>ZERO){
unsigned char takenOut;
boolean takeOut=ZERO;
for(int i=ZERO;i<notesInBuffer;i++){
if(midiBuffer[i]==note) takeOut=true, takenOut=i;
}
if(takeOut){
shiftBufferLeft(takenOut);
notesInBuffer--;
for(int i=notesInBuffer;i<BUFFER_SIZE;i++) midiBuffer[i]=EMPTY;
return true;
}
else return false;
}
}
unsigned char putNoteOut(unsigned char note){
if(note<6) hw.freezeAllKnobs();
if(removeNote(note)){
if(notesInBuffer>0){
if(midiBuffer[ZERO]!=sound) {
if(legato && midiBuffer[ZERO]>=23 && midiBuffer[ZERO]<66) sound=midiBuffer[ZERO],sampleRateNow=(pgm_read_word_near(noteSampleRateTable+sound-23)),wave.setSampleRate(sampleRateNow);
else playSound(midiBuffer[ZERO]),instantLoop=0;
} //legatoPosition=wave.getCurPosition(),
}
else if(!sustain) stopEnvelope(),instantLoop=0;
return midiBuffer[ZERO];
}
}
void initMidi(){
clearBuffer();
readMidiChannel();
Serial.begin(MIDI_BAUD);
}
#define SIDE_CHANNEL 1022
#define SIDE_NOTE 1021
#define SIDE_DECAY 1020
unsigned char controler, CCvalue;
void readMidiChannel(){
channelSide=EEPROM.read(SIDE_CHANNEL);
sideNote=EEPROM.read(SIDE_NOTE);
sideDecay=EEPROM.read(SIDE_DECAY);
channel=EEPROM.read(MIDI_CHANNEL);
if(channel>16) EEPROM.write(MIDI_CHANNEL,0), channel=0;
showValue(channel+1);
hw.displayChar('C',0);
hw.displayChar('H',1);
hw.update();
for(int i=0;i<6;i++){
if(hw.buttonState(bigButton[i])){
if(hw.buttonState(UP) && hw.buttonState(DOWN)) sideDecay=i, EEPROM.write(SIDE_DECAY,sideDecay), showValue(sideDecay), hw.displayChar('S',0), hw.displayChar('D',1);
else if(hw.buttonState(UP)) channelSide=i+6*hw.buttonState(FN), EEPROM.write(SIDE_CHANNEL,channelSide), showValue(channelSide+1), hw.displayChar('S',0), hw.displayChar('C',1);
else if(hw.buttonState(DOWN)) sideNote=i+60*hw.buttonState(FN), EEPROM.write(SIDE_NOTE,sideNote), showValue(sideNote), hw.displayChar('S',0), hw.displayChar('N',1);
else channel=i+6*hw.buttonState(FN),EEPROM.write(MIDI_CHANNEL,channel);
}
}
if(wave.isPlaying()){
while(!wave.isPaused()) hw.update();
}
stopSound();
}
long lastClockPosition, clockLength;
//unsigned char pByte1,pByte2;
//boolean pb;
boolean side;
int bytesAvailable;
void readMidi(){
//channel=map(analogRead(4),0,1024,0,16);
while(Serial.available() > 0){
bytesAvailable=Serial.available();
if (bytesAvailable <= 0) return;
if(bytesAvailable>=64) Serial.flush(); // If the buffer is full -> Don't Panic! Call the Vogons to destroy it.
else {
// read the incoming byte:
unsigned char incomingByte = Serial.read();
Serial.write(incomingByte); // thru
switch (state){
case 0:
if( handleRealTime(incomingByte));
else if(incomingByte < 128) { // if running status byte
if(!ignore){ //
if(cc) controler=incomingByte;
// else if(pb) pByte1=incomingByte;
else note=incomingByte;
state=2;
}
}
if (incomingByte== (144 | channel)){ // look for as status-byte, our channel, note on
state=1;
ignore=false;
cc=false;
comandOff=false;
side=false;
// pb=false;
}
else if (incomingByte== (0x80 | channel)){ // look for as status-byte, our channel, note off
state=1;
ignore=false;
comandOff=true;
cc=false;
side=false;
// pb=false;
}
else if (incomingByte== (0xB0 | channel)){ // look for as status-byte, our channel, controlchange
state=1;
ignore=false;
cc=true;
comandOff=false;
side=false;
// comandOff=true;
}
/*
else if (incomingByte== (0xE0 | channel)){ //pitchBend
state=1;
ignore=false;
cc=false;
pb=true;
}
*/
else if (incomingByte== (144 | channelSide)){ // look for as status-byte, our channel, note on
state=1;
ignore=false;
cc=false;
side=true;
// pb=false;
}
else if(incomingByte>127){
ignore=true;
}
break;
case 1:
// get the note to play or stop
if(incomingByte < 128) {
if(cc) controler=incomingByte;
// else if(pb) pByte1=incomingByte;
else note=incomingByte;
state=2;
}
else{
if(!handleRealTime(incomingByte)) state = 0;
// reset state machine as this should be a note number
}
break;
case 2: // get the velocity / cc value
if(incomingByte < 128) {
if(cc){
CCvalue=incomingByte;
proceedCC(controler,CCvalue);
}
//else if(pb) pByte2=incomingByte, proceedPB(pByte1,pByte2);
else if(side) proceedSideChain(note),side=false;
else{
if(incomingByte!=0){
if(comandOff){
putNoteOut(note);
comandOff=false;
}
else{
midiVelocity=incomingByte;
putNoteIn(note);
// hw.freezeAllKnobs();
// midiNoteOn=true;
}
}
else{
putNoteOut(note);
comandOff=false;
// midiNoteOn=false;
}
}
state = 0;
}
else if(!handleRealTime(incomingByte)) state = 0;
// state=0;
}
// Serial.write(incomingByte);
}
}
}
void proceedSideChain(unsigned char _note){
if(_note==sideNote){
if(sideDecay==0) startEnvelope(midiVelocity,attackInterval);
else startEnvelope(midiVelocity,sideDecay<<2);
}
}
boolean handleRealTime(unsigned char _incomingByte){
if(_incomingByte==0xF8){ //clock
clockCounter++;
// sync shiftSpeed - unnecessary
// clockLength=abs(wave.getCurPosition()-lastClockPosition);
// sync shiftSpeed - unnecessary
slave=true;
return true;
}
else if(_incomingByte==0xFA){ //start
clockCounter=0;
slave=true;
return true;
}
else if(_incomingByte==0xFC){ //stop
clockCounter=0;
slave=true;
return true;
}
else return false;
}
#define SUSTAIN_PEDAL_BYTE 64
#define PRESET_BY_CC_BYTE 0
#define RANDOMIZE_BYTE 127
#define CONTROL_CHANGE_BITS 7
#define CONTROL_CHANGE_OFFSET 102
#define CONTROL_CHANGE_OFFSET_2 110
void proceedCC(unsigned char _number,unsigned char _value){
if(_number==1) setVar(activeSound,LOOP_LENGTH,scale(_value,CONTROL_CHANGE_BITS,variableDepth[LOOP_LENGTH])), hw.unfreezeAllKnobs(),renderTweaking(1),hw.freezeAllKnobs(); //modwheel
if(_number==SUSTAIN_PEDAL_BYTE){
sustain=_value>>6;
if(!sustain && notesInBuffer==0) stopEnvelope(),instantLoop=0;
}
if(_number==PRESET_BY_CC_BYTE) loadPreset(currentBank,myMap(_value,128,NUMBER_OF_PRESETS));
// else if(_number==RANDOMIZE_BYTE) randomize(activeSound);
else if(_number>=CONTROL_CHANGE_OFFSET && _number<CONTROL_CHANGE_OFFSET_2){
_number=_number-CONTROL_CHANGE_OFFSET;
setVar(activeSound,_number,scale(_value,CONTROL_CHANGE_BITS,variableDepth[_number]));
hw.unfreezeAllKnobs();
renderTweaking(_number/VARIABLES_PER_PAGE);
hw.freezeAllKnobs();
}
}
/*
void proceedPB(unsigned char _byte1,unsigned char _byte2){
int pitchBendNow=word(_byte1,_byte2)-8192;
wave.setSampleRate(sampleRateNow+pitchBendNow);
}
*/