polymorphism: better names for .binaryValue and .booleanValue are .asInteger and...
[supercollider.git] / Help / Libraries / JITLib / tutorials / jitlib_fading.html
blob543b89ae776fe5b824315f231e1d801142924de3
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: 13.0px Helvetica; min-height: 16.0px}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.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: 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: #0025b2}
18 span.s1 {color: #0000ff}
19 span.s2 {color: #0025b2}
20 span.s3 {color: #606060}
21 span.s4 {color: #416d1f}
22 span.s5 {color: #982316}
23 span.s6 {color: #000000}
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>Fade envelope generation and crossfading</b></p>
30 <p class="p3"><br></p>
31 <p class="p3"><br></p>
32 <p class="p4"><span class="s1"><a href="../nodeproxy/NodeProxy.html">NodeProxy</a></span> (<a href="../nodeproxy/ProxySynthDef.html"><span class="s1">ProxySynthDef</span></a>) looks for inner envelopes in your definition function</p>
33 <p class="p4">to find out whether <b>a fade envelope is needed or not</b>. In case there is no other inner</p>
34 <p class="p4">possibility of freeing the synth, either<span class="Apple-converted-space"> </span></p>
35 <p class="p3"><br></p>
36 <p class="p4">a) <span class="Apple-tab-span"> </span>a fade envelope is created (audio / control rate output)</p>
37 <p class="p3"><br></p>
38 <p class="p4">b) <span class="Apple-tab-span"> </span>the synth is freed directly with no fading (scalar output or doneAction 1)</p>
39 <p class="p3"><br></p>
40 <p class="p4">c) <span class="Apple-tab-span"> </span>if you provide a gate arg and a doneAction 2 to your ugenGraph function, this is supposed</p>
41 <p class="p4"><span class="Apple-tab-span"> </span>to be a fade envelope for the synth</p>
42 <p class="p3"><br></p>
43 <p class="p4">d) <span class="Apple-tab-span"> </span>if a synthdef name is used, case c) is supposed</p>
44 <p class="p3"><br></p>
45 <p class="p4">... so in most cases, there is not much to worry about, just these two points ar important,</p>
46 <p class="p4">if one wants to use a self releasing synth or a different out ugen:</p>
47 <p class="p3"><br></p>
48 <p class="p4">e) <span class="Apple-tab-span"> </span>own responsibility:</p>
49 <p class="p4"><span class="Apple-tab-span"> </span>if the function creates a ugengraph that can be freed by trigger or other things, it<span class="Apple-converted-space"> </span></p>
50 <p class="p4"><span class="Apple-tab-span"> </span>waits for this action instead of the node proxy freeing the synth.<span class="Apple-converted-space"> </span></p>
51 <p class="p3"><br></p>
52 <p class="p4">f) <span class="Apple-tab-span"> </span>own out channel with 'out' arg: the control ugen with the name 'out' is set to the output channel</p>
53 <p class="p4"><span class="Apple-tab-span"> </span>number of the proxy.</p>
54 <p class="p5"><br></p>
55 <p class="p6">p = <span class="s2">ProxySpace</span>.push(s.boot);</p>
56 <p class="p5"><br></p>
57 <p class="p6">~out.play;</p>
58 <p class="p5"><br></p>
59 <p class="p7">// note that you can use this functionality also when using ProxySynthDef directly:</p>
60 <p class="p5"><br></p>
61 <p class="p6">d = <span class="s2">ProxySynthDef</span>(<span class="s3">"test"</span>, { <span class="s2">arg</span> freq=440; <span class="s2">SinOsc</span>.ar(freq) }).send(s);</p>
62 <p class="p6">s.sendMsg(<span class="s3">"/s_new"</span>, <span class="s3">"test"</span>, 1980, 1, 1, <span class="s4">\freq</span>, 340);</p>
63 <p class="p6">s.sendMsg(<span class="s3">"/n_set"</span>, 1980, <span class="s4">\freq</span>, 240);</p>
64 <p class="p6">s.sendMsg(<span class="s3">"/n_set"</span>, 1980, <span class="s4">\fadeTime</span>, 4);</p>
65 <p class="p6">s.sendMsg(<span class="s3">"/n_set"</span>, 1980, <span class="s4">\gate</span>, 0);</p>
66 <p class="p5"><br></p>
67 <p class="p2"><b>a) automatic fade envelope generation</b></p>
68 <p class="p5"><br></p>
69 <p class="p5"><br></p>
70 <p class="p7">// no inner envelope and audio / control rate output</p>
71 <p class="p6">(</p>
72 <p class="p6">~out = { <span class="s2">PinkNoise</span>.ar([1,1]*0.1) };</p>
73 <p class="p6">)</p>
74 <p class="p5"><br></p>
75 <p class="p6">(</p>
76 <p class="p6">~kout = { <span class="s2">PinkNoise</span>.kr([1,1]*0.1) };</p>
77 <p class="p6">)</p>
78 <p class="p5"><br></p>
79 <p class="p2"><b>b) automatic free instead of crossfade</b></p>
80 <p class="p5"><br></p>
81 <p class="p7">// inner envelope that cannot free the synth, the synth is freed when a new<span class="Apple-converted-space"> </span></p>
82 <p class="p7">// function is assigned.</p>
83 <p class="p6">(</p>
84 <p class="p6">~out = { <span class="s2">arg</span> t_trig; <span class="s2">EnvGen</span>.kr(<span class="s2">Env</span>.asr, t_trig) * <span class="s2">PinkNoise</span>.ar([1,1]) };</p>
85 <p class="p6">)</p>
86 <p class="p6">~out.group.set(<span class="s4">\t_trig</span>, 1);</p>
87 <p class="p5"><br></p>
88 <p class="p6">(</p>
89 <p class="p6">~out = { <span class="s2">arg</span> t_trig; <span class="s2">EnvGen</span>.kr(<span class="s2">Env</span>.asr, t_trig) * <span class="s2">SinOsc</span>.ar([1,1]*400) };</p>
90 <p class="p6">)</p>
91 <p class="p6">~out.group.set(<span class="s4">\t_trig</span>, 1);</p>
92 <p class="p5"><br></p>
93 <p class="p5"><br></p>
94 <p class="p7">// for a scalar output also no fade env is created, but the synth is freed (without fading)</p>
95 <p class="p6">(</p>
96 <p class="p6">~out = { <span class="s2">Out</span>.ar(0, <span class="s2">SinOsc</span>.ar(<span class="s2">Rand</span>(440,550),0,0.2)) };</p>
97 <p class="p6">)</p>
98 <p class="p5"><br></p>
99 <p class="p5"><br></p>
100 <p class="p2"><b>c) custom fade envelope</b></p>
101 <p class="p5"><br></p>
102 <p class="p7">// when a gate arg is provided, and the env can free the synth, this envelope</p>
103 <p class="p7">// is supposed to be the fade envelope for the synth: no extra fade env is created.</p>
104 <p class="p6">(</p>
105 <p class="p6">~out = { <span class="s2">arg</span> gate=1; <span class="s2">EnvGen</span>.kr(<span class="s2">Env</span>.asr, gate, doneAction:2) * 0.2 * <span class="s2">SinOsc</span>.ar([1,1]*<span class="s2">Rand</span>(440,550)) };</p>
106 <p class="p6">)</p>
107 <p class="p5"><br></p>
108 <p class="p2"><b>d) SynthDef name assignment</b></p>
109 <p class="p5"><br></p>
110 <p class="p7">// if a symbol is used as input, the defname of a def on the server is supposed</p>
111 <p class="p7">// to represent a SynthDef that has a gate, an out input and can free itself.</p>
112 <p class="p6">(</p>
113 <p class="p6">~out = <span class="s4">\default</span>;</p>
114 <p class="p6">)</p>
115 <p class="p5"><br></p>
116 <p class="p7">// this is the minimal requirement arguments for such a use (similar to Pbind)</p>
117 <p class="p6">(</p>
118 <p class="p6"><span class="s2">SynthDef</span>(<span class="s3">"test"</span>, { <span class="s2">arg</span> gate=1, out;</p>
119 <p class="p6"><span class="Apple-tab-span"> </span><span class="s2">Out</span>.ar(out, <span class="s2">Formant</span>.ar(300, 200, 10) * <span class="s2">EnvGen</span>.kr(<span class="s2">Env</span>.asr, gate, doneAction:2))</p>
120 <p class="p6">}).send(s);</p>
121 <p class="p6">)</p>
122 <p class="p5"><br></p>
123 <p class="p7">// you can also provide a fadeTime arg, whic is set by the proxy:</p>
124 <p class="p6">(</p>
125 <p class="p6"><span class="s2">SynthDef</span>(<span class="s3">"test"</span>, { <span class="s2">arg</span> gate=1, out, fadeTime=1;</p>
126 <p class="p6"><span class="Apple-tab-span"> </span><span class="s2">Out</span>.ar(out,<span class="Apple-converted-space"> </span></p>
127 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">Formant</span>.ar(Rand(20,40), 600, 10, 0.2)<span class="Apple-converted-space"> </span></p>
128 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>* <span class="s2">EnvGen</span>.kr(<span class="s2">Env</span>.asr(fadeTime,1,fadeTime), gate, doneAction:2)</p>
129 <p class="p6"><span class="Apple-tab-span"> </span>)</p>
130 <p class="p6">}).send(s);</p>
131 <p class="p6">)</p>
132 <p class="p5"><br></p>
133 <p class="p6">~out = \test;</p>
134 <p class="p6">~out.fadeTime = 3;</p>
135 <p class="p5"><br></p>
136 <p class="p4">note that the <b>number of channels</b> is your own responsibility when using symbols,<span class="Apple-converted-space"> </span></p>
137 <p class="p4">as a symbol carries no channel information!</p>
138 <p class="p4">(in all other cases the number of channels is wrapped or expanded to fit the proxy)</p>
139 <p class="p5"><br></p>
140 <p class="p7">// if the synthdef has a fixed duration envelope, there is a FAILURE /n_set Node not found message.</p>
141 <p class="p7">// with no further significance</p>
142 <p class="p6">(</p>
143 <p class="p6"><span class="s2">Sy</span>n<span class="s2">thDef</span>(<span class="s3">"test"</span>, { <span class="s2">arg</span> gate=1, out;</p>
144 <p class="p6"><span class="Apple-tab-span"> </span><span class="s2">Out</span>.ar(out,<span class="Apple-converted-space"> </span></p>
145 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">Formant</span>.ar(<span class="s2">Rand</span>(20,40), 600, 10, 0.6)<span class="Apple-converted-space"> </span></p>
146 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>* <span class="s2">EnvGen</span>.kr(<span class="s2">Env</span>.perc, gate, doneAction:2)</p>
147 <p class="p6"><span class="Apple-tab-span"> </span>)</p>
148 <p class="p6">}).send(s);</p>
149 <p class="p6">)</p>
150 <p class="p5"><br></p>
151 <p class="p6">~out = \test;</p>
152 <p class="p5"><br></p>
153 <p class="p5"><br></p>
154 <p class="p2"><b>e) own free responsibility</b></p>
155 <p class="p5"><br></p>
156 <p class="p5"><br></p>
157 <p class="p7">//inner envelope that can free the synth, no extra fade env is created:</p>
158 <p class="p6">(</p>
159 <p class="p6">~out = { <span class="s2">arg</span> t_trig; <span class="s2">EnvGen</span>.kr(<span class="s2">Env</span>.asr, t_trig, doneAction:2) * <span class="s2">PinkNoise</span>.ar([1,1]) };</p>
160 <p class="p6">)</p>
161 <p class="p6">~out.group.set(<span class="s4">\t_trig</span>, 1); <span class="s5">//end it</span></p>
162 <p class="p5"><br></p>
163 <p class="p7"><span class="s6">~out.send; </span>//start a new synth</p>
164 <p class="p6">~out.group.set(<span class="s4">\t_trig</span>, 1); <span class="s5">//end it again</span></p>
165 <p class="p5"><br></p>
166 <p class="p5"><br></p>
167 <p class="p7">// if there is a ugen that can free the synth, no extra fade env is created either,</p>
168 <p class="p7">// but it supposes the synth frees itself, so if a new function is assigned it does</p>
169 <p class="p7">// not get freed.</p>
170 <p class="p6">(</p>
171 <p class="p6">~out = {<span class="Apple-converted-space">  </span><span class="s2">arg t_trig</span>;</p>
172 <p class="p8"><span class="s6"><span class="Apple-tab-span"> </span></span>FreeSelf<span class="s6">.kr(</span>t_trig<span class="s6">);</span></p>
173 <p class="p6"><span class="Apple-tab-span"> </span><span class="s2">PinkNoise</span>.ar([1,1]*0.3);</p>
174 <p class="p6">};</p>
175 <p class="p6">)</p>
176 <p class="p6">~out.group.set(<span class="s4">\t_trig</span>, 1);</p>
177 <p class="p5"><br></p>
178 <p class="p5"><br></p>
179 <p class="p2"><b>f) own output responsibility</b></p>
180 <p class="p5"><br></p>
181 <p class="p7">// the arg name 'out' can be used to output through the right channel.</p>
182 <p class="p7">// of course with this one can get problems due to a wrong number of channels</p>
183 <p class="p7">// or deliberate hacks.</p>
184 <p class="p5"><br></p>
185 <p class="p7">//left speaker</p>
186 <p class="p6">(</p>
187 <p class="p6">~out = { <span class="s2">arg</span> out;</p>
188 <p class="p6"><span class="Apple-tab-span"> </span><span class="s2">OffsetOut</span>.ar(out, <span class="s2">Impulse</span>.ar(10,0,0.1))</p>
189 <p class="p6">}</p>
190 <p class="p6">)</p>
191 <p class="p5"><br></p>
192 <p class="p7">//both speakers</p>
193 <p class="p6">(</p>
194 <p class="p6">~out = { <span class="s2">arg</span> out;</p>
195 <p class="p6"><span class="Apple-tab-span"> </span><span class="s2">OffsetOut</span>.ar(out, <span class="s2">Impulse</span>.ar([10, 10],0,0.1))</p>
196 <p class="p6">}</p>
197 <p class="p6">)</p>
198 <p class="p5"><br></p>
199 <p class="p7">//this plays out into another adjacent bus: this is a possible source of confusion.<span class="Apple-converted-space"> </span></p>
200 <p class="p6">(</p>
201 <p class="p6">~out = { <span class="s2">arg</span> out;</p>
202 <p class="p6"><span class="Apple-tab-span"> </span><span class="s2">OffsetOut</span>.ar(out, <span class="s2">Impulse</span>.ar([10, 10, 10],0,0.1))</p>
203 <p class="p6">}</p>
204 <p class="p6">)</p>
205 <p class="p5"><br></p>
206 <p class="p5"><br></p>
207 <p class="p5"><br></p>
208 <p class="p5"><br></p>
209 <p class="p5"><br></p>
210 <p class="p5"><br></p>
211 </body>
212 </html>