1 title:: SuperCollider 3 Server Tutorial
2 summary:: A short tutorial covering many concepts
5 To follow this tutorial you should read
7 link::Reference/Server-Architecture::
11 link::Reference/Server-Command-Reference::.
13 There are two parts to SuperCollider. One part is the language application and another is a synthesis server that can run either inside the language application, or as a separate program on the same machine, or run on a different computer across a network connection. The language application sends command messages to the server using a subset of the Open Sound Control protocol.
15 section::Booting a Server
17 In order to run sound we need to start a server running. The easiest way to start a server is to click on one of the "Start Server" buttons in the server windows. Sometimes though it is useful to start a server programmatically. To do this we need to get or create a server object and tell it to "boot". Two servers, internal and local, are predefined.
19 The internal server runs in the same process as the SuperCollider application. It is internal to the program itself.
22 // set the interpreter variable s to the internal server object.
26 strong::VERY IMPORTANT:: : This line must be executed for the variable 's' to be set. The mechanics are different depending on your platform. The MacOSX standard is to place the cursor anywhere on this line and press the "Enter" key on the numeric keypad. Pressing the main return key does not execute code! This allows you to write code fragments of multiple lines. To execute a multi-line block of code, select the block and press "Enter." For convenience, a code block can be enclosed in parentheses, and the entire block selected by double-clicking just inside either parenthesis. (For instructions in other editors (e.g. on Linux or Windows), consult the documentation specific to that platform. See also the helpfile link::Reference/KeyboardShortcuts:: for key commands in other editors.) If you don't have an enter key, then you can use ctrl-Return, Ctrl-c, fn-Return (on Some Macs), or Shift-Return.
28 The local server runs on the same machine as the SuperCollider application, but is a separate program, 'scsynth'. strong::Note:: : By default the interpreter variable code::s:: is set to the local server at startup. For further information see the link::Classes/Server:: helpfile.
32 // set the interpreter variable s to the local server object.
36 To boot the server you send it the boot message.
42 To quit the server send it the quit message.
48 We can also create a server to run. To create a server object we need to provide the IP address or the server and a port number. Port numbers are somewhat arbitrary but they should not conflict with common protocols like telnet, ftp http, etc. The IP address 127.0.0.1 is defined to mean the local host. This is the IP address to use for running a server on your own machine.
51 // create a server object that will run on the local host using port #58009
52 s = Server(\myServer, NetAddr("127.0.0.1", 58009));
54 s.boot; //start the server
56 s.quit; // quit the server
59 It is not possible to boot a server on a remote machine, but if you have one running already or you know of one running, you can send messages to it. You create the server object using the IP address of the machine running the server and the port it is using.
62 // create a server object for talking to the server running on a machine having
63 // IP address 192.168.0.47 using port #57110
64 s = Server(\myServer, NetAddr("192.168.0.47", 57110));
69 (note: This tutorial uses raw OSC commands as described in link::Reference/Server-Command-Reference::, rather than the classes link::Classes/Synth:: and link::Classes/Group::. See those helpfiles also for some simpler ways of working with Synths. This tutorial explains the basic underlying design of Synths and SynthDefs).
71 Now lets make some audio.
74 s = Server.local; // assign it to interpreter variable 's'
83 Create a link::Classes/SynthDef::. A SynthDef is a description of a processing module that you want to run on the server. It can read audio from the server's audio buses, read control from the control buses and write control or audio back to buses. Here we will create a sine oscillator and send it to audio bus zero.
87 SynthDef("sine", { arg freq=800;
89 osc = SinOsc.ar(freq, 0, 0.1); // 800 Hz sine oscillator
90 Out.ar(0, osc); // send output to audio bus zero.
91 }).writeDefFile; // write the def to disk in the default directory synthdefs/
95 Send the SynthDef to the server.
98 s.sendSynthDef("sine");
101 Start the sound. The code::/s_new:: command creates a new Synth which is an instance of the "sine" SynthDef. Each synth running on the server needs to have a unique ID. The simplest and safest way to do this is to get an ID from the server's NodeIDAllocator. This will automatically allow IDs to be reused, and will prevent conflicts both with your own nodes, and with nodes created automatically for purposes such as visual scoping and recording. Each synth needs to be installed in a Group. We install it in group one which is the default group. There is a group zero, called the RootNode, which contains the default group, but it is generally best not to use it as doing so can result in order of execution issues with automatically created nodes such as those mentioned above. (For more detail see the link::Reference/default_group::, link::Classes/RootNode::, and link::Guides/Order-of-execution:: helpfiles.)
104 s.sendMsg("/s_new", "sine", x = s.nextNodeID, 1, 1);
110 s.sendMsg("/n_free", x);
119 SynthDef has two methods which send the def automatically, load which writes it to disk, and send which sends it without writing it to disk. The latter can be useful to avoid clutter on your drive.
123 SynthDef("sine", { arg freq=800;
125 osc = SinOsc.ar(freq, 0, 0.1); // 800 Hz sine oscillator
126 Out.ar(0, osc); // send output to audio bus zero.
127 }).load(s); // write to disk and send
131 SynthDef("sine", { arg freq=800;
133 osc = SinOsc.ar(freq, 0, 0.1); // 800 Hz sine oscillator
134 Out.ar(0, osc); // send output to audio bus zero.
135 }).send(s); // send without writing
139 section::Using Arguments
141 It is useful to be able to specify parameters of a synth when it is created. Here a frequency argument is added to the sine SynthDef so that we can create it
144 s = Server.local; // assign it to interpreter variable 's'
149 SynthDef("sine", { arg freq;
151 osc = SinOsc.ar(freq, 0, 0.1); // 800 Hz sine oscillator
152 Out.ar(0, osc); // send output to audio bus zero.
157 Play a 900 Hz sine wave.
160 s.sendMsg("/s_new", "sine", x = s.nextNodeID, 1, 1, "freq", 900);
162 s.sendMsg("/n_free", x);
165 Play a 1000 Hz sine wave.
168 s.sendMsg("/s_new", "sine", y = s.nextNodeID, 1, 1, "freq", 1000);
170 s.sendMsg("/n_free", y);
173 Playing three voices at once
177 s.sendMsg("/s_new", "sine", x = s.nextNodeID, 1, 1, "freq", 800);
178 s.sendMsg("/s_new", "sine", y = s.nextNodeID, 1, 1, "freq", 1001);
179 s.sendMsg("/s_new", "sine", z = s.nextNodeID, 1, 1, "freq", 1202);
183 s.sendMsg("/n_free", x);
184 s.sendMsg("/n_free", y);
185 s.sendMsg("/n_free", z);
189 Playing three voices at once using bundles. Bundles allow you to send multiple messages with a time stamp. The messages in the bundle will be scheduled to be performed together. The time argument to sendBundle is an offset into the future from the current thread's logical time.
194 ["/s_new", "sine", x = s.nextNodeID, 1, 1, "freq", 800],
195 ["/s_new", "sine", y = s.nextNodeID, 1, 1, "freq", 1001],
196 ["/s_new", "sine", z = s.nextNodeID, 1, 1, "freq", 1202]);
197 s.sendBundle(1.2, ["/n_free", x],["/n_free", y],["/n_free", z]);
201 section::Controlling a Synth
203 You can send messages to update the values of a Synth's arguments.
205 Play a 900 Hz sine wave.
208 s.sendMsg("/s_new", "sine", x = s.nextNodeID, 1, 1, "freq", 900);
211 Change the frequency using the /n_set command. You send the node ID, the parameter name and the value.
214 s.sendMsg("/n_set", x, "freq", 800);
216 s.sendMsg("/n_set", x, "freq", 700);
218 s.sendMsg("/n_free", x);
221 section::Adding an Effect Dynamically
223 You can dynamically add and remove an effect to process another synth. In order to do this, the effect has to be added after the node to be processed.
227 // define a noise pulse
228 SynthDef("tish", { arg freq = 1200, rate = 2;
230 trg = Decay2.ar(Impulse.ar(rate,0,0.3), 0.01, 0.3);
231 osc = {WhiteNoise.ar(trg)}.dup;
232 Out.ar(0, osc); // send output to audio bus zero.
237 // define an echo effect
238 SynthDef("echo", { arg delay = 0.2, decay = 4;
241 // use ReplaceOut to overwrite the previous contents of the bus.
242 ReplaceOut.ar(0, CombN.ar(in, 0.5, delay, decay, 1, in));
247 s.sendMsg("/s_new", "tish", x = s.nextNodeID, 1, 1, \freq, 200, \rate, 1.2);
250 s.sendMsg("/s_new", "echo", y = s.nextNodeID, 1, 1);
253 s.sendMsg("/n_free", y);
255 // add an effect (time has come today.. hey!)
256 s.sendMsg("/s_new", "echo", z = s.nextNodeID, 1, 1, \delay, 0.1, \decay, 4);
259 s.sendMsg("/n_free", z);
262 s.sendMsg("/n_free", x);
265 This works because we added the effect after the other node. Sometimes you will need to use groups or code::/n_after:: to insure that an effect gets added after what it is supposed to process.
267 section::Mapping an Argument to a Control Bus
272 SynthDef("line", { arg i_bus=10, i_start=1000, i_end=500, i_time=1;
273 ReplaceOut.kr(i_bus, Line.kr(i_start, i_end, i_time, doneAction: 2));
278 Play a 900 Hz sine wave.
281 s.sendMsg("/s_new", "sine", x = s.nextNodeID, 1, 1, "freq", 900);
284 Put a frequency value on the control bus.
287 s.sendMsg("/c_set", 10, x);
290 Map the node's freq argument to read from control bus #10.
293 s.sendMsg("/n_map", x, \freq, 10);
296 Change the value on the control bus.
299 s.sendMsg("/c_set", 10, 1200);
302 Start a control process that writes to bus #10.
303 The link::Classes/EnvGen:: doneAction will free this node automatically when it finishes.
306 s.sendMsg("/s_new", "line", s.nextNodeID, 0, 1);
312 s.sendMsg("/n_free", x);
315 section::Sequencing with Routines
319 var space,offset,timer, saw, envsaw, sampler, delay;
321 SynthDef("saw",{ arg out=100, pan=0, trig=0.0, freq=500, amp=1, cutoff=10000, rezz=1;
322 freq = Lag.kr(freq,0.1);
323 Out.ar(out,Pan2.ar(RLPF.ar(Saw.ar([freq,freq*2],amp),cutoff,rezz),
327 SynthDef("envsaw",{ arg out=100, pan=0, sustain=0.5, freq=500, amp=1, cutoff=10000, rezz=1;
329 env = EnvGen.kr(Env.perc(0.01, sustain, 0.2), doneAction:0, gate:amp);
330 Out.ar(out,Pan2.ar(RLPF.ar(Saw.ar(Lag.kr(freq,0.1),env),cutoff,rezz)*amp,
334 SynthDef("delay", { arg out=0, delay = 0.4, decay = 14;
337 Out.ar(out, CombN.ar(in, 0.5, delay, decay, 1, in));
340 SynthDef("sampler",{ arg sample, trig=1,rate=1.0,out=0,bufnum=0,pan=0,amp=1, sustain=0.25;
342 env = EnvGen.kr(Env.perc(0.001, sustain, 0.001), doneAction:2);
345 PlayBuf.ar(1,bufnum,rate,InTrig.kr(trig),0,0)*amp,
352 offset = Tempo.tempo.reciprocal;
354 space = Buffer.read(s, Help.dir +/+ "sounds/a11wlk01.wav");
357 delay=Synth.after(saw,"delay", [\decay, 20]);
361 r=Routine({ var wait, freq, cutoff,rezz;
362 wait = Pseq([2],inf).asStream;
363 freq = Pseq([30,40,42,40],inf).asStream;
364 cutoff = Pfunc({500.rand2+1000}).asStream;
366 inf.do({saw.set("freq", freq.next.midicps, "cutoff", cutoff.next, "rezz", rezz, "amp", 0.1, "out", 0);
367 (wait.next*offset).wait});});
373 r=Routine({ var wait, rate;
374 wait = Pseq([0.25],inf).asStream;
375 rate = Pfunc({0.5.rand}).asStream;
376 inf.do({Synth.before(delay, "sampler", [\bufnum, space, \trig, 1, \amp,0.1, \rate, rate.next, \sustain, wait.next]);
377 (wait.next*offset).wait});});
384 section::Sequencing with Patterns
388 //sappy emo electronica example...
390 SynthDef("patternefx_Ex", { arg out, in;
392 audio = In.ar([20,21],2);
393 efx=CombN.ar(audio, 0.5, [0.24,0.4], 2, 1);
394 Out.ar([0,1], audio+efx);
397 Synth.new("patternefx_Ex");
399 SynthDef("pattern_Ex", { arg out, freq = 1000, gate = 1, pan = 0, cut = 4000, rez = 0.8, amp = 1;
405 pan) * EnvGen.kr(Env.linen(0.01, 1, 0.3), gate, amp, doneAction:2);
409 SynthDef("bass_Ex", { arg out, freq = 1000, gate = 1, pan = 0, cut = 4000, rez = 0.8, amp = 1;
413 SinOsc.ar(freq,0.05),
415 pan) * EnvGen.kr(Env.linen(0.01, 1, 0.3), gate, amp, doneAction:2);
419 SynthDescLib.global.read;
425 0,Pbind(\instrument,\pattern_Ex, \out, 20, \dur,Pseq([2],16), \root,[-24,-17], \degree,Pseq([0,3,5,7,9,11,5,1],2), \pan,1,\cut,Pxrand([1000,500,2000,300],16), \rez,Pfunc({0.7.rand +0.3}), \amp,0.12),
427 0.5,Pbind(\instrument,\pattern_Ex, \out, 20, \dur,Pseq([Pseq([2],15),1.5],1), \root,-12, \degree,Pseq([0,3,5,7,9,11,5,1],2), \pan,-1,\cut,2000, \rez,0.6, \amp,0.1);
431 0,Pbind(\instrument,\pattern_Ex, \out, 20, \dur,2, \root,[-24,-17], \degree,Pseq([0,3,5,7,9,11,5,1],inf), \pan,1,\cut,Pxrand([1000,500,2000,300],inf), \rez,Pfunc({0.7.rand +0.3}), \amp,0.12),
433 0,Pbind(\instrument,\bass_Ex, \dur,1, \root,-24, \degree,Pseq([0],inf), \pan,0, \cut,128, \rez,0.1, \amp,0.3),
435 0.5,Pbind(\instrument,\pattern_Ex, \out, 20, \dur,2, \root,-12, \degree,Pseq([0,3,5,7,9,11,5,1],inf), \pan,-1,\cut,2000, \rez,0.6, \amp,0.1);