///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// 1. Simple Sine Sound
// Make and control a simple sound
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

// Ugen wrapped in a play.  This does all the nasty stuff for you, but then you want to be able to do the nasty stuff.

{ SinOsc.ar(200, 0, 0.5) }.play;

// and here's the nasty stuff
(
SynthDef( "simple", {
	var out;
	out = SinOsc.ar(200, 0, 0.5);
	Out.ar(0, out)
	}).send(s);
)

Synth( "simple" ); //fine, but I can't stop it without cmd-.

// Here I've called it 'a', but it could be ~anything with a tilde (~) in front.
// and now I can play it
a = Synth( "simple" );

// and stop it in code.
a.free;

// now here it is with an envelope
(
SynthDef( "simple", {
	var out, env;
	env = EnvGen.kr(Env.linen(1, 2, 3, 0.6));
	out = SinOsc.ar(200, 0, env);
	Out.ar(0, out)
	}).send(s);
)

// note synths in the localhost server window
a = Synth( "simple" );

// evaluate the above many times, noting the synths.
// then evalute the below, noting the number of synths.
a.free;
// to clear all the synths now, you have to use cmd-.  Use it!

// here, doneAction means that the synth is released after playing.
// Evaluate
(
SynthDef( "simple", {
	var out, env;
	env = EnvGen.kr(Env.linen(1, 2, 3, 0.6), doneAction: 2);
	out = SinOsc.ar(200, 0, env);
	Out.ar(0, out)
	}).send(s);
)

// play
Synth( "simple" );
// note synths in the localhost server window

(
SynthDef( "simple", { arg freq = 400;
	var out, env;
	env = EnvGen.kr(Env.linen(0.1, 0.2, 0.3, 0.6), doneAction: 2);
	out = SinOsc.ar(freq, 0, env);
	Out.ar(0, out)
	}).send(s);
)

Synth( "simple" );

(
SynthDef( "simple", { arg freq = 400;
	var out, env;
	env = EnvGen.kr(Env.perc(0.05, 1, 1, -4), 0.6, doneAction: 2);
	out = SinOsc.ar(freq, 0, env);
	Out.ar(0, out)
	}).send(s);
)

Synth( "simple", [ \freq, 800 ]);

Synth( "simple", [ \freq, 800.rand ]);

Synth( "simple", [ \freq, 60.midicps ]);

Synth( "simple", [ \freq, 127.rand.midicps ]);

Synth( "simple", [ \freq, 127.rand.postln.midicps ]);

Synth( "simple", [ \freq, 127.rand.midicps.postln ]);

(
SynthDef( "simple", { arg freq = 400, dur = 1.0;
	var out, env;
	env = EnvGen.kr(Env.linen(dur*0.1, dur*0.1, dur*0.8, 0.6), 0.6, doneAction: 2);
	out = SinOsc.ar(freq, 0, env);
	Out.ar(0, out)
	}).send(s); // NB send...
)

Synth( "simple", [ \freq, ( 800.rand + 400 ), \dur, 4.0.rand ]);