MidiOut

MIDI devices (e.g., a MIDI synthesizer) can generate sounds from MIDI messages sent to them. If connected to your computer (via cable), you may use a MidiOut object to send such messages from inside your program.

First, you need to import the MIDI library:

from midi import *

Then, you can create a MidiOut object using this function:

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

For example,

m = MidiOut()

creates a MidiOut object called m.  When executed, a display will open up to select an ouput MIDI device.

MidiOut objects may also be created with a specific device in mind:

Function Description
MidiOut( device ) Creates a new MidiOut object to connect to device (a string). If device is not available, a display will appear to select one from the available input MIDI devices.

For example,

m = MidiOut("M-Audio USB Uno MIDI Interface")

creates a connection to an output device directly (i.e., no selection display is shown – faster, if you already know the name of the device).

Once m has been created, the following functions are available:

Function Description
m.play( material ) Plays music material (Score, Part, Phrase, Note) on the m device.

NOTE: This is similar to Play.midi().

The other Play functions are more advanced, and are intended for building interactive musical instruments.

m.noteOn( pitch, volume, channel, panning ) Sends a NOTE_ON message with pitch (0-127), volume (0-127 – default is 100), on channel (0-15 – default is 0), and panning (0-125 – default is 64), on the m device.
m.noteOff( pitch, channel ) Sends a NOTE_OFF message with pitch (0-127), on channel (0-15 – default is 0) on the mOutput device.
m.note( pitch, start, duration,volume, channel, panning ) Schedules playing of a note with pitch (0-127), starting at start time (in milliseconds from now), lasting duration (in milliseconds from start time), volume (0-127 – default is 100), on channel (0-15 – default is 0), and panning (0-127 – default is 64), on the m device.
m.allNotesOff() Stops all notes from sounding on all channels.

You can also make global changes interactively on instrument, volume, panning, and pitch bend.

m.setInstrument( instrument, channel ) Sets a MIDI instrument (0-127 – default is 0) for channel (0 – 15, default is 0) on the m device.
m.setVolume(volume, channel) Sets the global (main) volume (0-127) for this channel (0-15)  This is different from the velocity level of individual notes – see m.noteOn().
m.getVolume(channel) Returns the global (main) volume (0-127) for this channel (0-15).
m.setPanning(position, channel) Sets the global (main) panning position (0-127) for this channel (0-15)  The default position is in the middle (64).

NOTE: Global panning does not affect the panning of a score being played through m.play().

m.getPanning(channel) Returns the global (main) position (0-127) for this channel (0-15).
m.setPitchBend(bend, channel) Sets the pitch bend for this channel (0-15 – default is 0). Pitch bend ranges from -8192 (max downward bend) to 8191 (max upward bend).  No pitch bend is 0 (this is the default).
m.getPitchBend(channel) Returns the current pitch bend for this channel (0-15 – default is 0).

Playing Microtonal material

To play microtonal material, you may create Note objects using float (e.g., 443.1) pitch. Or use the following functions:

m.frequencyOn( frequency, volume, channel, panning ) Plays a frequency in Hertz (8.17 to 12600.0), volume (0-127 – default is 100), on channel (0-15 – default is 0), and panning (0-125 – default is 64), on the m device.
m.frequencyOff( frequency, channel ) Stop a frequency in Hertz (8.17 to 12600.0) from sounding, on channel (0-15 – default is 0) on the mOutput device.
m.frequency( frequency, start, duration,volume, channel, panning ) Schedules playing of a frequency in Hertz (8.17 to 12600.0), starting at start time (in milliseconds from now), lasting duration (in milliseconds from start time), volume (0-127 – default is 100), on channel (0-15 – default is 0), and panning (0-127 – default is 64), on the m device.
m.allFrequenciesOff() Stops all frequencies from sounding on all channels. Same as m.allNotesOff()

WARNING: For polyphony (to play concurrent microtonal notes), you must play notes on different MIDI channels.

The MIDI standard does not support microtones.  Microtones are rendered here using MIDI pitch bend.  Since there is only one pitch bend per channel, you must spread concurrent notes across channels.  (Also, remember that channel 9 is special – percussion only.)

Sending arbitrary MIDI messages

It is also possible to send arbitrary MIDI messages to the MIDI synthesizer associated with m by using the following function:

m.sendMidiMessage( type, channel, data1, data2 ) Sends an arbitrary MIDI message type (MIDI event type), on channel (0-15), with data1 and data1, on the m device. For more information, see the MIDI standard, and/or documentation on the particular MIDI synthesizer.

NOTE:  If you use a MIDI learn feature in your program (i.e., to auto-learn MIDI messages sent from JEM), do NOT press JEM’s stop button, while your program is in learn mode.

When JEM’s stop button is pressed, it sends an ALL-NOTES-OFF message (i.e., CC 123 message to all channels).  This is done to turn off all sounds generated by equipment connected to JEM (as one might expect).

If your program is in MIDI learn more, first turn MIDI learning off, and then press the JEM stop button.  Otherwise, your program will also learn the ALL_NOTES_OFF message (which is something you probably don’t want).