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'.
29 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.
33 // set the interpreter variable s to the local server object.
37 To boot the server you send it the boot message.
43 To quit the server send it the quit message.
49 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.
52 // create a server object that will run on the local host using port #58009
53 s = Server(\myServer, NetAddr("127.0.0.1", 58009));
55 s.boot; //start the server
57 s.quit; // quit the server
60 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.
63 // create a server object for talking to the server running on a machine having
64 // IP address 192.168.0.47 using port #57110
65 s = Server(\myServer, NetAddr("192.168.0.47", 57110));
70 (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).
72 Now lets make some audio.
75 s = Server.local; // assign it to interpreter variable 's'
84 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.
88 SynthDef("sine", { arg freq=800;
90 osc = SinOsc.ar(freq, 0, 0.1); // 800 Hz sine oscillator
91 Out.ar(0, osc); // send output to audio bus zero.
92 }).writeDefFile; // write the def to disk in the default directory synthdefs/
96 Send the SynthDef to the server.
99 s.sendSynthDef("sine");
102 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.)
105 s.sendMsg("/s_new", "sine", x = s.nextNodeID, 1, 1);
111 s.sendMsg("/n_free", x);
121 SynthDef has three 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, but is limited to SynthDefs up to a certain complexity.
123 Most generally useful and recommended is to use the method strong::add::, which sends or writes to disk only if it can't send, and it sends to all servers listed in the SynthDefLib (A server can be added by SynthDescLib.global.addServer(server)).
127 SynthDef("sine", { arg freq=800;
129 osc = SinOsc.ar(freq, 0, 0.1); // 800 Hz sine oscillator
130 Out.ar(0, osc); // send output to audio bus zero.
136 SynthDef("sine", { arg freq=800;
138 osc = SinOsc.ar(freq, 0, 0.1); // 800 Hz sine oscillator
139 Out.ar(0, osc); // send output to audio bus zero.
140 }).load(s); // write to disk and send
144 SynthDef("sine", { arg freq=800;
146 osc = SinOsc.ar(freq, 0, 0.1); // 800 Hz sine oscillator
147 Out.ar(0, osc); // send output to audio bus zero.
148 }).send(s); // send without writing
152 section::Using Arguments
154 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
157 s = Server.local; // assign it to interpreter variable 's'
162 SynthDef("sine", { arg freq;
164 osc = SinOsc.ar(freq, 0, 0.1); // 800 Hz sine oscillator
165 Out.ar(0, osc); // send output to audio bus zero.
170 Play a 900 Hz sine wave.
173 s.sendMsg("/s_new", "sine", x = s.nextNodeID, 1, 1, "freq", 900);
175 s.sendMsg("/n_free", x);
178 Play a 1000 Hz sine wave.
181 s.sendMsg("/s_new", "sine", y = s.nextNodeID, 1, 1, "freq", 1000);
183 s.sendMsg("/n_free", y);
186 Playing three voices at once
190 s.sendMsg("/s_new", "sine", x = s.nextNodeID, 1, 1, "freq", 800);
191 s.sendMsg("/s_new", "sine", y = s.nextNodeID, 1, 1, "freq", 1001);
192 s.sendMsg("/s_new", "sine", z = s.nextNodeID, 1, 1, "freq", 1202);
196 s.sendMsg("/n_free", x);
197 s.sendMsg("/n_free", y);
198 s.sendMsg("/n_free", z);
202 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.
207 ["/s_new", "sine", x = s.nextNodeID, 1, 1, "freq", 800],
208 ["/s_new", "sine", y = s.nextNodeID, 1, 1, "freq", 1001],
209 ["/s_new", "sine", z = s.nextNodeID, 1, 1, "freq", 1202]);
210 s.sendBundle(1.2, ["/n_free", x],["/n_free", y],["/n_free", z]);
214 section::Controlling a Synth
216 You can send messages to update the values of a Synth's arguments.
218 Play a 900 Hz sine wave.
221 s.sendMsg("/s_new", "sine", x = s.nextNodeID, 1, 1, "freq", 900);
224 Change the frequency using the /n_set command. You send the node ID, the parameter name and the value.
227 s.sendMsg("/n_set", x, "freq", 800);
229 s.sendMsg("/n_set", x, "freq", 700);
231 s.sendMsg("/n_free", x);
234 section::Adding an Effect Dynamically
236 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.
240 // define a noise pulse
241 SynthDef("tish", { arg freq = 1200, rate = 2;
243 trg = Decay2.ar(Impulse.ar(rate,0,0.3), 0.01, 0.3);
244 osc = {WhiteNoise.ar(trg)}.dup;
245 Out.ar(0, osc); // send output to audio bus zero.
250 // define an echo effect
251 SynthDef("echo", { arg delay = 0.2, decay = 4;
254 // use ReplaceOut to overwrite the previous contents of the bus.
255 ReplaceOut.ar(0, CombN.ar(in, 0.5, delay, decay, 1, in));
260 s.sendMsg("/s_new", "tish", x = s.nextNodeID, 1, 1, \freq, 200, \rate, 1.2);
263 s.sendMsg("/s_new", "echo", y = s.nextNodeID, 1, 1);
266 s.sendMsg("/n_free", y);
268 // add an effect (time has come today.. hey!)
269 s.sendMsg("/s_new", "echo", z = s.nextNodeID, 1, 1, \delay, 0.1, \decay, 4);
272 s.sendMsg("/n_free", z);
275 s.sendMsg("/n_free", x);
278 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.
280 section::Mapping an Argument to a Control Bus
285 SynthDef("line", { arg i_bus=10, i_start=1000, i_end=500, i_time=1;
286 ReplaceOut.kr(i_bus, Line.kr(i_start, i_end, i_time, doneAction: 2));
291 Play a 900 Hz sine wave.
294 s.sendMsg("/s_new", "sine", x = s.nextNodeID, 1, 1, "freq", 900);
297 Put a frequency value on the control bus.
300 s.sendMsg("/c_set", 10, x);
303 Map the node's freq argument to read from control bus #10.
306 s.sendMsg("/n_map", x, \freq, 10);
309 Change the value on the control bus.
312 s.sendMsg("/c_set", 10, 1200);
315 Start a control process that writes to bus #10.
316 The link::Classes/EnvGen:: doneAction will free this node automatically when it finishes.
319 s.sendMsg("/s_new", "line", s.nextNodeID, 0, 1);
325 s.sendMsg("/n_free", x);
328 section::Sequencing with Routines
332 var space, offset, timer, saw, envsaw, sampler, delay;
334 SynthDef("saw", { arg out=100, pan=0, trig=0.0, freq=500, amp=1, cutoff=10000, rezz=1;
335 freq = Lag.kr(freq,0.1);
336 Out.ar(out,Pan2.ar(RLPF.ar(Saw.ar([freq,freq*2],amp),cutoff,rezz),
340 SynthDef("envsaw",{ arg out=100, pan=0, sustain=0.5, freq=500, amp=1, cutoff=10000, rezz=1;
342 env = EnvGen.kr(Env.perc(0.01, sustain, 0.2), doneAction:0, gate:amp);
343 Out.ar(out,Pan2.ar(RLPF.ar(Saw.ar(Lag.kr(freq,0.1),env),cutoff,rezz)*amp,
347 SynthDef("delay", { arg out=0, delay = 0.4, decay = 14;
350 Out.ar(out, CombN.ar(in, 0.5, delay, decay, 1, in));
353 SynthDef("sampler",{ arg sample, trig=1,rate=1.0,out=0,bufnum=0,pan=0,amp=1, sustain=0.25;
355 env = EnvGen.kr(Env.perc(0.001, sustain, 0.001), doneAction:2);
358 PlayBuf.ar(1,bufnum,rate,InTrig.kr(trig),0,0)*amp,
364 timer = BeatSched.new;
365 offset = Tempo.tempo.reciprocal;
367 space = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav");
370 delay = Synth.after(saw,"delay", [\decay, 20]);
374 r = Routine({ var wait, freq, cutoff,rezz;
375 wait = Pseq([2],inf).asStream;
376 freq = Pseq([30,40,42,40],inf).asStream;
377 cutoff = Pfunc({500.rand2+1000}).asStream;
380 saw.set("freq", freq.next.midicps, "cutoff", cutoff.next, "rezz", rezz, "amp", 0.1, "out", 0);
381 (wait.next*offset).wait
389 r=Routine({ var wait, rate;
390 wait = Pseq([0.25],inf).asStream;
391 rate = Pfunc({0.5.rand}).asStream;
393 Synth.before(delay, "sampler", [\bufnum, space, \trig, 1, \amp,0.1, \rate, rate.next, \sustain, wait.next]);
394 (wait.next*offset).wait});});
401 section::Sequencing with Patterns
405 //sappy emo electronica example...
407 SynthDef("patternefx_Ex", { arg out, in;
409 audio = In.ar([20,21],2);
410 efx=CombN.ar(audio, 0.5, [0.24,0.4], 2, 1);
411 Out.ar([0,1], audio+efx);
414 Synth.new("patternefx_Ex");
416 SynthDef("pattern_Ex", { arg out, freq = 1000, gate = 1, pan = 0, cut = 4000, rez = 0.8, amp = 1;
422 pan) * EnvGen.kr(Env.linen(0.01, 1, 0.3), gate, amp, doneAction:2);
426 SynthDef("bass_Ex", { arg out, freq = 1000, gate = 1, pan = 0, cut = 4000, rez = 0.8, amp = 1;
430 SinOsc.ar(freq,0.05),
432 pan) * EnvGen.kr(Env.linen(0.01, 1, 0.3), gate, amp, doneAction:2);
436 SynthDescLib.global.read;
442 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),
444 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);
448 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),
450 0,Pbind(\instrument,\bass_Ex, \dur,1, \root,-24, \degree,Pseq([0],inf), \pan,0, \cut,128, \rez,0.1, \amp,0.3),
452 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);