polymorphism: better names for .binaryValue and .booleanValue are .asInteger and...
[supercollider.git] / Help / Tutorials / Tutorial.html
blob52079a551a3039af28029d849c7f0f2c51d12a15
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.29">
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: 12.0px Helvetica; color: #0000ee}
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: 9.0px Monaco; color: #bf0000}
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: 14.0px Helvetica; min-height: 17.0px}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060}
20 p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0000bf}
21 p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #007300}
22 span.s1 {color: #000000}
23 span.s2 {color: #0000bf}
24 span.s3 {font: 14.0px Helvetica}
25 span.s4 {text-decoration: underline ; color: #0000bf}
26 span.s5 {color: #007300}
27 span.s6 {color: #606060}
28 span.s7 {color: #bf0000}
29 span.Apple-tab-span {white-space:pre}
30 </style>
31 </head>
32 <body>
33 <p class="p1"><b>SuperCollider 3 Server Tutorial</b></p>
34 <p class="p2"><br></p>
35 <p class="p3">To follow this tutorial you should read</p>
36 <p class="p2"><br></p>
37 <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span><a href="../ServerArchitecture/Server-Architecture.html"><b>Server-Architecture</b></a></span></p>
38 <p class="p3">and</p>
39 <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span><a href="../ServerArchitecture/Server-Command-Reference.html"><b>Server-Command-Reference</b></a></span></p>
40 <p class="p2"><b><span class="Apple-tab-span"> </span></b></p>
41 <p class="p2"><br></p>
42 <p class="p3">There are two parts to SuperCollider. One part is the language application and another is a synthesis server that can run either inside the language application, or as a separate program on the same machine, or run on a different computer across a network connection. The language application sends command messages to the server using a subset of the Open Sound Control protocol.</p>
43 <p class="p2"><br></p>
44 <p class="p5"><b>Booting a Server</b></p>
45 <p class="p2"><br></p>
46 <p class="p3">In order to run sound we need to start a server running. The easiest way to start a server is to click on one of the "Start Server" buttons in the server windows. Sometimes though it is useful to start a server programmatically. To do this we need to get or create a server object and tell it to "boot". Two servers, internal and local, are predefined.</p>
47 <p class="p2"><br></p>
48 <p class="p3">The internal server runs in the same process as the SuperCollider application. It is internal to the program itself.</p>
49 <p class="p2"><br></p>
50 <p class="p6">// set the interpreter variable s to the internal server object.</p>
51 <p class="p7">s = <span class="s2">Server</span>.internal;</p>
52 <p class="p8"><br></p>
53 <p class="p3"><span class="s3"><b>VERY IMPORTANT: </b></span>This line must be executed for the variable 's' to be set. The mechanics are different depending on your platform. The MacOSX standard is to place the cursor anywhere on this line and press the "Enter" key on the numeric keypad. Pressing the main return key does not execute code! This allows you to write code fragments of multiple lines. To execute a multi-line block of code, select the block and press "Enter." For convenience, a code block can be enclosed in parentheses, and the entire block selected by double-clicking just inside either parenthesis. (For instructions in other editors (e.g. on Linux or Windows), consult the documentation specific to that platform. See also the helpfile <a href="../Other Topics/Shortcuts.html"><span class="s4">Shortcuts</span></a> for key commands in other editors.)If you don't have an enter key, then you can use ctrl-Return, Ctrl-c, fn-Return( on Some Macs), or Shift-Return.</p>
54 <p class="p2"><br></p>
55 <p class="p3">The local server runs on the same machine as the SuperCollider application, but is a separate program, 'scsynth'. <b>Note:</b> By default the interpreter variable s is set to the local server at startup. For further information see the <a href="../ServerArchitecture/Server.html"><span class="s2"><b>Server</b></span></a> helpfile.</p>
56 <p class="p2"><br></p>
57 <p class="p6">// set the interpreter variable s to the local server object.</p>
58 <p class="p7">s = <span class="s2">Server</span>.local;</p>
59 <p class="p2"><br></p>
60 <p class="p3">To boot the server you send it the boot message.</p>
61 <p class="p2"><br></p>
62 <p class="p7"><span class="s2">s</span>.boot;</p>
63 <p class="p2"><br></p>
64 <p class="p3">To quit the server send it the quit message.</p>
65 <p class="p2"><br></p>
66 <p class="p7"><span class="s2">s</span>.quit;</p>
67 <p class="p2"><br></p>
68 <p class="p3">We can also create a server to run. To create a server object we need to provide the IP address or the server and a port number. Port numbers are somewhat arbitrary but they should not conflict with common protocols like telnet, ftp http, etc. The IP address 127.0.0.1 is defined to mean the local host. This is the IP address to use for running a server on your own machine.</p>
69 <p class="p2"><br></p>
70 <p class="p6">// create a server object that will run on the local host using port #58009</p>
71 <p class="p7">s = <span class="s2">Server</span>(<span class="s5">\myServer</span>, <span class="s2">NetAddr</span>(<span class="s6">"127.0.0.1"</span>, 58009));</p>
72 <p class="p8"><br></p>
73 <p class="p6"><span class="s1">s.boot; </span>//start the server</p>
74 <p class="p8"><br></p>
75 <p class="p6"><span class="s1">s.quit; </span>// quit the server</p>
76 <p class="p2"><br></p>
77 <p class="p3">It is not possible to boot a server on a remote machine, but if you have one running already or you know of one running, you can send messages to it. You create the server object using the IP address of the machine running the server and the port it is using.</p>
78 <p class="p2"><br></p>
79 <p class="p6">// create a server object for talking to the server running on a machine having</p>
80 <p class="p6">// IP address 192.168.0.47 using port #57110</p>
81 <p class="p7">s = <span class="s2">Server</span>(<span class="s5">\myServer</span>, <span class="s2">NetAddr</span>(<span class="s6">"192.168.0.47"</span>, 57110));</p>
82 <p class="p2"><br></p>
83 <p class="p5"><b>Making Sound</b></p>
84 <p class="p2"><br></p>
85 <p class="p3">(note: This tutorial uses raw OSC commands as described in <a href="../ServerArchitecture/Server-Command-Reference.html"><span class="s2"><b>Server-Command-Reference</b></span></a><b>, </b>rather than the classes <a href="../ServerArchitecture/Synth.html"><span class="s2"><b>Synth</b></span></a> and <a href="../ServerArchitecture/Group.html"><span class="s2"><b>Group</b></span></a>. See those helpfiles also for some simpler ways of working with Synths. This tutorial explains the basic underlying design of Synths and SynthDefs).</p>
86 <p class="p2"><br></p>
87 <p class="p3">Now lets make some audio.</p>
88 <p class="p2"><br></p>
89 <p class="p6"><span class="s1">s = </span><span class="s2">Server</span><span class="s1">.local; </span>// assign it to interpreter variable 's'</p>
90 <p class="p2"><br></p>
91 <p class="p3">Boot it.</p>
92 <p class="p2"><br></p>
93 <p class="p7">s.boot;</p>
94 <p class="p2"><br></p>
95 <p class="p3">Create a SynthDef. A SynthDef is a description of a processing module that you want to run on the server. It can read audio from the server's audio buses, read control from the control buses and write control or audio back to buses. Here we will create a sine oscillator and send it to audio bus zero.</p>
96 <p class="p2"><br></p>
97 <p class="p7">(</p>
98 <p class="p7"><span class="s2">SynthDef</span>(<span class="s6">"sine"</span>, { <span class="s2">arg</span> freq=800;</p>
99 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">var</span> osc;</p>
100 <p class="p7"><span class="Apple-tab-span"> </span>osc = <span class="s2">SinOsc</span>.ar(freq, 0, 0.1); <span class="s7">// 800 Hz sine oscillator</span></p>
101 <p class="p6"><span class="s1"><span class="Apple-tab-span"> </span></span><span class="s2">Out</span><span class="s1">.ar(0, osc); </span>// send output to audio bus zero.</p>
102 <p class="p6"><span class="s1">}).writeDefFile; </span>// write the def to disk in the default directory synthdefs/</p>
103 <p class="p7">)</p>
104 <p class="p2"><br></p>
105 <p class="p3">Send the SynthDef to the server.</p>
106 <p class="p2"><br></p>
107 <p class="p7">s.sendSynthDef(<span class="s6">"sine"</span>);</p>
108 <p class="p8"><br></p>
109 <p class="p3">Start the sound. The <b>/s_new</b> command creates a new Synth which is an instance of the "sine" SynthDef. Each synth running on the server needs to have a unique ID. The simplest and safest way to do this is to get an ID from the server's NodeIDAllocator. This will automatically allow IDs to be reused, and will prevent conflicts both with your own nodes, and with nodes created automatically for purposes such as visual scoping and recording. Each synth needs to be installed in a Group. We install it in group one which is the default group. There is a group zero, called the RootNode, which contains the default group, but it is generally best not to use it as doing so can result in order of execution issues with automatically created nodes such as those mentioned above. (For more detail see the <a href="../ServerArchitecture/default_group.html"><span class="s2"><b>default_group</b></span></a>, <a href="../ServerArchitecture/RootNode.html"><span class="s2"><b>RootNode</b></span></a>, and <a href="../ServerArchitecture/Order-of-execution.html"><span class="s2"><b>Order-of-execution</b></span></a> helpfiles.)</p>
110 <p class="p8"><br></p>
111 <p class="p7">s.sendMsg(<span class="s6">"/s_new"</span>, <span class="s6">"sine"</span>, x = s.nextNodeID, 1, 1);</p>
112 <p class="p2"><br></p>
113 <p class="p3">Stop the sound.</p>
114 <p class="p8"><br></p>
115 <p class="p7">s.sendMsg(<span class="s6">"/n_free"</span>, x);</p>
116 <p class="p8"><br></p>
117 <p class="p3">Stop the server.</p>
118 <p class="p8"><br></p>
119 <p class="p7">s.quit;</p>
120 <p class="p8"><br></p>
121 <p class="p3">SynthDef has two methods which send the def automatically, load which writes it to disk, and send which sends it without writing it to disk. The latter can be useful to avoid clutter on your drive.</p>
122 <p class="p2"><br></p>
123 <p class="p7">(</p>
124 <p class="p7"><span class="s2">SynthDef</span>(<span class="s6">"sine"</span>, { <span class="s2">arg</span> freq=800;</p>
125 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">var</span> osc;</p>
126 <p class="p7"><span class="Apple-tab-span"> </span>osc = <span class="s2">SinOsc</span>.ar(freq, 0, 0.1); <span class="s7">// 800 Hz sine oscillator</span></p>
127 <p class="p6"><span class="s1"><span class="Apple-tab-span"> </span></span><span class="s2">Out</span><span class="s1">.ar(0, osc); </span>// send output to audio bus zero.</p>
128 <p class="p6"><span class="s1">}).load(s); </span>// write to disk and send</p>
129 <p class="p7">)</p>
130 <p class="p8"><br></p>
131 <p class="p7">(</p>
132 <p class="p7"><span class="s2">SynthDef</span>(<span class="s6">"sine"</span>, { <span class="s2">arg</span> freq=800;</p>
133 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">var</span> osc;</p>
134 <p class="p7"><span class="Apple-tab-span"> </span>osc = <span class="s2">SinOsc</span>.ar(freq, 0, 0.1); <span class="s7">// 800 Hz sine oscillator</span></p>
135 <p class="p6"><span class="s1"><span class="Apple-tab-span"> </span></span><span class="s2">Out</span><span class="s1">.ar(0, osc); </span>// send output to audio bus zero.</p>
136 <p class="p6"><span class="s1">}).send(s); </span>// send without writing</p>
137 <p class="p7">)</p>
138 <p class="p9"><br></p>
139 <p class="p5"><b>Using Arguments</b></p>
140 <p class="p9"><br></p>
141 <p class="p3">It is useful to be able to specify parameters of a synth when it is created. Here a frequency argument is added to the sine SynthDef so that we can create it</p>
142 <p class="p2"><br></p>
143 <p class="p6"><span class="s1">s = </span><span class="s2">Server</span><span class="s1">.local; </span>// assign it to interpreter variable 's'</p>
144 <p class="p2"><br></p>
145 <p class="p7">s.boot;</p>
146 <p class="p8"><br></p>
147 <p class="p7">(</p>
148 <p class="p7"><span class="s2">SynthDef</span>(<span class="s6">"sine"</span>, { arg freq;</p>
149 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">var</span> osc;</p>
150 <p class="p7"><span class="Apple-tab-span"> </span>osc = <span class="s2">SinOsc</span>.ar(freq, 0, 0.1); <span class="s7">// 800 Hz sine oscillator</span></p>
151 <p class="p6"><span class="s1"><span class="Apple-tab-span"> </span></span><span class="s2">Out</span><span class="s1">.ar(0, osc); </span>// send output to audio bus zero.</p>
152 <p class="p7">}).add;</p>
153 <p class="p7">)</p>
154 <p class="p8"><br></p>
155 <p class="p8"><br></p>
156 <p class="p8"><br></p>
157 <p class="p3">Play a 900 Hz sine wave.</p>
158 <p class="p8"><br></p>
159 <p class="p7">s.sendMsg(<span class="s6">"/s_new"</span>, <span class="s6">"sine"</span>, x = s.nextNodeID, 1, 1, "freq", 900);</p>
160 <p class="p2"><br></p>
161 <p class="p7">s.sendMsg(<span class="s6">"/n_free"</span>, x);</p>
162 <p class="p8"><br></p>
163 <p class="p8"><br></p>
164 <p class="p3">Play a 1000 Hz sine wave.</p>
165 <p class="p8"><br></p>
166 <p class="p7">s.sendMsg(<span class="s6">"/s_new"</span>, <span class="s6">"sine"</span>, y = s.nextNodeID, 1, 1, "freq", 1000);</p>
167 <p class="p2"><br></p>
168 <p class="p7">s.sendMsg(<span class="s6">"/n_free"</span>, y);</p>
169 <p class="p8"><br></p>
170 <p class="p8"><br></p>
171 <p class="p3">Playing three voices at once</p>
172 <p class="p8"><br></p>
173 <p class="p7">(</p>
174 <p class="p7">s.sendMsg(<span class="s6">"/s_new"</span>, <span class="s6">"sine"</span>, x = s.nextNodeID, 1, 1, <span class="s6">"freq"</span>, 800);</p>
175 <p class="p7">s.sendMsg(<span class="s6">"/s_new"</span>, <span class="s6">"sine"</span>, y = s.nextNodeID, 1, 1, <span class="s6">"freq"</span>, 1001);</p>
176 <p class="p7">s.sendMsg(<span class="s6">"/s_new"</span>, <span class="s6">"sine"</span>, z = s.nextNodeID, 1, 1, <span class="s6">"freq"</span>, 1202);</p>
177 <p class="p7">)</p>
178 <p class="p8"><br></p>
179 <p class="p7">(</p>
180 <p class="p7">s.sendMsg(<span class="s6">"/n_free"</span>, x);</p>
181 <p class="p7">s.sendMsg(<span class="s6">"/n_free"</span>, y);</p>
182 <p class="p7">s.sendMsg(<span class="s6">"/n_free"</span>, z);</p>
183 <p class="p7">)</p>
184 <p class="p9"><br></p>
185 <p class="p3">Playing three voices at once using bundles. Bundles allow you to send multiple messages with a time stamp. The messages in the bundle will be scheduled to be performed together. The time argument to sendBundle is an offset into the future from the current thread's logical time.</p>
186 <p class="p8"><br></p>
187 <p class="p7">(</p>
188 <p class="p7">s.sendBundle(0.2,</p>
189 <p class="p7"><span class="Apple-tab-span"> </span>[<span class="s6">"/s_new"</span>, <span class="s6">"sine"</span>, x = s.nextNodeID, 1, 1, <span class="s6">"freq"</span>, 800],</p>
190 <p class="p7"><span class="Apple-tab-span"> </span>[<span class="s6">"/s_new"</span>, <span class="s6">"sine"</span>, y = s.nextNodeID, 1, 1, <span class="s6">"freq"</span>, 1001],</p>
191 <p class="p7"><span class="Apple-tab-span"> </span>[<span class="s6">"/s_new"</span>, <span class="s6">"sine"</span>, z = s.nextNodeID, 1, 1, <span class="s6">"freq"</span>, 1202]);</p>
192 <p class="p7">s.sendBundle(1.2, [<span class="s6">"/n_free"</span>, x],[<span class="s6">"/n_free"</span>, y],[<span class="s6">"/n_free"</span>, z]);</p>
193 <p class="p7">)</p>
194 <p class="p9"><br></p>
195 <p class="p5"><b>Controlling a Synth</b></p>
196 <p class="p9"><br></p>
197 <p class="p3">You can send messages to update the values of a Synth's arguments.</p>
198 <p class="p2"><br></p>
199 <p class="p3">Play a 900 Hz sine wave.</p>
200 <p class="p8"><br></p>
201 <p class="p7">s.sendMsg(<span class="s6">"/s_new"</span>, <span class="s6">"sine"</span>, x = s.nextNodeID, 1, 1, "freq", 900);</p>
202 <p class="p2"><br></p>
203 <p class="p3">Change the frequency using the <b>/n_set</b> command. You send the node ID, the parameter name and the value.</p>
204 <p class="p8"><br></p>
205 <p class="p7">s.sendMsg(<span class="s6">"/n_set"</span>, x, "freq", 800);</p>
206 <p class="p8"><br></p>
207 <p class="p7">s.sendMsg(<span class="s6">"/n_set"</span>, x, "freq", 700);</p>
208 <p class="p8"><br></p>
209 <p class="p7">s.sendMsg(<span class="s6">"/n_free"</span>, x);</p>
210 <p class="p9"><br></p>
211 <p class="p5"><b>Adding an Effect Dynamically</b></p>
212 <p class="p9"><br></p>
213 <p class="p3">You can dynamically add and remove an effect to process another synth. In order to do this, the effect has to be added after the node to be processed.</p>
214 <p class="p9"><br></p>
215 <p class="p7">(</p>
216 <p class="p6">// define a noise pulse</p>
217 <p class="p7"><span class="s2">SynthDef</span>(<span class="s6">"tish"</span>, { <span class="s2">arg</span> freq = 1200, rate = 2;</p>
218 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">var</span> osc, trg;</p>
219 <p class="p7"><span class="Apple-tab-span"> </span>trg = <span class="s2">Decay2</span>.ar(<span class="s2">Impulse</span>.ar(rate,0,0.3), 0.01, 0.3);</p>
220 <p class="p7"><span class="Apple-tab-span"> </span>osc = {<span class="s2">WhiteNoise</span>.ar(trg)}.dup;</p>
221 <p class="p6"><span class="s1"><span class="Apple-tab-span"> </span></span><span class="s2">Out</span><span class="s1">.ar(0, osc); </span>// send output to audio bus zero.</p>
222 <p class="p7">}).add;</p>
223 <p class="p7">)</p>
224 <p class="p9"><br></p>
225 <p class="p7">(</p>
226 <p class="p6">// define an echo effect</p>
227 <p class="p7"><span class="s2">SynthDef</span>(<span class="s6">"echo"</span>, { <span class="s2">arg</span> delay = 0.2, decay = 4;</p>
228 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">var</span> in;</p>
229 <p class="p7"><span class="Apple-tab-span"> </span>in = <span class="s2">In</span>.ar(0,2);</p>
230 <p class="p6"><span class="s1"><span class="Apple-tab-span"> </span></span>// use ReplaceOut to overwrite the previous contents of the bus.</p>
231 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">ReplaceOut</span>.ar(0, <span class="s2">CombN</span>.ar(in, 0.5, delay, decay, 1, in));</p>
232 <p class="p7">}).add;</p>
233 <p class="p7">)</p>
234 <p class="p8"><br></p>
235 <p class="p6">// start the pulse</p>
236 <p class="p7">s.sendMsg(<span class="s6">"/s_new"</span>, <span class="s6">"tish"</span>, x = s.nextNodeID, 1, 1, <span class="s5">\freq</span>, 200, <span class="s5">\rate</span>, 1.2);</p>
237 <p class="p8"><br></p>
238 <p class="p6">// add an effect</p>
239 <p class="p7">s.sendMsg(<span class="s6">"/s_new"</span>, <span class="s6">"echo"</span>, y = s.nextNodeID, 1, 1);</p>
240 <p class="p8"><br></p>
241 <p class="p6">// stop the effect</p>
242 <p class="p7">s.sendMsg(<span class="s6">"/n_free"</span>, y);</p>
243 <p class="p8"><br></p>
244 <p class="p6">// add an effect (time has come today.. hey!)</p>
245 <p class="p7">s.sendMsg(<span class="s6">"/s_new"</span>, <span class="s6">"echo"</span>, z = s.nextNodeID, 1, 1, <span class="s5">\delay</span>, 0.1, <span class="s5">\decay</span>, 4);</p>
246 <p class="p8"><br></p>
247 <p class="p6">// stop the effect</p>
248 <p class="p7">s.sendMsg(<span class="s6">"/n_free"</span>, z);</p>
249 <p class="p8"><br></p>
250 <p class="p6">// stop the pulse</p>
251 <p class="p7">s.sendMsg(<span class="s6">"/n_free"</span>, x);</p>
252 <p class="p9"><br></p>
253 <p class="p3">This works because we added the effect after the other node. Sometimes you will need to use groups or /n_after to insure that an effect gets added after what it is supposed to process.</p>
254 <p class="p9"><br></p>
255 <p class="p9"><br></p>
256 <p class="p5"><b>Mapping an Argument to a Control Bus</b></p>
257 <p class="p9"><br></p>
258 <p class="p7">(</p>
259 <p class="p6">// define a control</p>
260 <p class="p7"><span class="s2">SynthDef</span>(<span class="s6">"line"</span>, { <span class="s2">arg</span> i_bus=10, i_start=1000, i_end=500, i_time=1;</p>
261 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">ReplaceOut</span>.kr(i_bus, <span class="s2">Line</span>.kr(i_start, i_end, i_time, doneAction: 2));</p>
262 <p class="p7">}).add</p>
263 <p class="p7">)</p>
264 <p class="p9"><br></p>
265 <p class="p3">Play a 900 Hz sine wave.</p>
266 <p class="p8"><br></p>
267 <p class="p7">s.sendMsg(<span class="s6">"/s_new"</span>, <span class="s6">"sine"</span>, x = s.nextNodeID, 1, 1, "freq", 900);</p>
268 <p class="p2"><br></p>
269 <p class="p3">Put a frequency value on the control bus.</p>
270 <p class="p2"><br></p>
271 <p class="p7">s.sendMsg(<span class="s6">"/c_set"</span>, 10, x);</p>
272 <p class="p2"><br></p>
273 <p class="p3">Map the node's freq argument to read from control bus #10.</p>
274 <p class="p2"><br></p>
275 <p class="p7">s.sendMsg(<span class="s6">"/n_map"</span>, x, <span class="s5">\freq</span>, 10);</p>
276 <p class="p8"><br></p>
277 <p class="p3">Change the value on the control bus.</p>
278 <p class="p8"><br></p>
279 <p class="p7">s.sendMsg(<span class="s6">"/c_set"</span>, 10, 1200);</p>
280 <p class="p8"><br></p>
281 <p class="p3">Start a control process that writes to bus #10.</p>
282 <p class="p3">The EnvGen doneAction will free this node automatically when it finishes.</p>
283 <p class="p2"><br></p>
284 <p class="p7">s.sendMsg(<span class="s6">"/s_new"</span>, <span class="s6">"line"</span>, s.nextNodeID, 0, 1);</p>
285 <p class="p2"><br></p>
286 <p class="p3">Free the node.</p>
287 <p class="p2"><br></p>
288 <p class="p7">s.sendMsg(<span class="s6">"/n_free"</span>, x);</p>
289 <p class="p8"><br></p>
290 <p class="p9"><br></p>
291 <p class="p5"><b>Sequencing with Routines</b></p>
292 <p class="p9"><br></p>
293 <p class="p7">(</p>
294 <p class="p7"><span class="s2">var</span> space,offset,timer, saw, envsaw, sampler, delay;</p>
295 <p class="p8"><br></p>
296 <p class="p7"><span class="s2">SynthDef</span>(<span class="s6">"saw"</span>,{ <span class="s2">arg</span> out=100, pan=0, trig=0.0, freq=500, amp=1, cutoff=10000, rezz=1;</p>
297 <p class="p7"><span class="Apple-tab-span"> </span>freq = Lag.kr(freq,0.1);</p>
298 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">Out</span>.ar(out,<span class="s2">Pan2</span>.ar(<span class="s2">RLPF</span>.ar(<span class="s2">Saw</span>.ar(<span class="s2">[freq,freq*2]</span>,amp),cutoff,rezz),</p>
299 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>pan));</p>
300 <p class="p7"><span class="Apple-tab-span"> </span>}).load(s);</p>
301 <p class="p8"><br></p>
302 <p class="p7"><span class="s2">SynthDef</span>(<span class="s6">"envsaw"</span>,{ <span class="s2">arg</span> out=100, pan=0, sustain=0.5, freq=500, amp=1, cutoff=10000, rezz=1;</p>
303 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">var</span> env;</p>
304 <p class="p7"><span class="Apple-tab-span"> </span>env = <span class="s2">EnvGen</span>.kr(<span class="s2">Env</span>.perc(0.01, sustain, 0.2), doneAction:0, gate:amp);</p>
305 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">Out</span>.ar(out,<span class="s2">Pan2</span>.ar(<span class="s2">RLPF</span>.ar(<span class="s2">Saw</span>.ar(<span class="s2">Lag</span>.kr(freq,0.1),env),cutoff,rezz)*amp,</p>
306 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>pan));</p>
307 <p class="p7"><span class="Apple-tab-span"> </span>}).load(s);</p>
308 <p class="p8"><br></p>
309 <p class="p7"><span class="s2">SynthDef</span>(<span class="s6">"delay"</span>, { <span class="s2">arg</span> out=0, delay = 0.4, decay = 14;</p>
310 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">var</span> in;</p>
311 <p class="p7"><span class="Apple-tab-span"> </span>in = <span class="s2">In</span>.ar(out,2);</p>
312 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">Out</span>.ar(out, <span class="s2">CombN</span>.ar(in, 0.5, delay, decay, 1, in));</p>
313 <p class="p7">}).load(s);</p>
314 <p class="p8"><br></p>
315 <p class="p7"><span class="s2">SynthDef</span>(<span class="s6">"sampler"</span>,{ <span class="s2">arg</span> sample, trig=1,rate=1.0,out=0,bufnum=0,pan=0,amp=1, sustain=0.25;</p>
316 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">var</span> env;</p>
317 <p class="p7"><span class="Apple-tab-span"> </span>env = <span class="s2">EnvGen</span>.kr(<span class="s2">Env</span>.perc(0.001, sustain, 0.001), doneAction:2);</p>
318 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">Out</span>.ar(out,</p>
319 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">Pan2</span>.ar(</p>
320 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">PlayBuf</span>.ar(1,bufnum,rate,<span class="s2">InTrig</span>.kr(trig),0,0)*amp,</p>
321 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>pan);</p>
322 <p class="p7"><span class="Apple-tab-span"> </span>)</p>
323 <p class="p7">}).load(s);</p>
324 <p class="p8"><br></p>
325 <p class="p7"><span class="s2">Tempo</span>.bpm = 120;</p>
326 <p class="p7">timer=<span class="s2">BeatSched</span>.new;</p>
327 <p class="p7">offset = <span class="s2">Tempo</span>.tempo.reciprocal;</p>
328 <p class="p8"><br></p>
329 <p class="p10"><span class="s1">space = </span><span class="s2">Buffer</span><span class="s1">.read(s,</span>"sounds/a11wlk01.wav"<span class="s1">);</span></p>
330 <p class="p8"><br></p>
331 <p class="p7">saw=<span class="s2">Synth</span>(<span class="s6">"saw"</span>);</p>
332 <p class="p10"><span class="s1">delay=</span><span class="s2">Synth.after</span><span class="s1">(saw,</span>"delay", [\decay, 20]<span class="s1">);</span></p>
333 <p class="p8"><br></p>
334 <p class="p7">timer.sched(0,{</p>
335 <p class="p11"><span class="Apple-tab-span"> </span>var r;</p>
336 <p class="p7"><span class="s2"><span class="Apple-tab-span"> </span>r=Routine</span>({ <span class="s2">var</span> wait, freq, cutoff,rezz;</p>
337 <p class="p7"><span class="Apple-tab-span"> </span>wait = <span class="s2">Pseq</span>([2],<span class="s2">inf</span>).asStream;</p>
338 <p class="p7"><span class="Apple-tab-span"> </span>freq = <span class="s2">Pseq</span>([30,40,42,40],<span class="s2">inf</span>).asStream;</p>
339 <p class="p7"><span class="Apple-tab-span"> </span>cutoff = Pfunc({500.rand2+1000}).asStream;</p>
340 <p class="p7"><span class="Apple-tab-span"> </span>rezz = 0.5;</p>
341 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">inf</span>.do({saw.set(<span class="s6">"freq"</span>, freq.next.midicps, <span class="s6">"cutoff"</span>, cutoff.next, <span class="s6">"rezz"</span>, rezz, <span class="s6">"amp"</span>, 0.1, <span class="s6">"out"</span>, 0);</p>
342 <p class="p7">(wait.next*offset).wait});});</p>
343 <p class="p7">timer.sched(0,r);</p>
344 <p class="p7">});</p>
345 <p class="p8"><br></p>
346 <p class="p7">timer.sched(0,{</p>
347 <p class="p11"><span class="Apple-tab-span"> </span>var r;</p>
348 <p class="p7"><span class="s2"><span class="Apple-tab-span"> </span>r=Routine</span>({ <span class="s2">var</span> wait, rate;</p>
349 <p class="p7"><span class="Apple-tab-span"> </span>wait = <span class="s2">Pseq</span>([0.25],<span class="s2">inf</span>).asStream;</p>
350 <p class="p7"><span class="Apple-tab-span"> </span>rate = Pfunc({0.5.rand}).asStream;</p>
351 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">inf</span>.do({<span class="s2">Synth</span>.before(delay, <span class="s6">"sampler"</span>, [<span class="s5">\bufnum</span>, space, <span class="s5">\trig</span>, 1, <span class="s5">\amp</span>,0.1, <span class="s5">\rate</span>, <span class="s2">rate.next</span>, <span class="s5">\sustain</span>, wait.next]);</p>
352 <p class="p7">(wait.next*offset).wait});});</p>
353 <p class="p7">timer.sched(0,r);</p>
354 <p class="p7">});</p>
355 <p class="p8"><br></p>
356 <p class="p7">)</p>
357 <p class="p9"><br></p>
358 <p class="p9"><br></p>
359 <p class="p5"><b>Sequencing with Patterns</b></p>
360 <p class="p7">(</p>
361 <p class="p6">//sappy emo electronica example...</p>
362 <p class="p7"><span class="s2">Tempo</span>.bpm = 120;</p>
363 <p class="p10"><span class="s2">SynthDef</span><span class="s1">(</span>"patternefx_Ex"<span class="s1">, { </span><span class="s2">arg</span><span class="s1"> out, in;</span></p>
364 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">var</span> audio, efx;</p>
365 <p class="p7"><span class="Apple-tab-span"> </span>audio = <span class="s2">In</span>.ar([20,21],2);</p>
366 <p class="p7"><span class="Apple-tab-span"> </span>efx=<span class="s2">CombN</span>.ar(audio, 0.5, [0.24,0.4], 2, 1);</p>
367 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">Out</span>.ar([0,1], audio+efx);</p>
368 <p class="p7"><span class="Apple-tab-span"> </span>}).load(s);</p>
369 <p class="p8"><br></p>
370 <p class="p10"><span class="s2">Synth</span><span class="s1">.new(</span>"patternefx_Ex"<span class="s1">);</span></p>
371 <p class="p8"><br></p>
372 <p class="p7"><span class="s2">SynthDef</span>(<span class="s6">"pattern_Ex"</span>, { <span class="s2">arg</span> out, freq = 1000, gate = 1, pan = 0, cut = 4000, rez = 0.8, amp = 1;</p>
373 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">Out</span>.ar(out,</p>
374 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">Pan2</span>.ar(</p>
375 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">RLPF</span>.ar(</p>
376 <p class="p7"><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="s2">Pulse</span>.ar(freq,0.05),</p>
377 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>cut, rez),</p>
378 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>pan) * <span class="s2">EnvGen</span>.kr(<span class="s2">Env</span>.linen(0.01, 1, 0.3), gate, amp, doneAction:2);</p>
379 <p class="p7"><span class="Apple-tab-span"> </span>)</p>
380 <p class="p7"><span class="Apple-tab-span"> </span>}).load(s);</p>
381 <p class="p8"><br></p>
382 <p class="p7"><span class="s2">SynthDef</span>(<span class="s6">"bass_Ex"</span>, { <span class="s2">arg</span> out, freq = 1000, gate = 1, pan = 0, cut = 4000, rez = 0.8, amp = 1;</p>
383 <p class="p7"><span class="Apple-tab-span"> </span><span class="s2">Out</span>.ar(out,</p>
384 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">Pan2</span>.ar(</p>
385 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">RLPF</span>.ar(</p>
386 <p class="p7"><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="s2">SinOsc</span>.ar(freq,0.05),</p>
387 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>cut, rez),</p>
388 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>pan) * <span class="s2">EnvGen</span>.kr(<span class="s2">Env</span>.linen(0.01, 1, 0.3), gate, amp, doneAction:2);</p>
389 <p class="p7"><span class="Apple-tab-span"> </span>)</p>
390 <p class="p7"><span class="Apple-tab-span"> </span>}).load(s);</p>
391 <p class="p8"><br></p>
392 <p class="p7"><span class="s2">SynthDescLib</span>.global.read;</p>
393 <p class="p8"><br></p>
394 <p class="p8"><br></p>
395 <p class="p11">Pseq<span class="s1">([</span></p>
396 <p class="p8"><br></p>
397 <p class="p11">Ptpar<span class="s1">([</span></p>
398 <p class="p7">0,<span class="s2">Pbind</span>(<span class="s5">\instrument</span>,<span class="s5">\pattern_Ex</span>, <span class="s5">\out</span>, 20, <span class="s5">\dur</span>,<span class="s2">Pseq</span>([2],16), <span class="s5">\root</span>,[-24,-17], <span class="s5">\degree</span>,<span class="s2">Pseq</span>([0,3,5,7,9,11,5,1],2), <span class="Apple-tab-span"> </span><span class="s5">\pan</span>,1,<span class="s5">\cut</span>,<span class="s2">Pxrand</span>([1000,500,2000,300],16), <span class="s5">\rez</span>,<span class="s2">Pfunc</span>({0.7.rand +0.3}), <span class="s5">\amp</span>,0.12),</p>
399 <p class="p8"><br></p>
400 <p class="p7">0.5,<span class="s2">Pbind</span>(<span class="s5">\instrument</span>,<span class="s5">\pattern_Ex</span>, <span class="s5">\out</span>, 20, <span class="s5">\dur</span>,<span class="s2">Pseq</span>([<span class="s2">Pseq</span>([2],15),1.5],1), <span class="s5">\root</span>,-12, <span class="s5">\degree</span>,<span class="s2">Pseq</span>([0,3,5,7,9,11,5,1],2), <span class="s5">\pan</span>,-1,<span class="s5">\cut</span>,2000, <span class="s5">\rez</span>,0.6, <span class="s5">\amp</span>,0.1);</p>
401 <p class="p7">]),</p>
402 <p class="p8"><br></p>
403 <p class="p11">Ptpar<span class="s1">([</span></p>
404 <p class="p7">0,<span class="s2">Pbind</span>(<span class="s5">\instrument</span>,<span class="s5">\pattern_Ex</span>, <span class="s5">\out</span>, 20, <span class="s5">\dur</span>,2, <span class="s5">\root</span>,[-24,-17], <span class="s5">\degree</span>,<span class="s2">Pseq</span>([0,3,5,7,9,11,5,1],<span class="s2">inf</span>), <span class="Apple-tab-span"> </span><span class="s5">\pan</span>,1,<span class="s5">\cut</span>,<span class="s2">Pxrand</span>([1000,500,2000,300],<span class="s2">inf</span>), <span class="s5">\rez</span>,<span class="s2">Pfunc</span>({0.7.rand +0.3}), <span class="s5">\amp</span>,0.12),</p>
405 <p class="p8"><br></p>
406 <p class="p12"><span class="s1">0,</span><span class="s2">Pbind</span><span class="s1">(</span>\instrument<span class="s1">,</span>\bass_Ex<span class="s1">, </span>\dur<span class="s1">,1, </span>\root<span class="s1">,-24, </span>\degree<span class="s1">,</span><span class="s2">Pseq</span><span class="s1">([0],</span><span class="s2">inf</span><span class="s1">), </span>\pan<span class="s1">,0, </span>\cut<span class="s1">,128, </span>\rez<span class="s1">,0.1, </span>\amp<span class="s1">,0.3),</span></p>
407 <p class="p8"><br></p>
408 <p class="p7">0.5,<span class="s2">Pbind</span>(<span class="s5">\instrument</span>,<span class="s5">\pattern_Ex</span>, <span class="s5">\out</span>, 20, <span class="s5">\dur</span>,2, <span class="s5">\root</span>,-12, <span class="s5">\degree</span>,<span class="s2">Pseq</span>([0,3,5,7,9,11,5,1],<span class="s2">inf</span>), <span class="s5">\pan</span>,-1,<span class="s5">\cut</span>,2000, <span class="s5">\rez</span>,0.6, <span class="s5">\amp</span>,0.1);</p>
409 <p class="p7">]);</p>
410 <p class="p8"><br></p>
411 <p class="p8"><br></p>
412 <p class="p7">]).play;</p>
413 <p class="p8"><br></p>
414 <p class="p7">)</p>
415 <p class="p9"><br></p>
416 </body>
417 </html>