(
var primes, hsArrayToFreq, hsArrayDimDiff, file, seq, phraseLengths, musicData, patterns, fund, seqIndex;

thisThread.randSeed = 1954111620240509; //everything in between
//thisThread.randSeed = 20240509; //rise yitgadal
fund = 100;

~m1 = NetAddr("192.168.4.200", 54001);
~m2 = NetAddr("192.168.4.202", 54000);
~m3 = NetAddr("192.168.4.203", 54000);

primes = [2, 3, 5, 7, 11, 13, 17];

hsArrayToFreq = {
	arg array;
	array.collect({arg dim, d; pow(primes[d], dim)}).product
};

hsArrayDimDiff = {
	arg array1, array2;
	var fArray;
	fArray = array2.drop(1) - array1.drop(1);
	if(fArray.sum == 0, {1}, {(primes[fArray.abs.indexOf(1) + 1] * fArray.sum)})
};

//file = File("/home/mwinter/Portfolio/compact_sets/sirens.txt".standardizePath,"r");
//file = File("/home/mwinter/Sketches/compact_sets/rise_yitgadal.txt".standardizePath,"r");
file = File("/home/mwinter/Portfolio/compact_sets/everything_in_between.txt".standardizePath,"r");
seq = file.readAllString.interpret;

//seq = seq.collect({arg item; item.sort});
seq = seq.collect({arg chord, index;
	var ref_ins;
	ref_ins = if(index == 0, {
		[0] // this should actually check which is the 'centered' pitch
	}, {
		[seq[index - 1], chord].postln.flop.collect({arg pair, p; if(pair[0] == pair[1], {p}, {-1})}).postln.removeEvery([-1])
	});
	chord.collect({arg pitch;
		var dimDiff;
		ref_ins.postln;
		if(ref_ins.size == 1, {
			[pitch, [ref_ins[0], hsArrayDimDiff.value(chord[ref_ins[0]], pitch)]]
		}, {
			var min_ref_ins;
			min_ref_ins = ref_ins.minItem({arg ins; (chord[ins].drop(1) - pitch.drop(1)).abs.sum});
			[pitch.postln, [min_ref_ins.postln, hsArrayDimDiff.value(chord[min_ref_ins], pitch)]]
		});
	})
});

seq = seq.collect({arg chord; chord; chord.collect({arg pitch; fund * hsArrayToFreq.value(pitch[0])}).postln});
seq.postln;


seqIndex = -1;
w = Window.new("compact sets siren player");
w.view.keyDownAction = { arg view, char, modifiers, unicode, keycode;
	[char, keycode].postln;
	if((keycode == 44) && ((seqIndex - 1) >= 0), {
		seqIndex = seqIndex - 1;
		~m1.sendMsg("/freq", 1, seq[seqIndex][0].postln);
		~m1.sendMsg("/freq", 2, seq[seqIndex][1].postln);
		~m1.sendMsg("/freq", 3, seq[seqIndex][2].postln);
	});

	if((keycode == 46) && ((seqIndex + 1) < seq.size), {
		seqIndex = seqIndex + 1;
		~m1.sendMsg("/freq", 1, seq[seqIndex][0].postln);
		~m1.sendMsg("/freq", 2, seq[seqIndex][1].postln);
		~m1.sendMsg("/freq", 3, seq[seqIndex][2].postln);
	});
	if((keycode == 47) && ((seqIndex - 1) >= 0) && ((seqIndex + 1) < seq.size), {
		~m1.sendMsg("/freq", 1, seq[seqIndex][0].postln);
		~m1.sendMsg("/freq", 2, seq[seqIndex][1].postln);
		~m1.sendMsg("/freq", 3, seq[seqIndex][2].postln);
	});
};
w.front;

/*
~musicData = musicData;

~patterns = Ppar(
	musicData.collect({arg voice, v;
		var freqs, durs, attacks, delays, sustains, rels, amps;
		# freqs, durs, attacks, delays, sustains, rels, amps = voice.flop;
		//# durs, attacks, delays, sustains, rels = [durs, attacks, delays, sustains, rels].collect({arg data; data / 16});
		Pbind(
			\instrument, \string_model,
			\freq, Pseq(freqs, 1),
			\dur, Pseq(durs, 1),
			\attack, Pseq(attacks, 1),
			\sustain, Pseq(sustains, 1),
			\release, Pseq(rels, 1),
			\amp, Pseq(amps, 1),
			\del, Pseq(delays, 1),
			\busIndex, v
		)
	});
);

~patterns.play
*/
)

(
w = Window.new("I catch keystrokes");
w.view.keyDownAction = { arg view, char, modifiers, unicode, keycode;  [char, keycode].postln; };
w.front;
)