|
| 1 | +#include <MIDI.h> |
| 2 | + |
| 3 | +#if defined(USBCON) |
| 4 | +#include <midi_UsbTransport.h> |
| 5 | + |
| 6 | +static const unsigned sUsbTransportBufferSize = 16; |
| 7 | +typedef midi::UsbTransport<sUsbTransportBufferSize> UsbTransport; |
| 8 | + |
| 9 | +UsbTransport sUsbTransport; |
| 10 | + |
| 11 | +MIDI_CREATE_INSTANCE(UsbTransport, sUsbTransport, MIDI); |
| 12 | + |
| 13 | +#else // No USB available, fallback to Serial |
| 14 | +MIDI_CREATE_DEFAULT_INSTANCE(); |
| 15 | +#endif |
| 16 | + |
| 17 | +// -- |
| 18 | + |
| 19 | +void setupRxBenchmark() |
| 20 | +{ |
| 21 | + MIDI.setHandleNoteOff(handleNoteOff); |
| 22 | + MIDI.setHandleNoteOn(handleNoteOn); |
| 23 | + MIDI.setHandleControlChange(handleControlChange); |
| 24 | + MIDI.setHandleProgramChange(handleProgramChange); |
| 25 | + MIDI.setHandleAfterTouchChannel(handleAfterTouchChannel); |
| 26 | + MIDI.setHandleAfterTouchPoly(handleAfterTouchPoly); |
| 27 | + MIDI.setHandlePitchBend(handlePitchBend); |
| 28 | + MIDI.setHandleStart(handleStart); |
| 29 | + MIDI.setHandleStop(handleStop); |
| 30 | + MIDI.setHandleContinue(handleContinue); |
| 31 | + MIDI.setHandleClock(handleClock); |
| 32 | + MIDI.setHandleSystemExclusive(handleSysEx); |
| 33 | +} |
| 34 | + |
| 35 | +void startTxBenchmark() |
| 36 | +{ |
| 37 | + for (int i = 0; i < 128; ++i) |
| 38 | + { |
| 39 | + MIDI.sendNoteOn(i, 127, 1); |
| 40 | + MIDI.sendNoteOff(i, 127, 1); |
| 41 | + } |
| 42 | + for (int i = 0; i < 128; ++i) |
| 43 | + { |
| 44 | + MIDI.sendControlChange(i, 127, 1); |
| 45 | + MIDI.sendControlChange(i, 0, 1); |
| 46 | + } |
| 47 | + for (int i = 0; i < 128; ++i) |
| 48 | + { |
| 49 | + MIDI.sendProgramChange(i, 1); |
| 50 | + } |
| 51 | + MIDI.sendRealTime(midi::Start); |
| 52 | + MIDI.sendRealTime(midi::Stop); |
| 53 | + MIDI.sendRealTime(midi::Continue); |
| 54 | + MIDI.sendRealTime(midi::Clock); |
| 55 | + |
| 56 | + const byte length = 128; |
| 57 | + const byte data[128] = { |
| 58 | + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 59 | + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 60 | + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 61 | + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 62 | + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, |
| 63 | + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, |
| 64 | + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 65 | + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, |
| 66 | + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, |
| 67 | + 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, |
| 68 | + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, |
| 69 | + 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, |
| 70 | + 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, |
| 71 | + 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, |
| 72 | + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, |
| 73 | + 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, |
| 74 | + }; |
| 75 | + MIDI.sendSysEx(length, data, false); |
| 76 | +} |
| 77 | + |
| 78 | +// -- |
| 79 | + |
| 80 | +void printChannel(byte inChannel) |
| 81 | +{ |
| 82 | + Serial.print("["); |
| 83 | + Serial.print(inChannel); |
| 84 | + Serial.print("] "); |
| 85 | +} |
| 86 | + |
| 87 | +void handleNoteOff(byte inChannel, byte inNote, byte inVelocity) |
| 88 | +{ |
| 89 | + printChannel(inChannel); |
| 90 | + Serial.print("Note Off\t"); |
| 91 | + Serial.print(inNote); |
| 92 | + Serial.print(' '); |
| 93 | + Serial.println(inVelocity); |
| 94 | +} |
| 95 | +void handleNoteOn(byte inChannel, byte inNote, byte inVelocity) |
| 96 | +{ |
| 97 | + printChannel(inChannel); |
| 98 | + Serial.print("Note On\t"); |
| 99 | + Serial.print(inNote); |
| 100 | + Serial.print(' '); |
| 101 | + Serial.println(inVelocity); |
| 102 | +} |
| 103 | +void handleControlChange(byte inChannel, byte inControl, byte inValue) |
| 104 | +{ |
| 105 | + printChannel(inChannel); |
| 106 | + Serial.print("Control Change\t"); |
| 107 | + Serial.print(inControl); |
| 108 | + Serial.print(' '); |
| 109 | + Serial.println(inValue); |
| 110 | +} |
| 111 | +void handleProgramChange(byte inChannel, byte inProgram) |
| 112 | +{ |
| 113 | + printChannel(inChannel); |
| 114 | + Serial.print("Program Change\t"); |
| 115 | + Serial.println(inProgram); |
| 116 | +} |
| 117 | +void handleAfterTouchChannel(byte inChannel, byte inPressure) |
| 118 | +{ |
| 119 | + printChannel(inChannel); |
| 120 | + Serial.print("AT Channel\t"); |
| 121 | + Serial.println(inPressure); |
| 122 | +} |
| 123 | +void handleAfterTouchPoly(byte inChannel, byte inNote, byte inPressure) |
| 124 | +{ |
| 125 | + printChannel(inChannel); |
| 126 | + Serial.print("AT Poly\t"); |
| 127 | + Serial.print(inNote); |
| 128 | + Serial.print(' '); |
| 129 | + Serial.println(inPressure); |
| 130 | +} |
| 131 | +void handlePitchBend(byte inChannel, int inBend) |
| 132 | +{ |
| 133 | + printChannel(inChannel); |
| 134 | + Serial.print("Pitch Bend\t"); |
| 135 | + Serial.println(inBend); |
| 136 | +} |
| 137 | +void handleStart() |
| 138 | +{ |
| 139 | + Serial.println("Start"); |
| 140 | +} |
| 141 | +void handleStop() |
| 142 | +{ |
| 143 | + Serial.println("Stop"); |
| 144 | +} |
| 145 | +void handleContinue() |
| 146 | +{ |
| 147 | + Serial.println("Continue"); |
| 148 | +} |
| 149 | +void handleClock() |
| 150 | +{ |
| 151 | + Serial.println("Clock"); |
| 152 | +} |
| 153 | +void handleSysEx(byte* inArray, unsigned inSize) |
| 154 | +{ |
| 155 | + Serial.print("SysEx\t[ "); |
| 156 | + for (unsigned i = 0; i < inSize; ++i) |
| 157 | + { |
| 158 | + Serial.print(inArray[i], HEX); |
| 159 | + Serial.print(' '); |
| 160 | + } |
| 161 | + Serial.println(']'); |
| 162 | +} |
| 163 | + |
| 164 | +// -- |
| 165 | + |
| 166 | +void setup() |
| 167 | +{ |
| 168 | + Serial.begin(115200); |
| 169 | + while(!Serial); // Wait for the Serial Monitor to open |
| 170 | + |
| 171 | + MIDI.begin(); |
| 172 | + MIDI.turnThruOff(); |
| 173 | + |
| 174 | + setupRxBenchmark(); |
| 175 | + startTxBenchmark(); |
| 176 | +} |
| 177 | + |
| 178 | +void loop() |
| 179 | +{ |
| 180 | + MIDI.read(); |
| 181 | +} |
0 commit comments