////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// 'Simple' Synth
////////////////////////////////////////////////////////////////////////////
// Evaluate the below, both blip synth and reverb synth
// This uses a stereo set-up, below is a version for either stereo or four-channel
////////////////////////////////////////////////////////////////////////////

(
SynthDef("myBlip", { 
	arg pitch=69, nharm=4, myAmp=0.05, dur=1.0, attack=0.1, sust=0.1, release=0.8, 
	pan=0.0, // for panning
	outBus = 0, effectBus, direct=0.5; // for the global reverb
	
	var source, sourceDry, sourceWet;
	var env, wobble, freq;
	
	freq = pitch.midicps;
	wobble = LFNoise1.kr(Rand(2, 5), freq * 0.0025, freq * 0.0025);
	source = Blip.ar(freq + wobble, nharm);
 
	env = EnvGen.kr(Env.linen(dur*attack, dur*sust, dur*release, myAmp), myAmp*0.01, doneAction: 2);

	source = source * env;
	
	sourceDry = Pan2.ar( source * direct, pan, 0.3);
	sourceWet = Pan2.ar( source * (1 - direct), pan, 0.3);
		
	Out.ar(outBus, sourceDry);
	Out.ar(effectBus, sourceWet);
					
}).send(s);

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// Reverb
////////////////////////////////////////////////////////////////////////////

	(
	SynthDef("reverb", { arg outBus = 0, inBus, revTime=1.5;
		var input;
		input = In.ar(inBus, 4);
		 
		// a low rent reverb
		// {}.dup(n) will evaluate the function n times, and return an Array of the results
		// The default for n is 2, so this makes a stereo reverb
		16.do({ input = AllpassC.ar(input, 0.04, { Rand(0.001,0.04) }.dup(4), revTime)});
		Out.ar(outBus, input);
	}).send(s);
	);
	
	~effect = Bus.audio(s, 4); // this will be our effects bus
)

// evaluate this to create the reverb synth
~rev = Synth.new("reverb", [\inBus, ~effect]);

/*
now you can test it
Synth( "myBlip", [ \pitch, 60, \myAmp, 0.4, \dur, 0.2, \nharm, [ 3, 4, 5, 6, 7, 8 ].choose, \pan, ((2.0.rand)-1), \effectBus, ~effect ] );
*/

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// Choose between stereo and four-channel (via ~panSetup)
////////////////////////////////////////////////////////////////////////////

(
SynthDef("myBlip", { 
	arg pitch=69, nharm=4, myAmp=0.05, dur=1.0, attack=0.1, sust=0.1, release=0.8, 
	pan=0.0, panx=0.0, pany=0.0, // for panning
	outBus = 0, effectBus, direct=0.5; // for the global reverb
	
	var source, sourceDry, sourceWet;
	var env, wobble, freq;
	
	freq = pitch.midicps;
	wobble = LFNoise1.kr(Rand(2, 5), freq * 0.0025, freq * 0.0025);
	source = Blip.ar(freq + wobble, nharm);
 
	env = EnvGen.kr(Env.linen(dur*attack, dur*sust, dur*release, myAmp), myAmp*0.01, doneAction: 2);

	source = source * env;
	
	if ( ~panSetup == 2, {
		sourceDry = Pan2.ar( source * direct, pan, 0.3);
		sourceWet = Pan2.ar( source * (1 - direct), pan, 0.3);
		}, {
		sourceDry = Pan4.ar( source * direct, panx, pany, 0.3);
		sourceWet = Pan4.ar( source * (1 - direct), panx, pany, 0.3);
		});
		
	Out.ar(outBus, sourceDry);
	Out.ar(effectBus, sourceWet);
					
}).send(s);
)