pretty_midi

raw JSON →
0.2.11 verified Mon Apr 27 auth: no python maintenance

Python library for handling MIDI data conveniently. Provides classes for MIDI files, instruments, notes, and pitch bends. Works with time signatures, key signatures, and tempo changes. Current version 0.2.11, with irregular releases (last release 2021).

pip install pretty-midi
error AttributeError: module 'pretty_midi' has no attribute 'midi_processor'
cause The old midi_processor module was removed; mido is now required.
fix
Install mido: pip install mido
error AttributeError: module 'numpy' has no attribute 'int'
cause numpy 1.24+ removed np.int; use np.int32.
fix
Update pretty-midi to 0.2.10 or later, or downgrade numpy.
error TypeError: 'Note' object does not support item assignment
cause Trying to access notes like a list or dict instead of using the 'notes' attribute.
fix
Use instrument.notes[index] to access notes, e.g., instrument.notes[0].
error ValueError: Unknown key signature number -1
cause Key signature numbers must be in range -7 to 7. Check key signatures.
fix
Ensure key signature numbers are between -7 (7 flats) and 7 (7 sharps).
breaking numpy integer aliases removed in numpy 1.24+; use np.int32 directly.
fix Update to pretty-midi >=0.2.10.
gotcha Instrument(...) with default program (0) creates an acoustic grand piano; for drums, set program=0 and is_drum=True.
fix Use Instrument(program=0, is_drum=True) for drum tracks.
gotcha Note start and end times are in seconds, not beats. Use MIDI tick conversion manually.
fix Always use seconds for note times; for beat-based timing, convert using tempo.
deprecated Backend 'pretty_midi.midi_processor' was deprecated and replaced by 'mido'. Old code using 'from pretty_midi.midi_processor import ...' will fail.
fix Install mido and let pretty_midi use it as backend automatically.

Create a simple MIDI file with a piano note (C4).

import pretty_midi
# Create a MIDI file
midi = pretty_midi.PrettyMIDI()
piano = pretty_midi.Instrument(program=0)  # Acoustic Grand Piano
# Add a note (C4, start=0, end=1, velocity=100)
note = pretty_midi.Note(velocity=100, pitch=60, start=0, end=1)
piano.notes.append(note)
midi.instruments.append(piano)
midi.write('example.mid')
print('MIDI file created.')