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.getBachChorales():
work = converter.parse(workName)
firstTS = work.flat.getTimeSignatures()
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:
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.flat.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.
While everyone says that in Python you can import a module inside a function without it going through the overhead of actually reimporting, there is some real overhead still, especially if the function is called a lot of times:
In music21 you can easily plot the position of notes as a piano roll:
which preserves pitch names, measure numbers, etc. But the case we're asking for requires a plot more like this:
The numbers at the left are midi numbers while the bottom is number of quarter notes from the beginning. Here's some code to help you achieve this:
With this sort of graph it's easy to isolate each voice (not much overlap of voices in this chorale) and to see the preponderance of similar motion among the Soprano, Alto, and Tenor, but lack of coordination with the Bass (which would create forbidden parallels if it coordinated). More sophisticated examples with better labels are easily created by those with knowledge of matplotlib, but this simple demonstration will suffice to get things started.
In the three months since v. 1.6 some good changes and improvements have been introduced. We focused primarily on stabilizing features that were already in music21 in some form but were too experimental to advertise widely.
A noCorpus version of music21 has also been released, the first since v.1.0. This version can be used in pure Free/Libre projects since files that were licensed for music21 only or non-commercial use have been removed. If you are not sure which version to download, definitely get the full version. But maintainers of Debian Linux and others can update to 1.7 noCorpus.
Music21 v 1.7 (actually 1.7.1) will be the last version to support Python 2.6. Python 2.7 is over three years old and is supported by other flavors of Python including Jython (which skipped 2.6), PyPy, IronPython and is an easy upgrade for Python on Windows. Mac users have had 2.7 since Mountain Lion and we're happy to report that with Mavericks being free and supporting systems that can run Snow Leopard, we're happy to be able to use this opportunity to take advantage of the latest features and start a roadmap to supporting Python 3.3 as well. This is also the last release to use SVN. We are moving to GitHub. Updates soon.
The most important improvement for users is a much improved system of metadata searching (thanks to Josiah Oberholtzer). See:
for more details. LocalCorpus objects are elevated to equal status as the Core corpus so you can now build searchable indexes on any data you have and find the file you want much faster. Try corpus.search('haydn') and read the docs above to see what's possible.
Among the other 150+ changes since 1.6 include:
- Chord.inversion(2) will take a root position chord and put it in second inversion. (this is a change of behavior from before, where .inversion(2) would specify that the chord was in second inversion and override default inversion reporting (for things like Jazz 6 chords). To get the old behavior, use .inversion(2, transposeOnSet=False)
- Fixes for abc parsing (N.B. the next version will rename the "abc" module to "abcNotation" to avoid the occasional name clash with the python AbstractBaseClass (abc) module).
- Stream.getElementsByOffset(4.0) can now find a zero-length object at 4.0 -- bug fix.
- Many modules are now packages (Stream, for instance). This should not affect your code. Existing packages with X/base.py can now find their files in X/__init__.py. Again, this should not affect your code.
- Page break support in Lilypond.
- MIDI translate works better with instruments (thanks to Christopher Antilla)
- Improvements to Braille Music Code output (thanks to Mario Lang; more to come)
- Bug fixes in measure copying involving pivot chords and secondary dominants in RomanText
- Roman numerals for VII, VI, viio/vii, vi/vio in minor are made more robust. It6, Ger65, Fr43, are now supported.
- Many many many bug fixes. Thanks to community help!
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.