Backends

Choosing Backend

Mido comes with backends for PortMidi, RtMidi and Pygame.

RtMidi is the recommended backends. It has all the features of the other ones and more and is usually easier to install.

For historical reasons PortMidi is still the default. You can override this with the MIDO_BACKEND environment variable, for example:

$ MIDO_BACKEND=mido.backends.rtmidi ./program.py

Alternatively, you can set the backend from within your program:

>>> mido.set_backend('mido.backends.rtmidi')
>>> mido.backend
<backend mido.backends.rtmidi (not loaded)>

This will override the environment variable.

If you want to use more than one backend at a time, you can do:

rtmidi = mido.Backend('mido.backends.rtmidi')
portmidi = mido.Backend('mido.backends.portmidi')

input = rtmidi.open_input()
output = portmidi.open_output()
for message in input:
    output.send(message)

The backend will not be loaded until you call one of the open_ or get_ methods. You can pass load=True to have it loaded right away.

If you pass use_environ=True the module will use the environment variables MIDO_DEFAULT_INPUT etc. for default ports.

Environment Variables

You can override the backend’s choice of default ports with these three environment variables:

MIDO_DEFAULT_INPUT
MIDO_DEFAULT_OUTPUT
MIDO_DEFAULT_IOPORT

For example:

$ MIDO_DEFAULT_INPUT='SH-201' python program.py

or:

$ export MIDO_DEFAULT_OUTPUT='Integra-7'
$ python program1.py
$ python program2.py

PortMidi

Name: mido.backends.portmidi

The PortMidi backend is written with ctypes and requires only the shared library file portmidi.so or portmidi.dll.

Can send but doesn’t receive active_sensing messages.

PortMidi has no callback mechanism, so callbacks are implemented in Python with threads. Each port with a callback has a dedicated thread doing blocking reads from the device.

Due to limitations in PortMidi the port list will not be up-to-date if there are any ports open. (The refresh is implemented by re-initalizing PortMidi which would break any open ports.)

Pygame

Name: mido.backends.pygame

Uses pygame.midi which is implemented on top of PortMidi.

Doesn’t receive active_sensing.

Callbacks are currently not implemented.