An Approach to Custom Scales in SuperCollider

September 26, 2014 in Tutorials

I often work with arbitrary pitch collections in Just Intonation that can change from place to place. As such, I usually deal with my pitches as harmonies or sets of ratios rather than scales or modes, but sometimes it’s nice to be able to do both.

SuperCollider has a built in mechanism for working with scales and tunings conveniently called Scale and Tuning respectively. These are particularly useful with Patterns as you can supply and change the scale and tuning you are using independent of the scale degree. Without this schema, you are stuck dealing directly with the exact pitches which, while straight forward and sometimes useful, can be limiting in certain situations.

Working with Scale and Tuning is pretty simple and well documented but there are a few quirks and redundant things to using it the way I need. My goal is to go straight from an array of ratios to something I can directly plug into a Pattern. For this, I made a simple function that handles all the subtitles:

// a function to convert an array or ratios to a Scale
~makeScale = {|ratios| // supply an array of ratios
Scale( // make a Scale
(0..ratios.size-1), // list the steps in the scale from 0 to the number of ratios
ratios.size, // how many steps we have in our scale
Tuning( // make a Tuning
ratios.ratiomidi // first we convert out ratios to midi
% 12 // then we mod 12 to put everything
).sort // lastly we sort it from low to high

// three JI scales, each with different root
a = ~makeScale.value([1,9/8,6/5,4/3,3/2,8/5,7/4]); // our initial scale
b = ~makeScale.value((5/4)*[1,9/8,6/5,4/3,3/2,8/5,7/4]); // same scale transposed a 5/4
c = ~makeScale.value((3/2)*[1,9/8,6/5,4/3,3/2,8/5,7/4]); // same scale transposed a 3/2

// test it out
\root, 0, // the pitch class of the scale's root
\octave, 5, // the octave of the root with a scale degree of 0
\scale, Pstep([a,b,c],2,inf), // play each scale/tuning every 2 seconds
\degree, Pseq((0..a.size),inf), // walk up the scale
\dur, 0.125 // each note is a 1/16th

Here’s what’s going on and why.

Scale first asks for an array of scale degrees based on the total number of degrees. For something like a major scale in 12TET this would be [0,2,4,5,7,9,11]. Since I want to deal with arbitrary scales sizes and the step sizes will be handled by the Tuning later, I just use (0..ratios.size-1) to make an array from 0 to one less than the number of ratios I supplied. If I give it 5 ratios, this gives [0,1,2,3,4].

Next Scale asks for the number of steps per octave. Since we want each ratio to be its own step, we just use ratios.size here. This effectively maps each ratio in the Tuning to each scale degree. At this point, Scale is just a wrapper around the Tuning, which is the real point of interest.

My way of dealing with the Tuning here is slightly more complicated than it absolutely needs to be, but I chose this approach because it offers the most flexibility.

All you really need is Tuning(ratios.ratiomidi), which would make a Tuning by taking our ratio array and converting it into the “midi number” version. This would be more than enough if we always ensure that our ratios are between 1/1 and 2/1. However, I decided I wanted to be able to be lazy and do things like transpose my ratios like so: (5/4) * [1,9/8,6/5,4/3,3/2,8/5,7/4]. This needed a little more stuff going on under the hood.

With ratios.ratiomidi, we get the the “midi number” array. Then we % 12 to force everything between 0 and 12. Finally we sort the result. This means that even if we supply ratios that are beyond one octave, this will automatically correct them and put put them in the right order for a scale.

Hopefully this is somewhat helpful. Once we have our Scale, we can do all the cool Patterny things exactly the same way as with any other standard scale. I also like to think of Scale in this case as also being usable as a chord. For instance, if you supply a three note tuning representing a chord, you could have your pattern play degrees [0,1,2] which would be the chord. Then if you change the Scale to be a different chord, the notes will change even if the degrees do not. Since the function we made will adjust octaves of out of range ratios, this can automatically handle inversions for us and thus give smooth voice leading basically for free (if that’s something you want). It’s fun to play around with at any rate.

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

Just Intonation: Relative vs. Absolute Pitch

July 6, 2014 in Music, Thoughts

A common thing to do when working with pitch is transpose them by some amount: down a 4th, up an octave, etc. If you are working with equal temperament (12 or other) you simply add your transposition interval to every note:

[0, 4, 7] + 4 = [4, 8, 11]
[C, E, G] up a M3 = [E, G#, B]

(since I’m a programmer, I’m writing all of these as arrays, deal with it =P)

This is very intuitive and even someone who knows little or nothing about music could probably understand it (just go up by N keys from each note on the piano and you are done). When working with Just Intonation however, we are working with multiplication rather than addition. While the math involved is actually not terribly more difficult, the resulting numbers often appear really complicated:

[0, 4, 7] + 4 = [4, 8, 11]
as ratios would look like
[1:1, 5:4, 3:2] * 5:4 = [5:4, 25:16, 15:8]
or as partials
[4, 5, 6] * 5:4 = [20, 25, 30]

“Ack, those numbers are really big! How complex!!” is a common response to seeing this. You could imagine that if we were transposing by more “complex” ratios (those involving larger numbers and/or more distance between numerator and denominator) we would quickly end up with an unwieldily mess. No one wants to deal with 3, 4, or 5 digit numbers: this is music, not calculus (…hah)!

Beyond the ugly large numbers, I have one other major issue with the above approach (which I will call here absolute pitch JI: it completely looses its identity. Yes, seeing [5:4, 25:16, 15:8] means I can get out my calculator and figure out what frequencies I’ll get quite precisely, but I have no idea what is going on when I look at it. [1:1, 5:4] and [5:4, 25:16] are both the same interval but that’s difficult to see at first glance (and even more difficult when larger numbers are involved).

Stepping back to my first example, something like [0, 4, 7] is very familiar to anyone who’s studied 20th century music: it’s a pitch class set. What is a pitch class set at it’s core? It’s the relative intervals between pitches from some arbitrary reference point. Before I labeled [0, 4, 7] as [C, E, G] but [E, G#, B] is the exact same pitch class set, and when analyzing music with pitch class sets you would identify them as the same thing.

Applying the same approach to JI, what is the resulting chord from [1:1, 5:4, 3:2] * 5:4?

[1:1, 5:4, 3:2] * 5:4

Why would we change it? That just makes it less readable. In fact, now it’s perfectly clear both what the chord is AND that we are transposing it by 5:4. I’ll call this relative JI.

“But now I don’t know EXACTLY what notes I need!”
You’re doing it wrong! It doesn’t matter. An [0, 4, 7] is an [0, 4, 7] no matter where you start it from so why should it be any different with ratios?

“But… math!!”
Yes yes, I know. Eventually you’ll need to multiply everything out to get your frequencies for your fancy notes. You’ll also need to multiply all your ratios by your fundamental frequency for that anyway (since JI is always relative to a reference pitch in the first place).

The Anatomy of a Pitch

There is no reason to bounce everything down to frequencies until you are going to play them really. Before that, we might as well keep things in a format that’s more friendly for working with it. Taking an approach from programming, rather than thinking of pitches as a number, let’s think about if as a data structure:

// a single pitch
fundamental: 440,
ratio: 5/4,
transposition: 3/2

fundamental * ratio * transposition = frequency

Which equals 15:8 or 825Hz, but we can actually see how we got there. Often we don’t even want to include the fundamental with the actual pitch since it is more of a global thing (you’ll usually only have 1 fundamental for a piece or section). So really we are looking at:

ratio: 5/4,
transposition: 3/2

most of the time.

Here “ratio” is the identity (or Odentity/Udentity if you would rather use Partch’s terminology) of the pitch. What is “transposition” exactly? Just a series of ratios to multiply the ratio by to get to where you want to be. We can just as easily have this:

ratio: 5/4,
transposition: 3/2, 2/1, 6/5

which is 5:4 up a perfect fifth, then up an octave, then up a minor third: FAR more useful than seeing 225:32 by itself. Since multiplication is commutative, the order of the transpositions doesn’t matter. Just multiply everything together and you get your result!

Use Case: Pitch Lattice

A common way or working in JI is to use an n-limit lattice (just google “pitch lattice”). These usually look like a 2, 3, 4+ diminutional grid of number vomit when written out because people typically write the resulting ratio for each location. At its core however, a lattice isn’t all the possible ratios, it’s a set of transpositions. A typical 5-limit lattice in its simplest form is this:

4/3 1/1 3/2

What happens when you move one space in any direction? The lattice moves with you! That means that all you need to do is keep track of where you move. So in this example if I move right, right, up I would get:

ratio: ,
transposition: 3/2, 3/2, 5/4

“But derp, 3/2 * 3/2 is 9/8 man! Learn to math!!”
I don’t care what it is, I care how I got there. Remember, all a lattice really is is a grid of transpositions, not absolute notes. Much more useful than seeing 45/16, which, while it makes us look like we math pretty hard, doesn’t tell us anything.

Final Thoughts

Hopefully you will find this approach to be somewhat useful. As I mentioned many times, this is about valuing clarity over all else. It should also make working with ratios less daunting as I’m rather sick of hearing the complaint that “just intonation gets too complex/difficult/meh!” when you try to transpose, modulate, or even just stack ratios on top of ratios. It’s conceptually no different from equal temperament, only you end up multiplying rather than adding.

If math is a sticking point, why are you working with tunings?!?! (joking)

Seriously though, if you are working with things like this, it’s probably a good idea to use a computer to handle the math. This is 2014 after all! Even if you know nothing about programming in general, learn how to use a basic python interpreter or something like that so that you can write out:

440 * (3/2) * (3/2) * (5/4)

and it spits out 1237.5, rather than grabbing your calculator or abacus and slogging through all of it yourself (although you should be able to, it’s just way faster and less accident prone).

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

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

Live Coding Drones #3

October 15, 2013 in Electronic, Music

More tweaks to my live coding setup in vim. Trying out the motus color scheme. Using delimitMate for automatic enclosures, SuperTab for autocomplete, snipmate for snippet expansion. Also decided to try using keyCastr to display all my keystrokes.

Started out with a basic master effects synth and bussing setup. I have a routine spawning a one synth that I constantly tweak. Later I add in a simple band passed noise synth for a little variation while I keep messing with the other one. Pitches are all derived from the overtones of two ratios which I alter from time to time.

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

Live Coding Drones #2

October 11, 2013 in Electronic, Music

Practicing jumping around Vim to quickly edit parts of existing code. All code was pre-written with the intent that certain parts would be edited as things progressed.

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