MidiIn

The MidiIn class 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.

To use it, you need following in your program:

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 MidiIn object m has been created, the following functions are available:

Function Description
m.onNoteOn( function ) When a NOTE_ON event occurs on device m (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).
m.onNoteOff( function ) When a NOTE_OFF event occurs on device m (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.
m.onSetInstrument( function ) When a SET_INSTRUMENT (also known as CHANGE_PROGRAM) event occurs on device m (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.
m.onInput( eventType, function ) Associates an incoming eventType (128-224) with a callback function. When the specified event occurs on device m, the system calls the provided function. This function should expect four parameters, eventType, channel, data1, data2. Special symbol ALL_EVENTS can be used to signify all incoming events.
m.showMessages() Prints out incoming MIDI messages. This is the default. Allows seeing what MIDI messages are generated by a device, in order to associate them with callback functions.
m.hideMessages() Stops printing out incoming MIDI messages.