beanz Magazine

ChucK

mrbd on Flickr

Why would a musician learn to code? As it turns out coding is not just for those who want to create a new video game, control a robot, or design a website. Coding can be applied to the arts as well.

In the early 2000’s computer music designer (and now assistant professor at Stanford) Ge Wang decided to write a new programming language that would allow anyone interested in computer music to write algorithms to produce everything from cool sounds to entire songs and even instruments.

The language that Mr. Wang created is known as ChucK. As sound is a time-based medium, ChucK gives the programmer more control over time than most languages. Volume and frequency can also be programmed. For example, imagine we want to produce a sound as a sine wave with a frequency of 400 hertz for 3 seconds. We should also choose a volume, which must be a number between zero and one. For our first try we’ll choose 0.6. The following four lines of code will do the trick.

SinOsc foo => dac;
400 => foo.freq;
0.6 => foo.gain;
3::second => now;

At a glance, the code looks a bit cryptic. The first line is particularly tricky to decode. We chose for our sound to be produced by a sine oscillator, a sound wave with the following basic shape:

language-chuck-sine-wave

In order to hear the sound, we need to send the wave to the digital to analog converter, abbreviated as dac. We can think of the dac basically as the speaker or sound card.

The equal (=) signs that are used in many languages are replaced with arrows (=>)in ChucK. They are used to indicate that we wish to take the information on the left, and “chuck” it to the method on the right. We chuck our sound sine wave to the computer’s speaker. We chuck a frequency of 400 to the freq method and a volume of 0.6 to the gain method. We can’t very well create sounds or music without timing, so nearly all ChucK programs involve time. As there are many units used to measure time, we choose a quantity of time followed by the unit of time we are referring to. The last line of the program about (3::second => now) is read as “three seconds chucked to now”, or three second beginning now.

Ever wonder how to create the type of digital sound typically heard emanating from robots and computers in the movies? For example, how could we program the the rapidly changing pitches heard coming out of R2-D2 and BB-8 in Star Wars?

One approach is to write a few lines of code in ChucK that allow us to loop through randomly changing frequencies at a given rate. If we specify a certain range of frequencies to randomly choose from, we can quickly produce the typical robot sound effects that we hear in the movies. The code below is a good starting point:

SqrOsc wave => dac;
while( true )
{
    Math.random2f(50, 400) => wave.freq;
    .8 => wave.gain;
    .25::second => now;
}

By playing with the type of wave (SinOsc, SqrOsc, TriOsc), the range of random frequencies, the volume, and the time that passes between each new sound, we can produce a variety of robot-like tunes.

So far we have seen some entertaining examples of ChucK code, but what if we prefer something more beautiful, more reminiscent of an orchestra? That requires more coding, and some background knowledge of music; however, this is what ChucK was really created for.

In fact, ChucK is the main language used in Stanford’s Laptop Orchestra, Stanford’s Mobile Phone Orchestra, and at least a couple of fun apps (Ocarina and Magic Piano). Orchestra members not only can program songs, they can create entirely new instruments on their laptops using ChucK. The online version of this article has the code written by the ChucK team for a simple children’s melody (Programming for Musicians and Digital Artists, chapter 3).

es later
0.5 => float onGain;
0.0 => float offGain;

// declare and initialize our arrays of MIDI note #s
[57, 57, 64, 64, 66, 66, 64,
62, 62, 61, 61, 59, 59, 57] @=> int melNotes[];
[61, 61, 57, 61, 62, 62, 61,
59, 56, 57, 52, 52, 68, 69] @=> int harmNotes[];

// quarter note and half note durations
0.5 :: second => dur q;
1.0 :: second => dur h;
[ q, q, q, q, q, q, h, q, q, q, q, q, q, h] @=> dur myDurs[];

// make one more array to hold the words
["Twin","kle","twin","kle","lit","tle","star,",
"how", "I","won","der","what","you","are."] @=> string words[];

// loop over all the arrays
// (make sure they're the same length!!)
for (0 => int i; i < melNotes.cap(); i++)
{
    // print out index, MIDI notes, and words from arrays
    <<< i, melNotes[i], harmNotes[i], words[i] >>>;

    // set melody and harmony from arrays
    Std.mtof(harmNotes[i]) => s.freq;
    Std.mtof(melNotes[i]) => t.freq;

    // melody has a random pan for each note
    Math.random2f(-1.0,1.0) => mpan.pan;

    // notes are on for 70% of duration from array
    onGain => s.gain => t.gain;
    0.7*myDurs[i] => now;

    // space between notes is 30% of array duration
    offGain => s.gain => t.gain;
    0.3*myDurs[i] => now;
}

Like most coding, the complexity increases quickly as we move beyond the basic ideas and begin creating more interesting projects. However, with the sample snippets of code above, and in the online version of this article, programmers of all backgrounds can try creating sounds and music programmatically.

To get started, download the ChucK IDE, Chimera, paste any of the code from this article, and start adjusting the values provided to see how the sounds are affected.

Those who wish to learn more can refer to the book Programming for Musicians and Digital Artists, or even try out an online course. With a couple weeks or so of work, musicians will be able to programmatically create their own simple, synthetic, music!

Learn More

Programming for Musicians and Digital Artists

https://manning-content.s3.amazonaws.com/download/1/7a32089-b9b1-47bd-8304-bdee71b27e2c/PMDA_Ch03.pdf

Online ChucK course from CalArts

https://www.kadenze.com/courses/introduction-to-programming-for-musicians-and-digital-artists-iii/sessions/basics-sound-waves-and-chuck-programming

ChucK programming guide

http://chuck.cs.princeton.edu/doc/program/ugen.html

Create instruments with ChucK

https://blog.frogslayer.com/making-new-instruments-with-chuck/

Chimera IDE

http://chuck.stanford.edu/release/

Ocarina

http://www.gewang.com/ocarina/

Stanford Laptop Orchestra

http://slork.stanford.edu/


http://mopho.stanford.edu/


https://www.youtube.com/watch?v=2rpk461T6l4
https://www.youtube.com/watch?v=uHtCAAj8jFI
https://www.youtube.com/watch?v=S-T8kcSRLL0

Also In The October 2016 Issue

Virtual and augmented reality replace or add computing to our real world experience.

What would you build if you had 10 weeks and access to Microsoft HoloLens and HTC Vive equipment and developers?

With end of year holidays fast approaching, here are 35 of the more interesting ideas for holiday STEAM gifts that introduce STEAM concepts in fun ways.

If you work in a school or community library, or an after school group, STEAM events can be a way to offer technology events for kids.

A short history of virtual and augmented reality with lots of links to learn more.

One thing programmers do all day is imagine. When someone asks them to solve a problem with code, they start thinking and dreaming.

There are several key skills that I believe you need to have if you want to be a software programmer.

What makes a programmer lousy is a good way to identify what makes a programmer great.

Virtual reality has brought to the masses an old problem with flight simulators: what happens when our brain, ears, and eyes disagree?

The dots and lines used in graph theory can solve interesting problems.

Links from the bottom of all the October 2016 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about computer science, software programming, and technology for October 2016.