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.


Create a MidiOut object

To create a MidiOut object, you need to import the MIDI library:

from midi import *

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

FunctionDescription
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:

FunctionDescription
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).


m.play() – play musical material

Once m has been created, you can musical material to an external device.

The first function, m.play(), is used to play musical material stored in Note, Phrase, Part, and Score objects via the external device.

FunctionDescription
m.play( material )Plays musical material (Score, Part, Phrase, Note) on the m device.
NOTE: This is similar to Play.midi().

m.noteOn(), etc. – play musical notes interactively

Once m has been created, you can send musical notes interactively to an external device.

This functionality can be used to perform live, or to build 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-127- 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).

Play microtonal material

Once m has been created, you can play microtonal material through an external device.

To play microtonal material, simply create Note objects using float (e.g., 443.1) pitches. Then, you may store them in Phrase. Part, or Score objects, and play them normally.

You may also play microtonal material interactively, using 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-127 – 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.)


Send arbitrary MIDI messages

Once m has been created, you can send arbitrary MIDI messages (e.g., CC – control change, etc.) to an external device.

This is possible 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 data2, on the m device. For more information, see the MIDI standard, and/or documentation on the particular MIDI synthesizer.

WARNING:  If you use an automated MIDI learn feature in your external program (or device) to connect MIDI messages sent from JEM to arbitrary functionality, 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 external 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).