How to send/receive sysex messages

System Exclusive messages are stored in a separate buffer from regular MIDI input messages. They are stored in a Circular Buffer which has a length of 128 (or 256?). The MidiInPort class will collect a system exclusive message. When it has finished receiving the sysex message, it will store it into the sysex circular buffer and then place a regular MIDI message into the regular MIDI input buffer. The format of the MIDI input message for the sysex is:

If you extract a MIDI message from a MidiInPort and the MIDI command byte is 0xf0, then you know you have a system exclusive message available in the MIDI input's sysex buffer

Here is the basic code to check for sysex messages, and how to determine the length of the sysex message and how to get the actual data:

   MidiInput midiinput;
   MidiMessage message;
   int sysexSize;
   int bufferNum;
   unsigned char *sysexData;

   if (midiinput.getCount() > 0) {
      message = midiinput.extract();
      if (message.p0() == 0xf0) {
         // we have a system exclusive message
         bufferNum = message.p1();
         sysexData = midiinput.getSysex(bufferNum);
         sysexSize = midiinput.getSysexSize(bufferNum);
      }
   }

Sending a System Exclusive message through MIDI output is more simple. The starting 0xf0 byte and the ending command 0xf7 should be present in the sysex data.

   unsigned char *sysexData;
   int sysexLength;
   MidiOutput midiout;
   
   //fill the sysex array and set the size variable, then to send:
   midiout.sysex(sysexData, sysexLength);
   midiout.rawsend(sysexData, sysexLength);     // equivalent to last line

See the example program sysexio for a complete code example for system exclusive messages.







craig@ccrma.stanford.edu