Just Intonation and the Stern-Brocot Tree

October 21, 2013 in Thoughts

Since I work in Just Intonation I’m always looking for new ways to organize and think about ratios. Recently I came across the mathematical idea called the Stern-Brocot Tree from a very nice video posted by a friend. This is a way to represent all whole number ratios in their lowest terms as a binary tree growing from 1:1. Any ratio can be reached through a number of successive left or right moves down the tree. For specifics please consult the above links of other sources as I will not cover them here.


I decided to implement ways of traversing the tree in a SuperCollider class (this depends on the MathLib and dewdrop_lib Quarks). This works based on three representations of any position: the actual ratio, a matrix, and a continued fraction. By jumping back and forth between these three forms, you can easily move left, right, up, compare properties of different ratios, etc. It is functional but pretty rough and subject to major refactoring whenever I get around to it.

My current focus with the tree involves ratios between 1:1 and 2:1 (otonality) and the difference tones between them. Since playing with this I’ve noticed a few interesting things (that I’m sure others have seen before too).

Within the tree structure we have this notion of a path, which is a sequence of right (R) and left (L) steps downward. In Just Intonation theory, one of the most important types of ratios is the superparticular ((n+1)/n). If we look at the tree we can see that each of these are reached by starting from 1:1, moving R to 2:1, followed by consecutive L moves. This means every superparticular ratio falls in the path of RLLLLL…. etc. This can be represented as an array based on the continued fraction form of the ratio. After studying this representation we can see that each element is the number of steps in a direction, starting with a R, and alternating LRLR after that. For example, the path to 4:3 is RLL and the continued fraction is [1,2] (one R followed by 2 Ls). 5:4 would be [1,3], 6:5 is [1,4] and so on.

Difference Tones against 1:1
Say you want to know what difference tone you will get between a given fundamental and some ratio above it where both pitches are in the same octave (between 1:1 and 2:1) as each other. The math for that is simple: just subtract the two. However I’ve found that there is a correlation between the difference tone of any ratio between 1:1 and 2:1 and its path within the tree. Take (3/2) – (1/1) = 1/2. The path to 3:2 is [1,1] and the path to 1:2 is [0,1] (the 0 means that we do not have any R moves, so our first move is an L). How about (5/3) – (1/1) = 2/3. 5:3 is [1,1,1] and 2:3 is [0,1,1]. Seeing a pattern? It turns out that the path of the difference tone against 1:1 for any ratio between 1:1 and 2:1 is the path to the ratio with the first R move set to 0. For 7:4 [1,1,2], the difference against 1:1 is [0,1,2].

Axis and Mirroring
The next concept is that of a mirror. All this means is that you take the opposite path (if you moved R before, move L this time) around a central ratio that we will call an axis. If our axis is 1:1, the mirror is simply the reciprocal (the mirror of 2:3 is 3:2). Why do I have a new term if this already has a name? Because if we mirror around ratios other than 1:1 the result is not the reciprocal. Using 1:1 as an axis is the special case where the mirror and the reciprocal are the same thing.

Lets look at this in terms of the path to a ratio first. The mirror of 2:1 [1] around the axis 1:1 is 1:2 [0,1]. The mirror of 3:2 [1,1] around 1:1 is 2:3 [0,1,1]. Like we said earlier, if you want to move L first instead of R, you make the first slot in the array 0. With the difference between a ratio and 1:1 we replaced the first element with 0. Here we insert a 0 before the remaining operations if we are going left of 1:1, and we remove the 0 if we are going right of 1:1.

But why? Well, the path to 1:1 is [0] since we don’t need to go anywhere to get to it, but that still doesn’t tell us the whole story.

For this we have to understand how a change of direction in the tree is achieved. First lets look at the paths to the whole numbers. 2:1 [1], 3:1 [2], 4:1 [3]; each of these are consecutive R moves from 1:1 [0]. Now look at 3:2 [1,1] and 3:1 [2] in the tree. They are both children of 2:1 and are also mirrors around 2:1 since one is to the L and the other to the R. We can represent this in the path by separating 2:1′s path from the paths of both ratios. If we think for a second about the path in terms of Rs and Ls rather than just numbers:

2:1 = [1] = R
3:1 = [2] = RR
3:2 = [1,1] = RL

it becomes rather obvious that we are simply moving from the first R. So 3:2 and 3:1 are mirrors around the axis 2:1 since their paths are inversions of each other starting from the axis. Here’s one more example with 3:2 as the axis:

3:2 = [1,1] = RL
7:5 = [1,2,1] = RLLR
8:5 = [1,1,1,1] = RLRL

Now that we can think of it as Rs and Ls, we can manipulate the continues fraction to represent that:

3:2 = {1,1}
7:5 = [{1,(1}+1),1]
8:5 = [{1,1},1,1]

Difference Tones against 3:2
In exploring more of the difference tones within the tree I found a very special property of 3:2. The difference tone of 3:2 and any of its children is the same as the difference between 3:2 and the child’s mirror around 3:2. Let’s take the ratios we were just looking at. The difference between 3/2 and 7/5 is 1/10. The mirror of 7:5 around the axis 3:2 is 8:5. The difference between 8/5 and 3/2 is 1/10. The same is true for any child of 3:2 and its mirror. Also interesting is that the denominator of all mirrors around 3:2 will be the same (e.g. 7/5, 8/5).

Some SC Code Examples
Here are some things you can do with the SuperCollider class so far:

// get the path from a ratio
SternBrocotTree.asContinuedFraction(3/2); // returns [1,1]
// or make one based on its path
SternBrocotTree.fromContinuedFraction([1,1]).asRational; // returns 3/2

// there are also extensions to the relevant classes to make this easier
(3/2).asContinuedFraction; // returns [1,1]
[1,1].fromContinuedFraction.asRational; // returns 3/2

// moving around the tree
1.sbLeft.asRational; // returns 1/2
(3/2).sbRight.asRational; // returns 5/3
(3/2).sbUp; // returns 2

// test whether two ratios are adjacent
SternBrocotTree.areAdjacent(3/2,5/3); // returns true
SternBrocotTree.areAdjacent(3/2,5/4); // returns false

// mirror around an axis
(5/4).sbMirrorAround(3/2).asRational; // returns 7/4

I’ve only been playing with this for a few days but I’ve found it quite interesting and useful. There’s plenty more where this came from I’m sure!

Post to Twitter Post to Facebook Post to Google Buzz Send Gmail

SuperCollider Difference Tone Finder

June 25, 2013 in Music, Tutorials

Something that I need quite often is to find the difference tone between various pitches. For those unfamiliar with difference tones, it is a physical acoustic phenomenon where when any two frequencies are played simultaneously, you hear the difference between them (the higher minus the lower). If you’ve been around a pair of high instruments playing together you may have noticed a third, lower tone sounding below them. Or if you’ve heard power chords on a distorted electric guitar you actually hear a note an octave below the lowest note.

I’ve been working a lot with harmonies based on a set of just intonation ratios and amplifying their difference tone. When only dealing with two pitches at a time, it’s easy enough to just subtract them but once you have three or more, you must subtract every pair and find the lowest to know what the “primary” difference tone is. I’ve done this by hand before and it can be quite time consuming so I finally got around to making a little tool in SuperCollider to do it for me. It’s really simple: it loops through an array of ratios or frequencies, adds the difference of every pair to a list, sorts that list, and returns the lowest (first in the sorted list) difference tone. Here’s an exammple:


will return 8/5, which is the lowest ratio of all the difference tones in that set. Maybe someone out there will find a use for this in their work but it’s something I need constantly. The JustIntonationTools class currently only has this one method but I’m sure I’ll be adding more over time. It can be found on my github if you’d like to play with it. I should point out that it requires the TuningLib quark created by Charles Hutchins as he has a great method to adjust ratios to be within an octave.

Post to Twitter Post to Facebook Post to Google Buzz Send Gmail

Notes Between “Chromatic” Just Intonation

December 31, 2012 in Music, Tutorials

Yesterday I talked about how I’ve been taking MIDI note numbers and remapping them to different tunings here. If you only need to deal with discreet chromatic pitches this is enough; but what about if you are using pitch bends? Say you are bending a pitch up a half-step. If you simply add the bend amount to the initial note, you’ll get a sudden jump by the difference in tuning when you hit the next step. Let me illustrate this:

Starting Pitch: middle C + 16 cents (60.16)
Ending Pitch: C# – 14 cents (60.86)

If we have our pitch bend data so that it gives +1.0 is a half-step and add it to the MIDI note look what happens:

Starting Pitch + Bend up 0.25: 60.41
Starting Pitch + Bend up 0.5: 60.66
Starting Pitch + Bend up 0.75: 60.91 <- higher than our desired ending pitch
Starting Pitch + Bend up 1.0: 61.16 <- even higher

This is because here pitch bend is being treated as an alteration to the note rather than to the index of our tuning. The trick is that rather than linear bending across a linear range, we now want linear bending across a non-linear range (since each step is not a different distance from its neighbors). We need to take a few additional steps to make this work correctly. Here we go (in SuperCollider again):

First, to make our pitch bends map to floats where a delta of 1.0 = a half-step, you need something like this:

bend.linlin(8192,8874,0.0,1.0,nil).round(0.01) // .linlin maps a linear range to an other linear range

The first two arguments depend on what format your pitch bend data is in. In my case, it’s a 14-bit integer where 8192 is no bend and 8874 is up a half-step. The 0.0,1.0 is the new range these numbers will be. nil tells it to not clamp values (so this works with higher and lower values than those given). Finally .round(0.01) rounds the output to the nearest cent (you could skip that if you’d like).

Now for some magic! Arrays in SuperCollider have a method called blendAt() which takes a float as an index and spits out a linearly interpolated value between the neighbors around it. Here’s an example:

a = [ 2, 3];
a.blendAt(0.5); // returns 2.5 since the index 0.5 is half way between index 0 (2) and index 1 (3)

This behavior can be easily implemented in other languages as well of course but it’s nice that it’s just there in SuperCollider.

var tuning, note, bend, val, tunedNote;

tuning = [ 0.16, -0.14, -0.02, 0.17, 0.02, 0.14, 0.33, -0.31, -0.12, 0, 0.05, 0.04 ]; // our tuning
note = 60; // starting pitch
bend = 1.0; // bend amount

val = note + bend; // add the note and the bend

tunedNote = val + tuning.blendAt( val % 12, ‘wrapAt’ ).round(0.01); // magic!

Here’s what’s happening. We start by adding the pitch bend to the note like before (val). Next we convert that to an index by using mod 12. ‘wrapAt’ lets the last value in the array blend with the first in the array (so you can bend between 11 and 0). This spits out the correctly interpolated +/- cents for our new note. We then add this to val to get the real offset. Here’s some example output:

note = 60, bend = 0.0: 60.16 <- initial note
note = 60, bend = 0.25: 60.33
note = 60, bend = 0.5: 60.51
note = 60, bend = 0.75: 60.68
note = 60, bend = 1.0: 60.86 <- bend up a half-step
note = 61, bend = 0.0: 60.86 <- up a half-step with no bend

Now all bent values are scaled correctly and a bend of +1.0 gives the same result as just playing the next higher note with no bending.

Post to Twitter Post to Facebook Post to Google Buzz Send Gmail

Why I “always” write in A

August 28, 2012 in Music, Thoughts

If you’ve heard me talk about anything at all in the last four years I have probably brought up the fact that I always write in A. Of course this is not entirely true – any feedback/noise music I write is not in anything. However when it comes to pitched music this is basically true. Before I get into my reasoning, I’ll start by saying how I came to it.

When I was doing my Masters at Mills College, I had a seminar with professor Chris Brown which focused on alternate tunings. We looked at Harry Partch’s Genesis of a Music, the Just Intonation Network’s Just Intonation Primer, went to meet Ellen Fullman and see her “Long String Instrument” while she was in residence at the Marin Headlands Center for the Arts. Additionally I was studying Javanese Gamelan with Daniel Schmidt, one of the pioneers in bring Gamelan music to the west, instrument builder, and a composer who works extensively with tunings. This perfect storm of influences propelled me into the world of alternate tunings.

My first real venture in Just Intonation was my thesis at Mills Pulses (an acoustic work even though my degree was in Electronic Music, which I still find funny) which was premiered on the Signal Flow concert series. Due to working with Daniel, I decided to use the Bonang from the Mills Gamelan as well as building an instrument of my own which I called the Pentachord: a 6′ long five stringed instrument for playing natural harmonics. The ensemble was filled out with electric guitar and contrabass which would also only play natural harmonics. In this instance the Bonang tuning happened to be based on A 440Hz so that decision was made for me (although given the choice I probably would have stuck with A anyway). The piece was a success and won me the Paul Merritt Henry Prize for new works for string instruments that year but, more importantly, gave me a taste of working in Just Intonation, and once you’ve had Just you don’t go back!

During that same Signal Flow, my friend Chuck Johnson was also having a work for strings and Gamelan instrument in Just Intonation called Meet me by the pleroma. His piece however was based on 60Hz and centered around amplifying the difference tones produced by various just intervals. This was fascinating because it not only automatically produced a bass voice but also informs the performers when they are tuned correctly. I found this so compelling that ever since then all of my pitched music has also focused on difference tones (but based on 55Hz of course).

So that’s how I got started down this road. Now for some theory:

Just Intonation is all about breaking down the octave into smaller and smaller pieces. Rather than there being an arbitrary 12 tones, there are an infinite number restricted only by how far you want to take it. There are of course practical limits such as how precisely one can tune by ear, how in tune a particular instrument can play, how precise a floating point number on a computer can be, but barring that the sky is the limit. The way I look at it, if I have so many choices available based on one pitch, why would I need to go elsewhere?

Just Intonation is also all about the overtone series (by definition actually), and thus so are its difference tones. Anytime you have a justly tuned interval, when you subtract the two pitches you produce a difference tone that always falls within the overtone series of those pitches. Take the just perfect fifth. If I base it on a the overtone series of 100Hz, the first fifth I get is between 200Hz and 300Hz. Since 300-200 = 100, you can see that they are all part of the same series.

Difference tones dictate which octave you need. If I take that same example but make it 300Hz – 290Hz, you get 10Hz as the difference. Although 300Hz and 200Hz are both justly in tune with a fundamental of 10Hz, since this is below the threshold of pitch (somewhere around 25Hz), most people would perceive this interval as just being out of tune. If you however take the same ratio (30:29) and move it up, say to 3000Hz and 2900Hz, the difference tone is then in a range we can hear as a pitch (100Hz) and it sounds in-tune. The exact same interval has a different in-tuneness (because I hate the outdated term dissonance) solely due to a change in register.

Putting this all together:

Since Just Intonation can produce basically an unlimited number of relationships (individual pitches and difference tones) all from a single fundamental pitch (in my case A 55Hz), I have no need to go elsewhere.

If I write something tuned to any other pitch what would I gain? Nothing that I couldn’t achieve by using higher register ratios.

Why don’t I ever modulate?
- that would make things far more difficult on performers since all tuning relationships would change
- I have more than enough pitches right here in A
- modulation is a device for tonality and I am not writing tonal music

Don’t I ever get tired of hearing A all the time? Not at all! Since difference tones centered around 55Hz tend to be pretty close to the lowest pitches we can perceive I always get some killer bass. Going much lower pushes things towards the out of tune threshold and higher is, well, not low enough for me.

So to sum up: 55Hz rocks!

Post to Twitter Post to Facebook Post to Google Buzz Send Gmail