Registry / serialization / mido
library1.3.3pypypi✓ verified 85d ago

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.

pip install mido
INSTALL
IMPORT
SIG · MIDO
M
mido
serializationpythonv1.3.3
Install
1.8s avg
Import
115ms
Disk
20MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.3.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
4/8 runs
✓ 1.69s
py 3.11
4/8 runs
✓ 1.75s
py 3.12
4/8 runs
✓ 1.58s
py 3.13
4/8 runs
4/8 runs
py 3.9
4/8 runs
✓ 2.01s
20MB installed
● package 20MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

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

This quickstart demonstrates how to create MIDI messages, list available ports, send a message to an output port, and read/play messages from a MIDI file. It includes a fallback for when no physical MIDI ports are available and creates a dummy MIDI file for demonstration.

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}")
Debug
Known issues
breakingThe `mode` attribute was removed from `key_signature` messages in Mido 1.1.20. Minor keys are now appended with 'm', e.g., 'Cm'.
fix
Adjust code referencing `key_signature.mode` to parse the `key_signature.key` string directly.
affects: >=1.1.20
breakingMessage comparison (`==`) in Mido 1.1.18 started including the `time` attribute. This might break existing comparisons if time was not previously considered relevant.
fix
If `time` should be ignored for comparison, compare `msg1.bytes() == msg2.bytes()` instead of `msg1 == msg2`.
affects: >=1.1.18
breakingThe `pending()` method was removed from the port API in Mido 1.1.10. Instead, `_receive()` is now allowed to return messages.
fix
Refactor code using `port.pending()` to use `port.iter_pending()` for non-blocking iteration, or adapt to the new `_receive()` behavior if implementing custom ports.
affects: >=1.1.10
gotchaMido 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.
fix
Add or subtract 1 when converting between Mido's internal channel representation and a user-facing 1-16 range.
affects: All versions
gotchaDirectly 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.
fix
Use `new_msg = msg.copy(note=70)` instead of `msg.note = 70` to modify messages.
affects: All versions
gotchaAs 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.
fix
To explicitly use `PortMidi`, set the environment variable `MIDO_BACKEND=mido.backends.portmidi` or call `mido.set_backend('mido.backends.portmidi')` in your program.
affects: >=1.1.10
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'mido'
This error occurs when the `mido` library is not installed in the Python environment being used, or the Python interpreter cannot find the installed module.
fix
Ensure `mido` is installed by running: `pip install mido`. If you intend to use MIDI ports, you might also need to install a backend, for example: `pip install mido[ports-rtmidi]`.
OSError: no ports available
This typically means that `mido` cannot find any available MIDI input/output ports on your system, or the chosen port is already in use by another application. It can also happen if the necessary MIDI drivers are not installed or configured correctly on your operating system.
fix
First, check that your MIDI device is connected and its drivers are installed and up to date. Close any other applications that might be using the MIDI port. You can list available ports using `mido.get_input_names()` and `mido.get_output_names()` to verify the port name and availability. If using `open_input()` or `open_output()`, ensure you are attempting to open the correct type of port (e.g., don't use an input port name with `open_output()`).
IOError: MThd not found. Probably not a MIDI file
This error occurs when `mido` attempts to open a file as a MIDI file, but the file does not have the expected 'MThd' header, indicating it's either not a valid standard MIDI file or it's corrupted. Sometimes, SysEx dumps are mistaken for MIDI files.
fix
Verify that the file you are trying to open is indeed a standard MIDI (.mid) file. If it's a SysEx (.syx) file, use `mido.read_syx_file()` instead of `mido.MidiFile()` or `mido.open_midi_file()`.
Upgrade
Version history
1.3.3latest on PyPI · released Oct 25, 2024
Audit
Dependencies
packagingrequiredFor version introspection.
importlib_metadataoptionalFor version introspection on Python < 3.8.
Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
mido — pip install mido · libregistry