Advertisement
rinaldohack

Untitled

Jul 11th, 2019
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // rtpMIDI to serial MIDI converter
  2. // MIDI Thru is OFF
  3. // created by Rinaldo Jonathan <https://rinaldo.id>
  4. // built for Rinaldo's final project at Politeknik Caltex Riau <https://pcr.ac.id>, Diploma 3 Computer Engineer.
  5.  
  6. // These need to be included when using standard Ethernet
  7. #include <ESP8266WiFi.h>
  8. #include <WiFiClient.h>
  9. #include <WiFiUdp.h>
  10.  
  11. #include <AppleMidi.h>  // lathoub rtpMIDI llibrary <https://github.com/lathoub/Arduino-AppleMIDI-Library/>
  12. #include <MIDI.h>       // FortySeven MIDI library  <https://github.com/FortySevenEffects/arduino_midi_library/>
  13.  
  14.  
  15. char ssid[] = "ssid";   //  your network SSID (name)
  16. char pass[] = "pass";   // your network password (use for WPA, or use as key for WEP)
  17.  
  18. unsigned long t0 = millis();
  19. bool isConnected = false;
  20.  
  21. APPLEMIDI_CREATE_INSTANCE(WiFiUDP, AppleMIDI);  // start rtpMIDI
  22. MIDI_CREATE_DEFAULT_INSTANCE();                 // start serial MIDI
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30. // ====================================================================================
  31. // Event handlers for incoming MIDI messages
  32. // ====================================================================================
  33.  
  34. // -----------------------------------------------------------------------------
  35. // rtpMIDI session. Device connected
  36. // -----------------------------------------------------------------------------
  37. void OnAppleMidiConnected(uint32_t ssrc, char* name) {
  38.   isConnected  = true;
  39. }
  40.  
  41. // -----------------------------------------------------------------------------
  42. // rtpMIDI session. Device disconnected
  43. // -----------------------------------------------------------------------------
  44. void OnAppleMidiDisconnected(uint32_t ssrc) {
  45.   isConnected  = false;
  46. }
  47.  
  48. // -----------------------------------------------------------------------------
  49. // rtpMIDI receive Note ON
  50. // -----------------------------------------------------------------------------
  51. void OnAppleMidiNoteOn(byte channel, byte note, byte velocity) {
  52.   MIDI.sendNoteOn(note, velocity, channel); //send Note ON to serial MIDI
  53. }
  54.  
  55. // -----------------------------------------------------------------------------
  56. // rtpMIDI receive Note OFF
  57. // -----------------------------------------------------------------------------
  58. void OnAppleMidiNoteOff(byte channel, byte note, byte velocity) {
  59.   MIDI.sendNoteOff(note, velocity, channel); //send Note Off to serial MIDI
  60.  
  61. }
  62.  
  63. // -----------------------------------------------------------------------------
  64. // rtpMIDI receive MIDI CC
  65. // -----------------------------------------------------------------------------
  66. void OnAppleMidiCc(byte controlNumber, byte controlValue, byte channel){
  67.   MIDI.sendControlChange(controlNumber, controlValue, channel); //send Note Off to serial MIDI
  68.  
  69. }
  70.  
  71.  
  72. // rtpMIDI OnAppleMidiReceiveAfterTouchPoly
  73. void OnAppleMidiReceiveAfterTouchPoly(byte channel, byte note, byte pressure)
  74. {
  75.   MIDI.sendAfterTouch(note,pressure,channel);
  76. }
  77.  
  78. // rtpMIDI OnAppleMidiReceiveProgramChange
  79. void OnAppleMidiReceiveProgramChange(byte channel, byte number)
  80. {
  81.   MIDI.sendProgramChange(number,channel);
  82. }
  83.  
  84. // rtpMIDI OnAppleMidiReceiveAfterTouchChannel
  85.  
  86. void OnAppleMidiReceiveAfterTouchChannel(byte channel, byte pressure)
  87. {
  88.   MIDI.sendAfterTouch(pressure,channel);
  89. }
  90.  
  91. // rtpMIDI OnAppleMidiReceivePitchBend
  92. void OnAppleMidiReceivePitchBend(byte channel, int bend)
  93. {
  94.   MIDI.sendPitchBend(bend, channel);
  95. }
  96.  
  97. // rtpMIDI OnAppleMidiReceiveTimeCodeQuarterFrame
  98. void OnAppleMidiReceiveTimeCodeQuarterFrame(byte data)
  99. {
  100.   MIDI.sendTimeCodeQuarterFrame(data);
  101. }
  102.  
  103.  
  104.  
  105. // -----------------------------------------------------------------------------
  106. // serial MIDI receive Note ON
  107. // -----------------------------------------------------------------------------
  108. void OnSerialMidiNoteOn(byte channel, byte note, byte velocity)
  109. {
  110.   AppleMIDI.sendNoteOn(note, velocity, channel); //send Note On to rtpMIDI
  111. }
  112.  
  113. // -----------------------------------------------------------------------------
  114. // serial MIDI receive Note OFF
  115. // -----------------------------------------------------------------------------
  116. void OnSerialMidiNoteOff(byte channel, byte note, byte velocity)
  117. {
  118.   AppleMIDI.sendNoteOff(note, velocity, channel); //send Note OFF to rtpMIDI
  119. }
  120.  
  121.  
  122. // -----------------------------------------------------------------------------
  123. // serial MIDI CC
  124. // -----------------------------------------------------------------------------
  125. void OnSerialMidiCC (byte controlNumber, byte controlValue, byte channel)
  126. {
  127.   AppleMIDI.sendControlChange(controlNumber, controlValue, channel); //send Note OFF to rtpMIDI
  128. }
  129.  
  130.  
  131.  
  132. // OnSerialMidiAfterTouchPoly
  133. void OnSerialMidiAfterTouchPoly(byte channel, byte note, byte pressure)
  134. {
  135.   AppleMIDI.sendPolyPressure(note, pressure, channel);
  136. }
  137.  
  138. void OnSerialMidiProgramChange(byte channel, byte number)
  139. {
  140.   AppleMIDI.sendProgramChange(number,channel);
  141. }
  142.  
  143. void OnSerialMidiAfterTouchChannel (byte channel, byte pressure)
  144. {
  145.   AppleMIDI.sendAfterTouch(pressure,channel);
  146. }
  147.  
  148. void OnSerialMidiPitchBend (byte channel, int bend)
  149. {
  150.   AppleMIDI.sendPitchBend(bend, channel);
  151. }
  152.  
  153. void OnSerialMidiTimeCodeQuarterFrame (byte data)
  154. {
  155.   AppleMIDI.sendTimeCodeQuarterFrame(data);
  156. }
  157. // -----------------------------------------------------------------------------
  158. //
  159. // -----------------------------------------------------------------------------
  160. void setup()
  161. {
  162.   // Serial communications and wait for port to open:
  163.   Serial.begin(115200);
  164.  
  165.   Serial.print(F("Getting IP address..."));
  166.  
  167.  
  168.   WiFi.begin(ssid, pass);
  169.  
  170.   while (WiFi.status() != WL_CONNECTED) {
  171.     delay(500);
  172.     Serial.print(F("."));
  173.   }
  174.   Serial.println(F(""));
  175.   Serial.println(F("WiFi connected"));
  176.  
  177.  
  178.   Serial.println();
  179.   Serial.print(F("IP address is "));
  180.   Serial.println(WiFi.localIP());
  181.  
  182.   Serial.println(F("OK, now make sure you an rtpMIDI session that is Enabled"));
  183.   Serial.print(F("Add device named Arduino with Host/Port "));
  184.   Serial.print(WiFi.localIP());
  185.   Serial.println(F(":5004"));
  186.   Serial.println(F("Then press the Connect button"));
  187.   Serial.println(F("Then open a MIDI listener (eg MIDI-OX) and monitor incoming notes"));
  188.  
  189.   // Create a session and wait for a remote host to connect to us
  190.   AppleMIDI.begin("test");                            // rtpMIDI session name
  191.  
  192.   AppleMIDI.OnConnected(OnAppleMidiConnected);        // unused, leftover codes from examples
  193.   AppleMIDI.OnDisconnected(OnAppleMidiDisconnected);  // unused, leftover codes from examples
  194.  
  195.   AppleMIDI.OnReceiveNoteOn(OnAppleMidiNoteOn);       // handle rtpMIDI Note ON
  196.   AppleMIDI.OnReceiveNoteOff(OnAppleMidiNoteOff);     // handle rtpMIDI Note OFF
  197.   AppleMIDI.OnReceiveControlChange(OnAppleMidiCc);    // handle rtpMIDI CC
  198.  
  199.   //handle everything else that I don't fully understand
  200.   AppleMIDI.OnReceiveAfterTouchPoly(OnAppleMidiReceiveAfterTouchPoly);
  201.   AppleMIDI.OnReceiveProgramChange(OnAppleMidiReceiveProgramChange);
  202.   AppleMIDI.OnReceiveAfterTouchChannel(OnAppleMidiReceiveAfterTouchChannel);
  203.   AppleMIDI.OnReceivePitchBend(OnAppleMidiReceivePitchBend);
  204.   //AppleMIDI.OnReceiveSysEx(OnAppleMidiReceiveSysEx); //skipped
  205.   AppleMIDI.OnReceiveTimeCodeQuarterFrame(OnAppleMidiReceiveTimeCodeQuarterFrame);
  206.   //AppleMIDI.OnReceiveSongPosition(OnAppleMidiReceiveSongPosition);
  207.   //AppleMIDI.OnReceiveSongSelect(OnAppleMidiReceiveSongSelect);
  208.   //AppleMIDI.OnReceiveTuneRequest(OnAppleMidiReceiveTuneRequest);
  209.   //AppleMIDI.OnReceiveClock(OnAppleMidiReceiveClock);
  210.   //AppleMIDI.OnReceiveStart(OnAppleMidiReceiveStart);
  211.   //AppleMIDI.OnReceiveContinue(OnAppleMidiReceiveContinue);
  212.   //AppleMIDI.OnReceiveStop(OnAppleMidiReceiveStop);
  213.   //AppleMIDI.OnReceiveActiveSensing(OnAppleMidiReceiveActiveSensing);
  214.   //AppleMIDI.OnReceiveReset(OnAppleMidiReceiveReset);
  215.  
  216.   MIDI.begin(MIDI_CHANNEL_OMNI);                      // start serial MIDI
  217.   MIDI.turnThruOff();                                 // because we don't need one
  218.  
  219.   MIDI.setHandleNoteOn(OnSerialMidiNoteOn);           // handle serial MIDI note ON, Put only the name of the function,
  220.   MIDI.setHandleNoteOff(OnSerialMidiNoteOff);         // handle serial MIDI note OFF
  221.   MIDI.setHandleControlChange(OnSerialMidiCC);        // handle serial MIDI CC
  222.  
  223.   //handle everything else that I don't fully understand
  224.   MIDI.setHandleAfterTouchPoly(OnSerialMidiAfterTouchPoly);
  225.   MIDI.setHandleProgramChange(OnSerialMidiProgramChange);
  226.   MIDI.setHandleAfterTouchChannel(OnSerialMidiAfterTouchChannel);
  227.   MIDI.setHandlePitchBend(OnSerialMidiPitchBend);
  228.   //MIDI.setHandleSystemExclusive(OnSerialMidiSysEx); //skipped
  229.   MIDI.setHandleTimeCodeQuarterFrame(OnSerialMidiTimeCodeQuarterFrame);
  230.   //MIDI.setHandleSongPosition(OnSerialMidiSongPosition); //skipped
  231.   //MIDI.setHandleSongSelect(OnSerialMidiSongSelect); //skipped
  232.   //MIDI.setHandleTuneRequest(OnSerialMidiTuneRequest); //skipped
  233.   //MIDI.setHandleClock(OnSerialMidiClock); //skipped
  234.   //MIDI.setHandleStart(OnSerialMidiStart); //skipped
  235.   //MIDI.setHandleContinue(OnSerialMidiContinue); //skipped
  236.   //MIDI.setHandleStop(OnSerialMidiStop); //skipped
  237.   //MIDI.setHandleActiveSensing(OnSerialMidiActiveSensing); /skipped
  238.   //MIDI.setHandleSystemReset(OnSerialMidiReset); //skipped
  239.  
  240.  
  241. }
  242.  
  243. // -----------------------------------------------------------------------------
  244. //
  245. // -----------------------------------------------------------------------------
  246. void loop()
  247. {
  248.   // Listen to incoming notes
  249.   AppleMIDI.read();
  250.   MIDI.read();
  251.  
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement