Toronto, Ontario, Canada

Live-coding a permutation-based fugue

2018-01-28

If you've been following North Coast for a long time, you may remember the live video streams I did back in 2016. This week I spent some time getting the software and hardware set up to do that again, which pretty much meant starting over from scratch because enough things have changed that my former config was no longer appropriate. You can read about the technical side over on my personal site. I'm still not sure exactly how I'll be using this capability, but it's something I've wanted for a long time as a way to raise the profile of some of the stuff I do. In the days to come I'll probably plan out some scheduled broadcasts on music, electronics, and other topics.

For now, it's basically whatever I feel like streaming, whenever I feel like streaming it, and I encourage you to keep an eye on my Twitter and Mastodon accounts for announcements of when I'm doing a stream or just visit the server and see if anything's on. I don't plan to save recordings of these streans; what I found last time around was that although people enjoyed watching the recordings after the fact, people watching the recordings didn't really seem to get the idea that they were watching recorded live streams and I had too much feedback predicated on the assumption that the recordings were the main purpose of the exercise (for instance, recommending ways to edit them). But it's certainly possible that at some point I may also do some preplanned, edited, and packaged recordings.

In a live stream earlier today I went through the composition of a fugue based on permutations, which I'll present here in a somewhat more finished form.

A fugue based on permutations

First off, let me make clear that I'm not writing what is officially called a permutation fugue. So if you know what one of those is, try to forget about it. Instead, I'm going off in a different direction, doing something of my own which I think is interesting. It partakes a lot of the kind of thing that the "serialists" did, but they often went to some lengths to reject tonality whereas I'm embracing it.

So, first of all, fugues. These are a musical form that was popular in the Baroque era, and the detailed definition evolved a bit over time, but the general idea is that you have a bit of musical material called the subject which gets repeated in different voices and different keys in a pattern. Often there's some counterpoint for the subject which is also repeated, and some other non-repeated material woven into the structure. The overall form may be more or less rigid, with some of the repeated parts possibly varying or being modified in systematic or non-systematic ways.

Fugues appeal to me because of their semi-mechanical nature. A relatively small amount of carefully chosen musical material blows up into a longer piece just by following the rules for how it's supposed to fit into the structure. The bookkeeping of doing transpositions and repetitions is the sort of thing a computer can help with, freeing the human composer to work on the more interesting artistic aspects of the project.

One idea I've been playing with for a long time is that of modifying a melody by permuting some of the notes - in pitch, not in time. This For instance, I might start with a melody and then replace every C with a G and vice versa. For something in the key of C, these two notes have very similar function; in most places where you can play one, the other will sound okay too. So the hope is that after such a transformation, you end up with a melody that still sounds reasonable, and somewhat similar to the original, but is also noticeably different. Observe that some other things people do, such as flipping a melody upside down ("inversion"), can be seen as special cases of this kind of permutation.

For today's piece I picked the following permutation of note names, assuming a melody in the key of C:

This creates two cycles: (D F A) and (E B). You can check for yourself that applying it six times will return a melody to where it started. I chose it on the basis that I didn't want C and G to change (as the root and fifth of the scale, keeping them in place means the music's key will probably still be perceived as the same); B and E feel somewhat similar to each other as the furthest notes of the scale from C on the circle of fifths; and putting the remaining three notes into one cycle means the overall permutation will return to its starting point after six applications, which I hope will lead to the piece of music being about the length I want it to be. My plan is for there to be seven sections to the piece, one with a starting melody repeated in three different voices with some counterpoint; then, applying the permutation once and playing it again in the three voices; then applying the permutation again, and so on, with a final seventh section that just repeats the first. Sticking to just the seven notes of the diatonic scale in the melody and permuting them in this pattern ought to guarantee that all the permuted versions will be harmonically reasonable - maybe some of them more dissonant than the original, but still recognizable as music and

I figured that with the starting melody four bars long, three repeats of that per section plus a handwritten one-bar splice leading into the next section and a one-bar ending, that would give (4*3+1)*7 = 91 measures for the entire piece; roughly three minutes at a reasonable tempo. The planned outline would look something like this:

As a couple of extra wrinkles, I decided to switch around which of the voices would be "first," "second," and "third" in the different sections, and also use different keys for the different sections in the pattern C, G, C, F, F#, G, C. This all seems complicated, and it is, but that's part of the point: most of the complexity will be handled by the computer, and what I actually have to contribute to fill out the complete form is a four-measure subject, two four-measure sections of "counterpoint" for it, and seven one-measure three-voice chunks of transition or ending music. Perl and Lilypond will be used to write everything else.

Subject and counterpoint

Here's the subject for the fugue, a simple hand-composed melody that makes use of exactly the seven notes of the C major scale in one octave.

pftheme.png

[MP3] [FLAC]

The Lilypond code for the melody (actually one octave down from what's shown in the notation above, to reduce typing) is { c8 d8 e8 f8 g4 b4 | a4 f4 d4 r4 | g8 f8 e8 d8 c4 a4 | e4 f4 g4 r4 | } . It's easy to use a macro to repeat that in three voices, with octave transpositions, to form the outline of the first section. I leave one measure of silence at the end for the hand-composed transition to be filled in later.

fugue subject repeated in three voices

[MP3] [FLAC]

The next step is to add four measures of counterpoint, to be played against the subject on its second and third repetitions. Note that although I've read Fux, I am not carefully following the rules here; I figure any attempt at really strict Renaissance counterpoint would be messed up by the note permutations to follow in other sections anyway. The code for this added music is { b4 a4 e4 d4 | c4 d4 e8 f8 g8 a8 | a4 b4 g4 f4 | g4 f4 e8 d8 c4 | } , and as with the subject, I only type it in in one place and repeat it with a macro.

first counterpoint

[MP3] [FLAC]

The second chunk of counterpoint is added in a similar way. This one is only used once per section, when the other two voices are playing the subject and first counterpoint.

second counterpoint

[MP3] [FLAC]

Applying the note permutation

At this point, the piece is really almost finished, because the note permutation and key changes described earlier generate twelve of the thirteen measures of each of the remaining sections, just by transforming the twelve measures of music above. It only remains to get the computer to write down the specific notes. The way I did that in today's live stream was with a single-use Perl script, and although I fear you won't like it very much, here it is.

#!/usr/bin/perl
$ref=<<EOF;
QntryRA = {
  c8 d8 e8 f8 g4 b4 |
  a4 f4 d4 r4 |
  g8 f8 e8 d8 c4 a4 |
  e4 f4 g4 r4 |
}
QountQrRAX = {
  b4 a4 e4 d4 |
  c4 d4 e8 f8 g8 a8 |
  a4 b4 g4 f4 |
  g4 f4 e8 d8 c4 |
}
QountQrRAY = {
  e8 g8 c4 b4 e8 f8 |
  e2 b4 d8 c8 |
  d4 c4 e4 c4 |
  f4 c4 a8 b8 g4 |
}
EOF
$_=$ref;
tr/cdefgabA/cdefgabA/;
s/Qntry/entry/g;
s/QountQr/counter/g;
print;
$_=$ref;
tr/cdefgabA/cfbagdeB/;
s/Qntry/entry/g;
s/QountQr/counter/g;
print;
$_=$ref;
tr/cdefgabA/caedgfbC/;
s/Qntry/entry/g;
s/QountQr/counter/g;
print;
$_=$ref;
tr/cdefgabA/cdbfgaeD/;
s/Qntry/entry/g;
s/QountQr/counter/g;
print;
$_=$ref;
tr/cdefgabA/cfeagdbE/;
s/Qntry/entry/g;
s/QountQr/counter/g;
print;
$_=$ref;
tr/cdefgabA/cabdgfeF/;
s/Qntry/entry/g;
s/QountQr/counter/g;
print;

I justify this by the fact that it is, as I say, single use - it's meant only to generate the notes for this one piece of music and not for ongoing maintenance and modification. Spending effort on making it be prettier or more nicely factored would be a waste. The script starts by defining the subject and first and second counterpoint chunks in LilyPond syntax. Then there's a section for each of the first six sections of the piece (the seventh will reused the macros from the A section). The heart of each section of code is the tr/// command which translates lowercase letters cdefgab (for the notes) and the uppercase letter A (for the section) to what they should be for that particular section. Since the letters "c" and "e" also occur in the macro names and I don't want those to be changed as note names, I replaced them with capital Q and then un-did that with a couple more search and replace operations. It's messy, and you or I looking at this can easily think of better ways to do it, but this was what seemed expedient during the stream.

The output of this is LilyPond syntax defining macros \entryA...\entryF, \counterAX...\counterFX, and \counterAY...\counterAY, for subject and first and second counterpoint under transpositions A through F. The code that invokes these, applying octave and key transpositions, is handwritten with a lot of cut and paste. I won't put it right here in the body of the Web log entry but you can download it below. Here's the start of the resulting score, which is three pages long; I've just made the piece seven times as long without writing another note by hand. Note how the straightforward scale from the subject in the A section has been transformed by the permutation in the B section.

permutation applied

To finish this off, I need only add seven measures of handwritten music to smooth the transitions between the different sections and keys. I deliberately wrote these more loosely, with chromatic notes and some changes in rhythm, thinking that would make the piece a little more interesting. The first page is shown just to illustrate where the new notes go, but you shouldn't try to read this shrunk-down image; instead, download the PDF file.

first page of finished score, transitions highlighted

[MP3] [FLAC]

Downloads: PDF file, LilyPond main file, Perl-generated LilyPond macros.

Modular synthesis intro, part 5: Saving money || Modular synthesis intro, part 6: Voltage Controlled Amplifiers

MSK 013 Middle Path VCO, custom panel #53

MSK 013 Middle Path VCO, custom panel #53

US$489.67 including shipping

Anti-spam, fill in the blank: Coast Synthesis

Subscribe to our newsletter