Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

183 rindas
6.8KB

  1. /* Create a "class compliant " USB to 6 MIDI IN and 6 MIDI OUT interface,
  2. plus 10 more USB connected devices. Admittedly, you could just plug
  3. those 10 devices directly into your computer, but this example is meant
  4. to show how to forward any MIDI message between the 3 different MIDI
  5. libraries. A "real" application might do something more interesting,
  6. like translate or modify the MIDI messages....
  7. MIDI receive (6N138 optocoupler) input circuit and series resistor
  8. outputs need to be connected to Serial1-Serial6. A USB host cable
  9. is needed on Teensy 3.6's second USB port, and obviously USB hubs
  10. are needed to connect up to 10 USB MIDI devices. That's a *LOT* of
  11. extra hardware to connect to a Teensy!
  12. You must select MIDIx16 from the "Tools > USB Type" menu
  13. This example code is in the public domain.
  14. */
  15. #include <MIDI.h> // access to serial (5 pin DIN) MIDI
  16. #include <USBHost_t36.h> // access to USB MIDI devices (plugged into 2nd USB port)
  17. // Create the Serial MIDI ports
  18. MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI1);
  19. MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI2);
  20. MIDI_CREATE_INSTANCE(HardwareSerial, Serial3, MIDI3);
  21. MIDI_CREATE_INSTANCE(HardwareSerial, Serial4, MIDI4);
  22. MIDI_CREATE_INSTANCE(HardwareSerial, Serial5, MIDI5);
  23. MIDI_CREATE_INSTANCE(HardwareSerial, Serial6, MIDI6);
  24. //midi::MidiInterface &SerialMidiList[6] = {MIDI1, MIDI2, MIDI3, MIDI4, MIDI5, MIDI6};
  25. // Create the ports for USB devices plugged into Teensy's 2nd USB port (via hubs)
  26. USBHost myusb;
  27. USBHub hub1(myusb);
  28. USBHub hub2(myusb);
  29. USBHub hub3(myusb);
  30. USBHub hub4(myusb);
  31. MIDIDevice midi01(myusb);
  32. MIDIDevice midi02(myusb);
  33. MIDIDevice midi03(myusb);
  34. MIDIDevice midi04(myusb);
  35. MIDIDevice midi05(myusb);
  36. MIDIDevice midi06(myusb);
  37. MIDIDevice midi07(myusb);
  38. MIDIDevice midi08(myusb);
  39. MIDIDevice midi09(myusb);
  40. MIDIDevice midi10(myusb);
  41. MIDIDevice * midilist[10] = {
  42. &midi01, &midi02, &midi03, &midi04, &midi05, &midi06, &midi07, &midi08, &midi09, &midi10
  43. };
  44. // A variable to know how long the LED has been turned on
  45. elapsedMillis ledOnMillis;
  46. void setup() {
  47. Serial.begin(115200);
  48. pinMode(13, OUTPUT); // LED pin
  49. digitalWrite(13, LOW);
  50. MIDI1.begin(MIDI_CHANNEL_OMNI);
  51. MIDI2.begin(MIDI_CHANNEL_OMNI);
  52. MIDI3.begin(MIDI_CHANNEL_OMNI);
  53. MIDI4.begin(MIDI_CHANNEL_OMNI);
  54. MIDI5.begin(MIDI_CHANNEL_OMNI);
  55. MIDI6.begin(MIDI_CHANNEL_OMNI);
  56. // Wait 1.5 seconds before turning on USB Host. If connected USB devices
  57. // use too much power, Teensy at least completes USB enumeration, which
  58. // makes isolating the power issue easier.
  59. delay(1500);
  60. Serial.println("Interface_16x16 Example");
  61. delay(10);
  62. myusb.begin();
  63. }
  64. void loop() {
  65. bool activity = false;
  66. // First read messages from the 6 Serial MIDI IN ports
  67. if (MIDI1.read()) {
  68. sendToComputer(MIDI1.getType(), MIDI1.getData1(), MIDI1.getData2(), MIDI1.getChannel(), MIDI1.getSysExArray(), 0);
  69. activity = true;
  70. }
  71. if (MIDI2.read()) {
  72. sendToComputer(MIDI2.getType(), MIDI2.getData1(), MIDI2.getData2(), MIDI2.getChannel(), MIDI2.getSysExArray(), 1);
  73. activity = true;
  74. }
  75. if (MIDI3.read()) {
  76. sendToComputer(MIDI3.getType(), MIDI3.getData1(), MIDI3.getData2(), MIDI3.getChannel(), MIDI3.getSysExArray(), 2);
  77. activity = true;
  78. }
  79. if (MIDI4.read()) {
  80. sendToComputer(MIDI4.getType(), MIDI4.getData1(), MIDI4.getData2(), MIDI4.getChannel(), MIDI4.getSysExArray(), 3);
  81. activity = true;
  82. }
  83. if (MIDI5.read()) {
  84. sendToComputer(MIDI5.getType(), MIDI5.getData1(), MIDI5.getData2(), MIDI5.getChannel(), MIDI5.getSysExArray(), 4);
  85. activity = true;
  86. }
  87. if (MIDI6.read()) {
  88. sendToComputer(MIDI6.getType(), MIDI6.getData1(), MIDI6.getData2(), MIDI6.getChannel(), MIDI6.getSysExArray(), 5);
  89. activity = true;
  90. }
  91. // Next read messages arriving from the (up to) 10 USB devices plugged into the USB Host port
  92. for (int port=0; port < 10; port++) {
  93. if (midilist[port]->read()) {
  94. uint8_t type = midilist[port]->getType();
  95. uint8_t data1 = midilist[port]->getData1();
  96. uint8_t data2 = midilist[port]->getData2();
  97. uint8_t channel = midilist[port]->getChannel();
  98. const uint8_t *sys = midilist[port]->getSysExArray();
  99. sendToComputer(type, data1, data2, channel, sys, 6 + port);
  100. activity = true;
  101. }
  102. }
  103. // Finally, read any messages the PC sends to Teensy, and forward them
  104. // to either Serial MIDI or to USB devices on the USB host port.
  105. if (usbMIDI.read()) {
  106. // get the USB MIDI message, defined by these 5 numbers (except SysEX)
  107. byte type = usbMIDI.getType();
  108. byte channel = usbMIDI.getChannel();
  109. byte data1 = usbMIDI.getData1();
  110. byte data2 = usbMIDI.getData2();
  111. byte cable = usbMIDI.getCable();
  112. // forward this message to 1 of the 3 Serial MIDI OUT ports
  113. if (type != usbMIDI.SystemExclusive) {
  114. // Normal messages, first we must convert usbMIDI's type (an ordinary
  115. // byte) to the MIDI library's special MidiType.
  116. midi::MidiType mtype = (midi::MidiType)type;
  117. // Then simply give the data to the MIDI library send()
  118. switch (cable) {
  119. case 0: MIDI1.send(mtype, data1, data2, channel); break;
  120. case 1: MIDI2.send(mtype, data1, data2, channel); break;
  121. case 2: MIDI3.send(mtype, data1, data2, channel); break;
  122. case 3: MIDI4.send(mtype, data1, data2, channel); break;
  123. case 4: MIDI5.send(mtype, data1, data2, channel); break;
  124. case 5: MIDI6.send(mtype, data1, data2, channel); break;
  125. default: // cases 6-15
  126. midilist[cable - 6]->send(type, data1, data2, channel);
  127. }
  128. } else {
  129. // SysEx messages are special. The message length is given in data1 & data2
  130. unsigned int SysExLength = data1 + data2 * 256;
  131. switch (cable) {
  132. case 0: MIDI1.sendSysEx(SysExLength, usbMIDI.getSysExArray(), true); break;
  133. case 1: MIDI2.sendSysEx(SysExLength, usbMIDI.getSysExArray(), true); break;
  134. case 2: MIDI3.sendSysEx(SysExLength, usbMIDI.getSysExArray(), true); break;
  135. case 3: MIDI4.sendSysEx(SysExLength, usbMIDI.getSysExArray(), true); break;
  136. case 4: MIDI5.sendSysEx(SysExLength, usbMIDI.getSysExArray(), true); break;
  137. case 5: MIDI6.sendSysEx(SysExLength, usbMIDI.getSysExArray(), true); break;
  138. default: // cases 6-15
  139. midilist[cable - 6]->sendSysEx(SysExLength, usbMIDI.getSysExArray(), true);
  140. }
  141. }
  142. activity = true;
  143. }
  144. // blink the LED when any activity has happened
  145. if (activity) {
  146. digitalWriteFast(13, HIGH); // LED on
  147. ledOnMillis = 0;
  148. }
  149. if (ledOnMillis > 15) {
  150. digitalWriteFast(13, LOW); // LED off
  151. }
  152. }
  153. void sendToComputer(byte type, byte data1, byte data2, byte channel, const uint8_t *sysexarray, byte cable)
  154. {
  155. if (type != midi::SystemExclusive) {
  156. usbMIDI.send(type, data1, data2, channel, cable);
  157. } else {
  158. unsigned int SysExLength = data1 + data2 * 256;
  159. usbMIDI.sendSysEx(SysExLength, sysexarray, true, cable);
  160. }
  161. }