(
// 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)); // exponential curve of amplification calculated from distance? log????
	//var distance = {|x1,y1,x2,y2| ((x2-x1).squared + (y2-y1).squared).sqrt};
	var distance =   {|x1,y1,x2,y2| (x2-x1) hypot: (y2-y1) }; // hypothenuse
	var amp = 1 / (1 + (distance.cubed * factor));

	//in_x = (in_x + 1) * 0.5 * 650;
	in_x = in_x * 650;
	//in_y = (in_y + 1) * 0.5 * 1260;
	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.14, radius=1, outBus;
	var snd = In.ar(inBus,1);

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

(
SynthDef("reverBusser", {
	arg inBus, outBus, verb1wet=0, verb2wet=0, verb3wet=0, verb4wet=0;
	var verb1, verb2, verb3, verb4;
	var snd = In.ar(inBus, 8);

	verb1 = JPverb.ar(snd.at([0,1]) * verb1wet);
	verb2 = JPverb.ar(snd.at([2,3]) * verb2wet);
    verb3 = JPverb.ar(snd.at([4,5]) * verb3wet);
	verb4 = JPverb.ar(snd.at([6,7]) * verb4wet);
		
	snd.put(0, snd.at(0) + verb1.at(0) );
	snd.put(1, snd.at(1) + verb1.at(1) );
	snd.put(2, snd.at(2) + verb2.at(0) );
	snd.put(3, snd.at(3) + verb2.at(1) );
	snd.put(4, snd.at(4) + verb3.at(0) );
	snd.put(5, snd.at(5) + verb3.at(1) );
	snd.put(6, snd.at(6) + verb4.at(0) );
	snd.put(7, snd.at(7) + verb4.at(1) );
	
	Out.ar(outBus, snd);
}).add;
);
























//////////////////////////////////////////////////////////////////////////////////////////////////////////
// pseudo-UGen proposition by 'elgiano'
// https://scsynth.org/t/simple-spatialisation-function-synthdef-pseudo-ugen-or-ugen/1783/5
/*

OctoPan : MultiOutUGen {

    classvar <>speakers = #[
	[0,0],[0,360],[0,720],[0,1080],[650,1260],[650,900],[650,540],[650,180]
    ];

    *ar { arg in,x=0,y=0,speakers;
        ^this.multiNew('audio', in,x,y);
    }

    *new1 { arg rate,in,x,y;
        var amps = this.speakers.collect{|coords|
	    var dist = coords - [x,y];
	    1/(1+dist[0].hypot(dist[1]))
	};
	^in*amps;
    }
	
}

*/