Usage — python-rtmidi 1.5.8 documentation

# Usage

Here’s a quick example of how to use python-rtmidi to open the first available MIDI output port and send a middle C note on MIDI channel 1:

    import time
    import rtmidi

    midiout = rtmidi.MidiOut()
    available_ports = midiout.get_ports()

    if available_ports:
        midiout.open_port(0)
    else:
        midiout.open_virtual_port("My virtual output")

    with midiout:
        # channel 1, middle C, velocity 112
        note_on = [0x90, 60, 112]
        note_off = [0x80, 60, 0]
        midiout.send_message(note_on)
        time.sleep(0.5)
        midiout.send_message(note_off)
        time.sleep(0.1)

    del midiout

Note

On Windows it may be necessary to insert a small delay after the last message is sent and before the output port is closed, otherwise the message may be lost.

More usage examples can be found in the examples and tests directories of the source repository.