Mike Slinn

Music21

Published 2023-04-09. Last modified 2024-07-23.
Time to read: 3 minutes.

This page is part of the av_studio collection.

Music21 is a Python library, published by MIT, for computer-aided musicology. It can analyze single pieces and entire corpuses, transpose, translate between formats, and more.

Music21 is an object-oriented toolkit for analyzing, searching, and transforming music in symbolic (score-based) forms.

You can read a 11-year-old review.

The following YouTube video is an audio interview with the author:

The following YouTube video is a gentle introduction to working with music21:

Yet another video introduction:

JavaScript Version

A JavaScript version of music21 also exists, called music21j. Music21j runs in a web browser.

Although music21j does not have all the features of Music21 Python, music21j has the following additional features:

  • Can visualize and hear changes in Streams quickly (using Vexflow and MIDI.js)
  • Supports MIDI devices, including JazzSoft
  • A repository of modules such as metronomes, keyboards, and automatic transcribers.

The GitHub page has 143 stars and 41 forks. Unfortunately, this project only have one developer. Although pull requests have been handled in a timely manner, the last closed issue was a year ago. The documentation is also a bit thin.

OS Support Varies

Music21 requires MuseScore and/or Lilypond for conversions between musical formats. While music21 runs on many Python implementations, different versions of MuseScore produce very different results on Ubuntu, Windows and WSL.

MuseScore3

Installing MuseScore3 on native Ubuntu is simple, and the WSL experience is identical. Unfortunately, MuseScore3 is built on QT5, which does not handle 4K monitors well under some circumstances.

I found that MuseScore3 ran just fine on native Ubuntu, but on WSL, it was unusable with a 4K monitor, even after upgrading to Microsoft’s improved WSL graphics support for Windows.

MuseScore4

MuseScore4 is built on QT6, and it works well on native Windows, and probably works well on macOS also.

Although a few brave people have installed MuseScore4 on Linux, there is no prebuilt installation package for Debian/Ubuntu yet. MuseScore4 is reportedly difficult to build on Ubuntu, and incomplete on that platform. That means no WSL support, at least for now.

Native Windows

The following steps are all that are required to install and configure music21 for native Windows.

  1. Install MuseScore4.
  2. Install Python 3.11 from the Microsoft Store.
  3. Install music21 (see below).
  4. Configure music21 (see below).

Following is how I installed and configured music21 on native Windows. The RuntimeWarning does not appear to be significant.

Command Shell or PowerShell
Microsoft Windows [Version 10.0.19045.2728]
  (c) Microsoft Corporation. All rights reserved.
C:\Users\mslinn> pip3 install --upgrade music21 C:\Users\mslinn> python3 -m music21.configure /usr/lib/python3.10/runpy.py:126: RuntimeWarning: 'music21.configure' found in sys.modules after import of package 'music21', but prior to execution of 'music21.configure'; this may result in unpredictable behaviour warn(RuntimeWarning(msg))
______________________________________________________________________________
Welcome to the music21 Configuration Assistant. You will be guided through a number of questions to install and set up music21. Simply pressing return at a prompt will select a default, if available.
You may run this configuration again at a later time by running music21/configure.py.
______________________________________________________________________________
The BSD-licensed music21 software is distributed with a corpus of encoded compositions which are distributed with the permission of the encoders (and, where needed, the composers or arrangers) and where permitted under United States copyright law. Some encodings included in the corpus may not be used for commercial uses or have other restrictions: please see the licenses embedded in individual compositions or directories for more details.
In addition to the corpus distributed with music21, other pieces are not included in this distribution, but are indexed as links to other web sites where they can be downloaded (the "virtual corpus"). If you would like, music21 can help your computer automatically resolve these links and bring them to your hard drive for analysis. To the best of our knowledge, the music (if not the encodings) in the corpus are either out of copyright in the United States and/or are licensed for non- commercial use. These works, along with any works linked to in the virtual corpus, may or may not be free in your jurisdiction. If you believe this message to be in error regarding one or more works please contact Michael Cuthbert at cuthbert@mit.edu.
Would you like to: [1] Acknowledge these terms and allow music21 to aid in finding pieces in the corpus [2] Acknowledge these terms and block the virtual corpus [3] Do not agree to these terms and will not use music21 (agreeing to the terms of the corpus is mandatory for using the system).
Choose a number from the preceding options (default is 1):
1
Auto Download set to: allow
______________________________________________________________________________
Would you like to send a pre-formatted email to music21 regarding your installation? Installation reports help us make music21 work better for you: Enter Yes or No (default is Yes):
n
______________________________________________________________________________
The music21 discussion group provides a forum for asking questions and getting help. Would you like to see the music21 discussion list or sign up for updates? Enter Yes or No (default is Yes):
n
______________________________________________________________________________
Would you like to view the music21 documentation in a web browser? Enter Yes or No (default is Yes):
n
______________________________________________________________________________
The music21 Configuration Assistant is complete. Press return to continue.

Music21 immediately worked with MuseScore4.

Each time a music21 program executes a show command, music21 quickly launches MuseScore4, and displays rendered scores nicely. Then music21 hangs until MuseScore4 is closed. This means that MuseScore4 must be opened and closed many times while you work on a piece. Not the best user experience. Perhaps using music21 streaming options might help - I have not tried that yet.

Fail: Ubuntu / WSL Installation

This did not work well, and I do not recommend it.

I followed this procedure on WSL2 and native Ubuntu. First I setup and activated a Python virtual environment:

Shell
$ python3 -m venv ~/venv/music21

$ source ~/venv/music21/bin/activate
(music21) $ 

Next I installed the muscic21 library in the music21 virtual environment, and I configured it:

Shell
$ pip3 install --upgrade music21

$ python3 -m music21.configure
/usr/lib/python3.10/runpy.py:126: RuntimeWarning: 'music21.configure' found in sys.modules after import of package 'music21', but prior to execution of 'music21.configure'; this may result in unpredictable behaviour
  warn(RuntimeWarning(msg))
______________________________________________________________________________
Welcome to the music21 Configuration Assistant. You will be guided through a number of questions to install and set up music21. Simply pressing return at a prompt will select a default, if available.
You may run this configuration again at a later time by running music21/configure.py.
______________________________________________________________________________
The BSD-licensed music21 software is distributed with a corpus of encoded compositions which are distributed with the permission of the encoders (and, where needed, the composers or arrangers) and where permitted under United States copyright law. Some encodings included in the corpus may not be used for commercial uses or have other restrictions: please see the licenses embedded in individual compositions or directories for more details.
In addition to the corpus distributed with music21, other pieces are not included in this distribution, but are indexed as links to other web sites where they can be downloaded (the "virtual corpus"). If you would like, music21 can help your computer automatically resolve these links and bring them to your hard drive for analysis. To the best of our knowledge, the music (if not the encodings) in the corpus are either out of copyright in the United States and/or are licensed for non- commercial use. These works, along with any works linked to in the virtual corpus, may or may not be free in your jurisdiction. If you believe this message to be in error regarding one or more works please contact Michael Cuthbert at cuthbert@mit.edu.
Would you like to: [1] Acknowledge these terms and allow music21 to aid in finding pieces in the corpus [2] Acknowledge these terms and block the virtual corpus [3] Do not agree to these terms and will not use music21 (agreeing to the terms of the corpus is mandatory for using the system).
Choose a number from the preceding options (default is 1): 1
Auto Download set to: allow
______________________________________________________________________________
Would you like to send a pre-formatted email to music21 regarding your installation? Installation reports help us make music21 work better for you: Enter Yes or No (default is Yes): n
______________________________________________________________________________
The music21 discussion group provides a forum for asking questions and getting help. Would you like to see the music21 discussion list or sign up for updates? Enter Yes or No (default is Yes): n
______________________________________________________________________________
Would you like to view the music21 documentation in a web browser? Enter Yes or No (default is Yes): n
______________________________________________________________________________
The music21 Configuration Assistant is complete. Press return to continue.

I also installed MuseScore3 so music21 could create PDFs.

Shell
$ yes | sudo apt install musescore3

On Ubuntu / WSL, music21 stores configuration information in ~/.music21rc

.music21rc
<settings encoding="utf-8">
  <preference name="autoDownload" value="allow" />
  <preference name="braillePath" />
  <preference name="debug" value="0" />
  <preference name="directoryScratch" />
  <preference name="graphicsPath" value="/usr/bin/xdg-open" />
  <preference name="ipythonShowFormat" value="ipython.musicxml.png" />
  <preference name="lilypondBackend" value="ps" />
  <preference name="lilypondFormat" value="pdf" />
  <preference name="lilypondPath" value="/usr/bin/lilypond" />
  <preference name="lilypondVersion" />
  <localCorporaSettings />
  <localCorpusSettings />
  <preference name="manualCoreCorpusPath" />
  <preference name="midiPath" />
  <preference name="musescoreDirectPNGPath" value="/usr/bin/mscore3" />
  <preference name="musicxmlPath" value="/usr/bin/mscore3" />
  <preference name="pdfPath" value="/usr/bin/xdg-open" />
  <preference name="showFormat" value="musicxml" />
  <preference name="vectorPath" />
  <preference name="warnings" value="1" />
  <preference name="writeFormat" value="musicxml" />
  </settings>

On WSL2, I found that MuseScore3 took a really long time to load, and the window was much too large on my 4K monitor. After fussing with it for a while, I decided it was not worth the trouble.

Built With Music21

Applications can be built using the music21 API. For example, the MusicXML-to-MIDI-Converter preserves multiple parts/voices in the output MIDI file. If you drag the output MIDI file into Ableton Live, each voice will appear as a separate clip.

* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.