Include a header file required for build on mac 10.4
[supercollider.git] / Help / 3vs2 / SynthDefsVsSynths.html
blob3e8efdaf82edeb1032ce954a1058dc5a6c9a79b0
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="824.42">
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: 13.0px Helvetica}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #a71e12}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060}
18 span.s1 {text-decoration: underline}
19 span.s2 {color: #0019b7}
20 span.s3 {color: #000000}
21 span.s4 {color: #606060}
22 span.s5 {font: 12.0px Helvetica; color: #000000}
23 span.s6 {color: #326f17}
24 span.Apple-tab-span {white-space:pre}
25 </style>
26 </head>
27 <body>
28 <p class="p1"><b>SynthDefs versus Synths</b></p>
29 <p class="p2"><br></p>
30 <p class="p3">In SC2 Synth.play was the standard way to compile a ugenGraphFunc and play it. Each time you executed Synth.play, or Spawned a new event, that function was compiled anew. SC3 on the other hand, makes use of what are called SynthDefs. A <b>SynthDef</b> takes a ugenGraphFunc and compiles it to a kind of bytecode (sort of like Java bytecode) which can be understood by the server app. The server reads the SynthDef and creates a synth node based upon it.<span class="Apple-converted-space"> </span></p>
31 <p class="p2"><br></p>
32 <p class="p3">SynthDefs can be precompiled and saved to disk. Any def saved in the synthdefs/ directory (or in any directory set in the environment variable SC_SYNTHDEF_PATH) will be loaded into memory by a local <b>Server</b> when it is booted. If the def being used in a new <b>Synth</b> is already compiled and loaded, there is much less of a CPU spike when creating a new <b>Synth</b> than there was in SC2.</p>
33 <p class="p2"><br></p>
34 <p class="p3">SynthDefs can also be compiled and loaded into the Server without writing them to disk. This can be done while performing.</p>
35 <p class="p2"><br></p>
36 <p class="p3">The downside of this is that precompiled SynthDefs lack some of the programmatic flexibility that was one of SC2's great strengths. Much of this flexibility is gained back however, through the ability to set and change arguments (which you build into your ugenGraphFunc), and through new ugens such as <b>Rand</b> and <b>TRand</b>.</p>
37 <p class="p2"><br></p>
38 <p class="p3">When maximum flexibility is required, it is still possible to compile and send SynthDefs 'on the fly', albeit with SC2-like CPU spikes and a small amount of messaging latency.</p>
39 <p class="p2"><br></p>
40 <p class="p3">It is important to understand that creating and sending SynthDefs is <i>asynchronous. </i>This means that it is impossible to determine precisely how long it will take to compile and send a <b>SynthDef</b>, and thus when it will be available for creating new Synths. A simple way around this is to execute code in blocks, selecting them one at a time. More complicated is to use completion messages. SynthDef.play takes care of this for you, and returns a Synth object which you can then manipulate. See the example below.</p>
41 <p class="p2"><br></p>
42 <p class="p3">Another important distinction is between Synth in SC2 and <b>Synth</b> in SC3. The latter is a client-side object which <i>represents </i>a synth node on the server. Although it has some of the same methods, it does not function in the same way. There is no top level Synth in SC3, within which all scheduling and creation of other Synths occurs. There are only Synth objects which represent synth nodes on the server. These can be created at any time, within any scope.</p>
43 <p class="p2"><br></p>
44 <p class="p4"><b><span class="s1">Examples</span></b></p>
45 <p class="p2"><br></p>
46 <p class="p5">(</p>
47 <p class="p5">s = <span class="s2">Server</span>.local;</p>
48 <p class="p5">s.boot;</p>
49 <p class="p5">)</p>
50 <p class="p6"><br></p>
51 <p class="p7">// Compile a SynthDef and write it to disk</p>
52 <p class="p5"><span class="Apple-tab-span"> </span>(</p>
53 <p class="p8"><span class="s3"><span class="Apple-tab-span"> </span></span><span class="s2">SynthDef</span><span class="s3">(</span>"Help-SynthDef"<span class="s3">,<span class="Apple-converted-space"> </span></span></p>
54 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>{ <span class="s2">arg</span> out=0;</p>
55 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span> <span class="s2">Out</span>.ar(out, <span class="s2">PinkNoise</span>.ar(0.1))</p>
56 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>}).writeDefFile;</p>
57 <p class="p5"><span class="Apple-tab-span"> </span>)</p>
58 <p class="p6"><br></p>
59 <p class="p7">// Compile, write, and load it to the server</p>
60 <p class="p5"><span class="Apple-tab-span"> </span>(</p>
61 <p class="p8"><span class="s3"><span class="Apple-tab-span"> </span></span><span class="s2">SynthDef</span><span class="s3">(</span>"Help-SynthDef"<span class="s3">,<span class="Apple-converted-space"> </span></span></p>
62 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>{ <span class="s2">arg</span> out=0;</p>
63 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span> <span class="s2">Out</span>.ar(out, <span class="s2">PinkNoise</span>.ar(0.1))</p>
64 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>}).load(s);</p>
65 <p class="p5"><span class="Apple-tab-span"> </span>)</p>
66 <p class="p6"><span class="Apple-tab-span"> </span></p>
67 <p class="p7">// Load it to the server without writing to disk</p>
68 <p class="p5"><span class="Apple-tab-span"> </span>(</p>
69 <p class="p8"><span class="s3"><span class="Apple-tab-span"> </span></span><span class="s2">SynthDef</span><span class="s3">(</span>"Help-SynthDef"<span class="s3">,<span class="Apple-converted-space"> </span></span></p>
70 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>{ <span class="s2">arg</span> out=0;</p>
71 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span> <span class="s2">Out</span>.ar(out, <span class="s2">PinkNoise</span>.ar(0.1))</p>
72 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>}).send(s);</p>
73 <p class="p5"><span class="Apple-tab-span"> </span>)</p>
74 <p class="p6"><span class="Apple-tab-span"> </span></p>
75 <p class="p7">// Create a Synth with it</p>
76 <p class="p5"><span class="Apple-tab-span"> </span>x = <span class="s2">Synth</span>.new(<span class="s4">"Help-SynthDef"</span>, s);</p>
77 <p class="p5"><span class="Apple-tab-span"> </span>x.free;</p>
78 <p class="p2"><span class="Apple-tab-span"> </span></p>
79 <p class="p7">// Shorthand method to compile and write a SynthDef, and then play it in a Synth when done.<span class="Apple-converted-space"> </span></p>
80 <p class="p7">// Look familiar?</p>
81 <p class="p5"><span class="Apple-tab-span"> </span>(</p>
82 <p class="p8"><span class="s5"><span class="Apple-tab-span"> </span></span><span class="s3">x = </span><span class="s2">SynthDef</span><span class="s3">(</span>"Help-SynthDef"<span class="s3">,<span class="Apple-converted-space"> </span></span></p>
83 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>{ <span class="s2">arg</span> out=0;</p>
84 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span> <span class="s2">Out</span>.ar(out, <span class="s2">PinkNoise</span>.ar(0.1))</p>
85 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>}).play(s);<span class="Apple-converted-space"> </span></p>
86 <p class="p5"><span class="Apple-tab-span"> </span>)</p>
87 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>// The above only starts the new Synth after the def has been sent to the server.</p>
88 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>// Note that SynthDef.play returns a Synth object<span class="s3">!</span></p>
89 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
90 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span>x.set(</span><span class="s6">\out</span><span class="s3">, 1); </span>// change one of the arguments</p>
91 <p class="p5"><span class="Apple-tab-span"> </span>x.free;</p>
92 <p class="p6"><span class="Apple-tab-span"> </span></p>
93 <p class="p7">// SynthDef with a parameter that will be randomly determined each time a new Synth is created</p>
94 <p class="p7">// (try it several times to hear the differences)</p>
95 <p class="p6"><br></p>
96 <p class="p5"><span class="Apple-tab-span"> </span>(</p>
97 <p class="p8"><span class="s3"><span class="Apple-tab-span"> </span></span><span class="s2">SynthDef</span><span class="s3">(</span>"help-RandFreq"<span class="s3">, { </span><span class="s2">arg</span><span class="s3"> out=0;</span></p>
98 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">Out</span>.ar(out,<span class="Apple-converted-space"> </span></p>
99 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">FSinOsc</span>.ar(</p>
100 <p class="p7"><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><span class="s2">Rand</span><span class="s3">(200.0, 400.0), </span>// frequency between 200 and 400 Hz</p>
101 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>0, <span class="s2">Line</span>.kr(0.2, 0, 1, doneAction:2))</p>
102 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>)</p>
103 <p class="p5"><span class="Apple-tab-span"> </span>}).play(s);</p>
104 <p class="p5"><span class="Apple-tab-span"> </span>)</p>
105 <p class="p6"><span class="Apple-tab-span"> </span></p>
106 </body>
107 </html>