Last active
November 23, 2017 10:52
-
-
Save SpotlightKid/593e393e729a9017b0ea3ac03be91e1d to your computer and use it in GitHub Desktop.
Send an OSC message when a MIDI Note On message is received using python-rtmidi and pyliblo.
This file contains 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
#!/usr/bin/env python | |
# | |
# noteon2osc.py | |
# | |
"""Send an OSC message when a MIDI Note On message is received.""" | |
import time | |
import liblo | |
from rtmidi.midiconstants import NOTE_ON | |
from rtmidi.midiutil import open_midiinput | |
def midiin_callback(event, data=None): | |
message, deltatime = event | |
if message[0] & 0xF0 == NOTE_ON: | |
status, note, velocity = message | |
channel = (status & 0xF) + 1 | |
liblo.send( | |
('localhost', 9001), | |
'/midi/%i/noteon' % channel, | |
note, | |
velocity) | |
try: | |
# Prompts user for MIDI input port, defaulting to ALSA on Linux | |
with open_midiinput(client_name='noteon2osc')[0] as midiin: | |
midiin.set_callback(midiin_callback) | |
while True: | |
time.sleep(1) | |
except (EOFError, KeyboardInterrupt): | |
print("Bye.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Latest version: https://github.com/SpotlightKid/python-rtmidi/blob/master/examples/basic/noteon2osc.py