Skip to content

Instantly share code, notes, and snippets.

@SpotlightKid
Last active November 23, 2017 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpotlightKid/593e393e729a9017b0ea3ac03be91e1d to your computer and use it in GitHub Desktop.
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.
#!/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.")
@SpotlightKid
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment