Tutorial Basics Sheet Music: Basics music21

(Version 2022_01_03)

Table of Contents:

  1. Jupyter notebook: basic function
  2. Configuration of local settings of music21
  3. Handling of Jupyter notebooks and music21
  4. Tasks

This basics tutorial introduces

  1. into the structure and functioning of a Jupyter notebook,
  2. how to change the settings of your local music21 command library to be able to load sheet music from the internet and to include a sheet music editor,
  3. how to use Jupyter notebooks and music21 to analyze score data. Following on from this, the tutorials Part 2 and 3 show possibilities of displaying and visualizing sheet music as well as simple statistical queries.

1. Jupyter notebook: basic functions

A Jupyter notebook is a file that can be opened and edited in the browser. Each Jupyter notebook consists of a sequence of text blocks (markdown cells) and blocks with Python colde (code cells). Both blocks can be modified and edited by any user by pressing the Enter key within a block. Own modified versions of the notebook can be saved under a new name (Menu: File -> Save as...).

IMPORTANT: The commands of each code cell are executed with the Run button. During execution (which may take a few seconds for some commands), an hourglass icon is displayed at the top of the browser tab. Afterwards, results (or error messages) are displayed, if any, and the cursor jumps to the next box. In each tutorial, go through code cell by code cell, activate the code with the Run button, and wait to see what happens!

In the code cells all lines behind a hash sign (#) are not executed; this offers the possibility to either note explanations to the code in the code cells or to note additional deactivated commands, which are executed by deleting the hash sign (and then Run).

A new cell is created with the + button or the menu item Insert and can then be defined in the menu as markdown or code cell. Then, you can add your own comments or commands. (And please, don't forget to save the notebook under a new name beforehand).

Parallel to the notebook a command window ("Anaconda Prompt") is running, which you don't have to pay attention to - but please don't close it either! Please close a notebook with the menu command "File" -> "Close and Halt", then log out and close the command window at the end.

Every start of a Jupyter notebook, in which the Python library music21 is used, starts with the import of music21 - by this simple command:

2. Configuration of the local settings of music21

To be able to call up a score file from the Internet and to view and to save it as a score, you must once - and really only the very first time! - allow music21 both to load files from the Internet and to view and save them in your score editor (MuseScore). To do this, you need to set up the settings in music21's 'music21-settings.xml' file.

This is necessary primarily for Windows computers, but also for some Mac operating systems. Other Mac operating systems ask for the settings interactively during execution - which is much easier.

ATTENTION: In the following all commands are disabled by a hash key (#) at the beginning of the line. You have to activate each of them by deleting the hash key, adjust it by entering the file path on your local computer if necessary, and then press 'Run'. Once the settings have been successfully adjusted, deactivate the commands again - so that you don't accidentally trigger them again (for example by using the 'Run All' command)!

There are several possibilities, which are explained and executed in the following code cells. Unfortunately, not all possibilities work smoothly with all operating systems. Therefore four of the possibilities are presented in the following.

  1. configuration assistant: The easiest way is the 'music21 Configuration Assistant', which is opened by the command of the following code cell. The Assistant will then ask you questions or prompt you to enter the path of your score editor, enable automatic downloads, etc.

Please activate the following three command lines by deleting the hash (#) and click on "Run". Afterwards please answer the displayed questions of the 'music21 Configuration Assistant'.

Unfortunately, however, the folder where all graphics and other data are automatically saved cannot be specified with the assistant. You can either specify the location individually for each query, or - what is easier - specify the path with the following command (please delete the two # signs again, then 'Run'):

  1. Automatic configuration of the settings file:
  1. If you have problems with the first two options (which may be the case with some Mac operating systems), please use the following method with the command 'environment.set(...)':
  1. Alternatively, you can use the following commands to edit the settings file with a text editor.

Displayed ('Out[...]') is now the path to a file named 'music21-settings.xml', which you can change in a text editor by entering values in the 'value' field in the individual lines, e.g. 'allow' or a data path on your computer.

3. Operation of Jupyter notebooks and music21

The following music21 commands are also described on the website https://analyse.hfm-weimar.de/doku.php?id=music21.

3.1 Loading a music sheet file

To load a file from the internet, you only have to insert the internet address of the file with the following syntax (red marked URL). The URL can be taken from the sheet music database, e.g. https://analyse.hfm-weimar.de/doku.php?id=en:mowo (mouse right-click on the link: "Copy link address"). We now load the first movement of the String Quartet No. 1, D major, KV 80, by Wolfgang Amadeus Mozart.

You can also save the file on your local computer first (mouse right-click on the link: "Save target as") and then enter the local file path instead of the URL. This way you can also read your own music sheet files from your hard disk:

The 'converter.parse' command of music21 is able to read in a whole range of different note formats - besides MusicXML (.xml) also .krn, .abc, .mei and .mid (Midi) among others. A list of convertible sheet music formats can be found here: https://web.mit.edu/music21/doc/moduleReference/moduleConverter.html.

You can also listen to the piece in the browser - but only in a version with MIDI sounds and if Firefox or Chrome is used as browser.

music21 commands are usually structured according to this syntax: At the beginning there is the variable ('music') that you have defined yourself. Then follows the music21 command ('show') - or a series of commands - and finally an attribute, how the command should be executed (here: shown or played as 'midi').

3.2 Query metadata

All the metadata stored in the MusicXML file about the examined work (composer, work title, year of creation, etc.) can be displayed with the following command:

3.3. Display of music scores

Sheet music data can be displayed in full or in sections, voices, etc. For longer pieces, however, only the beginning is displayed in the browser window. To view the complete file you have to open it in the score editor.

3.4 Choice of voices and measures

3.5 Export of the graphics as image files (pgn)

For usage in other computer applications (Word, Powerpoint) the graphics (as png-file) can of course be exported:

4. Tasks

Load additional sheet music files from the sheet music archive (https://analyse.hfm-weimar.de/doku.php?id=en:komponisten) or from your hard disk. Test the commands presented here on these files.

Think about analytical questions for this: What would you like to know about the compositions?