-
Notifications
You must be signed in to change notification settings - Fork 0
/
MIDI_CC_to_NOTEs.ino
284 lines (246 loc) · 8.19 KB
/
MIDI_CC_to_NOTEs.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
/* Receive Incoming USB Host MIDI using functions. As usbMIDI
reads incoming messages, handler functions are run.
See the InputRead example for the non-function alterative.
This very long example demonstrates all possible handler
functions. Most applications need only some of these.
This example is meant to allow easy copy-and-paste of the
desired functions.
Use the Arduino Serial Monitor to view the messages
as Teensy receives them by USB MIDI
You must select MIDI from the "Tools > USB Type" menu
This example code is in the public domain.
*/
// Need this file and also the name.c file in the folder. You can change the name.c file to reflect the usb midi name that appears
// need to upload this to a teensy in serial + MIDI mode so the teensy appears as a usb midi device when plugged into a console/computer
#include <USBHost_t36.h>
USBHost myusb;
USBHub hub1(myusb);
USBHub hub2(myusb);
MIDIDevice midi1(myusb);
int programm = 0;
int programmold = 0;
int programnew = 0;
void setup() {
Serial.begin(115200);
// Wait 1.5 seconds before turning on USB Host. If connected USB devices
// use too much power, Teensy at least completes USB enumeration, which
// makes isolating the power issue easier.
delay(500);
Serial.println("USB Host CC to Notes");
delay(10);
myusb.begin();
// midi1.setHandleNoteOn(myNoteOn);
// midi1.setHandleNoteOff(myNoteOff);
// midi1.setHandleAfterTouchPoly(myAfterTouchPoly);
midi1.setHandleControlChange(myControlChange);
//midi1.setHandleProgramChange(myProgramChange);
// midi1.setHandleAfterTouchChannel(myAfterTouchChannel);
// midi1.setHandlePitchChange(myPitchChange);
// Only one of these System Exclusive handlers will actually be
// used. See the comments below for the difference between them.
// midi1.setHandleSystemExclusive(mySystemExclusiveChunk);
// midi1.setHandleSystemExclusive(mySystemExclusive);
// midi1.setHandleTimeCodeQuarterFrame(myTimeCodeQuarterFrame);
// midi1.setHandleSongPosition(mySongPosition);
// midi1.setHandleSongSelect(mySongSelect);
// midi1.setHandleTuneRequest(myTuneRequest);
// midi1.setHandleClock(myClock);
// midi1.setHandleStart(myStart);
// midi1.setHandleContinue(myContinue);
// midi1.setHandleStop(myStop);
// midi1.setHandleActiveSensing(myActiveSensing);
// midi1.setHandleSystemReset(mySystemReset);
// This generic System Real Time handler is only used if the
// more specific ones are not set.
// midi1.setHandleRealTimeSystem(myRealTimeSystem);
}
void loop() {
// The handler functions are called when midi1 reads data. They
// will not be called automatically. You must call midi1.read()
// regularly from loop() for midi1 to actually read incoming
// data and run the handler functions as messages arrive.
myusb.Task();
midi1.read();
}
/*
void myNoteOn(byte channel, byte note, byte velocity) {
// When a USB device with multiple virtual cables is used,
// midi1.getCable() can be used to read which of the virtual
// MIDI cables received this message.
Serial.print("Note On, ch=");
Serial.print(channel, DEC);
Serial.print(", note=");
Serial.print(note, DEC);
Serial.print(", velocity=");
Serial.println(velocity, DEC);
}
void myNoteOff(byte channel, byte note, byte velocity) {
Serial.print("Note Off, ch=");
Serial.print(channel, DEC);
Serial.print(", note=");
Serial.print(note, DEC);
Serial.print(", velocity=");
Serial.println(velocity, DEC);
}
void myAfterTouchPoly(byte channel, byte note, byte velocity) {
Serial.print("AfterTouch Change, ch=");
Serial.print(channel, DEC);
Serial.print(", note=");
Serial.print(note, DEC);
Serial.print(", velocity=");
Serial.println(velocity, DEC);
}
*/
void myControlChange(byte channel, byte control, byte value) {
Serial.print("Control Change, ch=");
Serial.print(channel, DEC);
Serial.print(", control=");
Serial.print(control, DEC);
Serial.print(", value=");
Serial.println(value, DEC);
usbMIDI.sendNoteOn(control, value, channel);
}
/*
void myProgramChange(byte channel, byte program) {
Serial.print("Program Change, ch=");
Serial.print(channel, DEC);
Serial.print(", program=");
Serial.println(program);
programm = program;
if (programm < programmold)
{
Serial.println("Lesser");
Serial.println(programmold, DEC);
Serial.println(programm, DEC);
Serial.println(programnew, DEC);
programnew = 0;
}
if (programm > programmold)
{
Serial.println("Greater");
Serial.println(programmold, DEC);
Serial.println(programm, DEC);
Serial.println(programnew, DEC);
programnew = 1;
}
Serial.println(programmold, DEC);
Serial.println(programm, DEC);
Serial.println(programnew, DEC);
programmold = programm;
usbMIDI.sendNoteOn(51, programnew, channel);
}
/*
void myAfterTouchChannel(byte channel, byte pressure) {
Serial.print("After Touch, ch=");
Serial.print(channel, DEC);
Serial.print(", pressure=");
Serial.println(pressure, DEC);
}
void myPitchChange(byte channel, int pitch) {
Serial.print("Pitch Change, ch=");
Serial.print(channel, DEC);
Serial.print(", pitch=");
Serial.println(pitch, DEC);
}
// This 3-input System Exclusive function is more complex, but allows you to
// process very large messages which do not fully fit within the midi1's
// internal buffer. Large messages are given to you in chunks, with the
// 3rd parameter to tell you which is the last chunk. This function is
// a Teensy extension, not available in the Arduino MIDI library.
//
void mySystemExclusiveChunk(const byte *data, uint16_t length, bool last) {
Serial.print("SysEx Message: ");
printBytes(data, length);
if (last) {
Serial.println(" (end)");
} else {
Serial.println(" (to be continued)");
}
}
// This simpler 2-input System Exclusive function can only receive messages
// up to the size of the internal buffer. Larger messages are truncated, with
// no way to receive the data which did not fit in the buffer. If both types
// of SysEx functions are set, the 3-input version will be called by midi1.
//
void mySystemExclusive(byte *data, unsigned int length) {
Serial.print("SysEx Message: ");
printBytes(data, length);
Serial.println();
}
void myTimeCodeQuarterFrame(byte data) {
static char SMPTE[8]={'0','0','0','0','0','0','0','0'};
static byte fps=0;
byte index = data >> 4;
byte number = data & 15;
if (index == 7) {
fps = (number >> 1) & 3;
number = number & 1;
}
if (index < 8 || number < 10) {
SMPTE[index] = number + '0';
Serial.print("TimeCode: "); // perhaps only print when index == 7
Serial.print(SMPTE[7]);
Serial.print(SMPTE[6]);
Serial.print(':');
Serial.print(SMPTE[5]);
Serial.print(SMPTE[4]);
Serial.print(':');
Serial.print(SMPTE[3]);
Serial.print(SMPTE[2]);
Serial.print('.');
Serial.print(SMPTE[1]); // perhaps add 2 to compensate for MIDI latency?
Serial.print(SMPTE[0]);
switch (fps) {
case 0: Serial.println(" 24 fps"); break;
case 1: Serial.println(" 25 fps"); break;
case 2: Serial.println(" 29.97 fps"); break;
case 3: Serial.println(" 30 fps"); break;
}
} else {
Serial.print("TimeCode: invalid data = ");
Serial.println(data, HEX);
}
}
void mySongPosition(uint16_t beats) {
Serial.print("Song Position, beat=");
Serial.println(beats);
}
void mySongSelect(byte songNumber) {
Serial.print("Song Select, song=");
Serial.println(songNumber, DEC);
}
void myTuneRequest() {
Serial.println("Tune Request");
}
void myClock() {
Serial.println("Clock");
}
void myStart() {
Serial.println("Start");
}
void myContinue() {
Serial.println("Continue");
}
void myStop() {
Serial.println("Stop");
}
void myActiveSensing() {
Serial.println("Actvice Sensing");
}
void mySystemReset() {
Serial.println("System Reset");
}
void myRealTimeSystem(uint8_t realtimebyte) {
Serial.print("Real Time Message, code=");
Serial.println(realtimebyte, HEX);
}
*/
void printBytes(const byte *data, unsigned int size) {
while (size > 0) {
byte b = *data++;
if (b < 16) Serial.print('0');
Serial.print(b, HEX);
if (size > 1) Serial.print(' ');
size = size - 1;
}
}