

(
// a function to position sound in space
~octoPanOut = {
	arg in, in_x, in_y, fact; 

	var s1_x=0, s1_y=0, s1_amp,
	s2_x=0, s2_y=360, s2_amp,
	s3_x=0, s3_y=720, s3_amp,
	s4_x=0, s4_y=1080, s4_amp,
	s5_x=650, s5_y=1260, s5_amp,
	s6_x=650, s6_y=900, s6_amp,
	s7_x=650, s7_y=540, s7_amp,
	s8_x=650, s8_y=180, s8_amp ;

	var factor = (1 / (5000000 * fact)); 
	var distance =   {|x1,y1,x2,y2| (x2-x1) hypot: (y2-y1) }; // hypothenuse
	var amp = 1 / (1 + (distance.cubed * factor));

	// x,y are expected in rage 0-1 
	in_x = in_x * 650;
	in_y = in_y  * 1260;
	
	s1_amp = amp.value(in_x,in_y,s1_x,s1_y);
	s2_amp = amp.value(in_x,in_y,s2_x,s2_y);
	s3_amp = amp.value(in_x,in_y,s3_x,s3_y);
	s4_amp = amp.value(in_x,in_y,s4_x,s4_y);
	s5_amp = amp.value(in_x,in_y,s5_x,s5_y);
	s6_amp = amp.value(in_x,in_y,s6_x,s6_y);
	s7_amp = amp.value(in_x,in_y,s7_x,s7_y);
	s8_amp = amp.value(in_x,in_y,s8_x,s8_y);

	[in * s1_amp, in * s2_amp, in * s3_amp, in * s4_amp, in * s5_amp, in * s6_amp, in * s7_amp, in * s8_amp]
	
};


SynthDef("octoPanner", {
	arg inBus, x=0, y=0, radius=1; 
	var snd = In.ar(inBus,1);

	y = SinOsc.kr(0.1,phase:1.5pi).range(0,1);
	x = SinOsc.kr(0.1,phase:1pi,mul:5).clip2.range(0,1);

	snd = ~octoPanOut.value(snd, x, y, radius);
	
	Out.ar(0, snd);
}).add;


SynthDef(\testOcto, {
	arg out;

	var son = WhiteNoise.ar(1) ;

	Out.ar(out,son);
	//Out.ar(1,son);
}).add;



)

( // init busses for octoPanners
~octoBus1.free;
~octoBus2.free;
~octoBus3.free;
~octoBus4.free;

~octoBus1 = Bus.audio(s,1);
~octoBus2 = Bus.audio(s,1);
~octoBus3 = Bus.audio(s,1);
~octoBus4 = Bus.audio(s,1);

// work with groups?
~grp1 = Group.new;
~grp2 = Group.new;
)

( // init octoPanners

// if using groups
~octoPanner1 = Synth("octoPanner", [\inBus, ~octoBus1], ~grp1, \addToTail);
~octoPanner2 = Synth("octoPanner", [\inBus, ~octoBus2], ~grp2, \addToTail);

// if using just one group, but control order of execution with \addBefore
~octoPanner3 = Synth("octoPanner", [\inBus, ~octoBus3]);
~octoPanner4 = Synth("octoPanner", [\inBus, ~octoBus4]);
//~octoPanner4.free
)

// groups
~whiteNoise = Synth(\testOcto, [\out, ~octoBus1], ~grp1);
~whiteNoise2 = Synth(\testOcto, [\out, ~octoBus2], ~grp2);

// without groups
~whiteNoise3 = Synth(\testOcto, [\out, ~octoBus3], ~octoPanner3, \addBefore);
~whiteNoise4 = Synth(\testOcto, [\out, ~octoBus4], ~octoPanner4, \addBefore);
~whiteNoise4.free

~octoPanner1.set(\radius, 0.01); // can control through
~octoPanner2.set(\radius, 0.01); // can control through
~octoPanner3.set(\radius, 0.01); // can control through
~octoPanner4.set(\radius, 0.01); // can control through




~grp1.free;
~grp2.free
