|
| 1 | +rtmidi-python |
| 2 | +============= |
| 3 | + |
| 4 | +Python wrapper for `RtMidi`_, the lightweight, cross-platform MIDI I/O |
| 5 | +library. For Linux, Mac OS X and Windows. |
| 6 | + |
| 7 | +Setup |
| 8 | +----- |
| 9 | + |
| 10 | +The wrapper is written in `Cython`_, but the generated C++ code is |
| 11 | +included, so you can install the module as usual:: |
| 12 | + |
| 13 | + python setup.py install |
| 14 | + |
| 15 | +If you want to build from the Cython source, make sure that you have a |
| 16 | +recent version of Cython (>= 0.17), and run:: |
| 17 | + |
| 18 | + python setup.py install --from-cython |
| 19 | + |
| 20 | +Usage Examples |
| 21 | +-------------- |
| 22 | + |
| 23 | +*rtmidi-python* uses the same API as `RtMidi`_, only reformatted to comply |
| 24 | +with PEP-8, and with small changes to make it a little more pythonic. |
| 25 | + |
| 26 | +Print all output ports |
| 27 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 28 | + |
| 29 | +:: |
| 30 | + |
| 31 | + import rtmidi_python as rtmidi |
| 32 | + |
| 33 | + midi_out = rtmidi.MidiOut() |
| 34 | + for port_name in midi_out.ports: |
| 35 | + print port_name |
| 36 | + |
| 37 | +Send messages |
| 38 | +~~~~~~~~~~~~~ |
| 39 | + |
| 40 | +:: |
| 41 | + |
| 42 | + import rtmidi_python as rtmidi |
| 43 | + |
| 44 | + midi_out = rtmidi.MidiOut() |
| 45 | + midi_out.open_port(0) |
| 46 | + |
| 47 | + midi_out.send_message([0x90, 48, 100]) # Note on |
| 48 | + midi_out.send_message([0x80, 48, 100]) # Note off |
| 49 | + |
| 50 | +Get incoming messages by polling |
| 51 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 52 | + |
| 53 | +:: |
| 54 | + |
| 55 | + import rtmidi_python as rtmidi |
| 56 | + |
| 57 | + midi_in = rtmidi.MidiIn() |
| 58 | + midi_in.open_port(0) |
| 59 | + |
| 60 | + while True: |
| 61 | + message, delta_time = midi_in.get_message() |
| 62 | + if message: |
| 63 | + print message, delta_time |
| 64 | + |
| 65 | +Note that the signature of ``get_message()`` differs from the original |
| 66 | +`RtMidi`_ API: It returns a tuple instead of using a return parameter. |
| 67 | + |
| 68 | +Get incoming messages using a callback |
| 69 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 70 | + |
| 71 | +:: |
| 72 | + |
| 73 | + import rtmidi_python as rtmidi |
| 74 | + |
| 75 | + def callback(message, time_stamp): |
| 76 | + print message, time_stamp |
| 77 | + |
| 78 | + midi_in = rtmidi.MidiIn() |
| 79 | + midi_in.callback = callback |
| 80 | + midi_in.open_port(0) |
| 81 | + |
| 82 | + # do something else here (but don't quit) |
| 83 | + |
| 84 | +Note that the signature of the callback differs from the original `RtMidi`_ |
| 85 | +API: ``message`` is now the first parameter, like in the tuple returned by |
| 86 | +``get_message()``. |
| 87 | + |
| 88 | +License |
| 89 | +------- |
| 90 | + |
| 91 | +*rtmidi-python* is licensed under the MIT License, see LICENSE. |
| 92 | + |
| 93 | +It uses `RtMidi`_, licensed under a modified MIT License, see |
| 94 | +RtMidi/RtMidi.h. |
| 95 | + |
| 96 | +.. _RtMidi: http://www.music.mcgill.ca/~gary/rtmidi/ |
| 97 | +.. _Cython: http://www.cython.org |
0 commit comments