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 importsn = 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 numbern.quarterLength = 2.0Duration, in quarter notesr = note.Rest()A restc = chord.Chord(['C4', 'E4', 'G4'])A chord from note namesStreams
s = stream.Stream()A generic containers.append(n)Append, advancing the offsets.insert(0.0, n)Insert at an explicit offsetp = stream.Part(); m = stream.Measure()Parts and measures for real scoress.flatten()Flatten nested streams (replaces the removed .flat)s.show(); s.show('midi'); s.show('text')Render as notation, play, or dump as textLoading & saving
from music21 import converter, corpusImport the loaderssc = converter.parse('piece.xml')Parse MusicXML, MIDI, ABC, Humdrum and moresc = converter.parse('tinyNotation: 4/4 c4 d e f')Quick input via TinyNotationbach = corpus.parse('bach/bwv66.6')Load from the bundled corpussc.write('midi', fp='out.mid')Write out MIDI or MusicXMLAnalysis
sc.analyze('key')Estimate the key of a scoresc.analyze('ambitus')The range from lowest to highest notec.commonName; c.isMajorTriad()Identify a chordsc.chordify()Collapse all parts into block chordsfor n in sc.recurse().notes: ...Walk every note in the hierarchyTransforming
sc.transpose('P5')Transpose by an interval stringfrom music21 import interval
interval.Interval(note.Note('C4'), note.Note('G4'))Measure the interval between two notesfrom music21 import key
key.Key('G')Build a key objectmusic21 — Python toolkit for symbolic music analysis. · music21 in the tool directory →