Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / HelpSource / Tutorials / JITLib / jitlib_basic_concepts_01.schelp
blob98c636edf82d33e1050aca7eb679028ce1eb2ef5
1 title:: jitlib_basic_concepts_01
2 summary:: some placeholders in supercollider
3 categories:: Libraries>JITLib>Tutorials
4 related:: Overviews/JITLib, Tutorials/JITLib/jitlib_basic_concepts_02
6 this helpfile explains some basic concepts of interactive programming with supercollider and proxy space.
8 section::a) What is a proxy?
10 A proxy is a place holder that is often used to operate on something that does not yet exist. For example, an emphasis::OutputProxy:: is used to represent multiple outputs of a ugen, even if only one ugen is created eventually. Any object can have proxy behaviour (it may delegate function calls to different objects for example) but specially functions and references can be used as operands while they keep their referential quality.
12 see also: link::Classes/OutputProxy::, link::Classes/Function::, link::Classes/Ref::
14 subsection::using a Ref as a proxy
16 code::
17 // reference example
19 // create a new Ref object
20 y = `(nil);
22 // you can start to calcuate with y, even if its value is not yet given:
23 z = y + 10; // returns a function
25 // now the source can be set:
26 y.value = 34;
28 // the function z can be evaluated now.
29 z.value
32 // the same without a reference does not work:
34 y = nil; // empty y first
36 z = y + 10; // this fails.
38 // also an array does not provide this referentiality:
40 y = [nil]; // array with nil as element
42 z = y + 10; // this fails.
44 // an environment without sufficient defaults has the same problem:
46 currentEnvironment.postln; // anEnvironment
47 ~x; // access the enironment: there is nothing stored: nil
48 ~x = 9; // store something
49 ~x;     // now 9 is stored
50 ~x + 100; // calculate with it
52 currentEnvironment.postln; // the value is stored in the environment
54 ~y + ~x; // cause an error: ~y is nil.
55 ~y = -90; // set ~y
57 ~y + ~x; // this works.
60 subsection::using a Function as a proxy
62 code::
63 // a function can serve the same purpose
65 y = nil; // empty y first
66 z = { y } + 10; // this does not fail, instead it creates a new function, which
67                 // does not fail when evaluating it after y is set to 34.
69 y = 34;
70 z.value;
73 see also client side proxies like link::Classes/Tdef::, link::Classes/Pdefn::, link::Classes/Pdef::, link::Classes/Fdef::
75 section::b) NodeProxy
77 For interactive programming it can be useful to be able to use something before it is there - it makes evaluation order more flexible and allows to postpone decisions to a later moment. Some preparations have to be done usually - like above, a reference has to be created. In other situations this sort of preparation is not enough, for example if one wants to do maths with running processes on the server.
79 Audio output on the server has mainly two properties - a emphasis::calculation rate:: (audio or control) and a certain emphasis::number of channels::. These are the main static properties of a node proxy, which cannot be changed while it is in use.
81 code::
82 // boot the server
83 s.boot;
85 // two proxies on a server. calculation rate is audio rate, number of channels is 2
86 y = NodeProxy.audio(s, 2);
87 z = NodeProxy.audio(s, 2);
89 // use them in calculation
90 z.play;
91 z.source = y.sin * 0.2;
94 // set its source now.
95 y.source = { Saw.ar([300, 301], 4*pi) };
97 // the source can be of various type, one of them would be a number:
98 y.source = 0.0;
100 // post the source
101 y.source;
103 // end them, free their bus number
104 y.clear;
105 z.clear;
108 In order to provide a simple way of creating node proxies, a proxy space can be used.
109 So the above reads like this:
111 code::
112 p = ProxySpace.push(s.boot); // store proxy space in p so it can be accessed easily.
113 ~z.play;
116 ~z = ~y.sin * 0.2;
119 ~y = { Saw.ar([300, 301], 4*pi) };
122 // clear the space (all proxies)
123 p.clear;
125 // move out of the proxyspace.
126 p.pop;
129 further readings: link::Classes/NodeProxy::, link::Classes/ProxySpace::, link::Classes/Ndef::
131 next: link::Tutorials/JITLib/jitlib_basic_concepts_02::