ChucK : Language Specification > Overview

version: 1.5.x.x (numchucks)

Overview

ChucK is a strongly-typed, strongly-timed, concurrent audio and multimedia programming language. It is compiled to virtual instructions, which are immediately run in the ChucK Virtual Machine. This guide documents the features of the Language, Compiler, and Virtual Machine for the ChucK programmer.

The latest version of chuck can be downloaded here.

Running ChucK

There are many ways to ChucK:

To run a ChucK file, foo.ck for example, simply type chuck and which .ck file to run:

chuck foo.ck

To run multiple files at once (or the same one concurrently with itself) type:

chuck foo.ck bar.ck bar.ck boo.ck

Flags specify how ChucK operates, or return information about the virtual machine. The following example uses --probe to print information on all available audio devices, MIDI devices, and human interface devices (HIDs). You may then refer to them (by number usually) from the command line or from your program (see VM Options for a complete list).

chuck --probe
[chuck]: [CoreAudio] driver found 7 audio device(s)...
[chuck]: 
[chuck]: ------( audio device: 1 )------
[chuck]: device name = "External Headphones"

Now we can specify an output device using --dac:N, where N is the audio device number.

chuck --dac:1

ChucK can run as a host/server that can receive code from a client. The server invokes the --loop flag to specify that the virtual machine shouldn't halt automatically when the current programs exit.

chuck --loop

(See the guide to On-the-fly Programming for more information)

If a ChucK host is running, we can send a program to the listener by using the + command line option:

chuck + foo.ck

Similarly, we can use - to remove and = to replace a program on the host. We can use ^ to find out the status of the host's virtual machine. Again, see On-the-fly Programming for more information.

To run most of the code or examples in this language specification, you only need to use the basic chuck program.

Comments

Comments are sections of code that are ignored. They help you and other programmers interpret your code. Double slashes indicate to the compiler to skip the rest of the line. /* and */ denotes block commenting - the compiler ignores the text in between (Note: block comments cannot be nested).

// this is a comment
int foo; // this is also a comment

/* 
this is a block comment
SinOsc bar => dac; this line is ignored
*/

Printing

The debug print syntax <<< >>> is used to display the value and type of any expression placed within it in the console. This syntax may be placed around, and will not affect the execution of, any non-declarative expression ( non l-value ). The value to be printed can also be passed through by using the chuck operator. Expressions which represent an object will print the value of that object's reference address.

// assign 5 to a newly declared variable 
5 => int i;
// prints "5 : (int)"
<<<i>>>;

// prints "hello! : (string)"
<<<"hello!">>>;

// prints "3.5 : (float)"
<<<1.0 + 2.5 >>> => float x;
// prints "3.5 : (float)" again
<<<x>>>;

To format and print data, use a comma-separated list of expressions. This will print their respective values with one space between:

// prints "the value of x is 3.5" (x from above)
<<<"the value of x is" , x >>>;

// prints "4 + 5 is 9"
<<<"4 + 5 is", 4 + 5>>>;

// prints "here are 3 random numbers 6 8 2" or similar
<<<"here are 3 random numbers", 
    Std.rand2(0,9), 
    Std.rand2(0,9),
    Std.rand2(0,9) >>>; 

There are also standard output and error objects chout and cherr.

chout <= "Hello World!";
chout <= IO.newline();
cherr <= "Goodbye";

// prints
// "Hello World!"
// "Goodbye"

Reserved Words