Automatically generating "tonal" canons with music21 and scamp

Hello all,

Some years ago I created a simple program to generate “tonal” canons using the music21 library.

With more recent versions of music21 I hit some problems to generate notation, but by using music21 for the “music calculations” and then using scamp to realize some notation/performance these were easily solved.

more info about how it works can be found in this old blog article that describes the system: A touch of music: Algorithmic composition: generating tonal canons with Python and music21

An example of such a canon (it’s a simple system :wink: ):

1 Like

Hi there! If it isn’t too much to ask, are you willing to help me in making one of those please? I read your post “a touch of music” on your blog and I’m really amazed by your algorithmic music. I have been fascinated with palindromes, inversions and such since as long as I can remember, but when I saw something like a table or crab canon, it just seemed like there was no way around it. You used a computer code to create it but that’s still really great to me and even more fascinating in a sense you can generate some with it.

I already found good enough palindromic chord progressions that I made some palindromes with. For now I’m just really clumsy with code. I didn’t install lilypond or scamp yet, but I was kinda confused with your crab canon for example, because you speak of chords but then it appears as if there are numbers that correspond to notes and you reverse two parts one after the other instead of just making one part that is longer. even more with your table canon where there’s no progression either and it seems as if you wrote each part individually which would be no different than directly creating it on a sheet? :confused:

Actually, even back when you created the canon generator in music21, I tried it, installed music21 and all but somehow it didn’t work. I’m curious on what canons with far pushed parameters like unusual time signatures and chord progressions like locrian ones would sound like since there’s barely any way to compose them normally. That’s why I’d really like your help. I’d have wrote this elsewhere but you have no mail and there’s no way to directly send messages to someone here from what I know.

Hi there,

I can try to help you but keep in mind this was made a long time ago, so maybe some changes/revisions will be needed to work with modern versions of the libraries.

To understand how the program works, you really need to have a high-level view of how the method works. Roughly, I came up with a way to make canons sound “good by construction”. This is a different method from how you would normally write a canon in sheet music.

In sheet music, you normally write the canon measure by measure. Every time you add a measure in one voice, new measures are automatically added to the other voices (it’s a canon after all, so what the first voice does fully determines what the other voices do) and you need to check all the time if the different voices keep sounding good together (and if not, change the notes you’re trying to add).

In the algorithmic method, I work the other way around: I start from a chord progression or chorale that I already know sounds good, then rip it apart into canon melody lines that, once they play together, will form the chord progression I started from.

Suppose I want to make a canon with four voices. I then first write (or generate) a chord progression - these are the chords you will hear once all the voices play together. Then I rip apart each of the chords to end up with 4 melodies (e.g. all the chord’s upper notes forms one melody, all the upper middle notes form a second melody, all the lower middle notes form a third melody and all the lowest notes form a fourth melody). By putting these 4 melodies after each other, and playing them four times simultaneously with a suitable delay (and optionally an octave transposition), I get the resulting canon. When all voices are playing together I hear the chord progression I started with.

It’s kind of abstract to explain in just words but once you “get” it, it’s an extremely simple method really :smiley:

Anyway, with that knowledge, perhaps the code will make more sense. In canon-generator/canon-gen.py at master · shimpe/canon-generator · GitHub you can see that I start from a chord progression (line 358), rip them apart to melodies which are concatenated into one long melody (lines 386-401), then I apply some transformations to these melodies to spice them up (inserting extra notes, modifying rhythms) (lines 404-408) and finally sequence multiple copies of these unraveled chords together, applying some octaviations to add more variation, into a canon (line 425).

Does this help or did I make things worse? :slight_smile:

Oh, no, thanks a lot, this is very helping. The thing is I already read it from the post. It’s the table canon and crab canon I don’t get because as I wrote, there are no chords indicated and it appears as if there are numbers that correspond to notes and for the crab canon you reverse two parts one after the other instead of just making one part that is longer and with your table canon where there’s no progression either and it seems as if you wrote each part individually which would be no different than directly creating it on a sheet which got me confused.

As you guessed, there were problems with the library. I’ll just try to redownload your code and the library, insert a progression and tell you how it goes.

Oh, seems like I also have to install scamp. Where to already?

When I try to run it, it says there’s no module named music21 but I just installed it and even did python3 -m music21.configure to put it to musescore. Any idea what’s happening?

For the crab canon, did you see this? crab_canon_book.pdf - Google Drive

For the table canon, did you see this? table_book.pdf - Google Drive

It is correct though that I didn’t write software to automate the steps for crab and table canons. However, the method is a pretty straightforward construction that should be automatable in a way quite similar to the basic canon program that was demonstrated in the code I linked to.

It’s been a while since I’ve used music21 to be honest - I spend most of my free time in supercollider nowadays. How did you install it? I don’t recall having problems with it.

Yes, I saw them.

About music21, I just went on the site, copy pasted and ran the command in the cmd

Are you able to change the code to generate a table or/and a crab canon? Is there any other way I can contact you like mail or discord? By the way, I don’t find why music21 doesn’t work so I still didn’t make a canon :sweat_smile:

Hi, maybe you can get some music21 help on the music21 mailing list?

While I can technically modify the code to generate more sophisticated canons, I’m afraid it will not be possible for me to spend time on actually doing it. In the short term I have concerts upcoming (in fact: tomorrow is one) and generally am working on very different things nowadays. I suspect this is not the answer you were hoping for, but I hope you understand.

I understand! I can surely wait some time before getting a table canon maker. I’m sure they’ll be able to find what’s wrong.

I just don’t manage to properly use music21. The import really appears to work fine, but once in pyscripter, it says “no module found” :smiling_face_with_tear:. I’ve been asking for help on the music21 forum but it wasn’t of any help yet.

I’ve downloaded the canon generator the 29/11/2020 but still didn’t manage to make it work since then… Python is not something I seem to be good at yet.
I’m seriously considering just giving you chords and asking you to give me the mscz file at this point… :sob:.

Maybe you can give me something where I can contact you beside here too if you got any idea of what to do?

If you can do the import if you start python on the command line, then it should be possible to also run the canon generator from the command line instead of inside pyscripter?

It could be that e.g. pyscripter by default is set up to use a different python executable than the one you use on the command line (another version of python perhaps, for which music21 is not installed), but such things are not really easy to diagnose “by email”.

Thanks, that’s a great idea! Where will the canon be generated though? For the email thing, I can send videos on discord or by email.

Something really strange happened when I copy pasted the code in the terminal. Seems like it has some problem with the #. Should I just remove all passages with it in the code?

Actually it seems like it tries to launch each lines individually by itself as soon as I copy paste it. And it can’t do it for many of them, not just the #.

you shouldn’t copy paste anything into the terminal

if you have the code of the canon generator in a file c:\fullpath\to\canon_generator.py (change the file path as needed for your system), then on the command prompt, run python with the name of the python code file as argument, e.g.

python c:\fullpath\to\canon_generator.py


I can’t believe it.
My jaw’s dropping.
Music21 doesn’t have the cream of the cream of the scales??
They have quarter tones but not the enigmatic scale???


I tried with C D Em F G Am B in major scale and got all those errors.

I installed the two other modules, python rtmidi and pynput but it gives me this? What else could there be that doesn’t work?

I suspect something has changed in music21 itself since I made it, so the code would have to be revised and updated for the new music21 version.

Will you do that soon? I’d like so much to test your canon generator!!
Also, what is path to musescore? Where I should put the path to where I want the created file to end up? What happens if I put nothing?