The Patchbay_

music21 Cheat Sheet

MIT's music21 in brief — building notes and streams, loading scores, and running the analysis it's famous for. Note that .flat is gone: current versions use .flatten().

Notes & chords

from music21 import note, chord, streamThe usual imports
n = note.Note('F5')A note from a name (also note.Note(65) from MIDI)
n.name, n.octave, n.pitch.midiRead pitch class, octave, MIDI number
n.quarterLength = 2.0Duration, in quarter notes
r = note.Rest()A rest
c = chord.Chord(['C4', 'E4', 'G4'])A chord from note names

Streams

s = stream.Stream()A generic container
s.append(n)Append, advancing the offset
s.insert(0.0, n)Insert at an explicit offset
p = stream.Part(); m = stream.Measure()Parts and measures for real scores
s.flatten()Flatten nested streams (replaces the removed .flat)
s.show(); s.show('midi'); s.show('text')Render as notation, play, or dump as text

Loading & saving

from music21 import converter, corpusImport the loaders
sc = converter.parse('piece.xml')Parse MusicXML, MIDI, ABC, Humdrum and more
sc = converter.parse('tinyNotation: 4/4 c4 d e f')Quick input via TinyNotation
bach = corpus.parse('bach/bwv66.6')Load from the bundled corpus
sc.write('midi', fp='out.mid')Write out MIDI or MusicXML

Analysis

sc.analyze('key')Estimate the key of a score
sc.analyze('ambitus')The range from lowest to highest note
c.commonName; c.isMajorTriad()Identify a chord
sc.chordify()Collapse all parts into block chords
for n in sc.recurse().notes: ...Walk every note in the hierarchy

Transforming

sc.transpose('P5')Transpose by an interval string
from music21 import interval
interval.Interval(note.Note('C4'), note.Note('G4'))
Measure the interval between two notes
from music21 import key
key.Key('G')
Build a key object

music21 — Python toolkit for symbolic music analysis. · music21 in the tool directory →

← All cheat sheets