-
Notifications
You must be signed in to change notification settings - Fork 1
/
midi.ino
218 lines (164 loc) · 4.82 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
#define CONTROL_CHANGE_BITS 7
#define CONTROL_CHANGE_OFFSET 3
unsigned char midiSound;
#define SWITCH_BYTE 14
#define PAGE_BYTE 13
#define BASTL_BYTE 0x7D
#define CHANNEL_BYTE 0x00
#define INSTRUMENT_BYTE 0X01 // poly 1.0
#define TEST_BYTE 0x0A
#define NUMBER_OF_MESSAGE_BYTES 2
#define NUMBER_OF_MESSAGE_BYTES 16
#define NUMBER_OF_SYSEX_BYTES 14
#define PRESET_BY_CC_BYTE 0
#define RANDOMIZE_BYTE 127
unsigned char sysExArray[NUMBER_OF_SYSEX_BYTES];
void HandleNoteOn(byte channel, byte note, byte velocity) {
if(channel==inputChannel){
if (velocity == 0) {
HandleNoteOff(channel,note,velocity);
}
else{
playSound(note%6, velocity);
if(shift && (note%6)>=3) hw.setLed(note%3,true);
else if(note%6<3) hw.setLed(note%3,true);
midiSound=note%6;
}
}
}
void HandleNoteOff(byte channel, byte note, byte velocity){
/*
unsigned char voice=note%3;
unsigned char _sound=note%6;
if(channel==inputChannel){
if(currentSound[voice]==_sound) ;
}
*/
}
void HandleControlChange(byte channel, byte number, byte value){
// implement knob movement
if(channel==inputChannel){
if(number==PRESET_BY_CC_BYTE) loadPreset(map(value,0,128,0,NUMBER_OF_PRESETS)), hw.freezeAllKnobs();
else if(number==RANDOMIZE_BYTE) randomize(midiSound);
else if((number>=CONTROL_CHANGE_OFFSET && number<39)){
number=number-CONTROL_CHANGE_OFFSET;
midiSound=number/NUMBER_OF_VARIABLES;
number=number%NUMBER_OF_VARIABLES;
setVar(midiSound,number,scale(value,CONTROL_CHANGE_BITS,variableDepth[number]));
hw.freezeAllKnobs();
renderTweaking((number)/VARIABLES_PER_PAGE);
/*
if((number<NUMBER_OF_VARIABLES){
setVar(midiSound,number,scale(value,CONTROL_CHANGE_BITS,variableDepth[number]));
hw.freezeAllKnobs();
renderTweaking((number)/VARIABLES_PER_PAGE);
}
*/
}
/*
else if(number==PRESET_BY_CC_BYTE)
else if(number==SUSTAIN_PEDAL_BYTE) sustainPedal=value>>6;
else if(number==RANDOMIZE_BYTE)
*/
}
if(test){
if(number==SWITCH_BYTE){
for(int i=0;i<3;i++) hw.setSwitch(i,bitRead(value,i));
}
else if(number==PAGE_BYTE){
page=value;
}
}
}
void HandleProgramChange(byte channel, byte number ){
// implement preset change
}
void HandlePitchBend(byte channel, int bend){
}
void HandleSystemExclusive(byte *array, byte size){
if(array[1]==BASTL_BYTE){
if(array[2]==CHANNEL_BYTE){
inputChannel=array[2];
array[2]++;
MIDI.sendSysEx(size,array);
}
else if(array[2]==INSTRUMENT_BYTE){
hw.setColor(WHITE);
hw.update();
for(int i=0;i<NUMBER_OF_SYSEX_BYTES;i++) sysExArray[i]=array[i+1];
extractSysExArray(sound);
hw.freezeAllKnobs();
}
else if(array[2]==TEST_BYTE) test=true, MIDI.turnThruOff(), MIDI.sendSysEx(NUMBER_OF_MESSAGE_BYTES,array,false);
}
}
void HandleSongSelect(byte songnumber){
}
void HandleClock(){
}
void HandleStart(){
}
void HandleContinue(){
}
void HandleStop(){
}
void indicateMidiChannel(unsigned char _channel){
boolean highChannel=false;
if(_channel>=10) hw.setLed(LED_1,true), hw.setLed(LED_2,true), hw.setLed(LED_3,true), highChannel=true;
for(int i=0;i<3;i++){
hw.setLed(_channel-1-highChannel*9,false);
hw.update();
delay(150);
hw.setLed(_channel-1-highChannel*9,true);
hw.update();
delay(150);
}
}
void initMidi(unsigned char _channel){
MIDI.begin(0);
inputChannel=_channel;
indicateMidiChannel(_channel);
MIDI.setHandleNoteOn(HandleNoteOn);
MIDI.setHandleNoteOff(HandleNoteOff);
MIDI.setHandlePitchBend(HandlePitchBend);
MIDI.setHandleControlChange(HandleControlChange);
MIDI.setHandleProgramChange(HandleProgramChange);
MIDI.setHandleSystemExclusive(HandleSystemExclusive);
MIDI.setHandleSongSelect(HandleSongSelect);
MIDI.setHandleClock(HandleClock);
MIDI.setHandleStart(HandleStart);
MIDI.setHandleContinue(HandleContinue);
MIDI.setHandleStop(HandleStop);
MIDI.turnThruOn(Full);
// MIDI.turnThruOff();
}
void sendSysExArray(){
MIDI.sendSysEx(NUMBER_OF_MESSAGE_BYTES,sysExArray,false);
}
void makeSysExArray(unsigned char _sound){
unsigned char _bit=0;
unsigned char _byte=0;
sysExArray[0]=BASTL_BYTE;
sysExArray[1]=INSTRUMENT_BYTE;
for(int i=0;i<NUMBER_OF_VARIABLES;i++){
int readFrom=getVar(_sound,i);
for(int j=0;j<variableDepth[i];j++){
_bit++;
if(_bit>=7) _bit=0,_byte++;
bitWrite(sysExArray[_byte+2],_bit, bitRead(readFrom,j));
}
}
}
void extractSysExArray(unsigned char _sound){
unsigned char _bit=0;
unsigned char _byte=0;
for(int i=0;i<NUMBER_OF_VARIABLES;i++){
int writeTo=0;
for(int j=0;j<variableDepth[i];j++){
_bit++;
if(_bit>=7) _bit=0,_byte++;
bitWrite(writeTo,j,bitRead(sysExArray[_byte+2],_bit));
}
setVar(_sound,i,writeTo);
}
}