(Version 2022_01_03)
This basics tutorial introduces
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).
# You can of course also calculate with the commands.
# Change, for example, the following calculation command:
21 * 7 + 1
# Then please, press 'Run'!
148
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:
from music21 import *
# with this command the music21 library is loaded:
# So click in the cell and then on the button 'Run'!
# This command must ALWAYS be executed,
# when working with a notebook.
# Afterwards all music21 commands and all pieces of the music21 corpus are available.
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.
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'.
# configure.run()
# settings = environment.UserSettings()
# settings['musescoreDirectPNGPath'] = settings['musicxmlPath']
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'):
# def run():
PathSelect = str(input("Specify a folder where newly created notes and graphics will be saved. Then confirm with Enter."))
settings = music21.environment.UserSettings()
# run()
File "C:\Users\Martin\AppData\Local\Temp/ipykernel_27360/2549806241.py", line 2 PathSelect = str(input("Specify a folder where newly created notes and graphics will be saved. Then confirm with Enter.")) ^ IndentationError: unexpected indent
# ATTENTION: Please enter the desired paths on your computer first.
# then remove the hash signs and finally press the 'Run' button:
# a. First of all, here are the commands for integrating your score editor
# (['musescoreDirectPNGPath'] and ['musicxmlPath']):
#settings = environment.UserSettings()
# settings['musescoreDirectPNGPath']='C:/Program Files/MuseScore 3/bin/MuseScore3.exe'
# settings['musicxmlPath']='C:/Program Files/MuseScore 3/bin/MuseScore3.exe'
# Please enter the path on your PC to the file 'MuseScore3.exe'.
# (it is located in the bin folder of the program folder, ex. see below);
# Alternatively, you can also activate another score editor here (e.g. Sibelius).
# b. You also need to specify a folder,
# where newly created notes and graphics will be saved:
# settings['directoryScratch']='C:/users/Name/Analyse'
# (Again, please add the desired path, remove hash marks, Run!)
# c. Finally, the command for permission to download files from the Internet:
#settings['autoDownload']='allow'
# ... and the integration of the preferred note format:
#settings['showFormat']='musicxml'
#settings['writeFormat']='musicxml'
# ATTENTION: Please enter the desired paths on your computer first.
# then remove the hash marks and finally press the 'Run' button:
# a. First, here are the commands for integrating your score editor:
# Please enter the path on your PC to the file 'MuseScore3.exe'.
# (it is located in the bin folder of the program folder, example see below);
# Alternatively, you can activate another score editor here (e.g. Sibelius).
# Then remove the hash marks and finally press the 'Run' button:
# settings = environment.UserSettings()
# environment.set('musescoreDirectPNGPath', 'C:/Program Files/MuseScore 3/bin/MuseScore3.exe')
# environment.set('musicxmlPath', 'C:/Program Files/MuseScore 3/bin/MuseScore3.exe')
# b. Please enter the path in the following command,
# in which the graphic and note files are to be saved.
# environment.set('directoryScratch', 'C:/users/Name/Analyse')
# c. Finally, the command for permission to download files from the Internet:
environment.set('autoDownload','allow')
# The commands allow you to access the settings file
# and show you the path where the settings file is located.
# (please remove the '#' in front of the command to activate it, then 'Run')
settings = environment.UserSettings()
settings.getSettingsPath()
# If a settings file has not yet been generated during the installation,
# you can generate it by the following command:
# settings.create()
# Then run it again:
# settings = environment.UserSettings()
# settings.getSettingsPath()
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.
The following music21 commands are also described on the website https://analyse.hfm-weimar.de/doku.php?id=music21.
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.
music = converter.parse('https://analyse.hfm-weimar.de/database/03/MoWo_K80_COM_1-4_StringQuar_003_00838.xml')
# 'music' is chosen as the name (variable) for the loaded ('parsed') note file
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:
# music = converter.parse('https://... local path... / ... /MoWo_K155_COM_1-3_StringQuar_003_00842.xml')
# Please enter your local file path (e.g. C:/analyse/MoWo_K155_COM_1-3_StringQuar_003_00842.xml)
# the path may only contain symbols (/), i.e. no (\)!
# remove the hash (#) before the first line (local = ...) to activate the command.
# Please press - as always - "Run" afterwards.
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.
music.show('midi')
# If you don't like the MIDI sounds and you want to set other sounds, you have to make these settings in the xml file.
# you can use the command whenever you want to listen to something;
# maybe you have to change / adjust the variable 'music'.
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').
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:
music.metadata.all()
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.
music.show()
# shows the beginning of the note text.
# This may take a few seconds...
music.measures(3,5).show()
# Measure three to 5 is selected.
len(music.parts)
# The number of voices (parts) is queried.
music.parts[0].measures(3,5).show()
# The first voice (parts[0]) is selected and shown here from bar 3-5.
# IMPORTANT: The counting of the parts does not start with 1, but with 0!
music.parts[0].write()
# The write command will write a new xml file,
# which will be saved in your "Scratch" folder (cf. above: settings['directoryScratch']),
# where you will be given the file name.
# Alternatively, you can specify a path and file name of your own choice:
# music.parts[0].write(fp="D:/Dokumente/mozart_violine1.xml")
# Delete the # in front of the command,
# specify the local file name and path and press Run!
# Here is an alternative method: You define a new variable ('excerpt')
# as the top voice (parts[0]) and from it the measures 2-4 are selected.
excerpt = music.parts[0].measures(2,4)
excerpt.show()
# The section 'excerpt' is displayed.
excerpt.write()
# The section 'excerpt' will be saved locally in the scratch folder.
# Alternatively you can define your own path and name, see above.
# and finally: listen!
excerpt.show('midi')
For usage in other computer applications (Word, Powerpoint) the graphics (as png-file) can of course be exported:
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?