MIDI Library

The MIDI library supports communication with MIDI enabled instruments and devices.  To use it, you need following in your program:

 

The MidiIn Class

MidiIn objects may be used in your programs to get input from MIDI devices that generate input events (e.g., a MIDI guitar, keyboard, or control surface).  These devices need to be physically connected (via wire) to your computer.

Use the following function to create a MidiIn object:

Function Description
MidiIn() Creates a new MidiIn object to connect to an input MIDI device.  When called, it presents the user with a GUI to select one from the available MIDI devices (see figure 9.3).

Once a MIDI input object, mInput, has been created the following functions are available:

Function Description
mInput.onNoteOn( function ) When a NOTE_ON event happens on the mInput device (i.e., the user starts a note), the system calls the provided function.  This function should expect four parameters, eventType, channel, data1, data2.  For NOTE_ON events, the eventType is always 144, the channel ranges from 0 to 15, data1 is the note pitch (0-127), and data2 is the volume of the note (0-127).
mInput.onNoteOff( function ) When a NOTE_OFF event happens on the mInput device (i.e., the user ends a note), the system calls the provided function.  This function should expect four parameters, eventType, channel, data1, data2.  For NOTE_OFF events, the eventType is always 128, the channel ranges from 0 to 15, data1 is the note pitch (0-127), and data2 is ignored.
mInput.onSetInstrument( function ) When a SET_INSTRUMENT (also known as CHANGE_PROGRAM) event happens on the mInput device (i.e., the user selects a different timbre), the system calls the provided function.  This function should expect four parameters, eventType, channel, data1, data2.  For SET_INSTRUMENT events, the eventType is always 192, the channel ranges from 0 to 15, data1 is the MIDI instrument (0-127), and data2 is ignored.
mInput.onInput( eventType, function ) Associates an incoming eventType with a callback function.  When the specified eventType event happens on the mInput device, the system calls the provided function.  This function should expect four parameters, eventType, channel, data1, data2.
Can be used repeatedly to associate different event types (128-224) with different callback functions (one function per event type).
If eventType is ALL_EVENTS, then function will be called for all incoming events that have not yet been assigned callback functions.
mInput.showMessages() Prints out incoming MIDI messages. This is the default. Used to explore what MIDI messages are generated by a device, so that they can be associated with callback functions.
mInput.hideMessages() Stops printing out incoming MIDI messages.

 

The MidiOut Class

MidiOut objects may be used in your programs to send output to MIDI devices that accept output events (e.g., an external MIDI synthesizer).  These devices need to be physically connected (via wire) to your computer.

Use the following function to create a MidiOut object:

Function Description
MidiOut() Creates a new MidiOut object to connect to an output MIDI device.  When called, it presents the user with a GUI to select one from the available MIDI devices (see figure 9.4).

 

Once a MIDI output object, mOutput, has been created the following functions are available:

Function Description
mOutput.noteOn(pitch, velocity, channel) Sends a NOTE_ON message with pitch (0-127), at a given velocity (0-127 – default is 100), to channel (0-15 – default is 0) on the mOutput device.
mOutput.noteOff(pitch, channel) Sends a NOTE_OFF message with pitch (0-127), on given channel (0-15 – default is 0) on the mOutput device.  If the pitch is not sounding on this channel, this has no effect.
mOutput.setInstrument(instrument, channel) Sets a MIDI instrument (0-127 – default is 0) for the given channel (0 – 15, default is 0) on the mOutput device.  Any notes played through channel will sound using instrument.
mOutput.playNote(pitch, start, duration, velocity, channel) Schedules playing of a note with pitch at the given start time (in milliseconds from now), with duration (in milliseconds from start time), velocity (0-127 – default is 100), to channel (0-15 – default is 0) on the mOutput device.
mOutput.play(material) Play music library material (Score, Part, Phrase, Note) on the mOutput device.

For more information, see chapter 9.