liveBook · Manning

Music21

As explained in Learn Generative AI with PyTorch

A Python library for analyzing, manipulating, and displaying symbolic music data such as notes and piano rolls.

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

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains installation, MIDI loading, visualization, note inspection, and encodings, but stops short of library internals.
Examples
Uses example.midi, JSB Chorales, piano-roll plots, note listings, and a four-track (4, 2, 16, 84) representation.
What it assumes
Assumes basic music concepts and Python notebook use while introducing MIDI, pitches, offsets, tracks, bars, and piano rolls.
What you can do after
Apply it via Python to load MIDI, inspect notes, render notation, plot piano rolls, and prepare music-generation inputs.
How it connects
Connects MIDI, piano rolls, staff notation, MuseScore, multitrack music, and music Transformers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.