Wednesday, May 30, 2012

Lab Idea 1: Intro to ChucK for Non-Programmers

Here's an idea for a lab for a class to do. It doesn't assume anything beyond a basic knowledge of how computers work and attempts to explain the basics of ChucK to someone who's never programmed before, while hopefully also giving some ideas for more advanced students to explore. Content is below the jump.





1. Download and install ChucK(executable version) and miniAudicle, if they are not already installed on your machine.

Instructions for Mac are here: http://learningchuck.blogspot.com/2012/05/basics-for-macs-downloading-chuck-for.html
Instructions for Windows are here: http://learningchuck.blogspot.com/2012/05/basics-for-pc-downloading-chuck-for.html

2. Start up miniAudicle. This is an interactive development environment (IDE) for the ChucK language. The main difference between this and the command line version of ChucK is that you can edit the files that you're working on in the IDE, and run them with just a couple mouse clicks. The command line version, on the other hand, requires you to use an external editor like TextEdit or Notepad to create your files, and then run them by typing into the command line. For beginners, miniAudicle (the IDE) is an easier place to start.

3. Now we'll do a modified version of the tutorial provided on the official ChucK website (http://chuck.cs.princeton.edu/doc/learn/tutorial.html). The tutorial there was written for the command line version of ChucK, so here's a step-by-step walkthrough using miniAudicle.

3a. Open up miniAudicle and paste this into the empty program:


// connect sine oscillator to D/A convertor (sound card)
      SinOsc s => dac;

Let's examine these two lines of code. The first line is a comment because it starts with two backslashes. Comments are not executed when the program is run, so you can use them to document what the actual code does. This is very useful, as often the code wouldn't make a lot of sense to a third party viewer without comments. The second line creates a new sine oscillator (SinOsc) called "s", and connects the output of s to the dac, (stands for D/A converter, which ChucK uses as an abstraction for the underlying audio interface). The semicolon tells the program that the line of code has ended. 

An extremely important part of this line is the => in the middle. This is the ChucK operator, and it "chucks" s to dac. The ChucK operator is overloaded, meaning that it does different things depending on what data types are given to it. In this case, it connects the output of s to the input of dac. In other cases, it may change the contents of a variable, or cause time to progress.

3b. Now add these lines under the previous lines in miniAudicle:

// allow 2 seconds to pass
      2::second => now;

 Now in the Virtual Machine window, click "Start Virtual Machine", and then back in the program, click the "Add Shred" button (the green plus symbol). You should hear your program running! If not, make sure your computer's audio is on and adjusted properly. You might need to go into the miniAudicle preferences to change the output device. 

3c. Next let's do something a little more interesting. Here's a whole new program:

// make our patch
      SinOsc s => dac;

      // time-loop, in which the osc's frequency is changed every 100 ms
      while( true ) {
          100::ms => now;
          Std.rand2f(30.0, 1000.0) => s.freq;
      }

The first two lines are similar to the previous program. The next chunk though is different. As the comments say, it's a loop in which the sine oscillator's frequency changes every 100 milliseconds. The "while(true)" line says that this is an infinite loop. For finite loops, it could have such values as "while(x>0)." The line "100::ms => now;" is another feature specific to ChucK.

The concept of time in ChucK is different than time as we experience it. Time is constantly passing for us, but we can control exactly when it stops and starts in ChucK. Because the basic output of ChucK is sound, and sound is linked to time, when time is passing in ChucK sound is generally happening. Therefore. you control how long you want a sound to last by telling ChucK to let that amount of time pass. To be clear: ChucK time has nothing to do with your computer clock, or with the time passing in the real world. The only similarities are that measurements like a second, minute, hour, etc., are all the length you would expect them to be. Otherwise, ChucK time is completely controlled by the program that is running, and time (and therefore sound as well) will only happen when it's told to.

4. Now that you've got a basic understanding of how ChucK can work, try some stuff on your own. 

4a. In the program above, you can change some numbers to get different effects. Play around with the time value and the range in which the frequency is randomly generated.

4b. Change the loop so that it's not infinite. A good way to do this is to make a variable outside of the loop (let's call it x) and then increment or decrement it every time you go through the loop. Change the while statement so that the loop only runs when x is equal to/is less than/is greater than a certain value. For help on this step, take a look here: http://chuck.cs.princeton.edu/doc/language/oper.html#chuck

5. Once you've completed all this, take a look through some of the ChucK Language Specification: http://chuck.cs.princeton.edu/doc/language/
It's a great document to use as a reference, and also explains a lot of the basic of both ChucK and simple programming.

No comments:

Post a Comment