Python MIDI libraries

Updated Nov 2026

Python MIDI libraries read and write MIDI files and exchange MIDI messages with instruments and software in real time. A file library edits the notes and timing stored in a file rather than the live messages that a port library sends to connected devices. These libraries let music apps and AI music models read and generate notes as data.

Popularity

Mido has the most monthly downloads among the dedicated MIDI libraries in this comparison, at 1.4M.

LibraryMonthly downloadsStarsLast releaseLicense
pygame
2.7M
9.0KSep 2024GNU Library or Lesser General Public License (LGPL)
Mido
1.4M
1.7KOct 2024MIT
music21
950.0K
2.6KOct 2026BSD
pretty_midi
692.7K
1.0KJul 2026MIT
Symusic
461.4K
190Apr 2026MIT
python-rtmidi
203.0K
417Nov 2023MIT
MIDIUtil
41.3K
273Mar 2018MIT
Miditoolkit
23.6K
285Nov 2023MIT
Downloads: PyPI, last thirty days. Stars and releases: GitHub. Amber: no release in over a year.

Libraries and code examples

Supports game development in Python.

Downloads / mo
2.7M
Stars
9.0K
Last release
2.6.1
License
GNU Library or Lesser General Public License (LGPL)

Quickstart

From midi.html
$ python3 -m pip install pygame
import pygame.midi
pygame.midi.init()
pygame.midi.get_count()
pygame.midi.quit()

pygame examples

From open-source projects on GitHub

Reads and writes MIDI messages and files, with a shared interface for live ports and backends.

Downloads / mo
1.4M
Stars
1.7K
Last release
1.3.3
License
MIT

Strengths

  • Exposes individual MIDI messages. Reads, edits, and writes messages, including common meta events.
  • Connects live MIDI ports. Choose a backend to send and receive real-time MIDI messages.

Limitations

  • Timing depends on the context. File tracks use tick deltas; file iteration returns elapsed seconds.
  • Independent tracks lack shared length. Asynchronous files cannot use the total playback-length property.

Quickstart

From create_midi_file.py
$ python3 -m pip install mido
import mido
msg = mido.Message('note_on', note=60)
msg.bytes()

Mido examples

From open-source projects on GitHub

Analyzes music with tools for computational musicology.

Downloads / mo
950.0K
Stars
2.6K
Last release
10.5.2
License
BSD

Quickstart

From moduleConverter.html
$ python3 -m pip install music21
from music21 import converter
score = converter.parse('example.mid', format='midi')

music21 examples

From open-source projects on GitHub

Represents MIDI notes and instruments with second-based timing for analysis, editing, and synthesis.

Downloads / mo
692.7K
Stars
1.0K
Last release
0.2.11.post0
License
MIT

Strengths

  • Uses seconds for note timing. Edit note start and end times directly in seconds.
  • Synthesizes sine-wave previews. Generate a sine-wave audio array with synthesize.

Limitations

  • Instrument synthesis needs external packages. Install FluidSynth and pyfluidsynth for instrument-based synthesis.
  • Sine synthesis skips drum sounds. Instrument.synthesize returns silence for drum instruments.

Quickstart

From README.md
$ python3 -m pip install pretty_midi
import pretty_midi
midi_data = pretty_midi.PrettyMIDI('example.mid')
print(midi_data.estimate_tempo())

pretty_midi examples

From open-source projects on GitHub

Parses MIDI files through a high-performance interface.

Downloads / mo
461.4K
Stars
190
Last release
0.6.0
License
MIT

Quickstart

From quickstart.html
$ python3 -m pip install symusic
from symusic import Score
score = Score("example.mid")         # format auto-detected

Symusic examples

From open-source projects on GitHub

Binds the RtMidi C++ library to Python with Cython.

Downloads / mo
203.0K
Stars
417
Last release
1.5.8
License
MIT

Quickstart

From README.md
$ python3 -m pip install python-rtmidi
import time
import rtmidi
midiout = rtmidi.MidiOut()
available_ports = midiout.get_ports()
if available_ports:
    midiout.open_port(0)
else:
    midiout.open_virtual_port("My virtual output")
with midiout:
    note_on = [0x90, 60, 112] # channel 1, middle C, velocity 112
    note_off = [0x80, 60, 0]
    midiout.send_message(note_on)
    time.sleep(0.5)
    midiout.send_message(note_off)
    time.sleep(0.1)
del midiout

python-rtmidi examples

From open-source projects on GitHub

Creates multitrack MIDI files from note, tempo, controller, and other event definitions.

Downloads / mo
41.3K
Stars
273
Last release
1.2.1
License
MIT

Strengths

  • Writes multitrack MIDI files directly. Build tracks by adding note and tempo events before writing the file.
  • Adds controller and system events. Write controller, SysEx, and registered-parameter events alongside notes.

Limitations

  • Ticks require an explicit mode. Set eventtime_is_ticks when passing native MIDI tick values.
  • Tempo uses the dedicated track. Format-one files ignore the requested track for tempo events.

Quickstart

From c-major-scale.py
$ python3 -m pip install MIDIUtil
from midiutil import MIDIFile
degrees  = [60, 62, 64, 65, 67, 69, 71, 72]  # MIDI note number
track    = 0
channel  = 0
time     = 0    # In beats
duration = 1    # In beats
tempo    = 60   # In BPM
volume   = 100  # 0-127, as per the MIDI standard
MyMIDI = MIDIFile(1)  # One track
MyMIDI.addTempo(track, time, tempo)
for i, pitch in enumerate(degrees):
    MyMIDI.addNote(track, channel, pitch, time + i, duration, volume)
with open("major-scale.mid", "wb") as output_file:
    MyMIDI.writeFile(output_file)

MIDIUtil examples

From open-source projects on GitHub

Edits MIDI instruments and events in native ticks and converts notes to piano-roll grids.

Downloads / mo
23.6K
Stars
285
Last release
1.0.1
License
MIT

Strengths

  • Edits notes in native ticks. Work with integer MIDI timing instead of second-based note positions.
  • Produces piano-roll note grids. Convert notes to time-and-pitch grids for analysis or visualization.

Limitations

  • Note timing stays in ticks. Convert ticks before comparing note positions with audio timestamps.

Quickstart

From README.md
$ python3 -m pip install miditoolkit
from miditoolkit import MidiFile
from miditoolkit.midi.utils import example_midi_file
path_midi = example_midi_file()
midi_obj = MidiFile(path_midi)
print(midi_obj)

Miditoolkit examples

From open-source projects on GitHub

FAQ

Mido suits most work with MIDI messages, files, and device ports. pretty_midi suits analysis of notes and instruments with times in seconds. music21 suits music analysis that treats MIDI as a score.

Mido works with MIDI messages, MIDI files, and real-time ports through selectable backends.

Mido reads a MIDI file as tracks of messages with timing in ticks. pretty_midi reads the same file as instruments and notes with times in seconds.

MIDIUtil creates multi-track MIDI files from note and tempo events in pure Python. It writes files only and does not read them or connect to live ports.

For coding agents

Found your MIDI library? Help your agent use it.

Connect Exa so your coding agent can find the library’s documentation, check its API, and look up implementations while you build.

Run in your terminal

claude plugin install exa@claude-plugins-official