ChucK is a music programming language (and compiler/synthesizer) written by a professor (Perry Cook) and his students at Princeton University. I'm learning how to use it; I thought that writing about how to use it would help...

## Wednesday, January 19, 2005

### The Simple A Scale Exercise

I know the tutorial material at Princeton is mostly aimed at using ChucK as a programming language (so far), but I wanted to start by doing some note (scale note) programming, so to start I found the article I noted in the early post: http://tyala.freeyellow.com/t4scales.htm, which gives you a discussion of musical scales, their principles and so on — and gives you a table of frequencies. (I'll probably rework that and put it up here, since it would be useful.)

But to start I did a simple exercise in writing ChucK. I set some variables to the frequencies of the A scale and wrote two loops, one to run a simple oscilator up and down the scale four times, and the second loop to run endlessly playing random notes of the A scale.

If you'd like a copy of the program Ascale.ck — right click and save here.

Here's the code:

```// A scale -- simple use of set scale frequencies
// set scale vars
440.0   => float A;
493.883 => float B;
554.365 => float Csharp;
587.330 => float D;
659.255 => float E;
739.989 => float Fsharp;
830.609 => float Gsharp;
880.0   => float Aoct;

// make our patch
sinosc s => dac;

// run the scale up, then down, at 100 ms notes 4 times
5 => int i;
while( i-- ) {
A => s.freq;
100::ms => now;
B => s.freq;
100::ms => now;
Csharp => s.freq;
100::ms => now;
D => s.freq;
100::ms => now;
E => s.freq;
100::ms => now;
Fsharp => s.freq;
100::ms => now;
Gsharp => s.freq;
100::ms => now;
Aoct => s.freq;
100::ms => now;
now => stdout;

Aoct => s.freq;
100::ms => now;
Gsharp => s.freq;
100::ms => now;
Fsharp => s.freq;
100::ms => now;
E => s.freq;
100::ms => now;
D => s.freq;
100::ms => now;
Csharp => s.freq;
100::ms => now;
B => s.freq;
100::ms => now;
A => s.freq;
100::ms => now;
now => stdout;
}
// now do random notes in the A scale at 100 ms intervals
while( true ) {
100::ms => now;
std.rand2(0, 7) => i;
i => stdout;
if( i == 0) A => s.freq;
else if( i == 1) B => s.freq;
else if( i == 2) Csharp => s.freq;
else if( i == 3) D => s.freq;
else if( i == 4) E => s.freq;
else if( i == 5) Fsharp => s.freq;
else if( i == 6) Gsharp => s.freq;
else if( i == 7) Aoct => s.freq;

}

```