Include a header file required for build on mac 10.4
[supercollider.git] / Help / Libraries / JITLib / tutorials / jitlib_efficiency.html
blob97613cf5f363b1710c0bb4bc1e60b196b3ac86b8
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 Futura}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.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; min-height: 14.0px}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #000000}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0000bf}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #000000; min-height: 12.0px}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #000000}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica; color: #000000}
20 span.s1 {color: #0000ff}
21 span.s2 {color: #0000bf}
22 span.s3 {color: #000000}
23 span.s4 {color: #007300}
24 span.s5 {color: #606060}
25 span.s6 {font: 9.0px Monaco}
26 span.Apple-tab-span {white-space:pre}
27 </style>
28 </head>
29 <body>
30 <p class="p1">Efficient coding with NodeProxy</p>
31 <p class="p2"><br></p>
32 <p class="p2"><br></p>
33 <p class="p3"><span class="s1"><a href="../nodeproxy/NodeProxy.html">NodeProxy</a></span> (and, in disguise <a href="../environments/ProxySpace.html"><span class="s1">ProxySpace</span></a>) represent "pronouns", placeholders for all kinds</p>
34 <p class="p3">of sound producing objects that are able to write to a specific bus on the server.</p>
35 <p class="p4"><br></p>
36 <p class="p3">To prepare such an object for playing, different objects require different things,</p>
37 <p class="p3">some very little, some more. As working with the placeolders does not show directly</p>
38 <p class="p3">which actions are very efficient and whic ones are not, it is shown here more in detail.</p>
39 <p class="p4"><br></p>
40 <p class="p3">This is also important if you want to automate certain processes (e.g. for control by an external interface or a task) - some things (a) are not meant to be used in certain ways and better solutions should be used instead then, others are much more efficient (b, c)</p>
41 <p class="p4"><br></p>
42 <p class="p4"><br></p>
43 <p class="p2"><br></p>
44 <p class="p5">a = <span class="s2">NodeProxy</span>.audio;</p>
45 <p class="p6">ProxySpace<span class="s3">.push;</span></p>
46 <p class="p7"><br></p>
47 <p class="p5">a.source = ... is equivalent to ~a = ...</p>
48 <p class="p5">a.add(...) a.put(0,...) a[0] = ... ~a[0] = ... are equivalent in cpu load.</p>
49 <p class="p7"><br></p>
50 <p class="p8"><b>a) rebuild and send: </b>manual rate</p>
51 <p class="p8">the following code requires a rebuild and send of a <span class="s2">SynthDef</span> and is thus most cpu-expensive.</p>
52 <p class="p8">though fine for slower use (esp.hand-use) for automatisation it is better to build a synthdef and assign it</p>
53 <p class="p7"><br></p>
54 <p class="p5"><span class="Apple-tab-span"> </span>~a = { someUGenFunction };<span class="Apple-converted-space"> </span></p>
55 <p class="p5"><span class="Apple-tab-span"> </span>~a = <span class="s2">Patch</span>(instrname, args);</p>
56 <p class="p5"><span class="Apple-tab-span"> </span>~a = <span class="s2">SynthDef</span>(<span class="s4">\name</span>, { someUGenFunction });</p>
57 <p class="p7"><span class="Apple-tab-span"> </span></p>
58 <p class="p7"><span class="Apple-tab-span"> </span></p>
59 <p class="p9"><span class="s3"><span class="Apple-tab-span"> </span></span>// the same applies to rebuilding the graphs:</p>
60 <p class="p5"><span class="Apple-tab-span"> </span>~a.rebuild</p>
61 <p class="p7"><span class="Apple-tab-span"> </span></p>
62 <p class="p9"><span class="s3"><span class="Apple-tab-span"> </span></span>// this rebuild is also used when setting one of the following properties:</p>
63 <p class="p5"><span class="Apple-tab-span"> </span>server, bus, setRates</p>
64 <p class="p7"><span class="Apple-tab-span"> </span></p>
65 <p class="p8"><b>b) starting synths and tasks</b></p>
66 <p class="p8">the following code sends commands to the server to start synths, which is load mainly on the server</p>
67 <p class="p8">and depends on the characteristics of the synthdef:</p>
68 <p class="p7"><br></p>
69 <p class="p9"><span class="s3"><span class="Apple-tab-span"> </span>~a = </span><span class="s4">\synthDefName</span><span class="s3">; </span>// the synthdef is already on the server</p>
70 <p class="p5"><span class="Apple-tab-span"> </span>~a = <span class="s2">Pbind</span>(<span class="s4">\instrument</span>, name, <span class="s4">\freq</span>, ...);</p>
71 <p class="p5"><span class="Apple-tab-span"> </span>~a = <span class="s2">Routine</span>({ loop({ s.sendMsg(<span class="s5">"/s_new"</span>, name, ...)})<span class="Apple-converted-space">  </span>});</p>
72 <p class="p7"><span class="Apple-tab-span"> </span></p>
73 <p class="p9"><span class="s3"><span class="Apple-tab-span"> </span>~a.refresh; ~a.wakeUp; </span>// waking up a stopped proxy does not require a resend</p>
74 <p class="p7"><span class="Apple-tab-span"> </span></p>
75 <p class="p8"><span class="s6"><span class="Apple-tab-span"> </span></span>these resend the synth with new properies</p>
76 <p class="p7"><br></p>
77 <p class="p9"><span class="s3"><span class="Apple-tab-span"> </span>~a.send(...) </span>// directly sends a message. the mapping bundle of the proxy is cached</p>
78 <p class="p5"><span class="Apple-tab-span"> </span>~a.sendAll(...)</p>
79 <p class="p7"><span class="Apple-tab-span"> </span></p>
80 <p class="p9"><span class="s3"><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>// for the following the bundle is recalculated if a new key is assigned.</p>
81 <p class="p9"><span class="s3"><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>// if you use the same key with a different value, the bundle is modified</p>
82 <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></p>
83 <p class="p5"><span class="Apple-tab-span"> </span>~a.xset(...) ~a.xmap(...)<span class="Apple-converted-space"> <span class="Apple-tab-span"> </span></span></p>
84 <p class="p5"><span class="Apple-tab-span"> </span>~a.nodeMap_(a map)<span class="Apple-converted-space"> </span></p>
85 <p class="p5"><span class="Apple-tab-span"> </span>~a.fadeToMap(a map)</p>
86 <p class="p7"><span class="Apple-tab-span"> </span></p>
87 <p class="p9"><span class="s3"><span class="Apple-tab-span"> </span></span>// synthdefs for these things are on the server already.</p>
88 <p class="p7"><span class="Apple-tab-span"> </span></p>
89 <p class="p5"><span class="Apple-tab-span"> </span>~a.gate, ~a.env, ~a.line, ~a.xline</p>
90 <p class="p7"><span class="Apple-tab-span"> </span></p>
91 <p class="p9"><span class="s3"><span class="Apple-tab-span"> </span></span>// some more calculations have to be made on client side, so if automated, it is better to use</p>
92 <p class="p9"><span class="s3"><span class="Apple-tab-span"> </span></span>// the above or a lag in the synth.<span class="Apple-converted-space"> </span></p>
93 <p class="p7"><span class="Apple-tab-span"> </span></p>
94 <p class="p5"><span class="Apple-tab-span"> </span>~a.lineAt(key), ~a.xlineAt(key)</p>
95 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-converted-space"> </span></p>
96 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-converted-space"> </span></p>
97 <p class="p7"><span class="Apple-tab-span"> </span></p>
98 <p class="p8"><b>c) sending messages to running synths: </b>for these the least calculation has to be done</p>
99 <p class="p7"><br></p>
100 <p class="p7"><br></p>
101 <p class="p9"><span class="s3"><span class="Apple-tab-span"> </span>~a.set(</span><span class="s4">\freq</span><span class="s3">, 400, </span><span class="s4">\dt</span><span class="s3">, 0.2); ~a.unset(</span><span class="s4">\freq</span><span class="s3">); </span>// if running the bundle will be recalculated</p>
102 <p class="p5"><span class="Apple-tab-span"> </span>~a.map(<span class="s4">\freq</span>, ~lfo); ~a.unmap(<span class="s4">\freq</span>);</p>
103 <p class="p5"><span class="Apple-tab-span"> </span>~a.fadeTime = 2;</p>
104 <p class="p5"><span class="Apple-tab-span"> </span>~a.gateAt(key)<span class="Apple-tab-span"> </span></p>
105 <p class="p7"><span class="Apple-tab-span"> </span></p>
106 <p class="p9"><span class="s3"><span class="Apple-tab-span"> </span></span>// for avoiding bundle recalculation you can directly talk to the group.</p>
107 <p class="p9"><span class="s3"><span class="Apple-tab-span"> </span></span>// this setting will not be kept when you exchange the synth</p>
108 <p class="p5"><span class="Apple-tab-span"> </span>~a.group.set(<span class="s4">\freq</span>, 500);</p>
109 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-converted-space"> </span></p>
110 <p class="p7"><span class="Apple-tab-span"> </span></p>
111 <p class="p7"><span class="Apple-tab-span"> </span></p>
112 <p class="p7"><span class="Apple-tab-span"> </span></p>
113 <p class="p7"><span class="Apple-tab-span"> </span></p>
114 <p class="p10"><b>switching audio (this can now be done with map!)</b></p>
115 <p class="p10"><b>todo: rewrite this part.</b></p>
116 <p class="p7"><br></p>
117 <p class="p8"><span class="s6"><span class="Apple-tab-span"> </span></span>control rate sources can be easily and efficiently switched using <b>map</b> or <b>xmap</b>.</p>
118 <p class="p8"><span class="Apple-tab-span"> </span>here is an example of how already running audio rate inputs can be switched.</p>
119 <p class="p8"><span class="Apple-tab-span"> </span>it is about as efficient as (b) - first example (setting a defname)</p>
120 <p class="p8"><span class="Apple-tab-span"> </span>it works only for 1 or 2 channels right now.</p>
121 <p class="p7"><br></p>
122 <p class="p7"><br></p>
123 <p class="p5">(</p>
124 <p class="p5">s.boot;</p>
125 <p class="p5">p = <span class="s2">ProxySpace</span>.push(s);</p>
126 <p class="p5">)</p>
127 <p class="p7"><br></p>
128 <p class="p7"><br></p>
129 <p class="p7"><br></p>
130 <p class="p5">~out.play;</p>
131 <p class="p7"><br></p>
132 <p class="p5">~s1 = { <span class="s2">Blip</span>.ar(<span class="s2">Rand</span>(32,15), 100, 0.5) };</p>
133 <p class="p5">~s2 = { <span class="s2">SinOsc</span>.ar(740, 0, 0.1) };</p>
134 <p class="p5">~s3 = { <span class="s2">Pulse</span>.ar(140, 0.2, 0.1) };</p>
135 <p class="p7"><br></p>
136 <p class="p7"><br></p>
137 <p class="p5">~out = {<span class="Apple-converted-space">  </span><span class="s2">Pan2</span>.ar(~mix.ar(1), <span class="s2">MouseX</span>.kr(-1,1)) };</p>
138 <p class="p7"><br></p>
139 <p class="p5">~mix.read(~s1);</p>
140 <p class="p5">~mix.read(~s2);</p>
141 <p class="p5">~mix.read(~s3);</p>
142 <p class="p7"><br></p>
143 <p class="p9">//resetting the source stops reading</p>
144 <p class="p5">~mix = <span class="s4">\default</span>;</p>
145 <p class="p7"><br></p>
146 <p class="p9">//now you can also crossfade audio efficiently:</p>
147 <p class="p5">~mix.fadeTime = 1.5;</p>
148 <p class="p7"><br></p>
149 <p class="p5">~mix.read(~s1);</p>
150 <p class="p5">~mix.read(~s2);</p>
151 <p class="p5">~mix.read(~s3);</p>
152 <p class="p7"><br></p>
153 <p class="p9">// automation:</p>
154 <p class="p5">(</p>
155 <p class="p5">t = <span class="s2">Task</span>({</p>
156 <p class="p5"><span class="Apple-tab-span"> </span><span class="s2">var</span> dt;</p>
157 <p class="p5"><span class="Apple-tab-span"> </span>loop({</p>
158 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>dt = rrand(0.01, 0.3);</p>
159 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>~mix.fadeTime = dt;</p>
160 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>~mix.read([~s1, ~s2, ~s3].choose);</p>
161 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>dt.wait;</p>
162 <p class="p5"><span class="Apple-tab-span"> </span>});</p>
163 <p class="p5">});</p>
164 <p class="p5">)</p>
165 <p class="p7"><br></p>
166 <p class="p6"><span class="s3">t.play(</span>SystemClock<span class="s3">);</span></p>
167 <p class="p7"><br></p>
168 <p class="p7"><br></p>
169 <p class="p9">// change the sources meanwhile:</p>
170 <p class="p5">~s1 = { <span class="s2">Blip</span>.ar(105, 100, 0.2) };</p>
171 <p class="p5">~s2 = { <span class="s2">SinOsc</span>.ar(350, 0, 0.1) };</p>
172 <p class="p5">~s3 = { <span class="s2">Pulse</span>.ar(60, 0.2, 0.1) };</p>
173 <p class="p7"><br></p>
174 <p class="p5">~freq = { <span class="s2">MouseX</span>.kr(200, 600, 2) };</p>
175 <p class="p7"><br></p>
176 <p class="p5">~s1 = { <span class="s2">Blip</span>.ar(~freq.kr * 0.3, 10, 0.2) };</p>
177 <p class="p5">~s2 = { <span class="s2">SinOsc</span>.ar(~freq.kr, 0, 0.1) };</p>
178 <p class="p5">~s3 = { <span class="s2">Pulse</span>.ar(~freq.kr * 0.2, 0.2, 0.1) };</p>
179 <p class="p7"><br></p>
180 <p class="p7"><span class="Apple-tab-span"> </span></p>
181 <p class="p5">t.stop;</p>
182 <p class="p7"><span class="Apple-tab-span"> </span></p>
183 <p class="p9">// note that when restarting ~out, the inputs have to be woken up.</p>
184 <p class="p9">// to avoid this, you can add the inputs to the mix nodeMap parents:</p>
185 <p class="p7"><br></p>
186 <p class="p5">~mix.nodeMap.parents.putAll( (s1: ~s1, s2: ~s2, s3: ~s3) );</p>
187 <p class="p7"><br></p>
188 <p class="p9">// also the task can be added to the proxy:</p>
189 <p class="p5">(</p>
190 <p class="p5">~mix.task = <span class="s2">Routine</span>({</p>
191 <p class="p5"><span class="Apple-tab-span"> </span>loop({</p>
192 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>~mix.fadeTime = rrand(0.01, 0.1);</p>
193 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>~mix.read([~s1, ~s2, ~s3].choose);</p>
194 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>[0.2, 0.4].choose.wait;</p>
195 <p class="p5"><span class="Apple-tab-span"> </span>});</p>
196 <p class="p5">});</p>
197 <p class="p5">)</p>
198 <p class="p7"><br></p>
199 </body>
200 </html>