Removing an old, cherished, yet pointless caveat "This documentation is
[supercollider.git] / Help / Streams-Patterns-Events / Streams-Patterns-Events2.html
blob6f9843f910fa74245e6d1e67a3e78bb91bdd2c1d
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: 16.0px Helvetica; min-height: 19.0px}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Helvetica}
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: 12.0px Helvetica}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; min-height: 16.0px}
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: #ad140d}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #001bb9}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0021e7}
20 span.s1 {color: #001bb9}
21 span.s2 {color: #000000}
22 span.s3 {color: #2c7014}
23 span.s4 {text-decoration: underline}
24 span.Apple-tab-span {white-space:pre}
25 </style>
26 </head>
27 <body>
28 <p class="p1"><br></p>
29 <p class="p2"><b>Understanding Streams, Patterns and Events - Part 2</b></p>
30 <p class="p3"><br></p>
31 <p class="p4"><b>Patterns</b></p>
32 <p class="p3"><br></p>
33 <p class="p4">Often one wants to be able to create multiple streams from a single stream specification. Patterns are just a way to make multiple Streams<span class="Apple-converted-space">  </span>from a single specification, like a cookie cutter. A pattern can be any object that responds to the asStream message by creating a Stream. Once again there is a default implementation in class Object of asStream that simply returns the receiver as its own stream. Thus any object is by default a pattern that returns itself as a stream when sent the asStream message.</p>
34 <p class="p5"><br></p>
35 <p class="p6">(</p>
36 <p class="p6">a = 7.asStream;</p>
37 <p class="p6">a.postln;</p>
38 <p class="p6">a.next.postln;</p>
39 <p class="p6">)</p>
40 <p class="p3"><br></p>
41 <p class="p4"><b>Pattern and its subclasses</b></p>
42 <p class="p3"><br></p>
43 <p class="p4">There is a class named Pattern that provides more functionality for the concept of a pattern.</p>
44 <p class="p3"><br></p>
45 <p class="p4">Pfunc is a Pattern that returns a FuncStream. The same function arguments are supplied as are supplied to FuncStream.</p>
46 <p class="p3"><br></p>
47 <p class="p6">(</p>
48 <p class="p6"><span class="s1">var</span> a, b;</p>
49 <p class="p6">a = <span class="s1">Pfunc</span>.new({ #[1, 2, 3, 4].choose });</p>
50 <p class="p7"><span class="s2">b = a.asStream;<span class="Apple-tab-span"> </span></span>// make a stream from the pattern</p>
51 <p class="p7"><span class="s2">5.do({ b.next.postln; });<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// print 5 values from the stream</p>
52 <p class="p8"><br></p>
53 <p class="p6">)</p>
54 <p class="p3"><br></p>
55 <p class="p4">Prout is a Pattern that returns a Routine. The same function argument is supplied as is supplied to Routine.</p>
56 <p class="p3"><br></p>
57 <p class="p6">(</p>
58 <p class="p6"><span class="s1">var</span> a, b, c;</p>
59 <p class="p6">a = <span class="s1">Prout</span>.new({<span class="Apple-converted-space"> </span></p>
60 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>3.do({ <span class="s1">arg</span> i; 3.rand.yield; })<span class="Apple-converted-space"> </span></p>
61 <p class="p6"><span class="Apple-tab-span"> </span>});</p>
62 <p class="p7">// make two streams from the pattern</p>
63 <p class="p6">b = a.asStream;</p>
64 <p class="p6">c = a.asStream;</p>
65 <p class="p7"><span class="s2">4.do({ b.next.postln; });<span class="Apple-tab-span"> </span></span>// print 4 values from first stream</p>
66 <p class="p7"><span class="s2">4.do({ c.next.postln; });<span class="Apple-tab-span"> </span></span>// print 4 values from second stream</p>
67 <p class="p6">)</p>
68 <p class="p3"><br></p>
69 <p class="p4">Pseries is a Pattern that generates an arithmetic series.</p>
70 <p class="p5"><br></p>
71 <p class="p6">(</p>
72 <p class="p6"><span class="s1">var</span> a, b;</p>
73 <p class="p7"><span class="s2">a = </span><span class="s1">Pseries</span><span class="s2">.new(10, 3, 8); </span>// stream starts at 10, steps by 3 and has length 8</p>
74 <p class="p6">b = a.asStream;</p>
75 <p class="p7"><span class="s2">9.do({ b.next.postln; });<span class="Apple-tab-span"> </span></span>// print 9 values from stream</p>
76 <p class="p6">)</p>
77 <p class="p3"><br></p>
78 <p class="p4">Pgeom is a Pattern that generates a geometric series.</p>
79 <p class="p3"><br></p>
80 <p class="p6">(</p>
81 <p class="p6"><span class="s1">var</span> a, b;</p>
82 <p class="p7">// stream starts at 10, steps by factor of 3 and has length 8</p>
83 <p class="p6">a = <span class="s1">Pgeom</span>.new(10, 3, 8);<span class="Apple-converted-space"> </span></p>
84 <p class="p6">b = a.asStream;</p>
85 <p class="p7"><span class="s2">9.do({ b.next.postln; });<span class="Apple-tab-span"> </span></span>// print 9 values from stream</p>
86 <p class="p6">)</p>
87 <p class="p3"><br></p>
88 <p class="p4"><b>Math operations on Patterns</b></p>
89 <p class="p3"><br></p>
90 <p class="p4">Patterns also respond to math operators by returning patterns that respond to asStream with appropriately modified streams.</p>
91 <p class="p3"><br></p>
92 <p class="p4">Applying a unary operator to a pattern</p>
93 <p class="p3"><br></p>
94 <p class="p6">(</p>
95 <p class="p6"><span class="s1">var</span> a, b, c;</p>
96 <p class="p7">// a is a pattern whose stream counts from 0 to 9</p>
97 <p class="p6">a = <span class="s1">Pseries</span>.new(0,1,10);</p>
98 <p class="p7"><span class="s2">b = a.squared;<span class="Apple-tab-span"> </span></span>// pattern b is a square of the pattern a</p>
99 <p class="p6">c = b.asStream;</p>
100 <p class="p6">12.do({ c.next.postln; });</p>
101 <p class="p6">)</p>
102 <p class="p3"><br></p>
103 <p class="p4">Using a binary operator on a pattern</p>
104 <p class="p5"><br></p>
105 <p class="p6">(</p>
106 <p class="p6"><span class="s1">var</span> a, b, c;</p>
107 <p class="p7">// a is a pattern whose stream counts from 0 to 9</p>
108 <p class="p6">a = <span class="s1">Pseries</span>.new(0,1,10);</p>
109 <p class="p7"><span class="s2">b = a + 100;<span class="Apple-tab-span"> </span></span>// add a constant value to pattern a</p>
110 <p class="p6">c = b.asStream;</p>
111 <p class="p6">12.do({ c.next.postln; });</p>
112 <p class="p6">)</p>
113 <p class="p3"><br></p>
114 <p class="p4"><b>Filtering operations on patterns</b></p>
115 <p class="p3"><br></p>
116 <p class="p4">Patterns also respond to the messages collect, select, and reject by returning a new Pattern.</p>
117 <p class="p3"><br></p>
118 <p class="p4">The collect message returns a Pattern whose Stream is modified by a function in the same way as the collect message sent to a Collection returns a modified Collection.</p>
119 <p class="p5"><br></p>
120 <p class="p6">(</p>
121 <p class="p6"><span class="s1">var</span> a, b, c;</p>
122 <p class="p7">// a is a pattern whose stream counts from 0 to 9</p>
123 <p class="p6">a = <span class="s1">Pseries</span>.new(0,1,10);</p>
124 <p class="p7">// b is a pattern whose stream adds 100 to even values</p>
125 <p class="p6">b = a.collect({ <span class="s1">arg</span> item; if (item.even, { item + 100 },{ item }); });</p>
126 <p class="p6">c = b.asStream;</p>
127 <p class="p6">6.do({ c.next.postln; });</p>
128 <p class="p6">)</p>
129 <p class="p3"><br></p>
130 <p class="p4">The select message creates a pattern whose stream passes only items that return true from a user supplied function.</p>
131 <p class="p5"><br></p>
132 <p class="p6">(</p>
133 <p class="p6"><span class="s1">var</span> a, b, c;</p>
134 <p class="p7">// a is a pattern whose stream counts from 0 to 9</p>
135 <p class="p6">a = <span class="s1">Pseries</span>.new(0,1,10);</p>
136 <p class="p7">// b is a pattern whose stream only returns the odd values</p>
137 <p class="p6">b = a.select({ <span class="s1">arg</span> item; item.odd; });</p>
138 <p class="p6">c = b.asStream;</p>
139 <p class="p6">6.do({ c.next.postln; });</p>
140 <p class="p6">)</p>
141 <p class="p3"><br></p>
142 <p class="p4">The reject message creates a pattern whose stream passes only items that return false from a<span class="Apple-converted-space"> </span></p>
143 <p class="p4">user supplied function.</p>
144 <p class="p3"><br></p>
145 <p class="p6">(</p>
146 <p class="p6"><span class="s1">var</span> a, b, c;</p>
147 <p class="p7">// a is a pattern whose stream counts from 0 to 9</p>
148 <p class="p6">a = <span class="s1">Pseries</span>.new(0,1,10);</p>
149 <p class="p7">// b is a pattern whose stream that only returns the non-odd values</p>
150 <p class="p6">b = a.reject({ <span class="s1">arg</span> item; item.odd; });</p>
151 <p class="p6">c = b.asStream;</p>
152 <p class="p6">6.do({ c.next.postln; });</p>
153 <p class="p6">)</p>
154 <p class="p5"><br></p>
155 <p class="p3"><br></p>
156 <p class="p4"><b>Making Music with Patterns</b></p>
157 <p class="p3"><br></p>
158 <p class="p4">Here is a variation of the example given in part 1 that uses a Pattern to create two instances of the random melody stream.</p>
159 <p class="p3"><br></p>
160 <p class="p6">(</p>
161 <p class="p6"><span class="Apple-tab-span"> </span>s = <span class="s1">Server</span>.local;</p>
162 <p class="p6"><span class="Apple-tab-span"> </span><span class="s1">SynthDef</span>(<span class="s3">\help_SPE2</span>, { <span class="s1">arg</span> i_out=0, sustain=1, freq;</p>
163 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s1">var</span> out;</p>
164 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>out = <span class="s1">RLPF</span>.ar(</p>
165 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s1">LFSaw</span>.ar( freq ),</p>
166 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s1">LFNoise1</span>.kr(1, 36, 110).midicps,</p>
167 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>0.1</p>
168 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>) * <span class="s1">EnvGen</span>.kr( <span class="s1">Env</span>.perc, levelScale: 0.3,<span class="Apple-converted-space"> </span></p>
169 <p class="p6"><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>timeScale: sustain, doneAction: 2 );</p>
170 <p class="p7"><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>//out = [out, DelayN.ar(out, 0.04, 0.04) ];</p>
171 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>4.do({ out = <span class="s1">AllpassN</span>.ar(out, 0.05, [0.05.rand, 0.05.rand], 4) });</p>
172 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s1">Out</span>.ar( i_out, out );</p>
173 <p class="p6"><span class="Apple-tab-span"> </span>}).send(s);</p>
174 <p class="p6">)</p>
175 <p class="p6">(</p>
176 <p class="p7">// streams as a sequence of pitches</p>
177 <p class="p6"><span class="Apple-tab-span"> </span><span class="s1">var</span> pattern, streams, dur, durDiff;</p>
178 <p class="p6"><span class="Apple-tab-span"> </span>dur = 1/7;</p>
179 <p class="p6"><span class="Apple-tab-span"> </span>durDiff = 3;</p>
180 <p class="p6"><span class="Apple-tab-span"> </span>pattern = <span class="s1">Prout</span>.new({</p>
181 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>loop({</p>
182 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>if (0.5.coin, {</p>
183 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>#[ 24,31,36,43,48,55 ].do({ <span class="s1">arg</span> fifth; fifth.yield });</p>
184 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>});</p>
185 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>rrand(2,5).do({</p>
186 <p class="p7"><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>// varying arpeggio</p>
187 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>60.yield;</p>
188 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>#[63,65].choose.yield;</p>
189 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>67.yield;</p>
190 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>#[70,72,74].choose.yield;</p>
191 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>});</p>
192 <p class="p7"><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// random high melody</p>
193 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>rrand(3,9).do({<span class="Apple-converted-space">  </span>#[74,75,77,79,81].choose.yield });</p>
194 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>});</p>
195 <p class="p6"><span class="Apple-tab-span"> </span>});</p>
196 <p class="p6"><span class="Apple-tab-span"> </span>streams = [ <span class="Apple-converted-space"> </span></p>
197 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>(pattern - <span class="s1">Pfunc</span>.new({ #[12, 7, 7, 0].choose })).midicps.asStream,</p>
198 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>pattern.midicps.asStream</p>
199 <p class="p6"><span class="Apple-tab-span"> </span>];</p>
200 <p class="p9"><span class="s2"><span class="Apple-tab-span"> </span></span>Routine<span class="s2">({</span></p>
201 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>loop({</p>
202 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s1">Synth</span>( <span class="s3">\help_SPE2</span>, [ <span class="s3">\freq</span>, streams.at(0).next, <span class="s3">\sustain</span>, dur * durDiff<span class="Apple-converted-space">  </span>] );</p>
203 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>durDiff.do({</p>
204 <p class="p6"><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="s1">Synth</span>( <span class="s3">\help_SPE2</span>, [ <span class="s3">\freq</span>, streams.at(1).next, <span class="s3">\sustain</span>, dur<span class="Apple-converted-space">  </span>] );</p>
205 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>dur.wait;</p>
206 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>});</p>
207 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>})</p>
208 <p class="p6"><span class="Apple-tab-span"> </span>}).play</p>
209 <p class="p6">)</p>
210 <p class="p3"><br></p>
211 <p class="p4">To go to the next file:</p>
212 <p class="p10"><span class="s4"><a href="Streams-Patterns-Events3.html">Streams-Patterns-Events3</a></span></p>
213 <p class="p3"><br></p>
214 <p class="p3"><br></p>
215 </body>
216 </html>