Advertisement
j0h

Sebastian

j0h
Feb 6th, 2024 (edited)
1,274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* License:
  2.  *
  3.  * Create a "class compliant " USB to 6 MIDI IN and 6 MIDI OUT interface,
  4.    plus 10 more USB connected devices.  Admittedly, you could just plug
  5.    those 10 devices directly into your computer, but this example is meant
  6.    to show how to forward any MIDI message between the 3 different MIDI
  7.    libraries.  A "real" application might do something more interesting,
  8.    like translate or modify the MIDI messages....
  9.  
  10.    MIDI receive (6N138 optocoupler) input circuit and series resistor
  11.    outputs need to be connected to Serial1-Serial6.  A USB host cable
  12.    is needed on Teensy 3.6's second USB port, and obviously USB hubs
  13.    are needed to connect up to 10 USB MIDI devices.  That's a *LOT* of
  14.    extra hardware to connect to a Teensy!
  15.  
  16.    You must select MIDIx16 from the "Tools > USB Type" menu
  17.  
  18.    This example code is in the public domain.
  19. */
  20. #include <MIDI.h>        // access to serial (5 pin DIN) MIDI
  21. #include <USBHost_t36.h> // access to USB MIDI devices (plugged into 2nd USB port)
  22. #include <Audio.h>
  23. #include <Wire.h>
  24. #include <SPI.h>
  25. #include <SD.h>
  26. #include <SerialFlash.h>
  27. //--------------------
  28. //Function prototypes:
  29. //----------------------
  30. void sendToComputer(byte type, byte data1, byte data2, byte channel, const uint8_t *sysexarray, byte cable);
  31.  
  32. //---------------------------------------
  33. // Globals:
  34. //______________________
  35.  
  36.  
  37. // Create the Serial MIDI ports
  38. MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI1);
  39. MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI2);
  40. MIDI_CREATE_INSTANCE(HardwareSerial, Serial3, MIDI3);
  41. MIDI_CREATE_INSTANCE(HardwareSerial, Serial4, MIDI4);
  42. MIDI_CREATE_INSTANCE(HardwareSerial, Serial5, MIDI5);
  43. MIDI_CREATE_INSTANCE(HardwareSerial, Serial6, MIDI6);
  44. //midi::MidiInterface &SerialMidiList[6] = {MIDI1, MIDI2, MIDI3, MIDI4, MIDI5, MIDI6};
  45.  
  46. // Create the ports for USB devices plugged into Teensy's 2nd USB port (via hubs)
  47. USBHost myusb;
  48. USBHub hub1(myusb);
  49. USBHub hub2(myusb);
  50. USBHub hub3(myusb);
  51. USBHub hub4(myusb);
  52. MIDIDevice midi01(myusb);
  53. MIDIDevice midi02(myusb);
  54. MIDIDevice midi03(myusb);
  55. MIDIDevice midi04(myusb);
  56. MIDIDevice midi05(myusb);
  57. MIDIDevice midi06(myusb);
  58. MIDIDevice midi07(myusb);
  59. MIDIDevice midi08(myusb);
  60. MIDIDevice midi09(myusb);
  61. MIDIDevice midi10(myusb);
  62. MIDIDevice * midilist[10] = {
  63.   &midi01, &midi02, &midi03, &midi04, &midi05, &midi06, &midi07, &midi08, &midi09, &midi10
  64. };
  65.  
  66. // A variable to know how long the LED has been turned on
  67. elapsedMillis ledOnMillis;
  68.  
  69. // GUItool: begin automatically generated code
  70. AudioInputI2S            i2s1;           //xy=161.20000839233398,110.20000648498535
  71. AudioInputUSB            usb2;           //xy=164.20001602172852,175.19998455047607
  72. AudioOutputUSB           usb1;           //xy=460.2000045776367,113.20000648498535
  73. AudioOutputI2S           i2s2;           //xy=462.20003509521484,187.19999885559082
  74. AudioConnection          patchCord1(i2s1, 0, usb1, 0);
  75. AudioConnection          patchCord2(i2s1, 1, usb1, 1);
  76. AudioConnection          patchCord3(usb2, 0, i2s2, 1);
  77. AudioConnection          patchCord4(usb2, 1, i2s2, 0);
  78. AudioControlSGTL5000     sgtl5000_1;     //xy=100.19999694824219,31.199996948242188
  79. // GUItool: end automatically generated code
  80.  
  81.  
  82.  
  83. void setup() {
  84.    Serial.begin(115200);
  85.   AudioMemory(80);
  86.   //Audio setup
  87.   sgtl5000_1.enable();
  88.   sgtl5000_1.inputSelect(AUDIO_INPUT_LINEIN);
  89.   sgtl5000_1.lineInLevel(10, 10);
  90.   sgtl5000_1.volume(0.8);
  91.   //Interface setup
  92.   pinMode(13, OUTPUT); // LED pin
  93.   digitalWrite(13, LOW);
  94.   MIDI1.begin(MIDI_CHANNEL_OMNI);
  95.   MIDI2.begin(MIDI_CHANNEL_OMNI);
  96.   MIDI3.begin(MIDI_CHANNEL_OMNI);
  97.   MIDI4.begin(MIDI_CHANNEL_OMNI);
  98.   MIDI5.begin(MIDI_CHANNEL_OMNI);
  99.   MIDI6.begin(MIDI_CHANNEL_OMNI);
  100.   // Wait 1.5 seconds before turning on USB Host.  If connected USB devices
  101.   // use too much power, Teensy at least completes USB enumeration, which
  102.   // makes isolating the power issue easier.
  103.   delay(1500);
  104.   Serial.println("Interface_16x16 Example");
  105.   delay(10);
  106.   myusb.begin();
  107.  
  108. }
  109.  
  110.  
  111.  
  112.  
  113. void loop() {
  114.   bool activity = false;
  115.    // read the PC's volume setting
  116.   float vol = usb1.volume();
  117.  
  118.   // scale to a nice range (not too loud)
  119.   // and adjust the audio shield output volume
  120.   if (vol > 0) {
  121.     // scale 0 = 1.0 range to:
  122.     //  0.3 = almost silent
  123.     //  0.8 = really loud
  124.     vol = 0.3 + vol * 0.5;
  125.   }
  126.    
  127.   // use the scaled volume setting.  Delete this for fixed volume.
  128.   sgtl5000_1.volume(vol);
  129.   delay(100);
  130.  
  131.   // First read messages from the 6 Serial MIDI IN ports
  132.   if (MIDI1.read()) {
  133.     sendToComputer(MIDI1.getType(), MIDI1.getData1(), MIDI1.getData2(), MIDI1.getChannel(), MIDI1.getSysExArray(), 0);
  134.     activity = true;
  135.   }
  136.   if (MIDI2.read()) {
  137.     sendToComputer(MIDI2.getType(), MIDI2.getData1(), MIDI2.getData2(), MIDI2.getChannel(), MIDI2.getSysExArray(), 1);
  138.     activity = true;
  139.   }
  140.   if (MIDI3.read()) {
  141.     sendToComputer(MIDI3.getType(), MIDI3.getData1(), MIDI3.getData2(), MIDI3.getChannel(), MIDI3.getSysExArray(), 2);
  142.     activity = true;
  143.   }
  144.   if (MIDI4.read()) {
  145.     sendToComputer(MIDI4.getType(), MIDI4.getData1(), MIDI4.getData2(), MIDI4.getChannel(), MIDI4.getSysExArray(), 3);
  146.     activity = true;
  147.   }
  148.   if (MIDI5.read()) {
  149.     sendToComputer(MIDI5.getType(), MIDI5.getData1(), MIDI5.getData2(), MIDI5.getChannel(), MIDI5.getSysExArray(), 4);
  150.     activity = true;
  151.   }
  152.   if (MIDI6.read()) {
  153.     sendToComputer(MIDI6.getType(), MIDI6.getData1(), MIDI6.getData2(), MIDI6.getChannel(), MIDI6.getSysExArray(), 5);
  154.     activity = true;
  155.   }
  156.  
  157.   // Next read messages arriving from the (up to) 10 USB devices plugged into the USB Host port
  158.   for (int port=0; port < 10; port++) {
  159.     if (midilist[port]->read()) {
  160.       uint8_t type =       midilist[port]->getType();
  161.       uint8_t data1 =      midilist[port]->getData1();
  162.       uint8_t data2 =      midilist[port]->getData2();
  163.       uint8_t channel =    midilist[port]->getChannel();
  164.       const uint8_t *sys = midilist[port]->getSysExArray();
  165.       sendToComputer(type, data1, data2, channel, sys, 6 + port);
  166.       activity = true;
  167.     }
  168.   }
  169.  
  170.   // Finally, read any messages the PC sends to Teensy, and forward them
  171.   // to either Serial MIDI or to USB devices on the USB host port.
  172.   if (usbMIDI.read()) {
  173.     // get the USB MIDI message, defined by these 5 numbers (except SysEX)
  174.     byte type = usbMIDI.getType();
  175.     byte channel = usbMIDI.getChannel();
  176.     byte data1 = usbMIDI.getData1();
  177.     byte data2 = usbMIDI.getData2();
  178.     byte cable = usbMIDI.getCable();
  179.  
  180.     // forward this message to 1 of the 3 Serial MIDI OUT ports
  181.     if (type != usbMIDI.SystemExclusive) {
  182.       // Normal messages, first we must convert usbMIDI's type (an ordinary
  183.       // byte) to the MIDI library's special MidiType.
  184.       midi::MidiType mtype = (midi::MidiType)type;
  185.  
  186.       // Then simply give the data to the MIDI library send()
  187.       switch (cable) {
  188.         case  0: MIDI1.send(mtype, data1, data2, channel); break;
  189.         case  1: MIDI2.send(mtype, data1, data2, channel); break;
  190.         case  2: MIDI3.send(mtype, data1, data2, channel); break;
  191.         case  3: MIDI4.send(mtype, data1, data2, channel); break;
  192.         case  4: MIDI5.send(mtype, data1, data2, channel); break;
  193.         case  5: MIDI6.send(mtype, data1, data2, channel); break;
  194.         default: // cases 6-15
  195.           midilist[cable - 6]->send(type, data1, data2, channel);
  196.       }
  197.  
  198.     } else {
  199.       // SysEx messages are special.  The message length is given in data1 & data2
  200.       unsigned int SysExLength = data1 + data2 * 256;
  201.       switch (cable) {
  202.         case 0: MIDI1.sendSysEx(SysExLength, usbMIDI.getSysExArray(), true); break;
  203.         case 1: MIDI2.sendSysEx(SysExLength, usbMIDI.getSysExArray(), true); break;
  204.         case 2: MIDI3.sendSysEx(SysExLength, usbMIDI.getSysExArray(), true); break;
  205.         case 3: MIDI4.sendSysEx(SysExLength, usbMIDI.getSysExArray(), true); break;
  206.         case 4: MIDI5.sendSysEx(SysExLength, usbMIDI.getSysExArray(), true); break;
  207.         case 5: MIDI6.sendSysEx(SysExLength, usbMIDI.getSysExArray(), true); break;
  208.         default: // cases 6-15
  209.           midilist[cable - 6]->sendSysEx(SysExLength, usbMIDI.getSysExArray(), true);
  210.       }
  211.     }
  212.     activity = true;
  213.   }
  214.  
  215.   // blink the LED when any activity has happened
  216.   if (activity) {
  217.     digitalWriteFast(13, HIGH); // LED on
  218.     ledOnMillis = 0;
  219.   }
  220.   if (ledOnMillis > 15) {
  221.     digitalWriteFast(13, LOW);  // LED off
  222.   }
  223.  
  224. }
  225.  
  226.  
  227. void sendToComputer(byte type, byte data1, byte data2, byte channel, const uint8_t *sysexarray, byte cable){
  228.   if (type != midi::SystemExclusive) {
  229.     usbMIDI.send(type, data1, data2, channel, cable);
  230.   } else {
  231.     unsigned int SysExLength = data1 + data2 * 256;
  232.     usbMIDI.sendSysEx(SysExLength, sysexarray, true, cable);
  233.   }
  234. }
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement