Playing with Toolkits (Music21)

Today's learning objectives:

Today we will look at the music21 toolkit (Cuthbert and Ariza, 2010).

music21 (named after the music building number at MIT) is a Python toolkit for dealing with notated music. It reads kern files, as we've been doing, and it adds a lot of other features, such as an easy notation output.

Personally, I don't use it too much, but it's still a very good toolkit that's worth knowing a bit about.

A Problem: Creating a Test Bank for Carmen Quizzes

I need to create a quiz bank to upload to carmen. In order to bulk upload things to Carmen, they need to be in a certain format. It looks like this. Basically I need the document to:

Carmen actually randomizes the order of answers, so I don't need to worry about that!

Let's use music21 for this!

music21 and notation

When I've previously done this task, I used humdrum and a tool called Verovio. I think Verovio looks really ncie, but it has some compatibility issues with python and it only outputs "svg" files, and Carmen needs png. It added an extra conversion step that wasn't really necessary. I can do it all in one go with music21.

You can use the show function in music21 to return the notation of an object.

The Converter

The converter.parse function can read a number of types of notation. There's the (very) limited tinynotation format, which can do simple monophonic things.

It can also read kern files like so:

{python}
for file in file_list:
  melody = converter.parse(file)
  melody.show()

but we will return to this in a minute.

Looping with music21

The code below can take the melody we've written above and transpose it to many keys.

Let's talk through the pseudocode first:

{python}
for each key signature:
  analyze the key of that little melody, and assign it a variable (k)
  use the interval function to show the distance from the tonic and the pitches, and assign it a new variable (i)
  transpose the melody by that interval.
  show it in notation.

Much of these are from the "key" class in music21. Check out the documentation here.

Reading in files.

What about reading in kern files?

First, because we are using Colab, we need to do the same thing we did before, and tell google that it can get to our drive.

Now we can run that filebrowswer function again. This will grab all polska files.

And now we can run this loop to show all polska files.

There are a couple of important things here:

Basically an f-string allows you to put variables into things, but also print other things around them. Here, I wanted to add quotation marks around a file variable. Adding the f means that I can put things around that variable, and it will add not just the variable, but whatever other symbols come after the f. The curly brackets are also necessary.

Back to the Exercise.

Remember, I need to:

Let's start by grabbing all of the files.

psssst: Have you noticed that we're using this code a lot? We should probably consider making it into a function?

Printing a question

Let's see how we might use f-strings to print a single question.

Transposing

We need this to happen in many keys, and for each chord. Perhaps a transposition function would be useful here.

For this, we need to loop over a bunch of keys, and we also need different correct answers depending on what the file is.

A function would be good for this. We could put everything in a single function, and then simply loop over the every file with that function.

Homework for Monday:

  1. Print off (here in Colab) all of Bach's 371 chorales, each in 3 keys.
  2. You can choose whichever keys you would like.

This is the directory of all chorales:

"content/MyDrive/python_scratch/bach_chorales/*.krn"