The Juicy, Frustrating Rhythms of the Thue-Morse Sequence
There's a lot of stuff out there on the sonification of mathematical objects of theoretical importance. I don't like most of these compositions | blog posts | 20th-century music theory papers | AI-generated YouTube shorts, because they usually neglect discussion of why that object's particular mathematical properties[1] might make it interesting perceptually to the human ear[2]. So, here is the Thue-Morse sequence, which is actually quite useful for procedural generation.
01101001100101101001011001101001100101100110100101101001100101101001011001101001011010...
It's a little easier to appreciate the texture of this sequence when shown as a bar chart, so I'll be showing those:
Just by scanning it visually, you can already see some strange things. In several places it looks as if there's a simple pattern, some kind of repeat, but it never lasts. There's also never a cluster of 0s or 1s, or a region where one is more dense than the other.
There's tons of ways you can define this sequence and tons of strange properties that it has. For an overview, I recommend this very entertaining and criminally underrated video by the YouTube channel Combo Class. The method that feels the most "natural" to me is the following procedure: Start with a sequence containing just a 0. Then, take the sequence and invert it (changing 0s to 1s and 1s to 0s) and append the result to the old sequence, getting 0 | 1 = 01. Then do it again: 01 | 10 = 0110. And 0110 | 1001 = 01101001. And so on:
0
0 | 1
01 | 10
0110 | 1001
01101001 | 10010110
0110100110010110 | 1001011001101001
The Thue-Morse sequence is a fun mathematical object for a lot of reasons. Beyond all of that, though, I think it's especially interesting for use for procgen art (and music in particular) because it feels very repetitive while actually containing few repeats at all. For instance, there is no appearance of the strings "000" or "111" anywhere, and also no appearance of "010101" or "101010" or "001001001" or any snippet repeated three times in a row. However, there's infinitely many subsequences that are repeated twice in succession, and all subsequences that appear will reappear infinitely many times, eventually. Like the works of all my favorite Interesting Drums Musicians, this puts us perceptually in a very interesting place, where your brain is constantly expecting that it will be able to predict the next event but often fails.
This post is also motivated by a recent video by Stand-Up Maths which discusses the Thue-Morse sequence and its extensions in the context of fair turn-taking. In particular, they show some musical realizations of the sequence in higher bases by mapping the numbers of the sequence to the degrees of a scale[3]. This is cool, but in my opinion this sequence is far more interesting to listen to when you use it to index into a list of rhythms instead, which I will show further down.
Extensions to Higher Bases
The natural next question is how to involve more symbols than just 0 and 1. I've seen a couple methods proposed to extend this sequence to higher bases, but the one that I prefer for musical purposes I first saw outlined in the aforementioned video by Combo Class. To calculate the nth entry of the Thue-Morse sequence for base b, first represent n in base b, add up the resulting digits, and take the resulting sum modulo base b. For example, the 319st value of the sequence in base 5 is 1, because
319 = 2*(5^3) + 2*(5^2) + 3*(5^1) + 4*(5^0)
=> 319 in base 5 is 2234
=> (2 + 2 + 3 + 4) = 11
=> 11 % 5 = 1
I guess you lose the nice "take the sequence and flip it and stick it to itself" intuition, but it's easy to calculate for arbitrary n and b.
Here is a SuperCollider class containing a Pattern-based implementation of this. To install it, save it as a .sc file anywhere in your user extensions folder.
PthueMorse : Pattern {
var <>start=0, <>step=1, <>base=2, <>length=inf;
*new { arg start = 0, step = 1, base = 2, length=inf;
// without this, the interpreter locks up and crashes if you give it a 1 or 0. oops
(base < 2).if({
Error("PthueMorse: Base (%) cannot be less than 2".format(base)).throw;
});
^super.newCopyArgs(start.asInteger, step.asInteger, base.asInteger, length)
}
storeArgs { ^[start,step,base,length] }
convertBase { arg inp, convert_base;
var arr = List();
({inp > 0}).while({
arr.addFirst((inp % convert_base).asInteger);
inp = (inp / convert_base).floor;
});
^arr
}
embedInStream { arg inval;
var outval, digitSum = 0;
var counter = start;
var len = length;
var digits = [];
while { counter < len } {
digits = this.convertBase(counter, base);
digitSum = (digits.sum % base).asInteger;
inval = digitSum.yield;
counter = counter + step;
};
^inval;
}
}
So, the sequence for b = 3 is:
01212020112020101220101212012020101220101212001212020120101212001212020112...

For higher bases you get this interesting behavior where it seems to be counting up from 0 to b over and over again but with slight imperfections each time. For example, here's b = 10:
0123456789123456789023456789013456789012456789012356789012346789012345789012...

To the SuperCollider class above I've also added a step argument which skips every couple of values of the sequence. When step and base have no common factors you get a kind of randomly phasing arpeggiator. Here's the sequence for b = 10, step = 3:
03693693693692692692692592592592583693693692692692692592592592582583693692692...

OK Now Let's Interesting Some Drums
For the SuperCollider example code, I'm using these three simple SynthDefs[4].
SynthDef (\kick,
{
var freqEnv, mul, click, sig, dec = \dec.kr(0.12);
freqEnv = Env.perc(0, dec, curve:\curve.kr(-5)).kr * \freq.kr(45) + 50;
mul = Line.ar(1,0,dec,doneAction:2);
click = WhiteNoise.ar * Env.perc(0, dec/8,level:0.3,curve:-3).kr;
click = BPF.ar(click, \clickCutoff.kr(5000), 0.2);
sig = SinOsc.ar(freq: freqEnv, mul: mul) + (click*\clickAmt.kr(0.7));
Out.ar(\out.kr(0),
Pan2.ar(sig,\pan.kr(0),\amp.kr(0.6));
)
}).add();
SynthDef(\snare, {
var drumosc, partials, volenv, noise, fullout, dec=\dec.kr(0.3);
partials = [0, 1.2, 4.05, 5.21, 7.11] * XLine.kr(1.2, 1, dec/8);
drumosc = SinOsc.ar(\freq.kr(51) * partials, 0, [1,0.422,0.25,0.1, 0.05]);
drumosc = (drumosc.sum * 2).tanh;
volenv = Env.perc(0.0001, dec * 0.8, curve:\curve.kr(-6)).kr(doneAction:2);
noise = LPF.ar(WhiteNoise.ar, 14700) * Env.perc(1e-5, dec).kr * 0.2;
fullout = (drumosc * volenv) + noise;
Out.ar(\out.kr(0),
Pan2.ar(fullout * \amp.kr(0.6), \pan.kr(0))
);
}).add();
SynthDef(\hat, {
var sig;
sig = BPF.ar(WhiteNoise.ar, \cutoff.kr(14000), 0.4);
sig = sig * Env.perc(1e-5, \dec.kr(0.05)).kr(doneAction:2);
Out.ar(\out.kr(0),
Pan2.ar(sig,\pan.kr(0),\amp.kr(0.6));
)
}).add;
So, the most obvious thing you can do is to treat it as the "on" switches of an ordinary drum sequencer (Pswitch1 is especially useful for this):
c = TempoClock(120/60);
~thueMorsePat = PthueMorse(0, 1, 2);
x = Pbind(\dur, 0.25) <> Pswitch1(
[
Pbind(
\instrument, \kick,
\freq, 40,
\amp, Pwhite(0.1, 0.3),
),
Pbind(
\instrument, \snare,
\freq, 60,
\amp, Pseq([0.2, 0.3], inf),
\dec, 0.2,
)
],
~thueMorsePat
);
x.play(c);
Here's a sequencer-style pattern that uses a higher base. You can control multiple different instruments in a drum machine like this:
~thueMorsePat = PthueMorse(0, 1, 4);
x = Pbind(\dur, 0.2) <> Pswitch1(
[
Pbind(
\instrument, \kick,
\freq, 40,
\amp, Pwhite(0.3, 0.4),
),
Pbind(
\instrument, \snare,
\freq, 60,
\amp, 0.3,
),
Pbind(
\instrument, \hat,
\freq, 15000,
\amp, 0.2,
),
Pbind(
\instrument, \snare,
\freq, 88,
\dec, 0.05,
\amp, 0.2,
),
],
~thueMorsePat
);
x.play;
It's a nice effect. Throw some FM-based pads underneath this and you've got most of a 1990's-era Autechre track. Consider a variant that replaces ~thueMorsePat with Prand([0, 1, 2, 3], inf), which just chooses indices at random:
I think this is pretty obviously less fun and less interesting.
The technique I use the most often, though, is to map outputs to note offsets / rhythms values directly. Here, a 0 gives us an eighth note on the snare while a 1 gives us a dotted eighth, with the kick underneath playing straight quarter notes:
~thueMorsePat = PthueMorse(0, 1, 2);
x = Pbind(
\instrument, \snare,
\dur, Pindex([0.5, 0.75], ~thueMorsePat, inf),
\freq, 60,
\dec, Pkey(\dur) / 2
);
y = Pbind(
\instrument, \kick,
\freq, 40,
\dur, 1,
\amp, Pwhite(0.3, 0.4),
);
y.play;
x.play;
I love how this feels. It's very frustrating. It sounds at many points as if it is about to lock into something regular but never does.
You can, of course, do this in higher bases: here the base-4 Thue-Morse sequence indexes into either a sixteenth note, an eighth note, a dotted eighth, or a quarter:
~thueMorsePat = PthueMorse(0, 1, 4);
x = Pbind(
\instrument, \snare,
\dur, Pindex([0.25, 0.5, 0.75, 1], ~thueMorsePat, inf),
\freq, 60,
\dec, Pkey(\dur) / 2
);
y = Pbind(
\instrument, \kick,
\freq, 40,
\dur, 1,
\amp, Pwhite(0.3, 0.4),
);
y.play;
x.play;
Which is neat in small doses, but in my opinion it quickly gets perceptually overwhelming and not that much more interesting than choosing rhythms randomly. Higher bases are more useful when you enforce that only the highest or lowest value of the sequence triggers a different duration value. For example, here, the snare drum plays an eighth note when the base-4 Thue-Morse sequence is one of 0, 1, 2 and a dotted eighth when it's a 3.
c = TempoClock(120/60);
~thueMorsePat = PthueMorse(0, 1, 4);
x = Pbind(
\instrument, \snare,
\dur, Pindex([0.5, 0.75], (~thueMorsePat >= 3).asInteger, inf),
\freq, 60,
\amp, Pseq([0.1, 0.2], inf),
\dec, Pkey(\dur) / 2
);
y = Pbind(
\instrument, \kick,
\freq, 40,
\dur, 1,
\amp, Pwhite(0.3, 0.4),
);
y.play(c);
x.play(c);
So we're triggering the dotted-eighth note about a quarter of the time, not randomly, but not predictably either. What we're doing now is essentially using the Thue-morse sequence as a very poor pseudo-random number generator. I think it has a lot of interesting qualities when used this way, and it sounds more "structured" than choosing entries randomly. Compare this to what you get if you just choose between [0.5, 0.75] randomly, using Prand([0.5, 0.5, 0.5, 0.75], inf) for the dur entry of the first Pbind:
I mean, I can't quantify my opinion, but this is obviously less interesting to my ears. The dotted-eighths don't appear for a while, which is boring, and then they pile up together, which ruins the groove.
Here's a more complicated sketch that shows how I like to use the Thue-Morse sequence in practice, with each of the snare, kick, and hat using it in different ways.
c = TempoClock(120/60);
x = Pbind(
\instrument, \snare,
\dur, Pseq([Rest(1)]) ++ Pindex(
[Pseq([0.75, 1.25]), 2, Pseq([1.25, 0.75])],
PthueMorse(55, 1, 7).linlin(0, 6, 0.99, 2.01).floor.asInteger,
inf
),
\freq, 50,
\amp, Pseq([0.3, 0.2], inf),
\dec, PthueMorse(0, 1, 4).linlin(0, 3, 0.2, 0.4) * (Pkey(\dur) / 2),
);
y = Pbind(
\instrument, \kick,
\tm, PthueMorse(0, 1, 8),
\freq, Pkey(\tm).linexp(0, 8, 160, 40),
\dur, Pindex(
[0.25, 0.5, 0.75],
Pkey(\tm).linlin(0, 7, 0.99, 2.5).floor.asInteger,
inf
),
\amp, Pseq([0.2, 0.1], inf) * Pkey(\tm).linlin(0, 7, 1.0, 0.7),
\clickAmt, (Pkey(\tm) % 2) * 1.4 + 0.1,
\dec, Pkey(\dur) * 0.3,
).trace;
z = Pbind(
\instrument, \hat,
\amp, PthueMorse(1111, 2, 9).linlin(0, 8, 0.01, 0.2),
\cutoff, 3333,
\dur, Pindex([0.25, 0.5], (PthueMorse(0, 1, 4) >= 3).asInteger, inf),
);
y.play(c);
x.play(c);
z.play(c);
The .linlin(0, 6, 0.99, 2.01).floor.asInteger bits are just to force most values of the sequence to get remapped to 1, except for 0 which gets mapped to 0 and 6 which gets mapped to 2.
I find this kind of thing really compelling in how it never perfectly settles down, yet only introduces new information slowly, at semi-regular intervals.
Finally, I haven't used this family of sequences much for generating pitches and melodies, as suggested by the Stand-Up Maths video linked to above, but I do like how it sounds when used as an arpeggiator, like this:
c = TempoClock(120/60);
x = Pbind(
\instrument, \default,
\scale, [0, 4, 7, 11],
\degree, PthueMorse(49494, 2, 11),
\octave, 3,
\amp, Pwhite(0.2, 0.3),
\dur, 0.25
);
x.play(c);
Sounds like the kind of thing that would be useful in a context that I cannot think of at the moment, but I'm sure I'll figure something out someday.
Conclusion
Well, whenever you see something that you're tempted to randomize, consider using the Thue-Morse sequence instead of arbitrary variations. You don't have to be using SuperCollider or doing procedural generation at all. It's got a nice and very distinct flavor to it. That's my pitch.
One of my all-time favorite composers, Conlon Nancarrow, once wrote a canon with two parts in a ratio of
e/π. Come on, man. That's nothing. If you're going to use an irrational ratio use an irrational number interesting for its properties as a ratio -- say, the golden ratio. It's right there. ↩︎Actually, no, I'm not done complaining. This one time in college I was at a concert where one of the composers had written a piece "based on the golden ratio" (according to the program notes). But on further investigation it turns out they'd meant taking the golden ratio's decimal expansion, like, each digit in its expansion individually,
[1, 6, 1, 8, 0, 3, 3]etcetera, and used those to choose pitches. It was perhaps the angriest I'd ever been while listening to music. ↩︎They use a different extension of Thue-Morse to bases >2 in this video, but I don't see how it can be easily calculated for arbitrarily large
nwithout calculating all previous terms as well, so I don't think it's worth looking at for this purpose. Also, it doesn't look like it'd sound as interesting, since for higher bases it gets chaotic quite fast. ↩︎I do not remember if I wrote the kick and snare myself or not. They've been on my hard drive for many years and I might have copied them from sccode.org or something. If you wrote these I am sorry. ↩︎