music21 is a Python library for analyzing, composing, manipulating, and visualizing symbolic music. It connects MIDI data with musical concepts such as tracks, bars, notes, pitches, and piano rolls. section 13.1.2 section 13.1.3
Use music21 to move between playable MIDI files and interpretable musical representations:
- Load a MIDI file into a music21 stream, then display its staff notation through MuseScore. section 13.1.2
- Plot a piano roll where horizontal position represents time, vertical position represents pitch, and block length represents duration. section 13.1.3
- Inspect quantized notes, offsets, pitch names, and MIDI pitch numbers before converting music into model-ready arrays. section 13.1.3
- Prepare MIDI processing for either a multitrack tensor representation or indexed event sequences used by a music Transformer. section 13.1 section 14.2
Inspecting a MIDI piece
A practical workflow opens example.midi, translates it into a stream, and calls stream.show() to render staff notation. Calling stream.plot() instead displays the piano roll; iterating through stream.recurse().notes exposes each note's offset and pitch. These observations help verify the four-track, two-bar structure before encoding it as a (4, 2, 16, 84) object. section 13.1.2 section 13.1.3
