12. Routine/.do

12. Routine

Routine은 function으로 실행을 멈추고 (중간으로 돌아가) 다시 계속할수 있게 해준다.

구문 : (Routine({function})

메세지 yielf, wait이 있는데 초단위의 숫자(SystemClock사용시)나 비트단위(TempoClock사용시)의 숫자를 요구한다.
//——————————————
//ex1)
(
        r = Routine({
                        var counter = 0;
                        { counter = counter + 1;
                          counter.postln;
                          1.yield;//1.wait
                          }.loop;//looo의 사용은 loop가 계속적으로 지속되도록 하기 위함
                          })
)//이렇게 루틴을 만든후 아래와 같이 실행

r.play(SystemClock)
r.stop
r.reset.play(SystemClock)

//ex2)Karplus routine(11장에서 사용한 Karplus사용)
(
SynthDef(“Karplus”,
{
        arg midiPitch = 69, decayTime = 2.0;
        var burstEnv, att = 0, dec = 0.001, signalOut, delayTime;
        delayTime = midiPitch.midicps.reciprocal;
        burstEnv = EnvGen.kr(Env.perc(att, dec));
        signalOut = CombL.ar(WhiteNoise.ar(burstEnv), delayTime, delayTime, decayTime);
        DetectSilence.ar(signalOut, doneAction: 2);
        Out.ar(0,signalOut);
}).load(s)
)

(
r = Routine({
        {        Synth(“Karplus”,[midiPitch, rrand(50,90), delayTime,rrand(2.0,3)]);
        [0.5,0.75,1].choose.wait;
        }.loop;
}).play(SysyemClock)
)

//ex3) Sine Grain
(
SynthDef(“SineGrain”, {
                |out = 0, freq, amp = 1, decay = 0.01|
                var signal;
        
                signal = SinOsc.ar(freq, mul:amp) *
                                EnvGen.kr(Env.perc(0.001,decay,0.2),1,doneAction:2);
                signal = Pan2.ar(signal, Rand(-1.0,1.0));//Rand geberates 1 randon value when SunthDef starts playing
                Out.ar(out,signal)
                }).send(s)
)        
//2가지 방법으로 SineGrain에 메세지를 보내보자.
//1.s_new, synthname, ID, add Action(0 is add to head). Target ( Group)ID
//if ID in s_new message is -1, the server generates an ID and you cannot access it by number
//This is convenient if SynthDef turns off and deallocates the synth.

(
r = Routine({
        { s.sendMsg(s_new, SineGrain, -1,0,0,freq,rrand(5000.0,5100));
                rrand(0.001,0.1).wait;
        }.loop
        }).play(SystemClock)
)
//using Synth
(
r = Routine({
                {Synth(SineGrain,[freq,rrand(1000.0,5100),decay,rrand(0.1,1),amp,rrand(0.05,0.1)]);
                rrand(0.001,0.1).wait;
                }.loop
        }).play(SystemClock)
)

//——————————————
number.do({function});
10.do({|i = 0| i.postln;})

위 구문은 0~10까지를 프린트.
do는 루틴을 여러개 만들때 유용하게 사용된다. 아래는 SineGrain의 레이어를 여러개 만든다.

(
20.do({Routine({
        { Synth(SineGrain,[freq,rrand(1000.0,5100),decay,rrand(0.1,1),amp,rrand(0.05,0.1)]);
                rrand(0.001,0.1).wait;
        }.loop
        }).play(SystemClock)
        })
)

array.do({function});
array의 값은 argument에 할당된다.
[1,3,5].do ({|i| i.postln})
//다음은 루틴의 array를 만든다.
(
a = { Routine({
        { Synth(SineGrain,[freq, rrand(1000.0,5100), decay, rrand(0.1,1), amp, rrand(0.05,0.1)]);
        rrand(0.001,0.1).wait}.loop
        })
        } ! 5;
)
do를 사용하여 play and stop
a.do({|i| i.play(SystemClock)})
a.do({|i| i.stop})
a.do({|i| i.reset.play(SystemClock)})
//——————————————
http://csound.x-y.net
Ji Youn Kang,
Csound Max Community

Leave a Comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.