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

ByteBeat Shell Script

April 28, 2013 in Tutorials

Recently I have been playing around with ByteBeat. You can play around with these in your browser here if you are interested. SuperCollider can also do this as of version 3.5 (although I’ve noticed some differences in the output) if you want more control. I however felt like being able to play with these in a more brute force way: directly from terminal.

Since the workflow for messing with these is always the same I decided to make a little shell script to speed up the process. I’m on a Mac so I have to use sox for playback. If you are on Linux you’d need to change the pipe to /dev/audio or /dev/dsp unless you wanted to use sox instead.


# This script creates and plays a simple ByteBeat
# $1: a string with the ByteBeat algorithm e.g. "((t * 3) & (t >> 5))"
# $2: the name of the file to be creates (without an extension)
# make the c file
echo "main(t) {
for( t = 0;;t++)
putchar( $1 );
}" > "$2.c"
# compile the source
gcc "$2.c" -o "$2"
# play it with standard ByteBeat settings
./"$2" | sox -traw -r8000 -b8 -u - -tcoreaudio

Save that to a file called something like and make it executable with chmod 755. after that you can just run

./ "((t * 3) & (t >> 5))" test

and automagically you get 8-bit goodness right from your terminal. There’s a slight delay between running the script and hearing the output because it needs to compile so I wouldn’t necessarily perform with this (unless you get really good a timing it). Now this could be modified to let you specify the arguments for sox or whatever… but meh. This is just intended as a quick and dirty way to look like you are more of a hacker than you really are.

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

“Chromatic” Just Intonation – fast version

January 26, 2013 in Music, Tutorials

About a month ago I talked here about the math required to take a set of ratios and generate a set of +/- 50 cent alterations for tuning a chromatic scale to any 12-tone just tuning. There were quite a number of steps and a lot of math as I wanted to show how to do it in a way that could be applied to any programming language (or done by hand if you are feeling masochistic). Now I’m going to show a shortcut if you are specifically using SuperCollider.

SuperCollider has a method called ratiomidi that takes a ratio and converts it to the float MIDI number version. This replaces the need to multiply the ratios by a fundamental and mod 12 it as, because all our ratios are within an octave, everything will be from 0.0 through 11.99. In other words: it gives us tuned versions of pitch classes. This one simple method removes 2 of the previous steps.


gives us

[ 0, 1.0495540950041, 2.0391000173077, 3.1564128700055, 3.8631371386483, 4.9804499913461, 6.1748780739571, 7.0195500086539, 8.1368628613517, 9.3312909439626, 9.6882590646912, 10.882687147302 ]

Now that we have that, like before we need to get the cents in the +/- 50 cents, here in decimals. Once again we just need to subtract a series of integers from 0 to 11 like so:

( [1,17/16,9/8,6/5,5/4,4/3,10/7,3/2,8/5,12/7,7/4,15/8].ratiomidi – (0..11) ).round(0.01);

which gives us

[ 0, 0.05, 0.04, 0.16, -0.14, -0.02, 0.17, 0.02, 0.14, 0.33, -0.31, -0.12 ]

Great, that was easy! Now, you may notice by comparing it to the result we got from the other post that although the numbers are all the same, they aren’t in the same order. That is because before we based the tuning on A being the fundamental/tonic. Here C (array slot or pitch class 0) is the tonic. This is fine if you want C to be the tonic but what if you don’t? Simple! Arrays in SuperCollider also provide a method called rotate(numPlaces) which, as you might guess, shifts items left or right by a number of slots. So for our final version of this:

( [1,17/16,9/8,6/5,5/4,4/3,10/7,3/2,8/5,12/7,7/4,15/8].ratiomidi – (0..11) ).round(0.01).rotate(9);

gives us

[ 0.16, -0.14, -0.02, 0.17, 0.02, 0.14, 0.33, -0.31, -0.12, 0, 0.05, 0.04 ]

which is exactly what we got in the old example. Why are we using 9? If you prefer to think about this as a transposition operation then T9 = A. You could also put a -3 in there and you would get the exact same result. Totally depends on how you want to think about it.

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

“Chromatic” Just Intonation

December 30, 2012 in Music, Tutorials

I’m not a fan of 12-tone based scales in general. However sometimes in order to use some standard controllers you are stuck with it. Recently I’ve been working on ways to perform music in just intonation using MIDI input from a guitar so I’ve been forced to think in 12 step groupings, even if I intend to not use all of them (or some more than once). Here’s one solution that I’ve been playing with. The code is all SuperCollider but is pretty easy to implement in other languages.

First we need to make a “chromatic” JI scale. Honestly this could be anything depending on your needs. In my case I’m trying to keep each step within +/- 50 cents of the tempered pitch but this doesn’t need to be the case. Here’s one option:


The next step is to give it a fundamental/tonic. As usual, I’ll use A 440:

440 * [1,17/16,9/8,6/5,5/4,4/3,10/7,3/2,8/5,12/7,7/4,15/8];

Now we have all our frequencies for our A “chromatic” scale. Time to convert it to the linear MIDI number version (using floats to keep the offset). Lets also mod 12 it so that we get everything in the 0-11 pitch-class range:

((440 * [1,17/16,9/8,6/5,5/4,4/3,10/7,3/2,8/5,12/7,7/4,15/8]).cpsmidi % 12); // .cpsmidi converts frequencies to midi numbers

Almost done. The goal here is to take any MIDI number as an input, mod 12 it to get it’s pitch class as an index into our scale, and get the cent deviation in the format of +/- 0.50 so we can add that back to the original input. That may sound complicated but it will make more sense after seeing this last part. Now we need to reorder this list so that pitches are ascending (0-11), subtract the integer series 0-11 from it, and in this case round to the nearest cent:

((((440 * [1,17/16,9/8,6/5,5/4,4/3,10/7,3/2,8/5,12/7,7/4,15/8]).cpsmidi % 12).sort) - (0..11)).round(0.01); // .sort, uh, sorts it, (0..11) creates an array of 0-11 to subtract from our scale, and .round(0.01) rounds to the hundredths place

Afterwards we are left with:

[ 0.16, -0.14, -0.02, 0.17, 0.02, 0.14, 0.33, -0.31, -0.12, 0, 0.05, 0.04 ]

which are the cent deviations for all chromatic pitches from C-B. Notice how the only 0 is A (index/pitch class 9), since it’s our fundamental. Now by using MIDInumber % 12 as an index into this array, you get the cents to add or subtract to get your tuning. This is essentially what MIDI tuning tables are. Later I’ll explain how to use this along with pitch bends.

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