1 <!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
4 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8">
5 <meta http-equiv=
"Content-Style-Type" content=
"text/css">
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
}
14 p
.p5
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #606060}
15 p
.p6
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; min-height: 12.0px}
16 p
.p7
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #ae1a19}
17 p
.p8
{margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Helvetica
}
18 p
.p9
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #0016bd}
19 p
.p10
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco
; min-height: 16.0px}
20 span
.s1
{color: #184dd8}
21 span
.s2
{color: #0016bd}
22 span
.s3
{color: #000000}
23 span
.s4
{color: #606060}
24 span
.s5
{color: #2553d3}
25 span
.s6
{color: #0042e7}
26 span
.s7
{color: #2b7000}
27 span
.s8
{color: #ae1a19}
28 span
.s9
{text-decoration: underline
; color: #001bec}
29 span
.Apple-tab-span
{white-space:pre
}
33 <p class=
"p1"><b>Routine
</b></p>
34 <p class=
"p2"><br></p>
35 <p class=
"p3"><b>Superclass: Thread
</b></p>
36 <p class=
"p2"><br></p>
37 <p class=
"p3">Routines are functions that can return in the middle and then resume where
<span class=
"Apple-converted-space"> </span></p>
38 <p class=
"p3">they left off when called again. Routines can be used to implement co-routines
</p>
39 <p class=
"p3">as found in Scheme and some other languages.
</p>
40 <p class=
"p3">Routines are useful for writing things that behave like Streams.
</p>
41 <p class=
"p3">Routines inherit behaviour for math operations and filtering from
<a href=
"../../Streams-Patterns-Events/Stream.html"><span class=
"s1">Stream
</span></a>.
</p>
42 <p class=
"p2"><br></p>
43 <p class=
"p3"><b>*new(func, stackSize, seed)
</b></p>
44 <p class=
"p2"><br></p>
45 <p class=
"p3">Creates a Routine instance with the given function.
</p>
46 <p class=
"p3">The stackSize and random seed may be overridden if desired.
</p>
47 <p class=
"p2"><br></p>
49 <p class=
"p4">a =
<span class=
"s2">Routine
</span>.new({
1.yield;
2.yield; });
</p>
50 <p class=
"p4">a.next.postln;
</p>
51 <p class=
"p4">a.next.postln;
</p>
52 <p class=
"p4">a.next.postln;
</p>
54 <p class=
"p2"><br></p>
55 <p class=
"p2"><br></p>
56 <p class=
"p3"><b>value(inval)
</b></p>
57 <p class=
"p3"><b>resume(inval)
</b></p>
58 <p class=
"p3"><b>next(inval)
</b></p>
59 <p class=
"p2"><br></p>
60 <p class=
"p3">These are all synonyms for the same method.
</p>
61 <p class=
"p2"><br></p>
62 <p class=
"p3">The Routine function is either started if it has not been called yet, or it is
<span class=
"Apple-converted-space"> </span></p>
63 <p class=
"p3">resumed from where it left off. The argument inval is passed as the argument
</p>
64 <p class=
"p3">to the Routine function if it is being started, or as the result of the yield
</p>
65 <p class=
"p3">method if it is being resumed from a yield. The result of the method will be
</p>
66 <p class=
"p3">what the Routine yields.
</p>
67 <p class=
"p2"><br></p>
68 <p class=
"p3">There are basically
2 conditions for a Routine: one is when the routine starts. The other case is
<span class=
"Apple-converted-space"> </span></p>
69 <p class=
"p3">that the routine continues after it has yielded.
<span class=
"Apple-converted-space"> </span></p>
70 <p class=
"p2"><br></p>
71 <p class=
"p3">When the routine starts (by calling the above methods), you are passing in a first inval.
<span class=
"Apple-converted-space"> </span></p>
72 <p class=
"p3">This inval is accessible as the routine function argument:
</p>
73 <p class=
"p2"><br></p>
75 <p class=
"p4"><span class=
"s2">Routine
</span> {
<span class=
"s2">arg
</span> inval;
</p>
76 <p class=
"p4"><span class=
"Apple-tab-span"> </span>inval.postln;
</p>
77 <p class=
"p5"><span class=
"s3">}.value(
</span>"hello routine"<span class=
"s3">);
</span></p>
79 <p class=
"p2"><br></p>
80 <p class=
"p3">When there is a yield in the routine, the next time you call
<i>next
</i> (or synonym), the routine continues
</p>
81 <p class=
"p3">from there, and you get a chance to pass in a value from the outside. To access that value within the
</p>
82 <p class=
"p3">continuing routine, you have to assign the
<b><i>result of the yield call
</i></b> to a variable:
</p>
83 <p class=
"p2"><br></p>
85 <p class=
"p4">r =
<span class=
"s2">Routine
</span> {
<span class=
"s2">arg
</span> inval;
</p>
86 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"s2">var
</span> valuePassedInbyYield;
</p>
87 <p class=
"p4"><span class=
"Apple-tab-span"> </span>inval.postln;
</p>
88 <p class=
"p4"><span class=
"Apple-tab-span"> </span>valuePassedInbyYield =
123.yield;
</p>
89 <p class=
"p4"><span class=
"Apple-tab-span"> </span>valuePassedInbyYield.postln;
</p>
90 <p class=
"p6"><span class=
"Apple-tab-span"> </span></p>
93 <p class=
"p6"><br></p>
94 <p class=
"p5"><span class=
"s3">r.value(
</span>"hello routine"<span class=
"s3">);
</span></p>
95 <p class=
"p5"><span class=
"s3">r.value(
</span>"goodbye world"<span class=
"s3">);
</span></p>
96 <p class=
"p2"><br></p>
97 <p class=
"p2"><br></p>
98 <p class=
"p3">Typically the name
<i>inval
</i> (or inevent) is reused, instead of declaring a variable like
"valuePassedInbyYield":
</p>
99 <p class=
"p2"><br></p>
101 <p class=
"p4">r =
<span class=
"s2">Routine
</span> {
<span class=
"s2">arg
</span> inval;
</p>
102 <p class=
"p4"><span class=
"Apple-tab-span"> </span>inval.postln;
</p>
103 <p class=
"p4"><span class=
"Apple-tab-span"> </span>inval =
123.yield;
</p>
104 <p class=
"p4"><span class=
"Apple-tab-span"> </span>inval.postln;
</p>
107 <p class=
"p6"><br></p>
108 <p class=
"p5"><span class=
"s3">r.value(
</span>"hello routine"<span class=
"s3">);
</span></p>
109 <p class=
"p5"><span class=
"s3">r.value(
</span>"goodbye world"<span class=
"s3">);
</span></p>
110 <p class=
"p2"><br></p>
111 <p class=
"p2"><br></p>
112 <p class=
"p3">Typically a routine uses a multiple yield, in which the inval is reassigned repeatedly:
</p>
113 <p class=
"p2"><br></p>
115 <p class=
"p4">r =
<span class=
"s2">Routine
</span> {
<span class=
"s2">arg
</span> inval;
</p>
116 <p class=
"p4"><span class=
"Apple-tab-span"> </span>inval.postln;
</p>
117 <p class=
"p4"><span class=
"Apple-tab-span"> </span>5.do {
<span class=
"s2">arg
</span> i;
</p>
118 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>inval = (i +
10).yield;
</p>
119 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>inval.postln;
</p>
120 <p class=
"p4"><span class=
"Apple-tab-span"> </span>}
</p>
124 <p class=
"p4">5.do {
</p>
125 <p class=
"p4"><span class=
"Apple-tab-span"> </span>r.value(
<span class=
"s4">"hello routine"</span>).postln;
</p>
128 <p class=
"p2"><br></p>
129 <p class=
"p2"><br></p>
130 <p class=
"p2"><br></p>
131 <p class=
"p3"><b>reset
</b></p>
132 <p class=
"p2"><br></p>
133 <p class=
"p3">Causes the Routine to start from the beginning next time it is called.
</p>
134 <p class=
"p3">A Routine cannot reset itself except by calling the yieldAndReset method.
</p>
135 <p class=
"p2"><br></p>
136 <p class=
"p3">See also in class
<b>Object :
</b></p>
137 <p class=
"p3"><b>yield(outval)
</b></p>
138 <p class=
"p3"><b>yieldAndReset(outval)
</b></p>
139 <p class=
"p3"><b>alwaysYield(outval)
</b></p>
140 <p class=
"p2"><br></p>
141 <p class=
"p3">If a Routine's function returns then it will always yield nil until reset.
</p>
142 <p class=
"p2"><br></p>
143 <p class=
"p2"><br></p>
144 <p class=
"p2"><br></p>
145 <p class=
"p2"><br></p>
146 <p class=
"p3"><b>play(clock, quant)
</b></p>
147 <p class=
"p3"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><b>clock
</b>:
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>a Clock,
<b>TempoClock
</b> by default
</p>
148 <p class=
"p3"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><b>quant
</b>:
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>see the
<a href=
"../../Scheduling/Quant.html"><span class=
"s5">Quant
</span></a> helpfile
</p>
149 <p class=
"p2"><br></p>
150 <p class=
"p3">In the SuperCollider application, a Routine can be played using a Clock, as can any Stream.
</p>
151 <p class=
"p3">every time the Routine yields, it should do so with a float, the clock will interpret that, usually
</p>
152 <p class=
"p3">pausing for that many seconds, and then resume the routine, passing it it the clock's current time.
</p>
153 <p class=
"p2"><br></p>
154 <p class=
"p2"><br></p>
155 <p class=
"p3"><b>using Object-idle:
<span class=
"Apple-converted-space"> </span></b></p>
156 <p class=
"p2"><br></p>
157 <p class=
"p3"><b>idle(time)
</b></p>
158 <p class=
"p3"><span class=
"Apple-tab-span"> </span>within a routine, return values until this time is over. Time is measured relative to the thread's clock.
</p>
159 <p class=
"p3"><span class=
"Apple-tab-span"> </span>(see
<a href=
"Routine.html"><span class=
"s6">Routine
</span></a>)
</p>
160 <p class=
"p6"><span class=
"Apple-tab-span"> </span></p>
161 <p class=
"p7"><span class=
"s3"><span class=
"Apple-tab-span"> </span></span>// for
6 seconds, return
200, then continue
</p>
162 <p class=
"p4"><span class=
"Apple-tab-span"> </span>(
</p>
163 <p class=
"p4"><span class=
"Apple-tab-span"> </span>r =
<span class=
"s2">Routine
</span> {
</p>
164 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>199.yield;
</p>
165 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>189.yield;
</p>
166 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>200.idle(
6);
</p>
167 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>199.yield;
</p>
168 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>189.yield;
</p>
169 <p class=
"p4"><span class=
"Apple-tab-span"> </span>};
</p>
170 <p class=
"p6"><span class=
"Apple-tab-span"> </span></p>
171 <p class=
"p4"><span class=
"Apple-tab-span"> </span>fork {
</p>
172 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>loop {
</p>
173 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>r.value.postln;
</p>
174 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>1.wait;
</p>
175 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>}
</p>
176 <p class=
"p4"><span class=
"Apple-tab-span"> </span>}
</p>
177 <p class=
"p4"><span class=
"Apple-tab-span"> </span>);
</p>
178 <p class=
"p6"><span class=
"Apple-tab-span"> </span></p>
179 <p class=
"p7"><span class=
"s3"><span class=
"Apple-tab-span"> </span></span>// the value can also be a stream or a function
</p>
180 <p class=
"p4"><span class=
"Apple-tab-span"> </span>(
</p>
181 <p class=
"p4"><span class=
"Apple-tab-span"> </span>r =
<span class=
"s2">Routine
</span> {
</p>
182 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>199.yield;
</p>
183 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>189.yield;
</p>
184 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"s2">Routine
</span> {
100.do {
<span class=
"s2">|i|
</span> i.yield } }.idle(
6);
</p>
185 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>199.yield;
</p>
186 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>189.yield;
</p>
187 <p class=
"p4"><span class=
"Apple-tab-span"> </span>};
</p>
188 <p class=
"p6"><span class=
"Apple-tab-span"> </span></p>
189 <p class=
"p4"><span class=
"Apple-tab-span"> </span>fork {
</p>
190 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>loop {
</p>
191 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>r.value.postln;
</p>
192 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>1.wait;
</p>
193 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>}
</p>
194 <p class=
"p4"><span class=
"Apple-tab-span"> </span>}
</p>
195 <p class=
"p4"><span class=
"Apple-tab-span"> </span>);
</p>
196 <p class=
"p6"><span class=
"Apple-tab-span"> </span></p>
197 <p class=
"p2"><br></p>
198 <p class=
"p2"><br></p>
199 <p class=
"p8"><b>Accessible instance variables
</b></p>
200 <p class=
"p2"><br></p>
201 <p class=
"p2"><br></p>
202 <p class=
"p2"><br></p>
203 <p class=
"p3"><b><i>Routine
</i> inherits from
<i>Thread
</i>, which allows access to some of its state:
</b></p>
204 <p class=
"p2"><br></p>
205 <p class=
"p2"><br></p>
206 <p class=
"p3"><b>beats
</b></p>
207 <p class=
"p3"><span class=
"Apple-tab-span"> </span>return the elapsed beats (logical time) of the routine. The beats do not proceed when the routine is
</p>
208 <p class=
"p3"><span class=
"Apple-tab-span"> </span>not playing.
</p>
209 <p class=
"p2"><b><span class=
"Apple-tab-span"> </span></b></p>
210 <p class=
"p3"><b>seconds
</b></p>
211 <p class=
"p3"><b><span class=
"Apple-tab-span"> </span></b>return the elapsed seconds (logical time) of the routine. The seconds do not proceed when the routine is
</p>
212 <p class=
"p3"><span class=
"Apple-tab-span"> </span>not playing, it is the converted beat value.
</p>
213 <p class=
"p2"><br></p>
214 <p class=
"p3"><b>clock
</b></p>
215 <p class=
"p3"><span class=
"Apple-tab-span"> </span>return the thread's clock. If it has not played, it is the SystemClock.
</p>
216 <p class=
"p2"><span class=
"Apple-tab-span"> </span></p>
217 <p class=
"p2"><br></p>
218 <p class=
"p2"><br></p>
220 <p class=
"p4">r =
<span class=
"s2">Routine
</span> {
<span class=
"s2">arg
</span> inval;
</p>
221 <p class=
"p4"><span class=
"Apple-tab-span"> </span>loop {
</p>
222 <p class=
"p7"><span class=
"s3"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>// thisThread refers to the routine.
</p>
223 <p class=
"p5"><span class=
"s3"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>postf(
</span>"beats: % seconds: % time: % \n"<span class=
"s3">,
<span class=
"Apple-converted-space"> </span></span></p>
224 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"s2">thisThread
</span>.beats,
<span class=
"s2">thisThread
</span>.seconds,
<span class=
"s2">Main
</span>.elapsedTime
</p>
225 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>);
</p>
226 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>1.0.yield;
</p>
227 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></p>
228 <p class=
"p4"><span class=
"Apple-tab-span"> </span>}
</p>
229 <p class=
"p4">}.play;
</p>
231 <p class=
"p6"><br></p>
232 <p class=
"p4">r.stop;
</p>
233 <p class=
"p4">r.beats;
</p>
234 <p class=
"p4">r.seconds;
</p>
235 <p class=
"p4">r.clock;
</p>
236 <p class=
"p2"><br></p>
237 <p class=
"p2"><br></p>
238 <p class=
"p2"><br></p>
239 <p class=
"p2"><br></p>
240 <p class=
"p3"><b>Routine Example:
</b></p>
241 <p class=
"p2"><br></p>
243 <p class=
"p4"><span class=
"s2">var
</span> r, outval;
</p>
244 <p class=
"p4">r =
<span class=
"s2">Routine
</span>.new({
<span class=
"s2">arg
</span> inval;
</p>
245 <p class=
"p4"><span class=
"Apple-tab-span"> </span>(
<span class=
"s4">"->inval was "</span> ++ inval).postln;
</p>
246 <p class=
"p4"><span class=
"Apple-tab-span"> </span>inval =
1.yield;
</p>
247 <p class=
"p4"><span class=
"Apple-tab-span"> </span>(
<span class=
"s4">"->inval was "</span> ++ inval).postln;
</p>
248 <p class=
"p4"><span class=
"Apple-tab-span"> </span>inval =
2.yield;
</p>
249 <p class=
"p4"><span class=
"Apple-tab-span"> </span>(
<span class=
"s4">"->inval was "</span> ++ inval).postln;
</p>
250 <p class=
"p4"><span class=
"Apple-tab-span"> </span>inval =
99.yield;
</p>
251 <p class=
"p4">});
</p>
252 <p class=
"p6"><br></p>
253 <p class=
"p4">outval = r.next(
<span class=
"s7">'a'
</span>);
</p>
254 <p class=
"p4">(
<span class=
"s4">"<-outval was "</span> ++ outval).postln;
</p>
255 <p class=
"p4">outval = r.next(
<span class=
"s7">'b'
</span>);
</p>
256 <p class=
"p4">(
<span class=
"s4">"<-outval was "</span> ++ outval).postln;
</p>
257 <p class=
"p4">r.reset;
<span class=
"s4">"reset"</span>.postln;
</p>
258 <p class=
"p4">outval = r.next(
<span class=
"s7">'c'
</span>);
</p>
259 <p class=
"p4">(
<span class=
"s4">"<-outval was "</span> ++ outval).postln;
</p>
260 <p class=
"p4">outval = r.next(
<span class=
"s7">'d'
</span>);
</p>
261 <p class=
"p4">(
<span class=
"s4">"<-outval was "</span> ++ outval).postln;
</p>
262 <p class=
"p4">outval = r.next(
<span class=
"s7">'e'
</span>);
</p>
263 <p class=
"p4">(
<span class=
"s4">"<-outval was "</span> ++ outval).postln;
</p>
264 <p class=
"p4">outval = r.next(
<span class=
"s7">'f'
</span>);
</p>
265 <p class=
"p4">(
<span class=
"s4">"<-outval was "</span> ++ outval).postln;
</p>
267 <p class=
"p6"><br></p>
268 <p class=
"p7">// wait
</p>
269 <p class=
"p6"><br></p>
271 <p class=
"p4"><span class=
"s2">var
</span> r;
</p>
272 <p class=
"p9"><span class=
"s3">r =
</span>Routine
<span class=
"s3"> {
</span></p>
273 <p class=
"p4"><span class=
"Apple-tab-span"> </span>10.do({
<span class=
"s2">arg
</span> a;
</p>
274 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>a.postln;
</p>
275 <p class=
"p7"><span class=
"s3"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>// Often you might see Wait being used to pause a routine
</p>
276 <p class=
"p7"><span class=
"s3"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>// This waits for one second between each number
</p>
277 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>1.wait;
</p>
278 <p class=
"p4"><span class=
"Apple-tab-span"> </span>});
</p>
279 <p class=
"p7"><span class=
"s3"><span class=
"Apple-tab-span"> </span></span>// Wait half second before saying we're done
</p>
280 <p class=
"p4"><span class=
"Apple-tab-span"> </span>0.5.wait;
</p>
281 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"s4">"done"</span>.postln;
</p>
282 <p class=
"p4">}.play;
</p>
284 <p class=
"p6"><br></p>
285 <p class=
"p7">// waitUntil
</p>
286 <p class=
"p6"><br></p>
288 <p class=
"p4"><span class=
"s2">var
</span> r;
</p>
289 <p class=
"p9"><span class=
"s3">r =
</span>Routine
<span class=
"s3"> {
</span></p>
290 <p class=
"p4"><span class=
"Apple-tab-span"> </span>var times = { rrand(
1.0,
10.0) }.dup(
10) + thisThread.beats;
</p>
291 <p class=
"p4"><span class=
"Apple-tab-span"> </span>times = times.sort;
</p>
292 <p class=
"p4"><span class=
"Apple-tab-span"> </span>times.do({
<span class=
"s2">arg
</span> a;
</p>
293 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>waitUntil(a);
</p>
294 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>a.postln;
</p>
295 <p class=
"p4"><span class=
"Apple-tab-span"> </span>});
</p>
296 <p class=
"p7"><span class=
"s3"><span class=
"Apple-tab-span"> </span></span>// Wait half second before saying we're done
</p>
297 <p class=
"p4"><span class=
"Apple-tab-span"> </span>0.5.wait;
</p>
298 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"s4">"done"</span>.postln;
</p>
299 <p class=
"p4">}.play;
</p>
301 <p class=
"p6"><br></p>
302 <p class=
"p6"><br></p>
303 <p class=
"p7">// Using Routine to set button states on the fly.
</p>
305 <p class=
"p4"><span class=
"s2">var
</span> update, w, b;
</p>
306 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>w =
<span class=
"s2">SCWindow
</span>.new(
<span class=
"s4">"State Window"</span>,
<span class=
"s2">Rect
</span>(
150,
<span class=
"s2">SCWindow
</span>.screenBounds.height-
140,
380,
60));
</p>
307 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></p>
308 <p class=
"p7"><span class=
"s3"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>// a convenient way to set the button label
</p>
309 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>update = {
<span class=
"Apple-converted-space"> </span></p>
310 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"s2">|but, string|
</span> but.states = [[string.asString,
<span class=
"s2">Color
</span>.black,
<span class=
"s2">Color
</span>.red]];
<span class=
"Apple-converted-space"> </span></p>
311 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>but.refresh;
</p>
312 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>};
</p>
313 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></p>
314 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>b =
<span class=
"s2">SCButton
</span>(w,
<span class=
"s2">Rect
</span>(
10,
10,
360,
40));
</p>
315 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>b.font_(
<span class=
"s2">Font
</span>(
<span class=
"s4">"Impact"</span>,
24));
</p>
316 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></p>
317 <p class=
"p5"><span class=
"s3"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>update.value(b,
</span>"there is only one state"<span class=
"s3">);
</span></p>
318 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></p>
319 <p class=
"p7"><span class=
"s3"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>// if an action should do something different each time it is called, a routine is the
<span class=
"Apple-converted-space"> </span></p>
320 <p class=
"p7"><span class=
"s3"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>// right thing to use. This is better than creating variables outside and setting them
</p>
321 <p class=
"p7"><span class=
"s3"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>// from the action function to keep state from one action to the next
</p>
322 <p class=
"p6"><br></p>
323 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>b.action_(
<span class=
"s2">Routine
</span> {
<span class=
"s2">|butt|
</span></p>
324 <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>rrand(
15,
45).do {
<span class=
"s2">|i|
</span></p>
325 <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><span class=
"Apple-tab-span"> </span>update.value(butt,
<span class=
"s4">"%. there is still only 1 state"</span>.format(i +
2));
</p>
326 <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><span class=
"Apple-tab-span"> </span>0.yield;
<span class=
"s8">// stop here
</span></p>
327 <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>};
</p>
328 <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>w.close;
</p>
329 <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>});
</p>
330 <p class=
"p6"><br></p>
331 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>w.front;
</p>
333 <p class=
"p6"><br></p>
334 <p class=
"p7">// drawing in a window dynamcially with
<a href=
"../../GUI/Main-GUI/Pen.html"><span class=
"s9">Pen
</span></a></p>
336 <p class=
"p4"><span class=
"s2">var
</span> w, much =
0.02, string, synth;
</p>
337 <p class=
"p6"><br></p>
338 <p class=
"p4">w =
<span class=
"s2">Window
</span>.new(
<span class=
"s4">"swing"</span>,
<span class=
"s2">Rect
</span>(
100,
100,
300,
500)).front;
</p>
339 <p class=
"p4">w.view.background_(
<span class=
"s2">Color
</span>.new255(
153,
255,
102).vary);
</p>
340 <p class=
"p6"><br></p>
341 <p class=
"p4">string =
<span class=
"s4">"swing "</span>.dup(
24).join;
</p>
342 <p class=
"p6"><br></p>
343 <p class=
"p4">w.drawHook =
<span class=
"s2">Routine
</span> {
<span class=
"Apple-tab-span"> </span></p>
344 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"s2">var
</span> i =
0;
</p>
345 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"s2">var
</span> size =
40;
</p>
346 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"s2">var
</span> func = {
<span class=
"s2">|i, j|
</span> sin(i *
0.07 + (j *
0.0023) +
1.5pi) * much +
1 };
</p>
347 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"s2">var
</span> scale;
</p>
348 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"s2">Pen
</span>.font =
<span class=
"s2">Font
</span>(
<span class=
"s4">"Helvetica-Bold"</span>,
40);
</p>
349 <p class=
"p4"><span class=
"Apple-tab-span"> </span>loop {
<span class=
"Apple-tab-span"> </span></p>
350 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>i = i +
1;
</p>
351 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>string.do {
<span class=
"Apple-tab-span"> </span><span class=
"s2">|char, j|
</span></p>
352 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></p>
353 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>scale = func.value(i, j).dup(
6);
</p>
354 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></p>
355 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"s2">Pen
</span>.fillColor =
<span class=
"s2">Color
</span>.new255(
0,
120,
120).vary;
</p>
356 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"s2">Pen
</span>.matrix = scale * #[
1,
0,
0,
1,
1,
0];
</p>
357 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"s2">Pen
</span>.stringAtPoint(char.asString,
</p>
358 <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>((size * (j %
9)) -
10) @ (size * (j div:
9))
</p>
359 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>);
</p>
360 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>};
</p>
361 <p class=
"p7"><span class=
"s3"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>0.yield
</span>// stop here, return something unimportant
</p>
362 <p class=
"p4"><span class=
"Apple-tab-span"> </span>}
</p>
364 <p class=
"p6"><br></p>
365 <p class=
"p4">fork { while { w.isClosed.not } { defer { w.refresh };
0.04.wait; } };
</p>
366 <p class=
"p6"><br></p>
367 <p class=
"p4">w.front;
</p>
368 <p class=
"p6"><br></p>
370 <p class=
"p10"><span class=
"Apple-converted-space"> </span></p>