Include a header file required for build on mac 10.4
[supercollider.git] / Help / 3vs2 / Spawning.html
blobf734b31faafdfb2421d57ecfaa7469b9300d32d8
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="949.43">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0000bf}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
19 p.p10 {margin: 0.0px 0.0px 0.0px 10.0px; font: 9.0px Monaco}
20 p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0000bf}
21 p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000; min-height: 12.0px}
22 span.s1 {font: 12.0px Helvetica; color: #000000}
23 span.s2 {color: #000000}
24 span.s3 {color: #0000bf}
25 span.s4 {color: #606060}
26 span.s5 {color: #bf0000}
27 span.s6 {color: #0000bf}
28 span.s7 {font: 12.0px Helvetica}
29 span.s8 {color: #007300}
30 span.Apple-tab-span {white-space:pre}
31 </style>
32 </head>
33 <body>
34 <p class="p1"><b>"Spawning" and "TSpawning"</b></p>
35 <p class="p2"><br></p>
36 <p class="p3">In SC2, Spawn and TSpawn were two of the most powerful and commonly used UGens. In SC3 the idea of a top level Synth in which everything is spawned is no longer valid. Synthesis is always running (at least as long as a server is) and new Synths can be created on the fly. This arrangement results in even greater flexibility than in SC2, but requires a slightly different approach.</p>
37 <p class="p2"><br></p>
38 <p class="p3">In SC3 one can create Synths at any time simply by executing blocks of code.</p>
39 <p class="p2"><br></p>
40 <p class="p4">// do this</p>
41 <p class="p5"><span class="Apple-tab-span"> </span>(</p>
42 <p class="p6"><span class="s1"><span class="Apple-tab-span"> </span></span><span class="s2">x = </span><span class="s3">SynthDef</span><span class="s2">(</span>"Help-SynthDef"<span class="s2">,<span class="Apple-converted-space"> </span></span></p>
43 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>{ <span class="s3">arg</span> out=0;</p>
44 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">Out</span>.ar(out, <span class="s3">PinkNoise</span>.ar(0.1))</p>
45 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>}).play; </span>// SynthDef-play returns a Synth object.</p>
46 <p class="p5"><span class="Apple-tab-span"> </span>)<span class="Apple-converted-space"> </span></p>
47 <p class="p7"><br></p>
48 <p class="p4">// then do this</p>
49 <p class="p5"><span class="Apple-tab-span"> </span>(</p>
50 <p class="p5"><span class="Apple-tab-span"> </span><span class="s3">SynthDef</span>(<span class="s4">"help-Rand"</span>, { <span class="s3">arg</span> out=0;</p>
51 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">Out</span>.ar(out,<span class="Apple-converted-space"> </span></p>
52 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">FSinOsc</span>.ar(</p>
53 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s3">Rand</span><span class="s2">(200.0, 400.0), <span class="Apple-tab-span"> </span></span>// frequency between 200 and 400 Hz</p>
54 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>0, <span class="s3">Line</span>.kr(0.2, 0, 1,<span class="Apple-converted-space"> </span></p>
55 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>doneAction:2)) <span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s5">// frees itself</span></p>
56 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>)</p>
57 <p class="p5"><span class="Apple-tab-span"> </span>}).play(s);</p>
58 <p class="p5"><span class="Apple-tab-span"> </span>)</p>
59 <p class="p7"><span class="Apple-tab-span"> </span></p>
60 <p class="p5"><span class="Apple-tab-span"> </span>x.free;</p>
61 <p class="p2"><br></p>
62 <p class="p3">Clocks, such as <b>SystemClock</b>, provide a way to schedule things at arbitrary points in the future. This is similar to Synth.sched in SC2.</p>
63 <p class="p2"><br></p>
64 <p class="p8"><span class="Apple-tab-span"> </span>(</p>
65 <p class="p5"><span class="Apple-tab-span"> </span><span class="s3">SystemClock</span>.sched(2.0,{<span class="Apple-converted-space"> </span></p>
66 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s4">"2.0 seconds later"</span><span class="s2">.postln; </span>// this could be any code, including Synth creation</p>
67 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s3">nil </span>// this means don't repeat<span class="s2"><span class="Apple-converted-space"> </span></span></p>
68 <p class="p5"><span class="Apple-tab-span"> </span>});</p>
69 <p class="p5"><span class="Apple-tab-span"> </span>)</p>
70 <p class="p2"><br></p>
71 <p class="p3">In SC3 time-based sequences of events can be implemented using Routines. A <b>Routine</b> which yields a number can be scheduled using a clock:</p>
72 <p class="p2"><br></p>
73 <p class="p5"><span class="Apple-tab-span"> </span>(</p>
74 <p class="p5"><span class="Apple-tab-span"> </span><span class="s6">var</span> w, r;</p>
75 <p class="p5"><span class="Apple-tab-span"> </span>w = <span class="s6">Window</span>.new(<span class="s4">"trem"</span>, <span class="s6">Rect</span>(512, 256, 360, 130));</p>
76 <p class="p5"><span class="Apple-tab-span"> </span>w.front;</p>
77 <p class="p5"><span class="Apple-tab-span"> </span>r = <span class="s6">Routine</span>({ <span class="s6">arg</span> time;</p>
78 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>60.do({ <span class="s6">arg</span> i;</p>
79 <p class="p9"><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>0.05.yield; </span>// wait for 0.05 seconds</p>
80 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>{</p>
81 <p class="p10"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>w.bounds = w.bounds.moveBy(10.rand2, 10.rand2);</p>
82 <p class="p10"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>w.alpha = cos(i*0.1pi)*0.5+0.5;</p>
83 <p class="p10"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>}.defer;</p>
84 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>});</p>
85 <p class="p9"><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>1.yield; </span>// wait for 1 second before closing w</p>
86 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>w.close;</p>
87 <p class="p5"><span class="Apple-tab-span"> </span>});</p>
88 <p class="p11"><span class="s2"><span class="Apple-tab-span"> </span></span>SystemClock<span class="s2">.play(r);</span></p>
89 <p class="p5"><span class="Apple-tab-span"> </span>)</p>
90 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
91 <p class="p3">Note that this implementation avoids one of the stranger aspects of the SC2 approach: The need to start a Synth to schedule time-based behavior, even if no audio is involved.</p>
92 <p class="p2"><br></p>
93 <p class="p3">Both <b>SystemClock</b> and <b>AppClock</b> (a less accurate version which can call Cocoa primitives) have only class methods. Thus one does not create instances of them. If you need to have an individual clock to manipulate (for instance to manipulate the tempi of different sequences of events) you can use <b>TempoClock</b>.</p>
94 <p class="p2"><br></p>
95 <p class="p3">A simple SC2 Spawn example is shown below, followed by its translation into SC3 style code.</p>
96 <p class="p2"><br></p>
97 <p class="p4">// This will not execute in SC3</p>
98 <p class="p5"><span class="Apple-tab-span"> </span>(</p>
99 <p class="p7"><br></p>
100 <p class="p5"><span class="Apple-tab-span"> </span><span class="s3">Synth</span>.play({</p>
101 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">Spawn</span>.ar({<span class="Apple-converted-space"> </span></p>
102 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">EnvGen</span>.ar(Env.perc) * <span class="s3">SinOsc</span>.ar(440,0,0.1)</p>
103 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>},<span class="Apple-converted-space"> </span></p>
104 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>1, </span>// one channels</p>
105 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>1) </span>// new event every second</p>
106 <p class="p5"><span class="Apple-tab-span"> </span>}))</p>
107 <p class="p7"><span class="Apple-tab-span"> </span></p>
108 <p class="p4">// The same example in SC3 (will execute)</p>
109 <p class="p7"><br></p>
110 <p class="p5"><span class="Apple-tab-span"> </span>s = <span class="s3">Server</span>.default;</p>
111 <p class="p5"><span class="Apple-tab-span"> </span>s.boot;</p>
112 <p class="p5"><span class="Apple-tab-span"> </span>(</p>
113 <p class="p6"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s3">SynthDef</span><span class="s2">(</span>"help-EnvGen"<span class="s2">,{ </span><span class="s3">arg</span><span class="s2"> out=0;</span></p>
114 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">Out</span>.ar(out,</p>
115 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">EnvGen</span>.kr(<span class="s3">Env</span>.perc,1.0,doneAction: 2)<span class="Apple-converted-space"> </span></p>
116 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>* <span class="s3">SinOsc</span>.ar(440,0,0.1)</p>
117 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>)</p>
118 <p class="p5"><span class="Apple-tab-span"> </span>}).send(s);</p>
119 <p class="p5"><span class="Apple-tab-span"> </span>)</p>
120 <p class="p7"><span class="Apple-tab-span"> </span></p>
121 <p class="p5"><span class="Apple-tab-span"> </span>(</p>
122 <p class="p5"><span class="Apple-tab-span"> </span>r = <span class="s3">Routine</span>.new({ { <span class="s3">Synth</span>.new(<span class="s4">"help-EnvGen"</span>); 1.yield; }.loop }); <span class="s5">// loop every one second</span></p>
123 <p class="p8"><span class="s2"><span class="Apple-tab-span"> </span></span>SystemClock<span class="s2">.play(r);</span></p>
124 <p class="p5"><span class="Apple-tab-span"> </span>)<span class="Apple-tab-span"> </span></p>
125 <p class="p7"><span class="Apple-tab-span"> </span></p>
126 <p class="p3">Note that the above example uses a precompiled <b>SynthDef</b>. This results in a lower CPU spike when Synths are created than SC2-style Spawning. It is possible to create SynthDefs on the fly, if this is necessary, but a great deal of variation can be achieved with arguments, or with ugens such as <b>Rand</b> and <b>TRand</b>. See the helpfile <b>SynthDefsVsSynths </b>for more detail.</p>
127 <p class="p2"><br></p>
128 <p class="p4">// SynthDefs on the fly</p>
129 <p class="p7"><br></p>
130 <p class="p5"><span class="Apple-tab-span"> </span>s = <span class="s3">Server</span>.default;</p>
131 <p class="p5"><span class="Apple-tab-span"> </span>s.boot;</p>
132 <p class="p5"><span class="Apple-tab-span"> </span>(</p>
133 <p class="p5"><span class="Apple-tab-span"> </span>t = <span class="s3">TempoClock</span>.new;</p>
134 <p class="p5"><span class="Apple-tab-span"> </span>r = <span class="s3">Routine</span>.new({<span class="Apple-converted-space"> </span></p>
135 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>10.do({</p>
136 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// could be done with an argument instead of a new def, but proves the point</p>
137 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">SynthDef</span>(<span class="s4">"help-EnvGen"</span> ++ i,{ <span class="s3">arg</span> out=0;</p>
138 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">Out</span>.ar(out,</p>
139 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">EnvGen</span>.kr(<span class="s3">Env</span>.perc,1.0,doneAction: 2)<span class="Apple-converted-space"> </span></p>
140 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>* <span class="s3">SinOsc</span>.ar(100 + (100 * t.elapsedBeats),0,0.1)</p>
141 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>)</p>
142 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>}).play(s);</p>
143 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>1.yield;</p>
144 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>});<span class="Apple-converted-space"> </span></p>
145 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span>}).play(t); </span>// Note the alternative syntax: Routine.play(aClock)</p>
146 <p class="p5"><span class="Apple-tab-span"> </span>)</p>
147 <p class="p7"><span class="Apple-tab-span"> </span></p>
148 <p class="p3">Note the alternative syntax for playing a Routine. aClock.play(aRoutine) and aRoutine.play(aClock) are functionally equivalent. The two make different things more or less convienent, like sending messages to the <b>Routine</b> or <b>Clock</b>. (See the <b>play</b> helpfile for a more detailed discussion.) For instance:</p>
149 <p class="p2"><br></p>
150 <p class="p5"><span class="s7"><span class="Apple-tab-span"> </span></span>(</p>
151 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span></span>// this, that and the other</p>
152 <p class="p5"><span class="Apple-tab-span"> </span>r = <span class="s3">Routine</span>.new({<span class="s3">var</span> i = 0; { (<span class="s4">"this: "</span> ++ i).postln; i = i + 1; 1.yield; }.loop });</p>
153 <p class="p5"><span class="Apple-tab-span"> </span>q = <span class="s3">Routine</span>.new({<span class="s3">var</span> i = 0; { (<span class="s4">"that: "</span> ++ i).postln; i = i + 1; 1.yield; }.loop });<span class="Apple-converted-space"> </span></p>
154 <p class="p5"><span class="s5"><span class="Apple-tab-span"> </span></span>t = <span class="s3">Routine</span>.new({<span class="s3">var</span> i = 0; { (<span class="s4">"the other: "</span> ++ i).postln; i = i + 1; 1.yield; }.loop });</p>
155 <p class="p5"><span class="Apple-tab-span"> </span>)</p>
156 <p class="p7"><span class="Apple-tab-span"> </span></p>
157 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s3">SystemClock</span><span class="s2">.play(r); <span class="Apple-tab-span"> </span></span>// start this</p>
158 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s3">SystemClock</span><span class="s2">.play(q); <span class="Apple-tab-span"> </span></span>// start that</p>
159 <p class="p4"><span class="Apple-tab-span"> </span><span class="s3">SystemClock</span><span class="s2">.play(t); <span class="Apple-tab-span"> </span></span>// start the other</p>
160 <p class="p7"><span class="Apple-tab-span"> </span></p>
161 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span>r.stop; <span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// stop this but not that or the other</p>
162 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span>q.reset;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// reset that while playing</p>
163 <p class="p12"><span class="Apple-tab-span"> </span></p>
164 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span>c = </span><span class="s3">TempoClock</span><span class="s2">.new;<span class="Apple-tab-span"> </span></span>// make a TempoClock</p>
165 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span>r.reset;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// have to reset this because it's stopped</p>
166 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span>c.play(r);<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// play this in the new clock; starts from the beginning</p>
167 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span>c.tempo = 16;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// increase the tempo of this</p>
168 <p class="p12"><span class="Apple-tab-span"> </span></p>
169 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s3">SystemClock</span><span class="s2">.clear; <span class="Apple-tab-span"> </span></span>// clear EVERYTHING scheduled in the SystemClock; so that and the other<span class="Apple-converted-space"> </span></p>
170 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>// but not this</p>
171 <p class="p12"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
172 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span>c.clear;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// clear everything scheduled in c, i.e. this</p>
173 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span>c.play(r);<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// since it wasn't stopped, we don't have to reset this</p>
174 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>// and it picks up where it left off</p>
175 <p class="p12"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
176 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span>c.stop<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// stop c, destroy its scheduler, and release its OS thread</p>
177 <p class="p2"><br></p>
178 <p class="p3">For convenience pauseable scheduling can be implemented with a <b>Task</b>. Task.new takes two arguments, a function and a clock, and creates it's own <b>Routine</b>. If you don't specify a clock, it will create a <b>TempoClock</b> for you. Since you don't have to explicitly create a Clock or Routine, use of Task can result in code that is a little more compact.</p>
179 <p class="p2"><br></p>
180 <p class="p5"><span class="s7"><span class="Apple-tab-span"> </span></span>(</p>
181 <p class="p5"><span class="Apple-tab-span"> </span>t = <span class="s3">Task.</span>new({<span class="Apple-converted-space"> </span></p>
182 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">inf</span>.do({ <span class="s3">arg</span> i;</p>
183 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>i.postln;<span class="Apple-converted-space"> </span></p>
184 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>0.5.wait<span class="Apple-converted-space"> </span></p>
185 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>});<span class="Apple-converted-space"> </span></p>
186 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>});</p>
187 <p class="p5"><span class="Apple-tab-span"> </span>)</p>
188 <p class="p7"><span class="Apple-tab-span"> </span></p>
189 <p class="p5"><span class="Apple-tab-span"> </span>t.start; <span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s5">// Start it</span></p>
190 <p class="p5"><span class="Apple-tab-span"> </span>t.stop;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s5">// Stop it</span></p>
191 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span>t.start;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// Start again from the beginning</p>
192 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span>t.reset;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// Reset on the fly</p>
193 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span>t.stop;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// Stop again</p>
194 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span>t.resume;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// Restart from where you left off</p>
195 <p class="p4"><span class="Apple-tab-span"> </span><span class="s2">t.clock.tempo = 0.25;</span><span class="Apple-tab-span"> </span>// Get the Task's clock and change the tempo. This works since the</p>
196 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>// default is a TempoClock.</p>
197 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span>t.pause;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// Same as t.stop</p>
198 <p class="p2"><br></p>
199 <p class="p2"><br></p>
200 <p class="p3">TSpawn's functionality can be replicated with <b>SendTrig</b> and <b>OSCresponder</b> or <b>OSCresponderNode</b>. See their individual helpfiles for details on their arguments and functionality.</p>
201 <p class="p2"><br></p>
202 <p class="p5"><span class="s7"><span class="Apple-tab-span"> </span></span>s = <span class="s3">Server</span>.default;</p>
203 <p class="p5"><span class="Apple-tab-span"> </span>s.boot;</p>
204 <p class="p7"><span class="Apple-tab-span"> </span></p>
205 <p class="p5"><span class="Apple-tab-span"> </span>(</p>
206 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span></span>// this Synth will send a trigger to the client app</p>
207 <p class="p6"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s3">SynthDef</span><span class="s2">(</span>"help-SendTrig"<span class="s2">,{</span></p>
208 <p class="p8"><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>SendTrig<span class="s2">.kr(</span></p>
209 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s3">Dust</span><span class="s2">.kr(1.0), </span>// trigger could be anything, e.g. Amplitude.kr(SoundIn.ar(0) &gt; 0.5)</p>
210 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>0,0.9</p>
211 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>);<span class="Apple-converted-space"> </span></p>
212 <p class="p5"><span class="Apple-tab-span"> </span>}).send(s);</p>
213 <p class="p5"><span class="Apple-tab-span"> </span>)</p>
214 <p class="p7"><span class="Apple-tab-span"> </span></p>
215 <p class="p5"><span class="Apple-tab-span"> </span>(</p>
216 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span></span>// this recieves the trigger on the client side and 'Spawns' a new Synth on the server</p>
217 <p class="p5"><span class="Apple-tab-span"> </span><span class="s3">OSCresponder</span>(s.addr,<span class="s8">'/tr'</span>,{<span class="Apple-converted-space"> </span></p>
218 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">SynthDef</span>(<span class="s4">"help-EnvGen"</span>,{ <span class="s3">arg</span> out=0;</p>
219 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">Out</span>.ar(out,</p>
220 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">EnvGen</span>.kr(<span class="s3">Env</span>.perc,1.0,doneAction: 2)<span class="Apple-converted-space"> </span></p>
221 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>* <span class="s3">SinOsc</span>.ar(440,0,0.1)</p>
222 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>)</p>
223 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>}).play(s);</p>
224 <p class="p5"><span class="Apple-tab-span"> </span>}).add;</p>
225 <p class="p7"><span class="Apple-tab-span"> </span></p>
226 <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span></span>// Start 'spawning'</p>
227 <p class="p6"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s3">Synth</span><span class="s2">(</span>"help-SendTrig"<span class="s2">);</span></p>
228 <p class="p5"><span class="Apple-tab-span"> </span>)</p>
229 </body>
230 </html>