What is music21?
Music21 is a set of tools for helping scholars and other active listeners answer questions about music quickly and simply. If you’ve ever asked yourself a question like, “I wonder how often Bach does that” or “I wish I knew which band was the first to use these chords in this order,” or “I’ll bet we’d know more about Renaissance counterpoint (or Indian ragas or post-tonal pitch structures or the form of minuets) if I could write a program to automatically write more of them,” then music21 can help you with your work.
How simple is music21 to use?
Extremely. After starting Python and typing "from music21 import *" you can do all of these things with only a single line of music21 code:
Display a short melody in musical notation:
converter.parse("tinynotation: 3/4 c4 d8 f g16 a g f#").show()
Print the twelve-tone matrix for a
tone row (in this case the opening of Schoenberg's Fourth String Quartet):
print (serial.rowToMatrix([2,1,9,10,5,3,4,0,8,7,6,11]) )
Convert a file from Humdrum's **kern data format to MusicXML for editing in Finale or Sibelius:
With five lines of music21 code or less, you can:
Prepare a thematic (incipit) catalog of every Bach chorale that is in 3/4:
for workName in corpus.chorales.Iterator():
work = converter.parse(workName)
firstTS = work.recurse().getElementsByClass('TimeSignature')
if firstTS.ratioString == '6/8':
Google every motet in your database that includes the word ‘exultavit’ in the superius (soprano) part (even if broken up as multiple syllables in the source file) to see how common the motet's text is (assuming you have a bunch of motets in "listOfMotets"):
for motet in listOfMotets:
superius = motet
lyrics = text.assembleLyrics(part)
if 'exultavit' in lyrics:
webbrowser.open('http://www.google.com/search?&q=' + lyrics)
Add the German name (i.e., B♭ = B, B = H, A♯ = Ais) under each note of a Bach chorale and show the new score:
for thisNote in bwv295.recurse().notes:
Of course, you are never limited to just using five lines to do tasks with music21. In the demos folder of the music21 package and in the sample problems page (and throughout the documentation) you’ll find examples of more complicated problems that music21 is well-suited to solving, such as cataloging the rhythms of a piece from most to least-frequently used.
Music21 builds on preexisting frameworks and technologies such as Humdrum, MusicXML, MuseData, MIDI, and Lilypond but music21 uses an object-oriented skeleton that makes it easier to handle complex data. But at the same time music21 tries to keep its code clear and make reusing existing code simple. With music21 once you (or anyone else) has written a program to solve a problem, that program can easily become a module to be adapted or built upon to solve dozens of similar (but not identical) problems.
Interested in learning more?
- Get Started with music21
- Browse the music21 documentation
- Download music21 from GitHub
- Get our latest news and updates at the music21 blog
- Read the Frequently Asked Questions list
- Sign up for the music21list mailing list through Google Groups.
Here are the main changes since alpha 1:
- Better parallel processing system (esp. in terms of docs)
- Pitches are 30% faster to create, notes are 15% faster. You do create notes, don't you? :-)
- Better musicxml support: volume. Improvements to transposition, glissando, barlines
- Corpus: added works by Amy Beach, Schubert (Lindenbaum), fixed missing Bach Chorales (thanks Dr. Schmidt!) and error in Haydn op. 1 no. 1 movement 1(thanks Joshua Ballance)
- Scales, IntervalNetwork: faster and better documented.
- NeoRiemannian analysis greatly improved (thanks Mark Gotham!)
- voiceLeading.VoiceLeadingQuartet improved. compatibility change: improperResolution renamed to isProperResolution and improved. Former title implied that False meant it was proper; now the title reflects the output.
- Instrument objects now have their MusicXML v.3 sound tags attached (thanks Luke P.!)
- Bugs fixed: chords not in voices in measures with voices were not found in some routines. Instrument objects without midiProgram explicitly set get a program on MIDI output. MIDI no longer inserts a rest at the beginning (thanks KKONZ). Chord.normalOrder fixed (thanks ), bugs in Capella parsing. Bugs related to Apple File System High Sierra not sorting files by default.
I neglected to post on this forum the announcement of music21 v.5 alpha 1, so the many great improvements there are listed below, with a new installation link:
- Python 3 only. Yes, I said that but I'm saying it again. This change has made developing much faster and a lot more fun. Also it's made music21 more powerful and faster.
- Chordify moves from O(n^2) to O(n) time -- Chordify on large scores works great now.
- MusicXML roundtrip now preserves much about appearance, style, metadata, etc. -- you can now load a musicxml file into music21 and back into your software and 90% of the time you'll get visually the same result as the original software. Finale roundtrip is especially good!
- Corpora searching is much better and much faster. Metadata is stored in pickle format.
- Feature Extraction runs multicore by default. Together with the average of 10x faster chordify, feature extraction on large datasets on multicore systems is now very strong.
- Many routines that used to return string filepaths now return pathlib.Path objects.
- Almost all deprecated functions are removed.
- Many keyword functions are now keyword only, so no worries about passing in "inPlace" accidentally.
- parsing of Volpiano (Gregorian chant notation) added.
- RehearsalMark is added (and in musicxml also).
- Empty spaces in MusicXML measures are converted to hidden rests, to avoid gapped streams.
- Pitches in chords on musicxml import are always sorted from lowest to highest.
- analysis.transposition -- searches pitch lists for number of distinct transpositions (thanks Mark Gotham)
- Copyright and other metadata is preserved in many formats on import. This is just being a good neighbor.
- Demos and most alpha code has been moved to a new separate repository: https://github.
com/cuthbertLab/music21-demos -- they will be updated much less frequently. This will also make code development faster. Thanks to all who have contributed to music21's development. We'll be able to get more demos into the codebase by not needing to update them at every moment.
pip3 install --upgrade music21==5.0.5a2
Download from https://github.com/cuthbertLab/music21/releases or from the terminal, type:
pip3 install --upgrade music21
(or without the "3" if you are using Python 2)
Version 4 is the last version of music21 that will support Python 2.7. If you run Version 4 on Python 2.7, you will see a warning that it's time to move up to the brilliance that is Python 3.6.
How can I contribute?
Music21 is a rapidly-progressing project, but it is always looking for researchers interested in contributing code, questions, freely-distributable pieces, bug fixes, or documentation. Please contact Michael Scott Cuthbert (cuthbert at mit.edu), Principal Investigator.
The development of music21 has been supported by the School of Humanities, Arts, and Social Sciences at M.I.T., the Music and Theater Arts section, and generous grants from the Seaver Institute and the NEH/Digging-Into-Data Challenge. Further donations to the project are always welcome.