Backends

A backend provides the interface between Mido and the operating system level MIDI stack.

Some Mido features are only available with select backends.

Mido’s backend subsystem has been designed to be extensible so you can add your own backends if required. See Writing a New or Custom Backend.

Providing platform specific Python-native backends is currently evaluated. See: https://github.com/mido/mido/issues/506

Todo

Insert a stack diagram to clear things up.

Choice

Mido comes with five backends:

  • RtMidi is the default and recommended backend. It has all the features of the other ones and more plus it is usually easier to install.

  • PortMidi was the default backend up until version 1.2. It uses the portmidi shared library and can be difficult to install on some systems.

  • Pygame uses the pygame.midi module.

  • rtmidi-python uses the rtmidi_python package, an alternative wrapper for PortMidi. It is currently very basic but easier to install on some Windows systems.

  • Amidi is an experimental backend for Linux/ALSA that uses the command amidi to send and receive messages.

You can set the backend using an environment variable: See Environment Variables.

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

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

Note

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

Select Backend

If you want to use a backend other than RtMidi you can override this with the MIDO_BACKEND environment variable, for example:

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

Set Default ports

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' python3 program.py

or:

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

Available Backends

Writing a New or Custom Backend

A backend is a Python module with one or more of these:

Input -- an input port class
Output -- an output port class
IOPort -- an I/O port class
get_devices() -- returns a list of devices

Once written, the backend can be used by setting the environment variable MIDO_BACKEND or by calling mido.set_backend(). In both cases, the path of the module is used.

Input

And input class for open_input(). This is only required if the backend supports input.

Output

And output class for open_output(). This is only required if the backend supports output.

IOPort

An I/O port class for open_ioport(). If this is not found, open_ioport() will return mido.ports.IOPort(Input(), Output()).

get_devices(**kwargs)

Returns a list of devices, where each device is dictionary with at least these three values:

{
  'name': 'Some MIDI Input Port',
  'is_input': True,
  'is_output': False,
}

These are used to build return values for get_input_names() etc.. This function will also be available to the user directly.

For examples, see mido/backends/.