supernova: fixes for boost-1.49 and gcc-4.7
[supercollider.git] / Help / Streams-Patterns-Events / Streams-Patterns-Events7.html
blob8f0000c14b7acac043b82e3eb67b37e1a9b61efe
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="1038.25">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica; min-height: 17.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; min-height: 16.0px}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Monaco; min-height: 17.0px}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
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: 12.0px Helvetica}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0000bf}
20 p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #007300}
21 p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
22 p.p13 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Helvetica}
23 span.s1 {color: #0000bf}
24 span.s2 {color: #007300}
25 span.s3 {color: #000000}
26 span.s4 {color: #bf0000}
27 span.Apple-tab-span {white-space:pre}
28 </style>
29 </head>
30 <body>
31 <p class="p1"><br></p>
32 <p class="p2"><br></p>
33 <p class="p3"><b>Understanding Streams, Patterns and Events - Part 7</b></p>
34 <p class="p4"><br></p>
35 <p class="p3"><b>Practical Considerations</b></p>
36 <p class="p4"><br></p>
37 <p class="p5"><b>Using your own ~instrument</b></p>
38 <p class="p6"><br></p>
39 <p class="p7">(</p>
40 <p class="p7"><span class="s1">SynthDef</span>(<span class="s2">\help_SPE7_BerlinB</span>, { <span class="s1">arg</span> i_out=0, freq = 80, amp = 0.2, pan=0;</p>
41 <p class="p7"><span class="Apple-tab-span"> </span><span class="s1">var</span> out, a, b;</p>
42 <p class="p7"><span class="Apple-tab-span"> </span>amp = <span class="s1">Decay2</span>.kr(<span class="s1">Impulse</span>.kr(0), 0.05, 8, amp);</p>
43 <p class="p7"><span class="Apple-tab-span"> </span>out = <span class="s1">RLPF</span>.ar(</p>
44 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s1">LFPulse</span>.ar(freq, 0, <span class="s1">SinOsc</span>.kr(0.12,[0,0.5pi],0.48,0.5), amp),</p>
45 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>freq * <span class="s1">SinOsc</span>.kr(0.21,0,4,8),</p>
46 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>0.07</p>
47 <p class="p7"><span class="Apple-tab-span"> </span>);</p>
48 <p class="p7"><span class="Apple-tab-span"> </span>#a, b = out;</p>
49 <p class="p7"><span class="Apple-tab-span"> </span><span class="s1">DetectSilence</span>.ar(a, 0.0001, doneAction: 2);</p>
50 <p class="p7"><span class="Apple-tab-span"> </span><span class="s1">Out</span>.ar(i_out, <span class="s1">Mix</span>.ar(<span class="s1">PanAz</span>.ar(4, [a, b], [pan, pan+1])));</p>
51 <p class="p7">}).add;</p>
52 <p class="p8"><br></p>
53 <p class="p7"><span class="s1">SynthDef</span>(<span class="s2">\help_SPE7_CFString1</span>, { <span class="s1">arg</span> i_out, freq = 360, gate = 1, pan, amp=0.1;</p>
54 <p class="p7"><span class="Apple-tab-span"> </span><span class="s1">var</span> out, eg, fc, osc, a, b, w;</p>
55 <p class="p7"><span class="Apple-tab-span"> </span>fc = <span class="s1">LinExp</span>.kr(<span class="s1">LFNoise1</span>.kr(<span class="s1">Rand</span>(0.25,0.4)), -1,1,500,2000);</p>
56 <p class="p7"><span class="Apple-tab-span"> </span>osc = <span class="s1">Mix</span>.fill(8, { <span class="s1">LFSaw</span>.ar(freq * [<span class="s1">Rand</span>(0.99,1.01),<span class="s1">Rand</span>(0.99,1.01)], 0, amp) }).distort * 0.2;</p>
57 <p class="p7"><span class="Apple-tab-span"> </span>eg = <span class="s1">EnvGen</span>.kr(<span class="s1">Env</span>.asr(1,1,1), gate, doneAction:2);</p>
58 <p class="p7"><span class="Apple-tab-span"> </span>out = eg * <span class="s1">RLPF</span>.ar(osc, fc, 0.1);</p>
59 <p class="p7"><span class="Apple-tab-span"> </span>#a, b = out;</p>
60 <p class="p7"><span class="Apple-tab-span"> </span><span class="s1">Out</span>.ar(i_out, <span class="s1">Mix</span>.ar(<span class="s1">PanAz</span>.ar(4, [a, b], [pan, pan+0.3])));</p>
61 <p class="p7">}).add;</p>
62 <p class="p7">)</p>
63 <p class="p1"><br></p>
64 <p class="p9">Pattern-play creates an EventStreamPlayer for you and also supplies a default protoEvent. If you were using your own event model you would just pass in your own protoEvent to the play method.</p>
65 <p class="p1"><br></p>
66 <p class="p7">(</p>
67 <p class="p10">Pbind<span class="s3">(</span></p>
68 <p class="p11"><span class="s3"><span class="Apple-tab-span"> </span></span>\instrument<span class="s3">, </span><span class="s1">Prand</span><span class="s3">([</span>\help_SPE7_BerlinB<span class="s3">,</span> \help_SPE7_CFString1<span class="s3">],</span><span class="s1">inf</span><span class="s3">),</span></p>
69 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">\degree</span>, <span class="s1">Pseq</span>([0,1,2,4,6,3,4,8],<span class="s1">inf</span>),<span class="Apple-converted-space"> </span></p>
70 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">\dur</span>, 0.8,<span class="Apple-converted-space"> </span></p>
71 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">\octave</span>, 3, <span class="Apple-converted-space"> </span></p>
72 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">\amp</span>, 0.03</p>
73 <p class="p12"><span class="s3">).play; </span>// this returns an EventStreamPlayer</p>
74 <p class="p7">)</p>
75 <p class="p6"><br></p>
76 <p class="p13"><b>Defining your own message bindings</b></p>
77 <p class="p1"><br></p>
78 <p class="p9">The default event prototype uses a msgFunc to determine which bindings to pass to the server. Synthdefs that have been stored in a SynthDescLib ("synth description library") construct the msgFunc automatically. The default event looks up the instrument name in a SynthDescLib of your choosing (using the \synthLib key). Normally only the global SynthDescLib is used; if \synthLib is empty, the global library is the default.</p>
79 <p class="p1"><br></p>
80 <p class="p9">You should not send or load synthdefs that you plan to use with patterns. Instead, store them in a SynthDescLib.</p>
81 <p class="p1"><br></p>
82 <p class="p12">// saves .scsyndef file on disk (like .load), and adds description to the global library</p>
83 <p class="p7"><span class="s1">SynthDef</span>(...).store;</p>
84 <p class="p8"><br></p>
85 <p class="p12">// adds description to the global library; no file is saved (like .send)</p>
86 <p class="p7"><span class="s1">SynthDef</span>(...).add;</p>
87 <p class="p1"><br></p>
88 <p class="p9">If you don't do this, nondefault bindings will be ignored. In that case, you can provide a custom msgFunc manually. Here's an example:</p>
89 <p class="p1"><br></p>
90 <p class="p7">(</p>
91 <p class="p7"><span class="s1">SynthDef</span>(<span class="s2">\help_SPE4_CFString2</span>, { <span class="s1">arg</span> i_out, freq = 360, gate = 1, pan, amp=0.1, dorkarg=1;</p>
92 <p class="p7"><span class="Apple-tab-span"> </span><span class="s1">var</span> out, eg, fc, osc, a, b, w;</p>
93 <p class="p7"><span class="Apple-tab-span"> </span>fc = <span class="s1">LinExp</span>.kr(<span class="s1">LFNoise1</span>.kr(<span class="s1">Rand</span>(0.25,0.4)), -1,1,500,2000);</p>
94 <p class="p7"><span class="Apple-tab-span"> </span>osc = <span class="s1">Mix</span>.fill(8, { <span class="s1">LFSaw</span>.ar(freq * [<span class="s1">Rand</span>(0.99,1.01),<span class="s1">Rand</span>(0.99,1.01)], 0, amp * dorkarg ) }).distort * 0.2;</p>
95 <p class="p7"><span class="Apple-tab-span"> </span>eg = <span class="s1">EnvGen</span>.kr(<span class="s1">Env</span>.asr(1,1,1), gate, doneAction:2);</p>
96 <p class="p7"><span class="Apple-tab-span"> </span>out = eg * <span class="s1">RLPF</span>.ar(osc, fc, 0.1);</p>
97 <p class="p7"><span class="Apple-tab-span"> </span>#a, b = out;</p>
98 <p class="p7"><span class="Apple-tab-span"> </span><span class="s1">Out</span>.ar(i_out, <span class="s1">Mix</span>.ar(<span class="s1">PanAz</span>.ar(4, [a, b], [pan, pan+0.3])));</p>
99 <p class="p12"><span class="s3">}).send(s);<span class="Apple-tab-span"> </span></span>// change .send(s) to .add</p>
100 <p class="p7">)</p>
101 <p class="p1"><br></p>
102 <p class="p9">As you can see I have added dorkarg to the arglist of the SynthDef from earlier.</p>
103 <p class="p1"><br></p>
104 <p class="p7">(</p>
105 <p class="p10">Pbind<span class="s3">(</span></p>
106 <p class="p11"><span class="s3"><span class="Apple-tab-span"> </span></span>\instrument<span class="s3">, </span>\help_SPE4_CFString2<span class="s3">,</span></p>
107 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">\degree</span>, <span class="s1">Pseq</span>([0,1,2,4,6,3,4,8],<span class="s1">inf</span>),<span class="Apple-converted-space"> </span></p>
108 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">\dur</span>, 0.4,<span class="Apple-converted-space"> </span></p>
109 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">\octave</span>, 3, <span class="Apple-converted-space"> </span></p>
110 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">\amp</span>, 0.03,</p>
111 <p class="p12"><span class="s3"><span class="Apple-tab-span"> </span></span><span class="s2">\dorkarg</span><span class="s3">, </span><span class="s1">Pseq</span><span class="s3">([1,0,1],</span><span class="s1">inf</span><span class="s3">) </span>// silence every second note - doesn't work</p>
112 <p class="p7">).play;</p>
113 <p class="p7">)</p>
114 <p class="p1"><br></p>
115 <p class="p9">'dorkarg' is ignored because the SynthDef was not properly .add'd and consequently, the event prototype doesn't know that dorkarg is important.</p>
116 <p class="p1"><br></p>
117 <p class="p9">You could also supply a \msgFunc that includes dorkarg:</p>
118 <p class="p1"><br></p>
119 <p class="p7">(</p>
120 <p class="p10">Pbind<span class="s3">(</span></p>
121 <p class="p11"><span class="s3"><span class="Apple-tab-span"> </span></span>\instrument<span class="s3">, </span>\help_SPE4_CFString2<span class="s3">,</span></p>
122 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">\degree</span>, <span class="s1">Pseq</span>([0,1,2,4,6,3,4,8],<span class="s1">inf</span>),<span class="Apple-converted-space"> </span></p>
123 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">\dur</span>, 0.4,<span class="Apple-converted-space"> </span></p>
124 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">\octave</span>, 3, <span class="Apple-converted-space"> </span></p>
125 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">\amp</span>, 0.03,</p>
126 <p class="p12"><span class="s3"><span class="Apple-tab-span"> </span></span><span class="s2">\dorkarg</span><span class="s3">, </span><span class="s1">Pseq</span><span class="s3">([1,0,1],</span><span class="s1">inf</span><span class="s3">), </span>// silence every second note - now works</p>
127 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">\msgFunc</span>, { <span class="s1">arg</span> out = 0, freq = 440, amp = 0.1, pan = 0, vol = 1,</p>
128 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>dorkarg = 1;</p>
129 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>[<span class="s2">\out</span>, out, <span class="s2">\freq</span>, freq, <span class="s2">\amp</span>, amp, <span class="s2">\pan</span>, pan, <span class="s2">\vol</span>, vol,</p>
130 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">\dorkarg</span>, dorkarg];</p>
131 <p class="p7"><span class="Apple-tab-span"> </span>}</p>
132 <p class="p7">).play;</p>
133 <p class="p7">)</p>
134 <p class="p1"><br></p>
135 <p class="p9">But this is quite clumsy. It is strongly recommended to get into the habit of using .add for all SynthDefs intended for use with Patterns.</p>
136 <p class="p1"><br></p>
137 <p class="p9">The other option you have if you will be using unspecified bindings, is of course to define an event with the appropriate msgFunc as default. Have a look at Event's source, it's easy, and it's cleaner than passing in the msgFunc every time.</p>
138 <p class="p4"><br></p>
139 <p class="p5"><b>Manipulating an EventStreamPlayer in Realtime</b></p>
140 <p class="p4"><br></p>
141 <p class="p7">(</p>
142 <p class="p7">p = <span class="s1">Pbind</span>(</p>
143 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">\degree</span>, <span class="s1">Pwhite</span>(0,12),<span class="Apple-converted-space"> </span></p>
144 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">\dur</span>, 0.2,<span class="Apple-converted-space"> </span></p>
145 <p class="p11"><span class="s3"><span class="Apple-tab-span"> </span></span>\instrument<span class="s3">, </span>\help_SPE4_CFString2</p>
146 <p class="p7">);</p>
147 <p class="p12">// e is an EventStreamPlayer</p>
148 <p class="p7">e = p.play;</p>
149 <p class="p7">)</p>
150 <p class="p8"><br></p>
151 <p class="p7">(</p>
152 <p class="p12">// you can change the stream at any point in time</p>
153 <p class="p7">e.stream = <span class="s1">Pbind</span>(</p>
154 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">\degree</span>, <span class="s1">Pseq</span>([0,1,2,4,6,3,4,8],<span class="s1">inf</span>),<span class="Apple-converted-space"> </span></p>
155 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">\dur</span>, <span class="s1">Prand</span>([0.2,0.4,0.8],<span class="s1">inf</span>),<span class="Apple-converted-space"> </span></p>
156 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">\amp</span>, 0.05,<span class="Apple-converted-space"> </span></p>
157 <p class="p11"><span class="s3"><span class="Apple-tab-span"> </span></span>\octave<span class="s3">, 5,<span class="Apple-converted-space"> </span></span></p>
158 <p class="p11"><span class="s3"><span class="Apple-tab-span"> </span></span>\instrument<span class="s3">, </span>\help_SPE7_BerlinB<span class="s3">, </span><span class="s4">// you can also use a symbol<span class="Apple-converted-space"> </span></span></p>
159 <p class="p11"><span class="s3"><span class="Apple-tab-span"> </span></span>\ctranspose<span class="s3">, 0</span></p>
160 <p class="p7">).asStream;</p>
161 <p class="p7">)</p>
162 <p class="p8"><br></p>
163 <p class="p7">(</p>
164 <p class="p7">e.stream = <span class="s1">Pbind</span>(</p>
165 <p class="p11"><span class="s3"><span class="Apple-tab-span"> </span>[</span>\degree<span class="s3">, </span>\dur<span class="s3">], </span><span class="s1">Pseq</span><span class="s3">(</span></p>
166 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>[</p>
167 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s1">Pseq</span>([[0,0.1],[2,0.1],[3,0.1],[4,0.1],[5,0.8]],2),</p>
168 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s1">Ptuple</span>([<span class="s1">Pxrand</span>([6,7,8,9],4), 0.4]),</p>
169 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s1">Ptuple</span>([<span class="s1">Pseq</span>([9,8,7,6,5,4,3,2]), 0.2])</p>
170 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>], <span class="s1">inf</span></p>
171 <p class="p7"><span class="Apple-tab-span"> </span>),</p>
172 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">\amp</span>, 0.05,<span class="Apple-converted-space"> </span></p>
173 <p class="p11"><span class="s3"><span class="Apple-tab-span"> </span></span>\octave<span class="s3">, 5,<span class="Apple-converted-space"> </span></span></p>
174 <p class="p11"><span class="s3"><span class="Apple-tab-span"> </span></span>\instrument<span class="s3">, </span>\Help_SPE7_CFString1</p>
175 <p class="p7">).asStream;</p>
176 <p class="p7">)</p>
177 <p class="p4"><br></p>
178 <p class="p9">The following methods are possible because an <a href="EventStreamPlayer.html"><span class="s1">EventStreamPlayer</span></a> is a PauseStream:</p>
179 <p class="p4"><br></p>
180 <p class="p12"><span class="s3">e.mute; </span>// keeps playing, but replaces notes with rests</p>
181 <p class="p8"><br></p>
182 <p class="p7">e.unmute;</p>
183 <p class="p8"><br></p>
184 <p class="p12"><span class="s3">e.reset;<span class="Apple-converted-space">  </span></span>// reset the stream.</p>
185 <p class="p8"><br></p>
186 <p class="p12"><span class="s3">e.pause;<span class="Apple-converted-space">  </span></span>// will resume where paused.</p>
187 <p class="p8"><br></p>
188 <p class="p7">e.resume;</p>
189 <p class="p8"><br></p>
190 <p class="p12"><span class="s3">e.stop;<span class="Apple-converted-space">  </span></span>// will reset before resume.</p>
191 <p class="p8"><br></p>
192 <p class="p7">e.resume;</p>
193 <p class="p1"><br></p>
194 <p class="p1"><br></p>
195 <p class="p1"><br></p>
196 </body>
197 </html>