This is the library I built to easily write and read MIDI messages via Python.
When I decided to build my own midi foot-controller, I wanted to run it using Python.
As I looked for Python MIDI libraries, I didn't find anything that suited me, so I decided to program it myself. I published it in Python officials package, and all the code is open-source.
import midi connector = midi.MidiConector('/dev/serial0') # the argument must match the path to serial port # If you want to read MIDI message message = connector.read() # return a Message type object # You can then access several attribute of the message t = message.type # could be ProgramChange, ControlChange, SysEx, NoteOn, ... channel = message.channel # Midi channel on which the message has been received, from 1 to 16 first_byte = message.status # Value of the first byte (a Midi message is composed of 2 or 3 bytes) # If you want to send MIDI messages note = NoteOn(32, 127) # Note number 32 (G#) at max velocity channel = 1 # You are about to send a message on MIDI channel 1 message = Message(note, channel) connector.write(message) # will send the MIDI message NoteOn on channel #1
You can also send or receive in OMNI mode (ie on every channel at the same time). For further details, please have a look at the source code, or run
to read the docs.
As always, you are more than welcome to contact me if you have any feedback or suggestions.