supernova: fixes for boost-1.49 and gcc-4.7
[supercollider.git] / Help / Streams-Patterns-Events / Stream.html
blob91b39a4491b2a8171f1aad1f38c70508ef5d1474
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; color: #0000ff}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Helvetica}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #982316}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #982316; min-height: 12.0px}
20 p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0025b2}
21 p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica; min-height: 17.0px}
22 p.p13 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Helvetica}
23 p.p14 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Helvetica}
24 p.p15 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Helvetica; min-height: 18.0px}
25 p.p16 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #416d1f}
26 span.s1 {color: #000000}
27 span.s2 {font: 12.0px Helvetica}
28 span.s3 {color: #0025b2}
29 span.s4 {color: #606060}
30 span.s5 {font: 12.0px Helvetica; color: #000000}
31 span.Apple-tab-span {white-space:pre}
32 </style>
33 </head>
34 <body>
35 <p class="p1"><b>Stream</b></p>
36 <p class="p2"><br></p>
37 <p class="p3"><span class="s1"><b>Inherits from: </b><a href="../Core/Object.html"><b>Object</b></a></span><b> </b><span class="s1"><b>: </b><a href="../Core/Kernel/AbstractFunction.html"><b>AbstractFunction</b></a></span></p>
38 <p class="p2"><br></p>
39 <p class="p4">Stream is an abstract class that is not used directly.<span class="Apple-converted-space">  </span>The following attempts to document some</p>
40 <p class="p4">aspects of the use of Streams for music generation.</p>
41 <p class="p2"><br></p>
42 <p class="p5"><b>Overview</b></p>
43 <p class="p2"><br></p>
44 <p class="p4">A Stream represents a sequence of values that are obtained incrementally by repeated</p>
45 <p class="p4"><b>next </b>messages.<span class="Apple-converted-space">  </span>A Stream can be restarted with a <b>reset </b>message. (Not all streams<span class="Apple-converted-space"> </span></p>
46 <p class="p4">actually implement reset semantics.)</p>
47 <p class="p2"><br></p>
48 <p class="p4">The class Object defines <b>next</b> to return the object itself.<span class="Apple-converted-space">  </span>Thus every object can be viewed</p>
49 <p class="p4">as a stream and most simply stream themselves. <span class="Apple-converted-space"> </span></p>
50 <p class="p2"><br></p>
51 <p class="p4"><b>Stream </b>is the base class for classes that define streams.</p>
52 <p class="p2"><br></p>
53 <p class="p4">In SuperCollider, Streams are primarily used for handling text and for generating music.</p>
54 <p class="p2"><br></p>
55 <p class="p2"><br></p>
56 <p class="p6"><b><i>Two Stream classes: FuncStream and Routine</i></b></p>
57 <p class="p2"><br></p>
58 <p class="p2"><br></p>
59 <p class="p2"><br></p>
60 <p class="p4"><b>FuncStream(nextFunction, resetFunction)</b></p>
61 <p class="p2"><br></p>
62 <p class="p4">A Function defines a stream consisting of the Function itself, a FuncStream defines a stream</p>
63 <p class="p4">that consists of <i>evaluations </i>of<i> </i>its nextFunction.</p>
64 <p class="p2"><br></p>
65 <p class="p7"><span class="Apple-tab-span"> </span>// Example 1: a Function vs. a FuncStream</p>
66 <p class="p8"><span class="s2"><span class="Apple-tab-span"> </span></span>(<span class="Apple-tab-span"> </span></p>
67 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>f = { 33.rand };</p>
68 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>x = <span class="s3">FuncStream</span>(f);</p>
69 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>10.do({ [f.next, x.next].postln });</p>
70 <p class="p8"><span class="Apple-tab-span"> </span>)</p>
71 <p class="p9"><span class="Apple-tab-span"> </span></p>
72 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span></span>// Example 2: the reset function</p>
73 <p class="p8"><span class="Apple-tab-span"> </span>(<span class="Apple-tab-span"> </span></p>
74 <p class="p9"><span class="Apple-tab-span"> </span></p>
75 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>f = { 33.rand };</p>
76 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>x = <span class="s3">FuncStream</span>(f, {<span class="s3">thisThread</span>.randSeed_(345)});</p>
77 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>x.reset;</p>
78 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>10.do({ [f.next, x.next].postln });</p>
79 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>x.reset;</p>
80 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>10.do({ [f.next, x.next].postln });</p>
81 <p class="p8"><span class="Apple-tab-span"> </span>)</p>
82 <p class="p2"><br></p>
83 <p class="p4"><b>Routine(nextFunction, stacksize)</b></p>
84 <p class="p2"><br></p>
85 <p class="p4">In a FuncStream, the nextFunction runs through to completion for each element of the stream.</p>
86 <p class="p4">In a Routine, the nextFunction returns values with <b>yield</b> and resumes execution (when it receives</p>
87 <p class="p4">a <b>next<span class="Apple-converted-space"> </span></b> message) at the expression folowing the yield.<span class="Apple-converted-space">  </span>This allows a sequence of expressions in</p>
88 <p class="p4">the function definition to represent a sequence of distinct events, like a musical score.</p>
89 <p class="p2"><br></p>
90 <p class="p10"><br></p>
91 <p class="p7"><span class="Apple-tab-span"> </span>// example</p>
92 <p class="p8"><span class="Apple-tab-span"> </span>(</p>
93 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>x = <span class="s3">Routine</span>({<span class="Apple-converted-space"> </span></p>
94 <p class="p8"><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>1.yield;</p>
95 <p class="p8"><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>2.yield;</p>
96 <p class="p8"><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>3.yield;<span class="Apple-converted-space"> </span></p>
97 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>});</p>
98 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>4.do({ x.next.postln });</p>
99 <p class="p8"><span class="Apple-tab-span"> </span>)</p>
100 <p class="p2"><br></p>
101 <p class="p4">Once the nextFunction completes execution, the Routine simply yields nil repeatedly.</p>
102 <p class="p4">Control structures (such as <b>do</b> or <b>while</b>) can be used within the nextFunction in a manner analogous</p>
103 <p class="p4">to repeat marks in a score</p>
104 <p class="p2"><br></p>
105 <p class="p7"><span class="Apple-tab-span"> </span>// example</p>
106 <p class="p8"><span class="s2"><span class="Apple-tab-span"> </span></span>(</p>
107 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>x = <span class="s3">Routine</span>({<span class="Apple-converted-space"> </span></p>
108 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>4.do({</p>
109 <p class="p8"><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>[1,2,3,4].do({ <span class="s3">arg</span> i; i.yield; });</p>
110 <p class="p8"><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>
111 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>});</p>
112 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>17.do({ x.next.postln });</p>
113 <p class="p8"><span class="Apple-tab-span"> </span>)</p>
114 <p class="p2"><br></p>
115 <p class="p2"><br></p>
116 <p class="p2"><br></p>
117 <p class="p5"><b><i>Playing streams</i></b></p>
118 <p class="p2"><br></p>
119 <p class="p4">Because streams respond like functions to the value message,<span class="Apple-converted-space"> </span></p>
120 <p class="p4">they can be used as a scheduling task.<span class="Apple-converted-space"> </span></p>
121 <p class="p2"><br></p>
122 <p class="p7"><span class="Apple-tab-span"> </span>// compare:</p>
123 <p class="p7"><span class="Apple-tab-span"> </span>// a function, returning 0.5</p>
124 <p class="p8"><span class="Apple-tab-span"> </span>(</p>
125 <p class="p8"><span class="Apple-tab-span"> </span><span class="s3">SystemClock</span>.sched(0.0,<span class="Apple-converted-space"> </span></p>
126 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>{ <span class="s4">"***"</span>.postln; 0.5 }</p>
127 <p class="p8"><span class="Apple-tab-span"> </span>);</p>
128 <p class="p8"><span class="Apple-tab-span"> </span>)</p>
129 <p class="p9"><span class="Apple-tab-span"> </span></p>
130 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span></span>// a stream, returning 0.5 and 0.1</p>
131 <p class="p8"><span class="Apple-tab-span"> </span>(</p>
132 <p class="p8"><span class="Apple-tab-span"> </span><span class="s3">SystemClock</span>.sched(0.0,<span class="Apple-converted-space"> </span></p>
133 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">Routine</span>({ loop {<span class="Apple-converted-space"> </span></p>
134 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s4">"***"</span>.postln; 0.5.yield;<span class="Apple-converted-space"> </span></p>
135 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s4">"_*_"</span>.postln; 0.1.yield;<span class="Apple-converted-space"> </span></p>
136 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>} })</p>
137 <p class="p8"><span class="Apple-tab-span"> </span>);</p>
138 <p class="p8"><span class="Apple-tab-span"> </span>)</p>
139 <p class="p9"><span class="Apple-tab-span"> </span></p>
140 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span></span>// this is the reason why 'wait' works the same (for numbers) like 'yield'<span class="Apple-converted-space"> </span></p>
141 <p class="p8"><span class="Apple-tab-span"> </span>(</p>
142 <p class="p8"><span class="Apple-tab-span"> </span><span class="s3">SystemClock</span>.sched(0.0,<span class="Apple-converted-space"> </span></p>
143 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">Routine</span>({ loop {<span class="Apple-converted-space"> </span></p>
144 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s4">"***"</span>.postln; 0.5.wait;<span class="Apple-converted-space"> </span></p>
145 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s4">"_*_"</span>.postln; 0.1.wait;<span class="Apple-converted-space"> </span></p>
146 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>} })</p>
147 <p class="p8"><span class="Apple-tab-span"> </span>);</p>
148 <p class="p8"><span class="Apple-tab-span"> </span>)</p>
149 <p class="p9"><br></p>
150 <p class="p2"><br></p>
151 <p class="p4">Streams that return <b>numbers</b> can be played directly with the <b>play</b> message:</p>
152 <p class="p2"><br></p>
153 <p class="p2"><span class="Apple-tab-span"> </span></p>
154 <p class="p4"><span class="Apple-tab-span"> </span><b>play(clock, quant)</b></p>
155 <p class="p4"><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, TempoClock by default</p>
156 <p class="p4"><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>either a number<b> n</b> (quantize to <b>n </b>beats)</p>
157 <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>or an array<b> [n, m]</b> (quantize to <b>n</b> beats, with offset <b>m</b>)</p>
158 <p class="p2"><br></p>
159 <p class="p2"><br></p>
160 <p class="p7"><span class="Apple-tab-span"> </span>// play at the next beat, with offset 0.4</p>
161 <p class="p11"><span class="s1"><span class="Apple-tab-span"> </span></span>(</p>
162 <p class="p8"><span class="s3"><span class="Apple-tab-span"> </span>Routine</span>({ loop {<span class="Apple-converted-space"> </span></p>
163 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s4">"***"</span>.postln; 0.5.wait;<span class="Apple-converted-space"> </span></p>
164 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s4">"_*_"</span>.postln; 0.1.wait;<span class="Apple-converted-space"> </span></p>
165 <p class="p8"><span class="Apple-tab-span"> </span>} }).play(quant:[1, 0.4]);</p>
166 <p class="p8"><span class="Apple-tab-span"> </span>)</p>
167 <p class="p9"><br></p>
168 <p class="p9"><br></p>
169 <p class="p4">Streams that return <b>Events</b> need to be wrapped in an <b>EventStreamPlayer</b>.<span class="Apple-converted-space"> </span></p>
170 <p class="p4">The Event's <b>delta</b> (can also be set by <b>dur</b>) is used as a scheduling beats value:</p>
171 <p class="p2"><br></p>
172 <p class="p7"><span class="s5"><b><span class="Apple-tab-span"> </span></b></span>// play at the next beat, with offset 0.4</p>
173 <p class="p11"><span class="s1"><span class="Apple-tab-span"> </span></span>(</p>
174 <p class="p8"><span class="s3"><span class="Apple-tab-span"> </span>Routine</span>({ loop {<span class="Apple-converted-space"> </span></p>
175 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s4">"///"</span>.postln; (delta:0.5).yield;<span class="Apple-converted-space"> </span></p>
176 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s4">"_/_"</span>.postln; (delta: 0.1).wait;<span class="Apple-converted-space"> </span></p>
177 <p class="p8"><span class="Apple-tab-span"> </span>} }).asEventStreamPlayer.play;</p>
178 <p class="p8"><span class="Apple-tab-span"> </span>)</p>
179 <p class="p2"><br></p>
180 <p class="p12"><br></p>
181 <p class="p12"><br></p>
182 <p class="p5"><b><i>Iteration</i></b></p>
183 <p class="p2"><br></p>
184 <p class="p2"><span class="Apple-tab-span"> </span></p>
185 <p class="p4"><span class="Apple-tab-span"> </span><b>do (function)</b></p>
186 <p class="p4"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>iterate until a nil is encountered</p>
187 <p class="p4"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>beware:</b> applying do to an endless stream will lock up the interpreter!</p>
188 <p class="p2"><br></p>
189 <p class="p4"><span class="Apple-tab-span"> </span>Where <b>do </b>effectively 'plays' a stream by iterating all of its contects,<span class="Apple-converted-space">  </span>the<span class="Apple-converted-space"> </span></p>
190 <p class="p4"><span class="Apple-tab-span"> </span>following messages create a stream by filtering another stream in some way.</p>
191 <p class="p2"><br></p>
192 <p class="p4"><span class="Apple-tab-span"> </span><b>collect (function)</b></p>
193 <p class="p4"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>iterate indefinitely</p>
194 <p class="p2"><br></p>
195 <p class="p4"><b><span class="Apple-tab-span"> </span>reject (function)</b></p>
196 <p class="p4"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>return only those elements for which function.value(element) is false</p>
197 <p class="p2"><br></p>
198 <p class="p4"><b><span class="Apple-tab-span"> </span>select (function)</b></p>
199 <p class="p4"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>return only those elements for which function.value(element) is true</p>
200 <p class="p2"><br></p>
201 <p class="p4"><span class="Apple-tab-span"> </span><b>dot(function, stream)</b></p>
202 <p class="p4"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>return <b>function.value(this.next, stream.next)</b> for each element</p>
203 <p class="p2"><br></p>
204 <p class="p4"><span class="Apple-tab-span"> </span><b>interlace(function, stream)</b></p>
205 <p class="p4"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>iterate all of stream for each element of this.<span class="Apple-converted-space">  </span>Combine the values using function.</p>
206 <p class="p2"><b><span class="Apple-tab-span"> </span></b></p>
207 <p class="p4"><b><span class="Apple-tab-span"> </span>appendStream(stream)</b></p>
208 <p class="p4"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>append stream after this<b> </b>returns nil. The same like <b>++</b></p>
209 <p class="p2"><b><span class="Apple-tab-span"> </span></b></p>
210 <p class="p4"><span class="Apple-tab-span"> </span><b>embedInStream(inval)</b></p>
211 <p class="p4"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>iterate all of this from within whatever Stream definition it is called.</p>
212 <p class="p2"><b><span class="Apple-tab-span"> </span></b></p>
213 <p class="p4"><b><span class="Apple-tab-span"> </span>trace(key, printStream, prefix)</b></p>
214 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>print out the results of a stream while returning the original values</p>
215 <p class="p2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
216 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><b>key</b>: <span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>when streaming events, post only this key.</p>
217 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><b>printStream</b>: <span class="Apple-tab-span"> </span>printOn this stream (default: Post)</p>
218 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><b>prefix</b>:<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>string added to the printout to separate different streams</p>
219 <p class="p12"><br></p>
220 <p class="p12"><br></p>
221 <p class="p12"><br></p>
222 <p class="p12"><br></p>
223 <p class="p2"><br></p>
224 <p class="p13"><b><i>Composite Streams</i></b></p>
225 <p class="p12"><br></p>
226 <p class="p12"><br></p>
227 <p class="p14">Routines can be embedded in each other, using <b>embedInStream</b>:</p>
228 <p class="p12"><br></p>
229 <p class="p7"><span class="Apple-tab-span"> </span>// example</p>
230 <p class="p8"><span class="Apple-tab-span"> </span>(</p>
231 <p class="p8"><span class="Apple-tab-span"> </span>x = <span class="s3">Routine</span>({<span class="Apple-converted-space"> </span></p>
232 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>2.do({</p>
233 <p class="p8"><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>[1,2,3,4].do({ <span class="s3">arg</span> i; i.yield; });</p>
234 <p class="p8"><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>
235 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>});</p>
236 <p class="p8"><span class="Apple-tab-span"> </span>y = <span class="s3">Routine</span>({</p>
237 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>100.yield;</p>
238 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>30.yield;</p>
239 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>x.embedInStream;</p>
240 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>440.yield;</p>
241 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>1910.yield</p>
242 <p class="p8"><span class="Apple-tab-span"> </span>});</p>
243 <p class="p8"><span class="Apple-tab-span"> </span>17.do({ y.next.postln });</p>
244 <p class="p8"><span class="Apple-tab-span"> </span>)</p>
245 <p class="p9"><br></p>
246 <p class="p9"><br></p>
247 <p class="p4">Routines can be <b>concatenated</b> just like Streams:</p>
248 <p class="p9"><br></p>
249 <p class="p8"><span class="Apple-tab-span"> </span>(</p>
250 <p class="p8"><span class="Apple-tab-span"> </span>x = <span class="s3">Routine</span>({<span class="Apple-converted-space"> </span></p>
251 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>2.do({</p>
252 <p class="p8"><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>[1,2,3,4].do({ <span class="s3">arg</span> i; i.yield; });</p>
253 <p class="p8"><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>
254 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>});</p>
255 <p class="p8"><span class="Apple-tab-span"> </span>y = <span class="s3">Routine</span>({</p>
256 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>100.yield;</p>
257 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>30.yield;</p>
258 <p class="p8"><span class="Apple-tab-span"> </span>});</p>
259 <p class="p8"><span class="Apple-tab-span"> </span>z = x ++ y;</p>
260 <p class="p8"><span class="Apple-tab-span"> </span>17.do({ z.next.postln });</p>
261 <p class="p8"><span class="Apple-tab-span"> </span>)</p>
262 <p class="p9"><br></p>
263 <p class="p4">Routines can be combined with the <b>composition</b> operator <b>&lt;&gt;</b></p>
264 <p class="p9"><br></p>
265 <p class="p8"><span class="Apple-tab-span"> </span>(</p>
266 <p class="p8"><span class="Apple-tab-span"> </span>x = <span class="s3">Routine</span>({ <span class="s3">arg</span> inval;</p>
267 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>2.do({</p>
268 <p class="p9"><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>
269 <p class="p8"><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>[1,2,3,4].do({ <span class="s3">arg</span> i;<span class="Apple-converted-space"> </span></p>
270 <p class="p8"><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>if(inval.isNil) { <span class="s3">nil</span>.alwaysYield };</p>
271 <p class="p8"><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>inval = (i * inval).yield;<span class="Apple-converted-space"> </span></p>
272 <p class="p8"><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>
273 <p class="p8"><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>
274 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>});</p>
275 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
276 <p class="p8"><span class="Apple-tab-span"> </span>y = <span class="s3">Routine</span>({</p>
277 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>100.yield;</p>
278 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>30.yield;</p>
279 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>4.do { 1.0.rand.yield };</p>
280 <p class="p8"><span class="Apple-tab-span"> </span>});</p>
281 <p class="p9"><span class="Apple-tab-span"> </span></p>
282 <p class="p8"><span class="Apple-tab-span"> </span>z = x &lt;&gt; y;</p>
283 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span>17.do({ z.value.postln }); </span>// call .value here, as this is a function.</p>
284 <p class="p8"><span class="Apple-tab-span"> </span>)</p>
285 <p class="p15"><br></p>
286 <p class="p15"><br></p>
287 <p class="p4">Composite Streams can be defined as combinations of Streams using the unary and binary<span class="Apple-converted-space"> </span></p>
288 <p class="p4">messages.</p>
289 <p class="p15"><br></p>
290 <p class="p15"><br></p>
291 <p class="p5"><b><i>Unary messages</i></b></p>
292 <p class="p15"><br></p>
293 <p class="p2"><br></p>
294 <p class="p4">Streams support most of the unary messages<span class="Apple-converted-space">  </span>defined in AbstractFunction:</p>
295 <p class="p2"><br></p>
296 <p class="p8">(</p>
297 <p class="p8"><span class="Apple-tab-span"> </span>a = <span class="s3">Routine</span>({ 20.do({ 33.rand.yield }) });</p>
298 <p class="p8"><span class="Apple-tab-span"> </span>b = <span class="s3">Routine</span>({ [-100,00,300,400].do({ <span class="s3">arg</span> v; v.yield}) });</p>
299 <p class="p9"><br></p>
300 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span>c = b.neg; </span>// define a composite stream<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
301 <p class="p9"><span class="Apple-tab-span"> </span></p>
302 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span></span>// enumerate and perform all of the unary messages<span class="Apple-tab-span"> </span>:<span class="Apple-tab-span"> </span></p>
303 <p class="p8"><span class="Apple-tab-span"> </span>[<span class="Apple-converted-space"> </span></p>
304 <p class="p16"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>\neg<span class="s1">, </span>\reciprocal<span class="s1">, </span>\bitNot<span class="s1">, </span>\abs<span class="s1">, </span>\asFloat<span class="s1">, </span>\asInteger<span class="s1">, </span>\ceil<span class="s1">,<span class="Apple-converted-space"> </span></span></p>
305 <p class="p16"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>\floor<span class="s1">, </span>\frac<span class="s1">, </span>\sign<span class="s1">, </span>\squared<span class="s1">, </span>\cubed<span class="s1">, </span>\sqrt<span class="s1">, </span>\exp<span class="s1">, </span>\midicps<span class="s1">,<span class="Apple-converted-space"> </span></span></p>
306 <p class="p16"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>\cpsmidi<span class="s1">, </span>\midiratio<span class="s1">, </span>\ratiomidi<span class="s1">, </span>\ampdb<span class="s1">, </span>\dbamp<span class="s1">, </span>\octcps<span class="s1">,<span class="Apple-converted-space"> </span></span></p>
307 <p class="p16"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>\cpsoct<span class="s1">, </span>\log<span class="s1">, </span>\log2<span class="s1">, </span>\log10<span class="s1">, </span>\sin<span class="s1">, </span>\cos<span class="s1">, </span>\tan<span class="s1">, </span>\asin<span class="s1">, </span>\acos<span class="s1">, </span>\atan<span class="s1">,<span class="Apple-converted-space"> </span></span></p>
308 <p class="p16"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>\sinh<span class="s1">, </span>\cosh<span class="s1">, </span>\tanh<span class="s1">, </span>\rand<span class="s1">, </span>\rand2<span class="s1">, </span>\linrand<span class="s1">, </span>\bilinrand<span class="s1">, </span>\sum3rand<span class="s1">,<span class="Apple-converted-space"> </span></span></p>
309 <p class="p16"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>\distort<span class="s1">, </span>\softclip<span class="s1">, </span>\coin<span class="s1">, </span>\even<span class="s1">, </span>\odd<span class="s1">, </span>\isPositive<span class="s1">, </span>\isNegative<span class="s1">,</span></p>
310 <p class="p16"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>\isStrictlyPositive</p>
311 <p class="p8"><span class="Apple-tab-span"> </span>]</p>
312 <p class="p8"><span class="Apple-tab-span"> </span>.do({ <span class="s3">arg</span> msg;</p>
313 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>postf(<span class="s4">"\n msg: % \n"</span>, msg);</p>
314 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>b.reset.perform(msg).do({<span class="s3">arg</span> v; v.post; <span class="s4">" "</span>.post;})<span class="Apple-converted-space"> </span></p>
315 <p class="p8"><span class="Apple-tab-span"> </span>});<span class="Apple-converted-space"> </span></p>
316 <p class="p8"><span class="Apple-tab-span"> </span>nil;</p>
317 <p class="p9"><span class="Apple-tab-span"> </span></p>
318 <p class="p8">)</p>
319 <p class="p2"><br></p>
320 <p class="p2"><br></p>
321 <p class="p2"><br></p>
322 <p class="p5"><b><i>Binary messages</i></b></p>
323 <p class="p2"><br></p>
324 <p class="p4">Streams support the following binary messages<span class="Apple-converted-space">  </span>defined in AbstractFunction:</p>
325 <p class="p9"><br></p>
326 <p class="p9"><br></p>
327 <p class="p8">(</p>
328 <p class="p8"><span class="Apple-tab-span"> </span>a = <span class="s3">Routine</span>({ 20.do({ 33.rand.yield }) });</p>
329 <p class="p8"><span class="Apple-tab-span"> </span>b = <span class="s3">Routine</span>({ [-100,00,300,400].do({ <span class="s3">arg</span> v; v.yield}) });</p>
330 <p class="p8"><span class="Apple-tab-span"> </span>[</p>
331 <p class="p16"><span class="s1"><span class="Apple-tab-span"> </span></span>'+'<span class="s1"> , </span>'-'<span class="s1"> , </span>'*'<span class="s1">, </span>'/'<span class="s1">, </span>\div<span class="s1">, </span>'%'<span class="s1">, </span>'**'<span class="s1">, </span>\min<span class="s1">, </span>\max<span class="s1">, </span>'&lt;'<span class="s1">, </span>'&lt;='<span class="s1">, </span>'&gt;'<span class="s1">, </span>'&gt;='<span class="s1">, </span>'&amp;'<span class="s1">, </span>'|'<span class="s1">,<span class="Apple-converted-space"> </span></span></p>
332 <p class="p16"><span class="s1"><span class="Apple-tab-span"> </span></span>\bitXor<span class="s1">, </span>\lcm<span class="s1">, </span>\gcd<span class="s1">, </span>\round<span class="s1">, </span>\trunc<span class="s1">, </span>\atan2<span class="s1">,<span class="Apple-converted-space"> </span></span></p>
333 <p class="p16"><span class="s1"><span class="Apple-tab-span"> </span></span>\hypot<span class="s1">, </span>'&gt;&gt;'<span class="s1">, </span>'+&gt;&gt;'<span class="s1">, </span>\ring1<span class="s1">, </span>\ring2<span class="s1">, </span>\ring3<span class="s1">, </span>\ring4<span class="s1">,<span class="Apple-converted-space"> </span></span></p>
334 <p class="p16"><span class="s1"><span class="Apple-tab-span"> </span></span>\difsqr<span class="s1">, </span>\sumsqr<span class="s1">, </span>\sqrdif<span class="s1">, </span>\absdif<span class="s1">, </span>\amclip<span class="s1">,</span></p>
335 <p class="p16"><span class="s1"><span class="Apple-tab-span"> </span></span>\scaleneg<span class="s1">, </span>\clip2<span class="s1">, </span>\excess<span class="s1">, </span>'&lt;!'<span class="s1">, </span>\rrand<span class="s1">, </span>\exprand</p>
336 <p class="p8"><span class="Apple-tab-span"> </span>]</p>
337 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>.do({ <span class="s3">arg</span> msg;</p>
338 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>postf(<span class="s4">"\n msg: % \n"</span>, msg);</p>
339 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>b.reset.perform(msg).do({ <span class="s3">arg</span> v; v.post; <span class="s4">" "</span>.post; })<span class="Apple-converted-space"> </span></p>
340 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>});<span class="Apple-converted-space"> </span></p>
341 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">nil</span>;</p>
342 <p class="p8">)</p>
343 <p class="p2"><span class="Apple-converted-space"> </span></p>
344 <p class="p2"><br></p>
345 <p class="p2"><span class="Apple-tab-span"> </span></p>
346 </body>
347 </html>