polymorphism: better names for .binaryValue and .booleanValue are .asInteger and...
[supercollider.git] / Help / Libraries / JITLib / tutorials / jitlib_basic_concepts_04.html
blobfc32c5b93c7daff2528f496066780f7550e94d11
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: 15.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: #0021e7}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.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: #982316}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #982316; min-height: 12.0px}
19 span.s1 {font: 36.0px Georgia}
20 span.s2 {color: #000000}
21 span.s3 {text-decoration: underline}
22 span.s4 {color: #0025b2}
23 span.s5 {color: #0000ff}
24 span.s6 {color: #982316}
25 span.s7 {color: #416d1f}
26 span.Apple-tab-span {white-space:pre}
27 </style>
28 </head>
29 <body>
30 <p class="p1"><b>Timing in NodeProxy<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b><span class="s1"><b>4</b></span></p>
31 <p class="p2"><br></p>
32 <p class="p3">Changes that happen to NodeProxy, most importantly setting its source, are normally done</p>
33 <p class="p3">whenever the put method is called (or, in ProxySpace, the assignment operation = ).</p>
34 <p class="p3">Sometimes it is desirable to time these changes relative to a clock.</p>
35 <p class="p2"><br></p>
36 <p class="p4"><span class="s2">previous: <a href="jitlib_basic_concepts_03.html"><span class="s3">jitlib_basic_concepts_03</span></a><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>next: <a href="../JITLib.html"><span class="s3">JITLib</span></a></span></p>
37 <p class="p2"><br></p>
38 <p class="p2"><br></p>
39 <p class="p3">a) clock</p>
40 <p class="p3">b) quant and offset</p>
41 <p class="p3">c) client and server tempo</p>
42 <p class="p3">d) sample accurate output</p>
43 <p class="p2"><br></p>
44 <p class="p2"><br></p>
45 <p class="p2"><br></p>
46 <p class="p3"><b>a) clock</b></p>
47 <p class="p2"><br></p>
48 <p class="p3">generally, every node proxy can have its own time base, usually</p>
49 <p class="p3">a tempo clock. the clock is responsible for the timing of insertion</p>
50 <p class="p3">of new functions, per default at the next beat of the clock.</p>
51 <p class="p5"><br></p>
52 <p class="p5"><br></p>
53 <p class="p5"><br></p>
54 <p class="p6">p = <span class="s4">ProxySpace</span>.push(s.boot);</p>
55 <p class="p6">~x.play; ~y.play;</p>
56 <p class="p5"><br></p>
57 <p class="p7">// these two synths are started at the time when they are inserted:</p>
58 <p class="p6">~x = { <span class="s4">Ringz</span>.ar(<span class="s4">Impulse</span>.ar(1), 400, 0.05).dup };</p>
59 <p class="p6">~y = { <span class="s4">Ringz</span>.ar(<span class="s4">Impulse</span>.ar(1), 600, 0.05).dup };</p>
60 <p class="p5"><br></p>
61 <p class="p7">// adding a common clock:</p>
62 <p class="p6">~x.clock = <span class="s4">TempoClock</span>.default; ~x.quant = 1.0;</p>
63 <p class="p6">~y.clock = <span class="s4">TempoClock</span>.default; ~y.quant = 1.0;</p>
64 <p class="p5"><br></p>
65 <p class="p7">// now they are in sync</p>
66 <p class="p6">~x = { <span class="s4">Ringz</span>.ar(<span class="s4">Impulse</span>.ar(1), 400, 0.05).dup };</p>
67 <p class="p6">~y = { <span class="s4">Ringz</span>.ar(<span class="s4">Impulse</span>.ar(1), 600, 0.05).dup };</p>
68 <p class="p5"><br></p>
69 <p class="p7">// for simplicity, one can provide a clock and a quant for a whole proxy space:</p>
70 <p class="p5"><br></p>
71 <p class="p6">p.clock = <span class="s4">TempoClock</span>.default; p.quant = 1.0;</p>
72 <p class="p6">~y = { <span class="s4">Ringz</span>.ar(<span class="s4">Impulse</span>.ar(1), 800, 0.05).dup };</p>
73 <p class="p5"><br></p>
74 <p class="p6">~z.play;</p>
75 <p class="p6">~z = { <span class="s4">Ringz</span>.ar(<span class="s4">Impulse</span>.ar(1), [500, 514], 0.8).dup * 0.1};</p>
76 <p class="p6">~z = { <span class="s4">Ringz</span>.ar(<span class="s4">Impulse</span>.ar(1), exprand(300, 400 ! 2), 1.8).dup * 0.1 };</p>
77 <p class="p6">~z = { <span class="s4">Ringz</span>.ar(<span class="s4">Impulse</span>.ar(2), exprand(300, 3400 ! 2), 0.08).dup * 0.2 };</p>
78 <p class="p6">~z.end;</p>
79 <p class="p5"><br></p>
80 <p class="p8"><span class="s2">p.clear; </span>// clear all.</p>
81 <p class="p5"><br></p>
82 <p class="p5"><br></p>
83 <p class="p3"><b>sequence of events:</b></p>
84 <p class="p3">When inserting a new function into the proxy, the synthdef is built, sent to the server</p>
85 <p class="p3">who sends back a message when it has completed. Then the proxy waits for the next beat to start</p>
86 <p class="p3">the synth. When using node proxies with patterns, the patterns are played using the clock as a scheduler.</p>
87 <p class="p5"><br></p>
88 <p class="p5"><br></p>
89 <p class="p5"><br></p>
90 <p class="p3"><b>b) quant and offset</b></p>
91 <p class="p2"><br></p>
92 <p class="p2"><br></p>
93 <p class="p3">In order to be able to control the offset/quant point of insertion, the 'quant' instance variable can be used,</p>
94 <p class="p3">which can be either a number or an array of the form [quant, offset], just like in pattern.play(quant).</p>
95 <p class="p5"><br></p>
96 <p class="p6">~z.play; ~y.play;</p>
97 <p class="p6">~z = { <span class="s4">Ringz</span>.ar(<span class="s4">Impulse</span>.ar(2), exprand(300, 3400 ! 2), 0.08).dup * 0.2 };</p>
98 <p class="p6">~y.quant = [1, 0.3]; // offset of 0.3, quant of 1.0</p>
99 <p class="p6">~y = { <span class="s4">Ringz</span>.ar(<span class="s4">Impulse</span>.ar(1), 600, 0.05).dup };</p>
100 <p class="p6">~y.quant = [2, 1/3]; // offset of 1/3, quant of 2.0</p>
101 <p class="p6">~y = { <span class="s4">Ringz</span>.ar(<span class="s4">Impulse</span>.ar(0.5), 600, 0.05).dup };</p>
102 <p class="p5"><br></p>
103 <p class="p5"><br></p>
104 <p class="p3">quant and offset scheduling is used for the following operations:</p>
105 <p class="p3"><b>play</b>, <b>put</b>, <b>removeAt</b>, <b>setNodeMap</b>, <b>wakeUp</b>, <b>rebuild</b> (and the rebuild operations lag, setRates, bus_)</p>
106 <p class="p3">For more information about quantisation<span class="Apple-converted-space">  </span>in SC, see <a href="../../../Scheduling/Quant.html"><span class="s5">Quant</span></a></p>
107 <p class="p5"><br></p>
108 <p class="p5"><br></p>
109 <p class="p5"><br></p>
110 <p class="p5"><br></p>
111 <p class="p5"><br></p>
112 <p class="p3"><b>c) connecting client and server tempo</b></p>
113 <p class="p5"><br></p>
114 <p class="p3">a ProxySpace has the method <b>makeTempoClock</b>, which creates an instance of <a href="../nodeproxy/TempoBusClock.html"><span class="s5">TempoBusClock</span></a></p>
115 <p class="p3">together with a node proxy (~tempo) which it keeps in sync.</p>
116 <p class="p5"><br></p>
117 <p class="p5"><br></p>
118 <p class="p8"><span class="s2">p.makeTempoClock(2.0); </span>// create a new tempoclock with 2 beats/sec</p>
119 <p class="p6">~y.play; ~x.play;</p>
120 <p class="p6">~y.quant = 1; // set the quant back to 1 and the offset to 0</p>
121 <p class="p6">~y = { <span class="s4">Ringz</span>.ar(<span class="s4">Impulse</span>.ar(~tempo.kr), 600, 0.05).dup }; <span class="s6">// impulse uses tempo</span></p>
122 <p class="p6">~x = <span class="s4">Pbind</span>(<span class="s7">\instrument</span>, <span class="s7">\default</span>, <span class="s7">\freq</span>, <span class="s4">Pseq</span>([300, 400], <span class="s4">inf</span>)); <span class="s6">// pattern uses tempoclock</span></p>
123 <p class="p5"><br></p>
124 <p class="p6">p.clock.tempo = 1.0; <span class="s6">// set the tempo to 1</span></p>
125 <p class="p6">p.clock.tempo = 2.2; // set the tempo to 2.2</p>
126 <p class="p5"><br></p>
127 <p class="p6">~x.free;</p>
128 <p class="p6">~y.free;</p>
129 <p class="p5"><br></p>
130 <p class="p5"><br></p>
131 <p class="p5"><br></p>
132 <p class="p5"><br></p>
133 <p class="p3"><b>d) sample accurate output</b></p>
134 <p class="p2"><br></p>
135 <p class="p3">for efficiency, NodeProxy uses a normal Out UGen for writing to its bus. If sample accurate</p>
136 <p class="p3">playback is needed (OffsetOut), the ProxySynthDef class variable sampleAccurate can be set to true.</p>
137 <p class="p3">Note that for audio through from external sources, this creates a delay for up to one block (e.g. about 1 ms.)</p>
138 <p class="p5"><br></p>
139 <p class="p9"><br></p>
140 <p class="p7">// example</p>
141 <p class="p5"><br></p>
142 <p class="p6"><span class="s4">ProxySynthDef</span>.sampleAccurate = <span class="s4">false</span>;</p>
143 <p class="p5"><br></p>
144 <p class="p6">~x.play;</p>
145 <p class="p5"><br></p>
146 <p class="p7">// the grain frees itself</p>
147 <p class="p6">~x = { <span class="s4">SinOsc</span>.ar(800) * <span class="s4">EnvGen</span>.ar(<span class="s4">Env</span>.perc(0.001, 0.03, 0.4), doneAction:2) };</p>
148 <p class="p5"><br></p>
149 <p class="p5"><span class="Apple-tab-span"> </span></p>
150 <p class="p7">// jittery tone.</p>
151 <p class="p6">(</p>
152 <p class="p6">r = Routine {</p>
153 <p class="p6"><span class="Apple-tab-span"> </span>loop {</p>
154 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>200.do { <span class="s4">arg</span> i;</p>
155 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>~x.spawn;</p>
156 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>(0.005).wait;</p>
157 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>};</p>
158 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>1.wait;</p>
159 <p class="p6"><span class="Apple-tab-span"> </span>}</p>
160 <p class="p6">}.play;</p>
161 <p class="p6">)</p>
162 <p class="p5"><br></p>
163 <p class="p6"><span class="s4">ProxySynthDef</span>.sampleAccurate = <span class="s4">true</span>;</p>
164 <p class="p5"><br></p>
165 <p class="p7">// steady tone, because sample accurate.</p>
166 <p class="p5"><br></p>
167 <p class="p6">~x.rebuild;</p>
168 <p class="p5"><br></p>
169 <p class="p6">r.stop;</p>
170 <p class="p5"><br></p>
171 <p class="p6">p.clear; // remove all.<span class="Apple-tab-span"> </span></p>
172 <p class="p5"><br></p>
173 <p class="p5"><br></p>
174 <p class="p5"><br></p>
175 <p class="p5"><br></p>
176 <p class="p5"><br></p>
177 <p class="p4"><span class="s2">previous: <a href="jitlib_basic_concepts_03.html"><span class="s3">jitlib_basic_concepts_03</span></a></span></p>
178 <p class="p5"><br></p>
179 <p class="p5"><br></p>
180 </body>
181 </html>