| 1 |
.. SPDX-FileCopyrightText: 2017 Ole Martin Bjorndalen <[email protected]> |
| 2 |
.. |
| 3 |
.. SPDX-License-Identifier: CC-BY-4.0 |
| 4 |
|
| 5 |
Backends |
| 6 |
======== |
| 7 |
|
| 8 |
A backend provides the interface between Mido and the operating system level |
| 9 |
MIDI stack. |
| 10 |
|
| 11 |
Some Mido features are only available with select backends. |
| 12 |
|
| 13 |
Mido's backend subsystem has been designed to be extensible so you can add |
| 14 |
your own backends if required. See :doc:`custom`. |
| 15 |
|
| 16 |
Providing platform specific Python-native backends is currently evaluated. |
| 17 |
See: https://github.com/mido/mido/issues/506 |
| 18 |
|
| 19 |
.. todo:: Insert a stack diagram to clear things up. |
| 20 |
|
| 21 |
|
| 22 |
Choice |
| 23 |
------ |
| 24 |
|
| 25 |
Mido comes with five backends: |
| 26 |
|
| 27 |
* :doc:`RtMidi <rtmidi>` is the *default* and *recommended* backend. It has all |
| 28 |
the features of the other ones and more plus it is usually easier to install. |
| 29 |
|
| 30 |
* :doc:`PortMidi <portmidi>` was the default backend up until version 1.2. It |
| 31 |
uses the ``portmidi`` shared library and can be difficult to install on some |
| 32 |
systems. |
| 33 |
|
| 34 |
* :doc:`Pygame <pygame>` uses the ``pygame.midi`` module. |
| 35 |
|
| 36 |
* :doc:`rtmidi-python <rtmidi_python>` uses the ``rtmidi_python`` package, an |
| 37 |
alternative wrapper for PortMidi. It is currently very basic but |
| 38 |
easier to install on some Windows systems. |
| 39 |
|
| 40 |
* :doc:`Amidi <amidi>` is an experimental backend for Linux/ALSA |
| 41 |
that uses the command ``amidi`` to send and receive messages. |
| 42 |
|
| 43 |
You can set the backend using an environment variable: See :ref:`env_vars`. |
| 44 |
|
| 45 |
Alternatively, you can set the backend from within your program:: |
| 46 |
|
| 47 |
>>> mido.set_backend('mido.backends.portmidi') |
| 48 |
>>> mido.backend |
| 49 |
<backend mido.backends.portmidi (not loaded)> |
| 50 |
|
| 51 |
.. note:: |
| 52 |
|
| 53 |
This will override the environment variable. |
| 54 |
|
| 55 |
If you want to use more than one backend at a time, you can do:: |
| 56 |
|
| 57 |
rtmidi = mido.Backend('mido.backends.rtmidi') |
| 58 |
portmidi = mido.Backend('mido.backends.portmidi') |
| 59 |
|
| 60 |
input = rtmidi.open_input() |
| 61 |
output = portmidi.open_output() |
| 62 |
for message in input: |
| 63 |
output.send(message) |
| 64 |
|
| 65 |
The backend will not be loaded until you call one of the ``open_`` or |
| 66 |
``get_`` methods. You can pass ``load=True`` to have it loaded right |
| 67 |
away. |
| 68 |
|
| 69 |
If you pass ``use_environ=True``, the module will use the environment |
| 70 |
variables ``MIDO_DEFAULT_INPUT`` etc. for default ports. |
| 71 |
|
| 72 |
|
| 73 |
.. _env_vars: |
| 74 |
|
| 75 |
Environment Variables |
| 76 |
--------------------- |
| 77 |
|
| 78 |
|
| 79 |
Select Backend |
| 80 |
^^^^^^^^^^^^^^ |
| 81 |
|
| 82 |
If you want to use a backend other than RtMidi you can override this with |
| 83 |
the ``MIDO_BACKEND`` environment variable, for example:: |
| 84 |
|
| 85 |
$ MIDO_BACKEND=mido.backends.portmidi ./program.py |
| 86 |
|
| 87 |
|
| 88 |
Set Default ports |
| 89 |
^^^^^^^^^^^^^^^^^ |
| 90 |
|
| 91 |
You can override the backend's choice of default ports with these |
| 92 |
three environment variables:: |
| 93 |
|
| 94 |
MIDO_DEFAULT_INPUT |
| 95 |
MIDO_DEFAULT_OUTPUT |
| 96 |
MIDO_DEFAULT_IOPORT |
| 97 |
|
| 98 |
For example:: |
| 99 |
|
| 100 |
$ MIDO_DEFAULT_INPUT='SH-201' python3 program.py |
| 101 |
|
| 102 |
or:: |
| 103 |
|
| 104 |
$ export MIDO_DEFAULT_OUTPUT='Integra-7' |
| 105 |
$ python3 program1.py |
| 106 |
$ python3 program2.py |
| 107 |
|
| 108 |
|
| 109 |
Available Backends |
| 110 |
------------------ |
| 111 |
|
| 112 |
.. toctree:: |
| 113 |
|
| 114 |
rtmidi |
| 115 |
portmidi |
| 116 |
pygame |
| 117 |
rtmidi_python |
| 118 |
amidi |
| 119 |
|
| 120 |
.. include:: custom.rst |