Package: rtmidi
pyrtmidiRealtime MIDI I/O for Python on Windows, OS X, and Linux. Includes comprehensive MidiMessage class, support for virtual ports on OS X and Linux, and multi-threaded lister utility classes.Pyrtmidi provides MIDI I/O forPKMidiCron.InstallationInstall using:pipinstallrtmidiUsagepyrtmidi is a Python interface to RtMidi. It provides real-time midi input and output.importrtmidimidiin=rtmidi.RtMidiIn()defprint_message(midi):ifmidi.isNoteOn():print('ON: ',midi.getMidiNoteName(midi.getNoteNumber()),midi.getVelocity())elifmidi.isNoteOff():print('OFF:',midi.getMidiNoteName(midi.getNoteNumber()))elifmidi.isController():print('CONTROLLER',midi.getControllerNumber(),midi.getControllerValue())ports=range(midiin.getPortCount())ifports:foriinports:print(midiin.getPortName(i))print("Opening port 0!")midiin.openPort(0)whileTrue:m=midiin.getMessage(250)# some timeout in msifm:print_message(m)else:print('NO MIDI INPUT PORTS!')The API is copiednearverbatim from the C++ code. Refer to theRtMidi tutorial, and take into account the following caveats:RtMidiIngetMessage(timeout_ms=None)The message argument has been replaced with an optional millisecond timeout value.The function will return a MidiMessage object, or None if no message is available.setCallbackThis function works just as described in the above docs, and takes a python callable object.This method is most useful with a queue.Queue object to communicate between threads.MidiMessageThis class has been taken from the juce library, and includes an excellent comprehensive set of midi functions.please check here for available methods.RecipesThe following implements a QObject wrapper for rtmidi.RtMidiIn that emits a 'message()' signal. The essential code is follows:classMidiInput(QThread):def__init__(self,devid,parent=None):QThread.__init__(self,parent)self.device=rtmidi.RtMidiIn()self.device.openPort(devid)self.running=Falsedefrun(self):self.running=Truewhileself.running:msg=self.device.getMessage(250)ifmsg:self.msg=msgself.emit(SIGNAL('message(PyObject *)'),self.msg)self.emit(SIGNAL('message()')midi=MidiInput(1)defslotMessage(msg):printmsgQObject.connect(midi,SIGNAL('message(PyObject *)'),slotMessage)The following implements a midi echo for all ports.importsysimportrtmidiimportthreadingdefprint_message(midi,port):ifmidi.isNoteOn():print'%s: ON: '%port,midi.getMidiNoteName(midi.getNoteNumber()),midi.getVelocity()elifmidi.isNoteOff():print'%s: OFF:'%port,midi.getMidiNoteName(midi.getNoteNumber())elifmidi.isController():print'%s: CONTROLLER'%port,midi.getControllerNumber(),midi.getControllerValue()classCollector(threading.Thread):def__init__(self,device,port):threading.Thread.__init__(self)self.setDaemon(True)self.port=portself.portName=device.getPortName(port)self.device=deviceself.quit=Falsedefrun(self):self.device.openPort(self.port)self.device.ignoreTypes(True,False,True)whileTrue:ifself.quit:returnmsg=self.device.getMessage()ifmsg:print_message(msg,self.portName)dev=rtmidi.RtMidiIn()collectors=[]foriinrange(dev.getPortCount()):device=rtmidi.RtMidiIn()print'OPENING',dev.getPortName(i)collector=Collector(device,i)collector.start()collectors.append(collector)print'HIT ENTER TO EXIT'sys.stdin.read(1)forcincollectors:c.quit=TrueCommon ProblemsThis is a commonly reported build error on linux, although I it works for me using python2.7 on ubuntu.:~/devel/pkaudio/pyrtmidi/tests$ python test_rtmidi.py 
Traceback (most recent call last):
File "test_rtmidi.py", line 29, in 
import rtmidi
ImportError: /usr/local/lib/python2.6/dist-packages/rtmidi.so: undefined symbol: _ZN11MidiMessageaSERKS_