pygame2.midi – MIDI input and output interaction module

MIDI input and output interaction module.

The midi module can send output to midi devices, and get input from midi devices. It can also list midi devices on the system, including real and virtual ones.

It uses the portmidi library (using PyPortMidi) and is portable to all platforms portmidi supports (currently windows, OSX, and linux).

Module Functions

pygame2.midi.get_count() → int

Gets the number of available midi devices.

Raises a pygame2.Error, if the midi module is not initialized..

pygame2.midi.get_default_input_id() → int

Returns the default device ID or -1 if there are no devices.

The result can be passed to the Input()/Ouput() class.

On the PC, the user can specify a default device by setting an environment variable. For example, to use device #1.

set PM_RECOMMENDED_INPUT_DEVICE=1

The user should first determine the available device ID by using the supplied application “testin” or “testout”.

In general, the registry is a better place for this kind of info, and with USB devices that can come and go, using integers is not very reliable for device identification. Under Windows, if PM_RECOMMENDED_OUTPUT_DEVICE (or PM_RECOMMENDED_INPUT_DEVICE) is NOT found in the environment, then the default device is obtained by looking for a string in the registry under:

HKEY_LOCAL_MACHINE/SOFTWARE/PortMidi/Recommended_Input_Device

and

HKEY_LOCAL_MACHINE/SOFTWARE/PortMidi/Recommended_Output_Device

for a string. The number of the first device with a substring that matches the string exactly is returned. For example, if the string in the registry is “USB”, and device 1 is named “In USB MidiSport 1x1”, then that will be the default input because it contains the string “USB”.

In addition to the name, get_device_info() returns “interf”, which is the interface name. (The “interface” is the underlying software system or API used by PortMidi to access devices. Examples are MMSystem, DirectX (not implemented), ALSA, OSS (not implemented), etc.) At present, the only Win32 interface is “MMSystem”, the only Linux interface is “ALSA”, and the only Max OS X interface is “CoreMIDI”. To specify both the interface and the device name in the registry, separate the two with a comma and a space, e.g.: MMSystem, In USB MidiSport 1x1 In this case, the string before the comma must be a substring of the “interf” string, and the string after the space must be a substring of the “name” name string in order to match the device.

Note: in the current release, the default is simply the first device (the input or output device with the lowest PmDeviceID).

Raises a pygame2.Error, if the midi module is not initialized.

pygame2.midi.get_default_output_id() → int

Return the default device ID or -1 if there are no devices.

The result can be passed to the Input()/Ouput() class.

On the PC, the user can specify a default device by setting an environment variable. For example, to use device #1.

set PM_RECOMMENDED_OUTPUT_DEVICE=1

The user should first determine the available device ID by using the supplied application “testin” or “testout”.

In general, the registry is a better place for this kind of info, and with USB devices that can come and go, using integers is not very reliable for device identification. Under Windows, if PM_RECOMMENDED_OUTPUT_DEVICE (or PM_RECOMMENDED_INPUT_DEVICE) is NOT found in the environment, then the default device is obtained by looking for a string in the registry under:

HKEY_LOCAL_MACHINE/SOFTWARE/PortMidi/Recommended_Input_Device and

HKEY_LOCAL_MACHINE/SOFTWARE/PortMidi/Recommended_Output_Device

for a string. The number of the first device with a substring that matches the string exactly is returned. For example, if the string in the registry is “USB”, and device 1 is named “In USB MidiSport 1x1”, then that will be the default input because it contains the string “USB”.

In addition to the name, get_device_info() returns “interf”, which is the interface name. (The “interface” is the underlying software system or API used by PortMidi to access devices. Examples are MMSystem, DirectX (not implemented), ALSA, OSS (not implemented), etc.) At present, the only Win32 interface is “MMSystem”, the only Linux interface is “ALSA”, and the only Max OS X interface is “CoreMIDI”. To specify both the interface and the device name in the registry, separate the two with a comma and a space, e.g.: MMSystem, In USB MidiSport 1x1 In this case, the string before the comma must be a substring of the “interf” string, and the string after the space must be a substring of the “name” name string in order to match the device.

Note: in the current release, the default is simply the first device (the input or output device with the lowest PmDeviceID).

Raises a pygame2.Error, if the midi module is not initialized.

pygame2.midi.get_device_info(id) → string, string, bool, bool, bool

Gets information about a midi device.

Gets enhanced information about a midi device. The return values are

  • the name of the device, e.g. ‘ALSA’
  • the enhanced description of the device, e.g. ‘Midi Through Port-0’
  • a boolean indicating, whether the device is an input device
  • a boolean indicating, whether the device is an output device
  • a boolean indicating, whether the device is opened

in this order.

Raises a TypeError, if the id is not a integer value. Raises a ValueError, if the id is not within the range of available devices. Raises a pygame2.Error, if the midi module is not initialized.

pygame2.midi.init() → None

Initializes the midi module.

Raises a pygame2.Error on failure.

pygame2.midi.quit() → None
Uninitializes the midi module and releases all hold resources.
pygame2.midi.time() → int
Gets the time in milliseconds since the midi module was initialized.
pygame2.midi.was_init() → bool
Gets, whether the midi module was already initialized.

Input

class pygame2.midi.Input(id, bufsize=4096) → Input

Creates a new Input instance for a specific device.

The Input class gives read access to a specific midi device, which allows input, with buffering support.

Raises a ValueError, if the id is not within the range of available devices. Raises a pygame2.Error, if the midi module is not initialized.

Methods

Input.close() → None

Closes the Input device.

Raises a pygame2.Error, if the midi module is not initialized.

Input.poll() → bool

Gets, whether data is available on the buffer or not.

Raises a pygame2.Error, if the midi module is not initialized.

Input.read(amount) → list

Reads a certain amount of midi events from the buffer.

Reads from the Input buffer and gives back midi events in the form

[ [[status,data1,data2,data3],timestamp], [[status,data1,data2,data3],timestamp], ...]

Raises a pygame2.Error, if the midi module is not initialized.

Output

class pygame2.midi.Output(id, latency=0, bufsize=4096) → Output

Creates a new Output instance for a specific device.

The Output class gives write access to a specific midi device, which allows output.

latency is the delay in milliseconds applied to timestamps to determine when the output should actually occur. (If latency is < 0, 0 is assumed.)

If latency is zero, timestamps are ignored and all output is delivered immediately. If latency is greater than zero, output is delayed until the message timestamp plus the latency. (NOTE: time is measured relative to the time source indicated by time_proc. Timestamps are absolute, not relative delays or offsets.) In some cases, PortMidi can obtain better timing than your application by passing timestamps along to the device driver or hardware. Latency may also help you to synchronize midi data to audio data by matching midi latency to the audio buffer latency.

Raises a ValueError, if the id is not within the range of available devices. Raises a pygame2.Error, if the midi module is not initialized.

Methods

Output.abort() → None

Aborts outgoing messages immediately.

The caller should immediately close the output port; this call may result in transmission of a partial midi message.

Raises a pygame2.Error, if the midi module is not initialized.

Output.close() → None

Closes the Output device.

Raises a pygame2.Error, if the midi module is not initialized.

Output.note_off(note, velocity=None, channel = 0) → None

Turn a note off in the output stream.

Turn a note off in the output stream. The note must already be on for this to work correctly.

Raises a ValueError, if channel is not in the range [0, 15]. Raises a pygame2.Error, if the midi module is not initialized.

Output.note_on(note, velocity=None, channel=0) → None

Turn a note on in the output stream.

Turn a note on in the output stream. The note must already be off for this to work correctly.

Raises a ValueError, if channel is not in the range [0, 15]. Raises a pygame2.Error, if the midi module is not initialized.

Output.set_instrument(id, channel=0) → None

Select an instrument, with a value between 0 and 127.

Selects an instrument, where the id is in a range of [0, 127].

Raises a ValueError, if id is not in the range [0, 127]. Raises a ValueError, if channel is not in the range [0, 15]. Raises a pygame2.Error, if the midi module is not initialized.

Output.write(data) → None

Writes midi data to the output device.

Writes series of MIDI information in the form of a list:

write([[[status <,data1><,data2><,data3>],timestamp], [[status <,data1><,data2><,data3>],timestamp],...])

<data> fields are optional.

Example: choose program change 1 at time 20000 and send note 65 with velocity 100 500 ms later.

write([[[0xc0,0,0],20000],[[0x90,60,100],20500]])

Notes: 1. timestamps will be ignored if latency = 0. 2. To get a note to play immediately, send MIDI info with timestamp read from function time(). 3. understanding optional data fields:

write([[[0xc0,0,0],20000]])

is equivalent to

write([[[0xc0],20000]])

This can send up to 1024 elements in your data list, otherwise an IndexError is raised. Raises a pygame2.Error, if the midi module is not initialized.

Output.write_short(status, data1=None, data2=None) → None

Writes MIDI information of 3 bytes or less.

Writes a short MIDI information to the device. The data fields are optional and assumed 0, if omitted. The status byte could be:

0xc0 = program change 0x90 = note on ...

Example: note 65 on with velocity 100

write_short(0x90,65,100)

Raises a pygame2.Error, if the midi module is not initialized.

Output.write_sys_ex(timestamp, msg) → None

Writes a timestamped, system-exclusive message.

Writes a system-exclusive message msg, which can be either a byte buffer or string - or - list of bytes.

Example:

write_sys_ex (0, ‘xF0x7Dx10x11x12x13xF7’)

is equivalent to

write_sys_ex (pygame2.midi.time (), [0xF0,0x7D,0x10,0x11,0x12,0x13,0xF7])

Raises a pygame2.Error, if the midi module is not initialized.