[chuck-users] Looping
Michael Heuer
heuermh at gmail.com
Tue Sep 25 15:59:59 EDT 2012
Alberto Alassio wrote:
> 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?
The curly brackets '{' and '}' encapsulate what you would like to loop
over with the while construct:
SinOsc s => dac;
220.0 => s.freq;
while (true)
{
0.2 => s.gain;
2::second => now;
0.0 => s.gain;
2::second => now;
}
Does that make sense? If you want to loop a certain number of times,
you can use the for construct, e.g.
SinOsc s => dac;
220.0 => s.freq;
for (0 => int i; i < 10; i++)
{
0.2 => s.gain;
2::second => now;
0.0 => s.gain;
2::second => now;
}
would loop 10 times.
If you want to take this further, with LiCK you can put the part
between the {}s into a functor class
class Play extends Procedure
{
SinOsc s => dac;
fun void run()
{
freq => s.freq;
0.2 => s.gain;
2::second => now;
0.0 => s.gain;
}
}
and then use the Loops class to do the looping
Play playA;
220.0 => playA.s.freq;
Play playC;
130.81278 => playC.s.freq;
spork ~ Loops.loop(playA, 2::second, 10).run();
spork ~ Loops.loop(playC, 1::second, 2::second, 10).run();
michael
More information about the chuck-users
mailing list