(
	s = Server.local;
	s.boot;
	s.notify;
)

(
SynthDef(\MOOplayer, {arg i_out = 0, freq = 440, gate = 1, amp =0.3,wavetable, beats = 1.5, 
					a = 0.4, d = 0.1, s = 0.1, r =1, 
					cutoff = 8.25, q = 0.25, 
					fa = 0.09, fd = 0.32, fs = 0.85, fr = 0.686;

	var modFreq, sin, filterMul;
	freq = freq *10;
	modFreq = freq * 3/2 + 1;
	
	sin = SinOsc.ar(freq/2, Saw.ar(modFreq, 20.dbamp), 20.dbamp);
	
	Out.ar (i_out, EnvGen.kr(Env.adsr(a,d,s,r), gate, doneAction:2) *
		LPF.ar(Resonz.ar((sin), freq, q), modFreq));
}).send(s);

)

(

SynthDef(\MOOobject, {arg i_out = 1, freq = 440, gate = 1, amp =0.3;	var modFreq;
	modFreq = freq * 3/2 + 2;
	
	Out.ar (i_out, EnvGen.kr(Env.perc, gate, doneAction:2) *
		LPF.ar(SinOsc.ar(freq, 0, SinOsc.ar(modFreq,0, 20.dbamp)),
			modFreq)
	);
}).send(s);

)

(
var genRespondFunc, getDataType, getData, playerDurs, objectDurs, 
	playerPBind, objectPBind, manageDurs, densityRespondFunc, densityPbind;

"--- NEW Run -----\n\n".postln;

// intialize some variables

playerDurs = [];
objectDurs = [];

getDataType = {

	arg message;
	
	message.at(1);
};

getData = {

	arg message;
	var values;

	// get rid of the first two items in the array
	values = message.reverse;
	values.pop;
	values.pop;
	values = values.reverse;
	
	// make sure they're ints
	"values ".post;
	values.do({arg val, index;
		val.post;
		" ".post;
		val = val + 0;
		values.put(index, val);
	});

	"".postln;
	values;
};	

manageDurs = {

	arg fracDurs;
	var actualDursMOO;
	
	actualDursMOO  = [];
	
	"manageDurs".postln;
	
	fracDurs.do ({arg val, index;
		var fraq;
		fraq = 1/val; // was 6/val
		fraq.postln;
		actualDursMOO = actualDursMOO.add(fraq);
	});
	
	"actualDursMOO ".post;
	actualDursMOO.postln;
	actualDursMOO;
};

genRespondFunc = {
	arg isPlayer;
	{
		arg time, responder, message;
	
		var instrType, dataType, values;
	
		"respondFunc ".post;
	 	message.postln;
	
		//instrType = message.at(0);
		dataType = getDataType.value(message);

		values = getData.value(message);

		// are we dealing with pitches or durations here?
	
		dataType.postln;
		values.postln;
		// cast to a string
		dataType = "" ++ dataType;
		//instrType = "" ++ instrType;
	
		//if((dataType == "dur".class), {
		if(("dur".compare(dataType) == 0), {
			//if ((instrType == '/player') , {
		
			values = manageDurs.value(values);
		
			if ((isPlayer), {
				playerDurs = values;
				p = values } , 
			{
				objectDurs = values;
				o = values
			});
				
		}, { 
			//if ((dataType == "pitch".class), {
			if((dataType.compare("pitch") == 0), {
			
				//if ((instrType == '/player') , {
				if ((isPlayer), {

					//player
				
					playerDurs.postln;
					p.postln;
					"shouldplay".postln;

					Pbind(\instrument, \MOOplayer, \midinote, Pseq(values,1),
						\dur, Pseq(playerDurs, 1), \amp, 20.dbamp).play;
					} , 
				{	// object
					Pbind(\instrument, \MOOobject, \midinote, Pseq(values,1),
						\dur, Pseq(objectDurs, 1), \amp, 20.dbamp).play
				
				});		

			}, {
				//some other argument
				("Error: invalid arguments: " ++ message).postln;
			});
		});
		nil;
	};
};

densityRespondFunc = { 	
	arg time, responder, message;

	var amp, density;
	
	density = message.at(1);

	amp = 5 * density;
	
	Pbind(\instrument, \MOOdensity, \midinote, Pseq([50, 70], 1),
			\dur, Pseq ([5], 2), \amp, amp.dbamp).play;
};

OSCresponder (nil, '/player', genRespondFunc.value(true)).add;
OSCresponder (nil, '/object', genRespondFunc.value(false)).add;
//OSCresponder (nil, '/density', densityRespondFunc).add;

)