-
Notifications
You must be signed in to change notification settings - Fork 2
/
receive.ino
245 lines (207 loc) · 7.94 KB
/
receive.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
//Is executed everytime a Sysex message is received
void sysExInterpreter(byte* data, unsigned messageLength) {
//check if this is a valid Sysex message
if (data[MANUFACTURER] == BASTL_MANUFACTURER_ID) {
//check the command byte and acts accordingly
switch (data[COMMAND]) {
case SETKNOBASGLOBALCC : //Sets a knob as a global CC knob
{
//PARAM 1 : which knob do we affect ?
//PARAM 2 : the CC number
if (data[PARAM1] < NUMBEROFKNOBS) {
uint8_t knobIndex = data[PARAM1];
activePreset.knobInfo[knobIndex].CC = data[PARAM2];
activePreset.knobInfo[knobIndex].NRPN = 0;
activePreset.knobInfo[knobIndex].SYSEX = 128;
//knob in normal mode by default
clearBits64(activePreset.invertBits, data[PARAM1]);
}
break;
}
case SETKNOBASINDEPCC : //Sets a knob as an independent CC knob
{
//PARAM 1 : which knob do we affect ?
//PARAM 2 : CC number
//PARAM 3 : the MIDI channel of that knob
if (data[PARAM1] < NUMBEROFKNOBS) {
uint8_t knobIndex = data[PARAM1];
activePreset.knobInfo[knobIndex].CC = data[PARAM2];
activePreset.knobInfo[knobIndex].NRPN = 0;
activePreset.knobInfo[knobIndex].SYSEX = data[PARAM3] | 0x80;
//knob in normal mode by default
clearBits64(activePreset.invertBits, data[PARAM1]);
}
break;
}
case DISABLEKNOB : //Sets a knob as an inactive CC knob
{
//PARAM 1 : which knob do we affect ?
if (data[PARAM1] < NUMBEROFKNOBS) {
uint8_t knobIndex = data[PARAM1];
activePreset.knobInfo[knobIndex].CC = 0; //bullshit CC
activePreset.knobInfo[knobIndex].NRPN = 0;
activePreset.knobInfo[knobIndex].SYSEX = 17 | 0x80; //out of range -> knob disabled
}
break;
}
case SETKNOBASBNRPN : //Sets a knob as a Bipolar NRPN (-63, +63) knob
{
//PARAM 1 : which knob do we affect ?
//PARAM 2 : NRPN number LSB
//PARAM 3 : NRPN number MSB
//PARAM 4 : NRPN range (-range to +range), range max : 63
if (data[PARAM1] < NUMBEROFKNOBS) {
uint8_t range = data[PARAM4];
uint8_t knobIndex = data[PARAM1];
activePreset.knobInfo[knobIndex].CC = data[PARAM2];
activePreset.knobInfo[knobIndex].NRPN = data[PARAM3] | 0x80;
if (range > 63) range = 63;
activePreset.knobInfo[knobIndex].SYSEX = 128 + range;
//knob in normal mode by default
clearBits64(activePreset.invertBits, data[PARAM1]);
}
break;
}
case SETKNOBASUNRPN : //Sets a knob as a Unipolar NRPN (0, +127) knob
{
//PARAM 1 : which knob do we affect ?
//PARAM 2 : NRPN number LSB
//PARAM 3 : NRPN number MSB
//PARAM 4 : NRPN range (0 to +range), max range : 127
if (data[PARAM1] < NUMBEROFKNOBS) {
uint8_t knobIndex = data[PARAM1];
activePreset.knobInfo[knobIndex].CC = data[PARAM2] | 0x80;
activePreset.knobInfo[knobIndex].NRPN = data[PARAM3] | 0x80;
activePreset.knobInfo[knobIndex].SYSEX = 128 + data[PARAM4];
//knob in normal mode by default
clearBits64(activePreset.invertBits, data[PARAM1]);
}
break;
}
//for the ranges +164, +200, +1600 and +2000
case SETKNOBASENRPN : //Sets a knob as an Extended Unipolar NRPN (0, XX) knob
{
//PARAM 1 : which knob do we affect ?
//PARAM 2 : NRPN number LSB
//PARAM 3 : NRPN number MSB
//PARAM 4 : NRPN range (1 : +164, 1 : +200, 1 : +1600, 1 : +2000)
if (data[PARAM1] < NUMBEROFKNOBS) {
uint8_t range = data[PARAM4];
uint8_t knobIndex = data[PARAM1];
activePreset.knobInfo[knobIndex].CC = data[PARAM2];
activePreset.knobInfo[knobIndex].NRPN = data[PARAM3] | 0x80;
switch (range) {
case 1 :
range = 63 + 1; //+164
break;
case 2 :
range = 63 + 2; //+200
break;
case 3 :
range = 63 + 3; //+1600
break;
case 4 :
range = 63 + 4; //+2000
break;
default :
range = 63 + 10; //wrong number -> no action
break;
}
activePreset.knobInfo[knobIndex].SYSEX = 128 + range;
//knob in normal mode by default
clearBits64(activePreset.invertBits, data[PARAM1]);
}
break;
}
case SETKNOBASDX : //Sets a knob as a DX7 parameter change knob
{
//PARAM 1 : which knob do we affect ?
//PARAM 2 : DX7 parameter number most significant bit
//PARAM 3 : DX7 parameter number last 7 bits
//PARAM 4 : maximum value that can be reached by that parameter
if (data[PARAM1] < NUMBEROFKNOBS) {
uint8_t knobIndex = data[PARAM1];
activePreset.knobInfo[knobIndex].CC = 0;
activePreset.knobInfo[knobIndex].NRPN = (data[PARAM2] << 7) | data[PARAM3];
activePreset.knobInfo[knobIndex].SYSEX = data[PARAM4];
//knob in normal mode by default
clearBits64(activePreset.invertBits, data[PARAM1]);
}
break;
}
case INVERTKNOB : //Sets a knob to be inverted or not
{
//PARAM1 : which knob do we affect ?
//PARAM2 : 0-> knob in normal mode; 1-> knob in invert mode
if (data[PARAM1] < NUMBEROFKNOBS) {
if (data[PARAM2] == 0) {
clearBits64(activePreset.invertBits, data[PARAM1]);
}
else {
activePreset.invertBits |= (((uint64_t)1) << data[PARAM1]);
//bitSet(invertBits, data[PARAM1]);
}
}
break;
}
case DROPNRPNLSB :
{
if(data[PARAM1] > 0) {
activePreset.dropNRPNLSBvalue = true;
} else {
activePreset.dropNRPNLSBvalue = false;
}
}
case PRESETSAVE : //Saves the current state of the machine to the specified slot
{
//PARAM 1 : the slot where the preset will be saved
if (data[PARAM1] < 5) {
savePreset(data[PARAM1]);
}
break;
}
case PRESETLOAD : //Sets the state of the machine according to the specified preset
{
//PARAM 1 : number of the preset to load
if (data[PARAM1] < 5) {
loadPreset(data[PARAM1]);
}
break;
}
case SYNCKNOBS : //Forces the emission of the messages associated to every knob
{
//NO PARAM
for (uint8_t i = 0; i < NUMBEROFKNOBS; i++) {
interpretKnob(i, true, false);
}
break;
}
case CHANNELCHANGE : //Changes the global MIDI channel according to the one specified
{
//PARAM 1 : MIDI channel (1 to 16)
if (data[PARAM1] < 17 && data[PARAM1] > 0) {
activePreset.channel = data[PARAM1];
}
break;
}
case RANDOMIZER : //Sends random CC messages forthe CC range specified
{
//PARAM 1 : Min CC number
//PARAM 2 : Max CC number
if (data[PARAM1] < 127 && data[PARAM2] >= data[PARAM1]) {
for (int i = data[PARAM1]; i <= data[PARAM2]; i++) {
MIDI.sendControlChange(i, random(0, 127), activePreset.channel);
}
}
break;
}
}
}
}
//Is executed everytime a MIDI Program Change message is received
//loads the specified preset
void handleProgramChange(byte channel, byte number) {
if (number < 5) {
loadPreset(number);
}
}