-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpio_midi.ino
290 lines (250 loc) · 7.65 KB
/
gpio_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
/*
* This turns an arduino into a USB MIDI instrument.
*
* Author: Rudolf W Byker
*/
#include "MIDIUSB.h"
#include "midiUtil.h"
#include "analogMux.h"
#include "analogControl.h"
#include "digitalControl.h"
#define NOTE_VELOCITY 64
uint8_t numNotes = 0;
uint8_t numMuxes = 0;
uint8_t numAnalogControls = 0; // Maximum `numMuxes*8`
uint8_t numDigitalControls = 0;
struct DigitalControl * notes;
struct AnalogMux8 * muxes;
struct AnalogControl * analogControls;
struct DigitalControl * digitalControls;
void setup() {
Serial.begin(115200);
while (!Serial) { ; }
// Arduino Due has 12-bit ADCs.
analogReadResolution(12);
// Choose one, or make your own:
// setupDolfOrganUpper();
// setupDolfOrganLower();
setupDolfOrganPedal();
printNotes();
printAnalogControls();
printDigitalControls();
}
/**
* Setup the Arduino for the upper ("swell") keyboard on Dolf's organ.
*/
void setupDolfOrganUpper() {
// 61 notes on a 5-octave keyboard.
numNotes = 61;
notes = (struct DigitalControl*) malloc(numNotes * sizeof *notes);
if (notes == NULL) {
Serial.println("Error: Unable to allocate memory for notes.");
Serial.flush();
return;
}
createConsecutiveDigitalControlsInArray(
notes,
numNotes,
2, // MIDI channels in Hauptwerk are numbered starting from the pedal at 0 and going up.
36, // Note number 36 is C2 in MIDI.
2 // Leave pins 0 and 1 open for the serial port.
);
}
/**
* Setup the Arduino for the lower ("great") keyboard on Dolf's organ.
*/
void setupDolfOrganLower() {
// 61 notes on a 5-octave keyboard.
numNotes = 61;
notes = (struct DigitalControl*) malloc(numNotes * sizeof *notes);
if (notes == NULL) {
Serial.println("Error: Unable to allocate memory for notes.");
Serial.flush();
return;
}
createConsecutiveDigitalControlsInArray(
notes,
numNotes,
1, // MIDI channels in Hauptwerk are numbered starting from the pedal at 0 and going up.
36, // Note number 36 is C2 in MIDI.
2 // Leave pins 0 and 1 open for the serial port.
);
}
/**
* Setup the Arduino for the pedals on Dolf's organ.
*/
void setupDolfOrganPedal() {
uint8_t midiChannel = 0; // MIDI channels in Hauptwerk are numbered starting from the pedal at 0 and going up.
// 25 notes on a 3-octave pedal.
numNotes = 25;
notes = (struct DigitalControl*) malloc(numNotes * sizeof *notes);
if (notes == NULL) {
Serial.println("Error: Unable to allocate memory for notes.");
Serial.flush();
return;
}
createConsecutiveDigitalControlsInArray(
notes,
numNotes,
midiChannel,
36, // Note number 36 is C2 in MIDI.
2 // Leave pins 0 and 1 open for the serial port.
);
// Because my pedal is only 3 octaves, I have many open pins for analog and digital controls on this arduino.
// Each 8-bit mux requires 3 selector pins.
// The analog pins are consecutive, luckily, so A0+1 = A1, etc.
numMuxes = 6;
muxes = (struct AnalogMux8*) malloc(numMuxes * sizeof *muxes);
if (muxes == NULL) {
Serial.println("Error: Unable to allocate memory for muxes.");
Serial.flush();
return;
}
createConsecutiveMuxesInArray(
// Mux1 to Mux3 use D45 to D53 as selector pins and A0 to A2 as analog input pins.
45,
A0,
INPUT,
0,
3,
muxes
);
createConsecutiveMuxesInArray(
// Mux4 to Mux6 use D61 to D69 as selector pins and A3 to A5 as analog input pins.
61,
A3,
INPUT,
0,
3,
&muxes[3]
);
uint8_t i;
for (i=0; i<numMuxes; i++) {
analogMux8_init(&muxes[i]);
}
// 6 Muxes provide 48 analog inputs, but we use fewer.
numAnalogControls = 44;
analogControls = (struct AnalogControl*) malloc(numAnalogControls * sizeof *analogControls);
if (analogControls == NULL) {
Serial.println("Error: Unable to allocate memory for analog controls.");
Serial.flush();
return;
}
createConsecutiveAnalogControlsInArray(
muxes,
numMuxes,
analogControls,
numAnalogControls,
midiChannel,
0
);
// Reassign control from MIDI control number 6 to 65, because Hauptwerk does not respond to 6 for some unknown reason.
analogControls[6].number = 65;
for (i=0; i<numAnalogControls; i++) {
analogControl_init(&analogControls[i]);
}
numDigitalControls = 2;
digitalControls = (struct DigitalControl*) malloc(numDigitalControls * sizeof *digitalControls);
if (digitalControls == NULL) {
Serial.println("Error: Unable to allocate memory for digital controls.");
Serial.flush();
return;
}
createConsecutiveDigitalControlsInArray(
digitalControls,
numDigitalControls,
midiChannel,
48,
27
);
}
void loop() {
uint8_t i;
// TODO: We probably need to read notes more frequently than controls.
for (i=0; i<numNotes; i++) {
digitalControl_map(¬es[i], noteChange);
}
for (i=0; i<numAnalogControls; i++) {
analogControl_map(&analogControls[i], analogControlChange);
}
for (i=0; i<numDigitalControls; i++) {
digitalControl_map(&digitalControls[i], digitalControlChange);
}
MidiUSB.flush();
}
void printNotes() {
Serial.println("<notes>");
Serial.println("i,pin,pitch");
uint8_t i;
for (i=0; i<numNotes; i++) {
Serial.print(i);
Serial.print(",");
Serial.print(notes[i].pin);
Serial.print(",");
Serial.print(notes[i].midiControlNumber);
Serial.println();
}
Serial.println("</notes>");
Serial.flush();
}
void printAnalogControls() {
Serial.println("<analogControls>");
Serial.println("i,analogPin,channel,number,selector,value");
uint8_t i;
for (i=0; i<numAnalogControls; i++) {
Serial.print(i);
Serial.print(",");
Serial.print(analogControls[i].mux->analogPin);
Serial.print(",");
Serial.print(analogControls[i].channel);
Serial.print(",");
Serial.print(analogControls[i].number);
Serial.print(",");
Serial.print(analogControls[i].selector);
Serial.print(",");
Serial.print(analogControls[i].value);
Serial.println();
}
Serial.println("</analogControls>");
Serial.flush();
}
void printDigitalControls() {
Serial.println("<digitalControls>");
Serial.println("i,pin,midiChannel,midiControlNumber,value,on");
uint8_t i;
for (i=0; i<numDigitalControls; i++) {
Serial.print(i);
Serial.print(",");
Serial.print(digitalControls[i].pin);
Serial.print(",");
Serial.print(digitalControls[i].midiChannel);
Serial.print(",");
Serial.print(digitalControls[i].midiControlNumber);
Serial.print(",");
Serial.print(digitalControls[i].value);
Serial.print(",");
Serial.print(digitalControls[i].on);
Serial.println();
}
Serial.println("</digitalControls>");
Serial.flush();
}
void noteChange(const struct DigitalControl * const control) {
if (control->on) {
MidiUSB.sendMIDI(MIDI_EVENT_NOTE_ON(control->midiChannel, control->midiControlNumber, NOTE_VELOCITY));
} else {
MidiUSB.sendMIDI(MIDI_EVENT_NOTE_OFF(control->midiChannel, control->midiControlNumber, NOTE_VELOCITY));
}
}
void analogControlChange(const struct AnalogControl * const control) {
// MIDI control values are between 0 and 127 (7-bit unsigned).
// The 12-bit ADC gives values between 0 and 4095 (12-bit unsigned).
// We need to scale the ADC values to fit in the MIDI range.
// We can do this by bit shifting (>> 5), which is the same as dividing by 32.
MidiUSB.sendMIDI(MIDI_EVENT_CONTROL_CHANGE(control->channel, control->number, control->value >> 5));
}
void digitalControlChange(const struct DigitalControl * const control) {
// MIDI control values are between 0 and 127 (7-bit unsigned).
// Digital controls are either on or off.
MidiUSB.sendMIDI(MIDI_EVENT_CONTROL_CHANGE(control->midiChannel, control->midiControlNumber, control->on ? 127 : 0));
}