| 1 |
.. SPDX-FileCopyrightText: 2017 Ole Martin Bjorndalen <[email protected]> |
| 2 |
.. |
| 3 |
.. SPDX-License-Identifier: CC-BY-4.0 |
| 4 |
|
| 5 |
RtMidi (Default, Recommended) |
| 6 |
----------------------------- |
| 7 |
|
| 8 |
Name: ``mido.backends.rtmidi`` |
| 9 |
|
| 10 |
Resources: |
| 11 |
|
| 12 |
* `python-rtmidi Python Library <https://pypi.org/project/python-rtmidi/>`_ |
| 13 |
* `RtMidi C Library <https://www.music.mcgill.ca/~gary/rtmidi/>`_ |
| 14 |
|
| 15 |
The RtMidi backend is a thin wrapper around `python-rtmidi |
| 16 |
<https://pypi.org/project/python-rtmidi/>`_. |
| 17 |
|
| 18 |
|
| 19 |
Features |
| 20 |
^^^^^^^^ |
| 21 |
|
| 22 |
* callbacks |
| 23 |
|
| 24 |
* true blocking ``receive()`` in Python 3 (using a *callback* and a |
| 25 |
*queue*) |
| 26 |
|
| 27 |
* virtual ports (Except on Microsoft Windows) |
| 28 |
|
| 29 |
* ports can be opened multiple times, each will receive a copy of all messages |
| 30 |
|
| 31 |
* a *client name* can be specified when opening a virtual port |
| 32 |
|
| 33 |
* sends but doesn't receive active sensing (By default) |
| 34 |
|
| 35 |
* port list is always up to date |
| 36 |
|
| 37 |
* all methods but ``close()`` are thread safe |
| 38 |
|
| 39 |
|
| 40 |
Port Names (Linux/ALSA) |
| 41 |
^^^^^^^^^^^^^^^^^^^^^^^ |
| 42 |
|
| 43 |
When you're using Linux/ALSA the port names include client name and |
| 44 |
ALSA client and port numbers, for example: |
| 45 |
|
| 46 |
.. code-block:: python |
| 47 |
|
| 48 |
>>> mido.get_output_names() |
| 49 |
['TiMidity:TiMidity port 0 128:0'] |
| 50 |
|
| 51 |
The ALSA client and port numbers ("``128:0``" in this case) can change |
| 52 |
from session to session, making it hard to hard code port names or use |
| 53 |
them in configuration files. |
| 54 |
|
| 55 |
To get around this the RtMidi backend allows you to leave out the |
| 56 |
port number of port number and client names. These lines will all open |
| 57 |
the same port as above: |
| 58 |
|
| 59 |
.. code-block:: python |
| 60 |
|
| 61 |
mido.open_output('TiMidity port 0') |
| 62 |
|
| 63 |
.. code-block:: python |
| 64 |
|
| 65 |
mido.open_output('TiMidity:TiMidity port 0') |
| 66 |
|
| 67 |
.. code-block:: python |
| 68 |
|
| 69 |
mido.open_output('TiMidity:TiMidity port 0 128:0') |
| 70 |
|
| 71 |
There is currently no way to list ports without port number or client |
| 72 |
name. This can be added in a future version of there is demand for it |
| 73 |
and a suitable API is found. |
| 74 |
|
| 75 |
|
| 76 |
Virtual Ports |
| 77 |
^^^^^^^^^^^^^ |
| 78 |
|
| 79 |
RtMidi is the only backend that can create virtual ports: |
| 80 |
|
| 81 |
.. code-block:: python |
| 82 |
|
| 83 |
>>> port = mido.open_input('New Port', virtual=True) |
| 84 |
>>> port |
| 85 |
<open input 'New Port' (RtMidi/LINUX_ALSA)> |
| 86 |
|
| 87 |
Other applications can now connect to this port. (One oddity is that, |
| 88 |
at least in Linux, RtMidi can't see its own virtual ports, while |
| 89 |
PortMidi can see them.) |
| 90 |
|
| 91 |
.. note:: |
| 92 |
|
| 93 |
Virtual Ports are **not** available under Microsoft Windows. An alternative |
| 94 |
is to use third party software such as Tobias Erichsen's `loopMIDI |
| 95 |
<https://www.tobias-erichsen.de/software/loopmidi.html>`_. |
| 96 |
|
| 97 |
|
| 98 |
Client Name |
| 99 |
^^^^^^^^^^^ |
| 100 |
|
| 101 |
.. versionadded:: 1.2 |
| 102 |
|
| 103 |
You can specify a client name for the port: |
| 104 |
|
| 105 |
.. code-block:: python |
| 106 |
|
| 107 |
>>> port = mido.open_input('New Port', client_name='My Client') |
| 108 |
|
| 109 |
This requires ``python-rtmidi >= 1.0rc1``. If ``client_name`` is passed |
| 110 |
the port will be a virtual port. |
| 111 |
|
| 112 |
.. note:: |
| 113 |
|
| 114 |
Unfortunately, at least with ALSA, opening two ports with the same |
| 115 |
``client_name`` creates two clients with the same name instead of one |
| 116 |
client with two ports. |
| 117 |
|
| 118 |
There are a couple of problems with port names in Linux. First, RtMidi |
| 119 |
can't see some software ports such as ``amSynth MIDI IN``. PortMidi |
| 120 |
uses the same ALSA sequencer API, so this is problem in RtMidi. |
| 121 |
|
| 122 |
Second, in some versions of RtMidi ports are named inconsistently. For |
| 123 |
example, the input port '``Midi Through 14:0``' has a corresponding output |
| 124 |
named '``Midi Through:0``'. Unless this was intended, it is a bug in |
| 125 |
RtMidi's ALSA implementation. |
| 126 |
|
| 127 |
|
| 128 |
Choosing an API |
| 129 |
^^^^^^^^^^^^^^^ |
| 130 |
|
| 131 |
The RtMidi library can be compiled with support for more than one API. |
| 132 |
|
| 133 |
To get a list of all available APIs at runtime:: |
| 134 |
|
| 135 |
>>> mido.backend.module.get_api_names() |
| 136 |
['LINUX_ALSA', 'UNIX_JACK'] |
| 137 |
|
| 138 |
You can select the API by adding it after the module name, either in |
| 139 |
the environment variable:: |
| 140 |
|
| 141 |
$ export MIDO_BACKEND=mido.backends.rtmidi/LINUX_ALSA |
| 142 |
$ export MIDO_BACKEND=mido.backends.rtmidi/UNIX_JACK |
| 143 |
|
| 144 |
or within the program using one of these:: |
| 145 |
|
| 146 |
>>> mido.set_backend('mido.backends.rtmidi/LINUX_ALSA') |
| 147 |
>>> mido.backend |
| 148 |
<backend mido.backends.rtmidi/LINUX_ALSA (not loaded)> |
| 149 |
|
| 150 |
>>> mido.Backend('mido.backends.rtmidi/UNIX_JACK') |
| 151 |
<backend mido.backends.rtmidi/UNIX_JACK (not loaded)> |
| 152 |
|
| 153 |
This allows you to, for example, use both ALSA and JACK ports in the |
| 154 |
same program. |