Created
January 14, 2023 00:37
-
-
Save ChrisWellsWood/c30eed98c2062f0059276587535acc22 to your computer and use it in GitHub Desktop.
A simple example of using rtmidi to listen for MIDI messages from a device.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import rtmidi | |
# initialise midi in class | |
midi_in = rtmidi.MidiIn() | |
# connect to a device | |
midi_in.open_port(3) | |
# get midi msgs | |
while True: | |
# get a message, returns None if there's no msg in queue | |
# also include the time since the last msg | |
msg_and_dt = midi_in.get_message() | |
# check to see if there is a message | |
if msg_and_dt: | |
# unpack the msg and time tuple | |
(msg, dt) = msg_and_dt | |
# convert the command integer to a hex so it's easier to read | |
command = hex(msg[0]) | |
print(f"{command} {msg[1:]}\t| dt = {dt:.2f}") | |
else: | |
# add a short sleep so the while loop doesn't hammer your cpu | |
time.sleep(0.001) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment