polymorphism: better names for .binaryValue and .booleanValue are .asInteger and...
[supercollider.git] / Help / Scheduling / Scheduler.html
blob4e92fe8d02193f8826506a6ec66664de738af631
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="824.48">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #982316}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0025b2}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #982316; min-height: 12.0px}
17 span.s1 {font: 18.0px Helvetica}
18 span.s2 {color: #0000ff}
19 span.s3 {font: 9.0px Monaco}
20 span.s4 {color: #000000}
21 span.s5 {color: #606060}
22 span.s6 {color: #0025b2}
23 span.s7 {color: #416d1f}
24 span.Apple-tab-span {white-space:pre}
25 </style>
26 </head>
27 <body>
28 <p class="p1"><span class="s1"><b>Scheduler</b></span><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>schedules functions to be evaluated in the future</b></p>
29 <p class="p2"><br></p>
30 <p class="p2"><br></p>
31 <p class="p1">superclass: Object</p>
32 <p class="p3"><br></p>
33 <p class="p1">A Scheduler can be used to schedule and reschedule functions to be evaluated at a specific time-point. The Scheduler's time needs to be advanced manually. In most cases you will probably want to use a Clock (e.g. <a href="TempoClock.html"><span class="s2">TempoClock</span></a>, <a href="SystemClock.html"><span class="s2">SystemClock</span></a>, <a href="AppClock.html"><span class="s2">AppClock</span></a>) instead, in which the march of time is handled for you.</p>
34 <p class="p3"><br></p>
35 <p class="p3"><br></p>
36 <p class="p1"><b>*new(clock, drift)</b></p>
37 <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><b>clock</b>: <span class="Apple-tab-span"> </span>a clock, like SystemClock.</p>
38 <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><b>drift</b>: <span class="Apple-tab-span"> </span>if true, it will schedule the events relative to Main.elapsedTime,</p>
39 <p class="p1"><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>otherwise to the current seconds of the scheduler.</p>
40 <p class="p3"><span class="Apple-tab-span"> </span></p>
41 <p class="p1"><b>play(aTask)</b></p>
42 <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>schedules the task to play, with the delta time returned from it.</p>
43 <p class="p3"><br></p>
44 <p class="p1"><b>sched(delta, aTask)</b></p>
45 <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>schedule the task<span class="Apple-converted-space"> </span></p>
46 <p class="p3"><br></p>
47 <p class="p1"><b>advance(bySeconds)</b></p>
48 <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>advance time by n seconds. Any task that is scheduled within the new time,</p>
49 <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>is evaluated, and, if it returns a new time, rescheduled.</p>
50 <p class="p3"><br></p>
51 <p class="p1"><b>seconds_(newSeconds)</b></p>
52 <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>set new time. Any task that is scheduled within the new time,</p>
53 <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>is evaluated, and, if it returns a new time, rescheduled.</p>
54 <p class="p3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
55 <p class="p1"><b>isEmpty</b></p>
56 <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>returns whether the scheduling queue is empty.</p>
57 <p class="p3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
58 <p class="p1"><b>clear</b></p>
59 <p class="p1"><span class="s3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>clear the scheduling queue</p>
60 <p class="p2"><br></p>
61 <p class="p2"><br></p>
62 <p class="p2"><br></p>
63 <p class="p4">// example:</p>
64 <p class="p2"><br></p>
65 <p class="p5"><span class="s4">a = </span>Scheduler<span class="s4">(</span>SystemClock<span class="s4">);</span></p>
66 <p class="p2"><br></p>
67 <p class="p6">a.sched(3, { <span class="s5">"now it is 3 seconds."</span>.postln; <span class="s6">nil</span> });</p>
68 <p class="p6">a.sched(5, { <span class="s5">"now it is 5 seconds."</span>.postln; <span class="s6">nil</span> });</p>
69 <p class="p6">a.sched(1, { <span class="s5">"now it is 1 second."</span>.postln; <span class="s6">nil</span> });</p>
70 <p class="p2"><br></p>
71 <p class="p6">a.advance(0.5);</p>
72 <p class="p6">a.advance(0.5);</p>
73 <p class="p6">a.advance(2);</p>
74 <p class="p6">a.advance(2);</p>
75 <p class="p2"><br></p>
76 <p class="p4">// the beats, seconds and clock are passed into the task function:</p>
77 <p class="p6">a.sched(1, { <span class="s6">arg</span> beats, secs, clock; [beats, secs, clock].postln });</p>
78 <p class="p6">a.advance(1);</p>
79 <p class="p2"><br></p>
80 <p class="p4">// the scheduling is relative to "now":</p>
81 <p class="p6">a.sched(3, { <span class="s5">"now it was 3 seconds."</span>.postln });</p>
82 <p class="p6">a.sched(5, { <span class="s5">"now it was 5 seconds."</span>.postln });</p>
83 <p class="p6">a.sched(1, { <span class="s5">"now it was 1 second."</span>.postln });</p>
84 <p class="p2"><br></p>
85 <p class="p6">a.advance(0.5);</p>
86 <p class="p6">a.advance(0.5);</p>
87 <p class="p6">a.advance(2);</p>
88 <p class="p6">a.advance(2);</p>
89 <p class="p2"><br></p>
90 <p class="p4">// play a Routine or a task:</p>
91 <p class="p6">a.play(<span class="s6">Routine</span>5.do { <span class="s6">arg</span> i; i.postln; 1.yield } });</p>
92 <p class="p6">a.advance(0.9);</p>
93 <p class="p2"><br></p>
94 <p class="p2"><br></p>
95 <p class="p2"><br></p>
96 <p class="p4">// scheduling tasks</p>
97 <p class="p7"><br></p>
98 <p class="p2"><br></p>
99 <p class="p6">(</p>
100 <p class="p5"><span class="s4">x = </span>Scheduler<span class="s4">(</span>TempoClock<span class="s4">.default);</span></p>
101 <p class="p2"><br></p>
102 <p class="p5">Task<span class="s4"> {<span class="Apple-converted-space"> </span></span></p>
103 <p class="p6"><span class="Apple-tab-span"> </span><span class="s6">inf</span>.do { <span class="s6">|i|</span></p>
104 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>(<span class="s5">"next "</span> ++ i ++<span class="Apple-converted-space">  </span><span class="s5">" in task."</span> + <span class="s6">Main</span>.elapsedTime).postln;</p>
105 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>0.5.wait;</p>
106 <p class="p6"><span class="Apple-tab-span"> </span>}</p>
107 <p class="p6">}.play(x)</p>
108 <p class="p6">)</p>
109 <p class="p2"><br></p>
110 <p class="p6">x.advance(0.1);</p>
111 <p class="p6">x.seconds;</p>
112 <p class="p6">x.advance(5);</p>
113 <p class="p6">x.seconds;</p>
114 <p class="p2"><br></p>
115 <p class="p6">(</p>
116 <p class="p5">Routine<span class="s4"> {</span></p>
117 <p class="p6"><span class="Apple-tab-span"> </span>loop { x.advance(0.1); 0.1.wait }</p>
118 <p class="p6">}.play;</p>
119 <p class="p6">)</p>
120 <p class="p2"><br></p>
121 <p class="p6">(</p>
122 <p class="p6"><span class="s6">Task</span> { 5.do {</p>
123 <p class="p6"><span class="Apple-tab-span"> </span>x.advance(1);</p>
124 <p class="p6"><span class="Apple-tab-span"> </span>2.0.rand.wait;</p>
125 <p class="p6"><span class="Apple-tab-span"> </span>}</p>
126 <p class="p6">}.play;</p>
127 <p class="p6">)</p>
128 <p class="p2"><br></p>
129 <p class="p6">x.advance(8.1);</p>
130 <p class="p2"><br></p>
131 <p class="p6"><span class="s6">Pbind</span>(<span class="s7">\degree</span>, <span class="s6">Pseries</span>(0, 2, 8), <span class="s7">\dur</span>, 0.25).play(x);</p>
132 <p class="p2"><br></p>
133 <p class="p6">(</p>
134 <p class="p6"><span class="s6">Task</span> { 5.do {</p>
135 <p class="p6"><span class="Apple-tab-span"> </span>x.advance(0.20);</p>
136 <p class="p6"><span class="Apple-tab-span"> </span>1.0.wait;</p>
137 <p class="p6"><span class="Apple-tab-span"> </span>}</p>
138 <p class="p6">}.play;</p>
139 <p class="p6">)</p>
140 <p class="p2"><br></p>
141 </body>
142 </html>