// a one-liner like this ...

{ Resonz.ar(Saw.ar(100), LFNoise1.kr(1).range(300, 1200), 0.1) }.play

// ... is actually just a shortcut for this:

// 1.step: 
// evaluate this block by selecting everything between the round brackets (parenthesis)
// and press [ENTER] (OSX) or [CTRL-RETURN] (Win)
(
SynthDef("test", {
	Out.ar(0, Resonz.ar(Saw.ar(100), LFNoise1.kr(1).range(300, 1200), 0.1));
}).store;
)		

// 2.step:
// evaluate this line:
Synth("test")

/* (this is a multi-line comment)

the normal way of doing sound with SC3:
1. write a SynthDef (something like a preset or a model)
2. send it to the server (with .store or .send or .load)
3. start a new Synth based on that synthdef

{ ... }.play (Function-play) is just a shortcut for these steps

*/

// within a SynthDef one could write the code more clearly 
// with the aid of variables:
(
SynthDef("test", {
	var resfreq, sig;
	resfreq = LFNoise1.kr(1).range(300, 1200);
	sig = Saw.ar(100);
	sig = Resonz.ar(sig, resfreq, 0.1);
	Out.ar(0, sig);
	}).store;
)

Synth("test")

// if we use a variable for the new synth ...
y = Synth("test")

// ... we can stop it by sending it the message .free
y.free

/*
remark on variable names:

within functions {}
or within code surrounded by () 
we can declare variable names or arguments 
with more than one letter  

the SC Interpreter (the context where we interactively evaluate commands and code)
has already predefined one-letter variables 'a' to 'z' (their value is 'nil')

during the startup of SC the interpreter variable 's' will be set to Server.local ,
so don't use this variable for other purposes !

*/

// with arguments in the SynthDef ...
(
SynthDef("test2", { arg freq=100, speed=1;
	var resfreq, sig;
	resfreq = LFNoise1.kr(speed).range(300, 1200);
	sig = Saw.ar(freq);
	sig = Resonz.ar(sig, resfreq, 0.1);
	Out.ar(0, sig);
	}).store;
)

// ... it is possible to start new synths with different parameters
y = Synth("test2", [\freq, 130])

y.free 

y = Synth("test2", [\freq, 455, \speed, 20])

y.free 

// ... or even to change a running synth
y = Synth("test2", [\freq, 200])
y.set(\freq, 30)
y.set(\speed, 10)
y.set(\speed, 40, \freq, 70)
y.set(\speed, 12, \freq, 220)
y.set(\freq, 7)
y.set(\freq, rrand(1, 40))

y.free

// a more complicated example (simple physical model)
(
SynthDef("pluck", {arg beat=1.0, dec=1.0, pitch=60, amp=0.5;
	var sig, delay, trig, source;
	// calculate the delay time from MIDI pitch:
	delay = pitch.midicps.reciprocal;	
	// a clock:
	trig = Impulse.ar(beat);
	// a sound source to process:
	source = GrayNoise.ar(amp);
	// a triggered percussive envelope on the source:
	sig = Decay2.ar(trig, 0.003, 0.05, source);
	// comb filter (delay with feedback) on the percussive signal:
	sig = CombL.ar(sig, 0.2, [delay, delay*1.01], dec);
	// remove the DC offset that comes from the comb:
	sig = LeakDC.ar(sig);
	// send the signal to the output:
	Out.ar(0, sig)
	}).send(s);
)	

// test it with a single synth:
y = Synth("pluck", [\beat, 1, \pitch, 60, \dec, 3, \amp, 0.4 ])
y.free

// use a do loop to start 18 synths at the same time with different args:
(
18.do({ arg p;
	Synth("pluck", [\beat, (p+1)/4, \pitch, p*2 + 48, \dec, 6.0/(p+1), \amp, 0.8/(p+1) ])
	})
)