ChucK : Programming Guide

Reserved Keywords

version: 1.4.x.x (numchucks)

control

for

for loop - example:

   // print out now for the next 100 seconds, once a second
   for( 0 => int i; i < 100; i++ )
       1::second => now => stdout;


while

while loop - example:

   // make time variable 'later', set to 5 seconds after now
   now + 5::second => time later;
   
   // while we are not there yet...
   while( now < later )
   {
       // print out the current ChucK time
       now => stdout;
       // advance time by 1 second
       1::second => now;
   }


until

until loop - kind of the opposite of the while loop - example:

   // infinite time-loop
   until( false )
   {
       // print out now
       now => stdout;
       // advance time by 100 ms
       100::ms => now;
   }


if

if - example:

   if( 1.0 > 0.0 ) "wow" => stdout;


else

else - example:

   if( Std.rand2f( 0.0, 1.0 ) > .5 )
       "yes" => stdout;
   else
       "no" => stdout;


spork

  • dynamically spork a shred, from a function or an expression
  • this operation is sample-synchronous, the new shred is shredule to execute immediate
  • in the current implementation, when a parent shred exits, all child shreds exit (this behavior will be enhanced in the future)
  • example (also see examples/ (http://chuck.cs.princeton.edu/doc/examples/) : wind2.ck (http://chuck.cs.princeton.edu/doc/examples/wind2.ck), pwm.ck (http://chuck.cs.princeton.edu/doc/examples/pwm.ck), fmsynth.ck (http://chuck.cs.princeton.edu/doc/examples/fmsynth.ck) and others (> grep spork *.ck) in examples/:
  // define function
  fun void foo( string s )
  {
       while( true )
       {
           s => stdout;
           500::ms => now;
       }
   }
   
   // spork shred, passing in "you" as argument to foo
   spork ~ foo( "you" );
   // advance time by 250 ms
   250::ms => now;
   // spork another shred
   spork ~ foo( "me" );
   
   // infinite time loop - to keep child shreds around
   while( true )
        1::second => now;


return

return from function - example:

   // define function
   fun int bar( int a, int b )
   {
       return a < b;
   }


(coming soon)

  • switch
  • break
  • continue


special values

now

  • special (global) value of type time
  • when read as a value, now holds current time in ChucK
  • when modified (by chucking a duration or time value to it), now advances time for the current shred
  • now never advances except when time is explicitly advanced in the program
  • because the above properties, now is guaranteed to always hold the correct ChucK time

example 1 (read):

   // assign now to a time value (note that foo will not advance with now)
   now => time foo;

example 2 (read):

   // print the current time
   now => stdout;

example 3 (write):

   // advance time by 500 ms
   500::ms => now;

example 4 (both in one line):

   // first advance time, then print out now
   500::ms =>now => stdout;

example 5 (addition/subtraction - also see 'arithmetic on time and duration')

   // later
   now + 10::second => time later;

   // subtract now from later (at this point, duration should equal exactly 10::second)
   later - now => dur duration;

example 6 (comparison)

   // with later from example 5 defined
   while( now < later )
   {
       now => stdout;
       1::second => now;
   }


start

start time of the current shred


NULL (or null)

for objects


true

integer 1 - example:

   // infinite time-loop
   while( true )
       1::second => now;


false

integer 0 - example:

   // infinite time-loop
   until( false )
       1::second => now;


maybe

evaluates to 0 or 1, each with maybe (0.0-1.0) probability, maybe defaults to .5 - example:

   // for the non-decisive programmer
   if( maybe )
   {
       // do something
   }


function (or fun)

used to define function - example:

   // define function
   fun int foo( int a )
   { return a+1; }

pi

3.1415926... (and so on) (see also Math.pi and Math.e)


(coming soon)

  • class
  • extends
  • implements
  • public
  • protected
  • private
  • static
  • const
  • new


default types

  • int : integer
  • float : floating point number
  • dur : duration
  • time : time
  • void : no type!
  • string : string
  • ugen : unit generator
  • stdout : output hack

(coming soon)

  • object : like java.lang.Object, this is the class all non-primitives extend (directly or indirectly)


default durations (dur)

  • samp : duration of 1 sample in ChucK time
  • ms : duration of 1 millisecond
  • second : duration of 1 second
  • minute : 1 minute
  • hour : 1 hour
  • day : 1 day
  • week : 1 week
  • (use these to inductively construct you own) - example:
   // construct duration
   .5::second => dur foo;
   // use foo as part of another duration
   4::foo => dur bar;


arithmetic on time and duration

in ChucK, there are well-defined arithmetic operations on values of type 'time' and 'dur'"

example 1 (time offset):

   // offset time by duration
   now + 10::second => time later;

example 2 (time subtraction):

   // time - time yields dur
   later - now => dur D;

example 3 (addition):

   // dur + dur yields dur
   10::second + 100::samp => dur foo;

example 4 (subtraction):

   // dur - dur yields dur
   10::second - 100::samp => dur bar;

example 5 (division):

   // dur / dur yields number
   10::second / 20::ms => float n;

example 6 (time mod):

    // time mod dur yields dur
    now % 1::second => dur remainder;


example 7 (synchronize to period):

   // synchronize to period
   .5::second => dur T;
   T - (now % T) => now;

the above is useful in precisely timing on-the-fly shreds (see otf_0[1 (http://chuck.cs.princeton.edu/doc/examples/otf_01.ck), 2 (http://chuck.cs.princeton.edu/doc/examples/otf_02.ck), 3 (http://chuck.cs.princeton.edu/doc/examples/otf_03.ck), 4 (http://chuck.cs.princeton.edu/doc/examples/otf_04.ck), 5 (http://chuck.cs.princeton.edu/doc/examples/otf_05.ck), 6 (http://chuck.cs.princeton.edu/doc/examples/otf_06.ck), 7 (http://chuck.cs.princeton.edu/doc/examples/otf_07.ck)].ck in examples/)

(also see the on-the-fly programming (http://on-the-fly.cs.princeton.edu/) paper)


operators

  • +
  • -
  • *
  • /
  • %
  • =>
  • =<
  • &&
  • ||
  • ==
  • ^
  • &
  • |
  • ~
  •  ::
  • ++
  • --
  • <
  • >
  • @=>
  • +=>
  • -=>
  • *=>
  • /=>
  • %=>


reserved

  • then
  • before
  • after
  • at



return to the Programmer's Guide


chuck | soundlab | cs | music | ccrma