forked from billieblaze/BBlazeSEQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi.h
106 lines (80 loc) · 2.09 KB
/
midi.h
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
int MIDIClockCounter = 0;
int tickCounter = 0;
int runSequencer=0;
int editMode=0;
int editParam=0;
int currentChannel = 0; // which channel are we viewing?
int currentStep = 0;
int lastNote[4] = {0,0,0,0};
int recordLastNote = 0;
int recordLastPosition = 0;
int curPosition = 0;
int keyOctave = 3;
void playNotes(){
for(int channel=0; channel <= channels; channel++){
int noteOn = patternData[channel][0][tickCounter];
if (noteOn == 0 ){
MIDI.sendNoteOff( lastNote[channel],0,channel+1);
digitalWriteFast(gate[channel ], LOW );
}
if ( noteOn == 1 ) {
int note = patternData[channel][1][tickCounter];
int velocity = patternData[channel][2][tickCounter];
MIDI.sendNoteOn( note, velocity,channel+1);
digitalWriteFast(gate[channel], HIGH);
lastNote[channel] = note;
}
// if (noteOn == 2) hold the note plz
}
}
// MIDI Callbacks
void HandleStart(){
MIDIClockCounter = 0;
tickCounter=0;
runSequencer = 1;
ShiftMatrixPWM.SetAll(0);
delay(1000);
// send patch numbers
//for(int channel=0; channel <= channels; channel++){
// MIDI.sendProgramChange(patternData[channel][3][0], channel+1);
// }
updateMatrix=1;
}
void HandleStop(){
runSequencer = 0;
for ( int i = 0; i < channels; i++){
MIDI.sendNoteOff(lastNote[i],0, i+1);
digitalWriteFast(gate[i], LOW);
}
}
void HandleClock(){
if( runSequencer == 1){
// play notes every time the clock divider rolls over
if (MIDIClockCounter == 0){
playNotes();
updateMatrix=1;
tickCounter++;
}
MIDIClockCounter++;
// bar complete - start over
if(tickCounter == 32){
tickCounter=0;
}
// reset the click divider
if (MIDIClockCounter == 6){
MIDIClockCounter=0;
}
}
}
// Setup Midi events
void setupMIDI(){
MIDI.begin();
MIDI.setHandleStart(HandleStart);
MIDI.setHandleClock(HandleClock);
MIDI.setHandleStop(HandleStop);
MIDI.setHandleContinue(HandleStart);
MIDI.turnThruOn();
for ( int i = 0; i < channels; i++){
pinMode(gate[i], OUTPUT);
}
}