|
| 1 | +======= |
| 2 | +rtmidi2 |
| 3 | +======= |
| 4 | + |
| 5 | +Python wrapper for RtMidi_, the lightweight, cross-platform MIDI I/O library. For Linux, Mac OS X and Windows. |
| 6 | + |
| 7 | +Based on rtmidi-python |
| 8 | + |
| 9 | +Setup |
| 10 | +----- |
| 11 | + |
| 12 | +The wrapper is written in Cython_. Cython should be installed for this module to be installed. |
| 13 | +RtMidi is included in the source tree, so you only need to do:: |
| 14 | + |
| 15 | + python setup.py install |
| 16 | + |
| 17 | +Python 2 & 3 |
| 18 | +------------ |
| 19 | + |
| 20 | +This module is compatible with both Python 2 and Python 3. The only visible difference is that under python 3, all strings are byte strings. If you pass a unicode string to any function taking a string (open_virtual_port), an attempt will be made to encode the string as ASCII, through .encode("ASCII", errors="ignore"). |
| 21 | + |
| 22 | +Usage Examples |
| 23 | +-------------- |
| 24 | + |
| 25 | +`rtmidi2` uses a very similar API as RtMidi |
| 26 | + |
| 27 | +Print all in and out ports |
| 28 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 29 | + |
| 30 | +.. code-block:: python |
| 31 | +
|
| 32 | + import rtmidi2 |
| 33 | + print(rtmidi2.get_in_ports()) |
| 34 | + print(rtmidi2.get_out_ports()) |
| 35 | +
|
| 36 | +
|
| 37 | +Send messages |
| 38 | +~~~~~~~~~~~~~ |
| 39 | + |
| 40 | +.. code-block:: python |
| 41 | +
|
| 42 | + import rtmidi2 |
| 43 | + |
| 44 | + midi_out = rtmidi2.MidiOut() |
| 45 | + # open the first available port |
| 46 | + midi_out.open_port(0) |
| 47 | + # send C3 with vel. 100 on channel 1 |
| 48 | + midi_out.send_noteon(0, 48, 100) |
| 49 | +
|
| 50 | +
|
| 51 | +
|
| 52 | +Get incoming messages - blocking interface |
| 53 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 54 | + |
| 55 | +.. code-block:: python |
| 56 | +
|
| 57 | + midi_in = rtmidi.MidiIn() |
| 58 | + midi_in.open_port(0) |
| 59 | +
|
| 60 | + while True: |
| 61 | + message, delta_time = midi_in.get_message() # will block until a message is available |
| 62 | + if message: |
| 63 | + print(message, delta_time) |
| 64 | +
|
| 65 | +
|
| 66 | +Get incoming messages using a callback -- non blocking |
| 67 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 68 | + |
| 69 | +.. code-block:: python |
| 70 | +
|
| 71 | + def callback(message, time_stamp): |
| 72 | + print(message, time_stamp) |
| 73 | +
|
| 74 | + midi_in = rtmidi2.MidiIn() |
| 75 | + midi_in.callback = callback |
| 76 | + midi_in.open_port(0) |
| 77 | +
|
| 78 | +
|
| 79 | +Open multiple ports at once |
| 80 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 81 | + |
| 82 | +.. code-block:: python |
| 83 | +
|
| 84 | + # get messages from all available ports |
| 85 | + midi_in = MidiInMulti().open_ports("*") |
| 86 | +
|
| 87 | + def callback(msg, timestamp): |
| 88 | + msgtype, channel = splitchannel(msg[0]) |
| 89 | + print(msgtype2str(msgtype), msg[1], msg[2]) |
| 90 | +
|
| 91 | + midi_in.callback = callback |
| 92 | +
|
| 93 | +
|
| 94 | +You can also get the device which generated the event by changing your callback to: |
| 95 | + |
| 96 | +.. code-block:: python |
| 97 | +
|
| 98 | + def callback(src, msg, timestamp): |
| 99 | + # src will hold the name of the device |
| 100 | + print("got message from", src) |
| 101 | +
|
| 102 | + |
| 103 | +Send multiple notes at once |
| 104 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 105 | + |
| 106 | +.. code-block:: python |
| 107 | +
|
| 108 | + # send a cluster of ALL notes with a duration of 1 second |
| 109 | + midi_out = MidiOut().open_port() |
| 110 | + notes = range(127) |
| 111 | + velocities = [90] * len(notes) |
| 112 | + midi_out.send_noteon_many(0, notes, velocities) |
| 113 | + time.sleep(1) |
| 114 | + midi_out.send_noteon_many(0, notes, [0] * len(notes)) |
| 115 | +
|
| 116 | +
|
| 117 | +---- |
| 118 | + |
| 119 | + |
| 120 | +License |
| 121 | +------- |
| 122 | + |
| 123 | +`rtmidi2` is licensed under the MIT License, see `LICENSE`. |
| 124 | + |
| 125 | +It uses RtMidi, licensed under a modified MIT License, see `RtMidi/RtMidi.h`. |
| 126 | + |
| 127 | + |
| 128 | +.. _RtMidi: http://www.music.mcgill.ca/~gary/rtmidi/ |
| 129 | +.. _Cython: http://www.cython.org |
0 commit comments