SCDoc: Use proper static string constants instead of comparing string literals.
[supercollider.git] / Help / Tutorials / How-to-Use-the-Interpreter.html
blob235e4b6a680746c95ecf04a083eb15b083e960b0
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.46">
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: 9.0px Monaco; min-height: 12.0px}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000; min-height: 12.0px}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Helvetica; min-height: 11.0px}
18 span.s1 {color: #000000}
19 span.s2 {color: #0000bf}
20 span.s3 {font: 12.0px Monaco}
21 span.s4 {text-decoration: underline ; color: #0000bf}
22 span.s5 {color: #606060}
23 span.s6 {color: #bf0000}
24 span.Apple-tab-span {white-space:pre}
25 </style>
26 </head>
27 <body>
28 <p class="p1"><b>How to use the Interpreter</b></p>
29 <p class="p2"><br></p>
30 <p class="p3">This document is OSX (SCapp) specific in key commands, though the principles extend to all platforms. See the helpfile <a href="../Other Topics/Shortcuts.html">Shortcuts</a> for key commands in other editors. For the emacs sclang interface please also see <a href="../Linux/EmacsEditor.html">emacs specific documentation</a>.</p>
31 <p class="p2"><br></p>
32 <p class="p3">You can execute any single line expression by clicking anywhere in that line and pressing the <b>'Enter'</b> key.</p>
33 <p class="p3"><b>Note that the 'Enter' key is not the same key as 'Return'. </b>If you don't have an enter key, then you can use ctrl-Return, Ctrl-c, fn-Return( on Some Macs), or Shift-Return.</p>
34 <p class="p2"><br></p>
35 <p class="p3">You will need to start the default server before you can hear any examples. By convention the default server is assigned to the interpreter variable 's'. (At startup the default will be the localhost server.) You can start the server app by pressing the 'Boot' button on the localhost server window, or you can do it in code:</p>
36 <p class="p4"><br></p>
37 <p class="p5">// execute these lines one at a time by placing the cursor on the line and then pressing 'enter'</p>
38 <p class="p4"><br></p>
39 <p class="p5"><span class="s1">s.boot; </span>// this boots the default Server. Watch the post window and server window for the result</p>
40 <p class="p6"><br></p>
41 <p class="p5">// once that's done execute this to make a sound</p>
42 <p class="p7">{ <span class="s2">FSinOsc</span>.ar(800, 0, 0.1) }.play;</p>
43 <p class="p2"><br></p>
44 <p class="p3">(Press and hold Cmd (the Apple key) and then press period to stop the sound started above.)</p>
45 <p class="p2"><br></p>
46 <p class="p3">In the help files all executable fragments are written in the <span class="s3">Monaco</span> font.</p>
47 <p class="p2"><br></p>
48 <p class="p3">If an expression has multiple lines you can select all of the lines before typing 'Enter'. Note that this example uses <a href="../GUI/GUI-Tools/GUI.html"><span class="s2">GUI</span></a> objects that are standard in Mac OSX. Windows and Linux platforms can run this using SwingOSC. SwingOSC is installed as part of the Windows package. Linux users might need to download SwingOSC for themselves. <a href="http://www.sciss.de/swingOSC/"><span class="s4">http://www.sciss.de/swingOSC/</span></a></p>
49 <p class="p2"><br></p>
50 <p class="p5">// Select all 9 of the following lines and press 'Enter':</p>
51 <p class="p7">w = <span class="s2">Window</span>.new(<span class="s5">"Fading"</span>).front;<span class="Apple-converted-space"></span></p>
52 <p class="p7">r = <span class="s2">Routine</span>({<span class="Apple-converted-space"></span></p>
53 <p class="p7"><span class="Apple-tab-span"> </span>200.do({<span class="s2">|i|</span><span class="Apple-converted-space"></span></p>
54 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>w.view.background = <span class="s2">Color</span>.blue(val: 1, alpha: 1 - (i * 0.005));<span class="Apple-converted-space"></span></p>
55 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>0.005.wait;<span class="Apple-converted-space"></span></p>
56 <p class="p7"><span class="Apple-tab-span"> </span>});</p>
57 <p class="p7"><span class="Apple-tab-span"> </span>w.close;</p>
58 <p class="p7">});</p>
59 <p class="p7"><span class="s2">AppClock</span>.play(r);</p>
60 <p class="p2"><br></p>
61 <p class="p3">Some examples do require lines to be executed one at a time, or certain lines to be executed first. By far the most common case of this is booting the server app, as we did at the top of the page. Until the server has completed booting, no sound producing code will work.</p>
62 <p class="p2"><br></p>
63 <p class="p3">However, most of the examples included with the app have parentheses around lines of code which should be executed at the same time. (This is a convention which should be followed in your own code.) This allows you to double click to the right of the open paren and select the entire expression. Then press 'enter'.</p>
64 <p class="p8"><br></p>
65 <p class="p7">(</p>
66 <p class="p5">// ^^^^^^^^ double click above this line ^^^^^^^^</p>
67 <p class="p7">play({</p>
68 <p class="p5">// Three patches in one...</p>
69 <p class="p5"><span class="s1">n = 5;<span class="Apple-tab-span"> </span></span>// number of strings</p>
70 <p class="p5"><span class="s1">b = [<span class="Apple-tab-span"> </span></span>// array of possible impulse excitation behaviours</p>
71 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>{ <span class="s2">Impulse</span>.ar(2 + 0.2.rand, 0.3) }, <span class="s6">// slow phasing</span></p>
72 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>{ <span class="s2">Dust</span>.ar(0.5, 0.3) },<span class="Apple-tab-span"> </span><span class="s6">// "wind chimes"</span></p>
73 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>{ <span class="s2">Impulse</span>.ar(<span class="s2">SinOsc</span>.kr(0.05+0.1.rand, 2pi.rand, 5, 5.2), 0.3) } <span class="s6">// races</span></p>
74 <p class="p5"><span class="s1"><span class="Apple-tab-span"> </span>].choose;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// choose one at random to use for all voices</p>
75 <p class="p7"><span class="s2">Mix</span>.new(</p>
76 <p class="p5"><span class="s1"><span class="Apple-tab-span"> </span></span><span class="s2">Array</span><span class="s1">.fill(n, {<span class="Apple-tab-span"> </span></span>// n strings tuned randomly to MIDI keys 60-90</p>
77 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">var</span> delayTime;</p>
78 <p class="p5"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// calculate delay based on a random note</p>
79 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>delayTime = 1 / (60 + 30.rand).midicps;<span class="Apple-converted-space"></span></p>
80 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">Pan2</span>.ar(</p>
81 <p class="p5"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s2">LeakDC</span><span class="s1">.ar(<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// removes DC buildup</p>
82 <p class="p5"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s2"><span class="Apple-tab-span"> </span>CombL</span><span class="s1">.ar(<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// used as a string resonator</p>
83 <p class="p5"><span class="Apple-tab-span"> </span><span class="s1"><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">Decay</span><span class="s1">.ar(<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// decaying envelope for noise</p>
84 <p class="p5"><span class="Apple-tab-span"> </span><span class="s1"><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 class="Apple-tab-span"> </span>b.value,<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>// instantiate an exciter</p>
85 <p class="p5"><span class="Apple-tab-span"> </span><span class="s1"><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 class="Apple-tab-span"> </span>0.04,<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>// decay time of excitation</p>
86 <p class="p5"><span class="Apple-tab-span"> </span><span class="s1"><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 class="Apple-tab-span"> </span></span><span class="s2">PinkNoise</span><span class="s1">.ar(0.2)),<span class="Apple-tab-span"> </span></span>// multiply noise by envelope</p>
87 <p class="p5"><span class="Apple-tab-span"> </span><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>delayTime, <span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// max delay time</p>
88 <p class="p5"><span class="Apple-tab-span"> </span><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>delayTime,<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// actual delay time</p>
89 <p class="p5"><span class="Apple-tab-span"> </span><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>4)), <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>// decay time of string</p>
90 <p class="p5"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>1.0.rand2 </span>// random pan position</p>
91 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>)</p>
92 <p class="p7"><span class="Apple-tab-span"> </span>}))</p>
93 <p class="p7">})</p>
94 <p class="p7">)</p>
95 <p class="p4"><br></p>
96 <p class="p3">Again, press Cmd-. to stop the sound. This will stop all audio (and free all nodes on the server) at any time.</p>
97 <p class="p2"><br></p>
98 <p class="p3">When you're done you can quit the server app by pressing the 'Quit' button on the localhost server window, or do it by executing the following code:</p>
99 <p class="p4"><br></p>
100 <p class="p7">s.quit;</p>
101 <p class="p8"><br></p>
102 </body>
103 </html>