[chuck-users] Looping

Rich Caloggero rjc at mit.edu
Tue Sep 25 15:58:55 EDT 2012


> If I just want to do a really stupid thing like a Sine at 220 Hz that 
> sounds for 2 seconds and then stops for 2 seconds and then goes again. How 
> could I do?


// define connection
SinOsc s => dac;
.2 => s.gain;
220 => s.freq;

// now we let the machine run for two seconds producing the sine wave
2::second => now;

// now we let it run for two seconds, producing no sound
0 => s.gain;
2::second => now;

// ... and again for two seconds producing a tone
.2 => s.gain;
2::second => now;

The secret is in the "=>' operator. Its not strictly an assignment when its 
second argument is the now keyword.  What this is doing is essentially 
suspending your code for the indicated amount of time and letting the Chuck 
virtual machine compute the samples and send them to the dac.

When you use looping is when you want to modify some of the parameters of 
the thing being computed in realtime.  In that case, you generally loop and 
each time around the loop you change parameters, then set now to some value, 
say .1 second. This then would suspend your code for .1 seconds each time 
around the loop. When your code regains control, you change parameters and 
then suspend it again and let Chuck compute.  So, for instance:

// using same connection graph as above:

while (true) {
s.freq + .1 => s.freq;
.1::second => now;
} // while

Hope this helps ...
-- Rich


From: Alberto Alassio
Sent: Tuesday, September 25, 2012 3:36 PM
To: chuck-users at lists.cs.princeton.edu
Subject: [chuck-users] Looping

I cannot understand how the while structure has to be used. I mean, I 
understood that while ( true ) means infinite loop
and also that while ( n::second => now ) means an "n" second loop duration 
but...

If I just want to do a really stupid thing like a Sine at 220 Hz that sounds 
for 2 seconds and then stops for 2 seconds and then goes again.
How could I do? I thought that it was necessary just to do something like 
this

SinOsc s => dac;
.2 => s.gain;
220 => s.freq;
while ( 2::second => now)
0 =>s.gain

but I know that it's not the right way, also because in this method I would 
have to rewrite this thing an infinite ( whiletrue ahahah) number of times 
to have what I'm looking for.

And I don't actually understand {} . How am I supposed to use these?

Thanks

_______________________________________________
chuck-users mailing list
chuck-users at lists.cs.princeton.edu
https://lists.cs.princeton.edu/mailman/listinfo/chuck-users 



More information about the chuck-users mailing list