Mido - MIDI Objects for Python
Mido is a Python library for working with MIDI 1.0 ports, messages, and files. It provides a straightforward and Pythonic interface for interacting with MIDI hardware and software. The library is currently at version 1.3.3 and has an active development cycle, with new features and bug fixes being regularly released.
Warnings
- breaking The `mode` attribute was removed from `key_signature` messages in Mido 1.1.20. Minor keys are now appended with 'm', e.g., 'Cm'.
- breaking Message comparison (`==`) in Mido 1.1.18 started including the `time` attribute. This might break existing comparisons if time was not previously considered relevant.
- breaking The `pending()` method was removed from the port API in Mido 1.1.10. Instead, `_receive()` is now allowed to return messages.
- gotcha Mido represents MIDI channels as 0-15, while many MIDI specifications and user interfaces use 1-16. Be mindful of this offset when displaying or receiving channel numbers from users.
- gotcha Directly setting attributes on `mido.Message` objects (e.g., `msg.note = 70`) is possible but discouraged. It's generally better to use `msg.copy()` to create a new message with updated attributes for immutability and validation.
- gotcha As of Mido 1.1.10, `RtMidi` became the default backend, replacing `PortMidi`. While generally an improvement, this might lead to different port availability or behavior for users who relied on `PortMidi`'s default selection.
Install
-
pip install mido -
pip install mido[ports-rtmidi]
Imports
- Message
from mido import Message
- open_input
import mido; inport = mido.open_input()
- open_output
import mido; outport = mido.open_output()
- MidiFile
from mido import MidiFile
Quickstart
import mido
# Create a MIDI message
msg = mido.Message('note_on', note=60, velocity=64, time=0)
print(f"Created message: {msg}")
# List available MIDI ports (example, actual names vary)
print("Available input ports:", mido.get_input_names())
print("Available output ports:", mido.get_output_names())
# Open an output port and send a message (replaces 'Port Name' with an actual port if available)
try:
outport_name = next(iter(mido.get_output_names()), None)
if outport_name:
print(f"Attempting to open output port: {outport_name}")
with mido.open_output(outport_name) as outport:
print(f"Sending {msg} to {outport_name}")
outport.send(msg)
print("Message sent.")
else:
print("No output MIDI ports found to send message.")
except Exception as e:
print(f"Could not send MIDI message: {e}")
# Read a MIDI file (requires a 'test.mid' file, creating a dummy one)
try:
mid = mido.MidiFile()
track = mido.MidiTrack()
mid.tracks.append(track)
track.append(mido.Message('note_on', note=60, velocity=100, time=0))
track.append(mido.Message('note_off', note=60, velocity=100, time=120))
mid.save('test.mid')
print("Created dummy 'test.mid'")
print("Reading messages from 'test.mid':")
for msg in mido.MidiFile('test.mid').play():
print(msg)
except Exception as e:
print(f"Could not read/create MIDI file: {e}")