Improv How-to: Basic MIDI input

    Example 1 demonstrates how to receive a MIDI message from an external MIDI synthesizer connected to your computer via a MIDI cable. MIDI synthesizers and other MIDI devices send MIDI messages which are typically three-bytes long. MIDI messages start with the MIDI command byte (a number between 128 and 255) which is then followed by 1, 2, or a variable length of parameter bytes depending on the MIDI command. Typical MIDI messages include note-ons, note-offs, program changes, continuous controllers, and system-exclusive messages, among others.

      Example 1: A simple MIDI input program.
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      
      #include "improv.h"
      
      int main(void) {
         MidiInput midi(0);
         MidiMessage message;
      
         while (1) {
            while (midi.getCount > 0) {
               message = midi.extract();
               cout << message << endl;
            }
         }
      
         return 0;
      }
      

  • Line 1 includes the file improv.h which contains definitions needed for reading MIDI input.
  • Line 4 creates a MidiInput object connected to the first available MIDI input port (0).
    • You can ask the MidiInput class how many input ports are available by using the member function getNumPorts().
    • You can ask for the names of the ports with the MidiInput member function getName().
  • Line 5 creates a MidiMessage object which will be used to extract MIDI messages from the MidiInput object whenever MIDI data arrives.
  • Lines 7-12 form a continuous loop which will extract MIDI messages as they arrive at the MIDI input port and print out the message to the terminal.
  • Lines 8-11 check the MidiInput object to see if it has any messages waiting in its buffer. If so, then extract the messages one-by-one and print them. The extract command extracts the oldest MIDI message which has not yet been extracted.
    • You can examine MIDI messages without extracting them by using the [] operator; for example, to look at the most recent complete MIDI message which has arrived, use midi[0]. To look at the second-to-last message which has arrived, you would use midi[1] or midi[-1].
  • Line 9 removes one incoming MIDI message from the input buffer and stores it in the message variable.
    
    
    

    Example 2 demonstrates how you can print out a list of possible MIDI ports in the event that you need to choose between two or more input ports from which to read MIDI input.

      Example 2: How to display MIDI input port names.
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      
      #include "improv.h"
      
      int main(void) {
         MidiInput midi;
      
         for (int i=0; i<midi.getNumPorts(); i++) {
            cout << "Port " << i << " = " 
                 << midi.getName(i) << endl;
         }
      
         return 0;
      }
      

  • Lines 6-9 cycles through all possible MIDI input ports and prints the port number and the port name to the terminal.
  • You can get the name of the port which the midi variable is currently attached to by using the getName() function without any parameters: midi.getName().
  • You can get the number of the port to which the midi variable current belong with the command: midi.getPort().
    
    
    
    

    Example 3 shows how to select a new port after the MidiInput object is created.

  • On line 4, a MidiInput object is created with the default contructor which sets the input port for the midi variable to 0 but does not open port 0. This means that the midi object is set to port 0, but even if MIDI data is coming into port 0, no MIDI messages will be received by the midi variable.
  • Line 5 opens the MidiInput port 0 which means that any MIDI messages coming from port 0 are now accessible through the midi variable.
  • Line 7 demonstrates how you can select a new port other than input port 0. This command is useful if you have more than one MIDI input card on your computer.
  • Line 8 opens the MIDI port 1, since line 7 does not open input port 1 when you set the input to look at port 1.
  • Line 10 demonstrates how you can combine the set and open commands into a single command.
  • Note than you should not set to a port which is equal to or larger than the value returned by the command midi.getNumPorts().







This How-to was last updated on Mon May 8 22:59:19 PDT 2000
by craig@ccrma.stanford.edu