// This generates EVERYTHING!
~seed = 11735;

~dir = thisProcess.nowExecutingPath.dirname;
PathName.new(~dir).files.do({arg path; if((path.fileName != "main.scd") && (path.fileName != "installation_control.scd") && (path.fileName != "visualize_legacy.scd")  && (path.fileName != "live_utilities.scd"), {path.fileName.loadRelative})});


~bergerTiling = ~berger.value(500, 500, true);
~bergerCreateSynths.value;
~bergerRelTiling = ~berger.value(400, 400, false);
~bergerMusic = ~bergerMusicify.value(~bergerRelTiling, 0, 0, 383, 383, 8, 3, 3.5, ~seed);
~bergerSound = ~bergerSonify.value(~bergerMusic);
~bergerSound.play;
~bergerTranscribe.value(~bergerMusic);
~visualize.value(~berger.value(100, 100), 0, 0, scale: 1, name: "berger")
s.record(~dir +/+ ".." +/+ "recs" +/+ "berger_knuth.wav", duration: (30 * 60));
~bergerMusicFile = File((~dir +/+ "music_data" +/+ "berger.txt").standardizePath, "w");
~bergerMusicFile.write(~bergerMusic.asCompileString);
~bergerMusicFile.close


~robinsonTiling = ~robinson.value(8);
~robinsonCreateSynths.value;
~robinsonMusic = ~robinsonMusicify.value(~robinsonTiling, 0, 0, 200, 200, 6, 3, 3.5, ~seed);
~robinsonSound = ~robinsonSonify.value(~robinsonMusic);
~robinsonSound.play;
~robinsonTranscribe.value(~robinsonMusic);
~visualize.value(~robinsonTiling, 0, 0, 200, 200, scale: 1, name: "robinson");
s.record(~dir +/+ ".." +/+ "recs" +/+ "robinson.wav", duration: (30 * 60));
~robinsonMusicFile = File((~dir +/+ "music_data" +/+ "robinson.txt").standardizePath, "w");
~robinsonMusicFile.write(~robinsonMusic.asCompileString);
~robinsonMusicFile.close


~penroseTiling = ~penrose.value(120, 5, ~seed);
~penroseCreateSynths.value;
~penroseMusic = ~penroseMusicify.value(~penroseTiling, 0, 0, 99, 99, 6, 3, 3.5, ~seed);
~penroseSound = ~penroseSonify.value(~penroseMusic);
~penroseSound.play;
~penroseTranscribe.value(~penroseMusic);
~visualize.value(~penroseTiling, 0, 0, name: "penrose");
s.record(~dir +/+ ".." +/+ "recs" +/+ "penrose.wav", duration: (30 * 60));
~penroseMusicFile = File((~dir +/+ "music_data" +/+ "penrose.txt").standardizePath, "w");
~penroseMusicFile.write(~penroseMusic.asCompileString);
~penroseMusicFile.close


~ammannTiling = ~ammann.value(645, 105);
~ammannCreateSynths.value;
~ammannMusic = ~ammannMusicify.value(~ammannTiling, 0, 0, 640, 100, 7, 3, 3.5, ~seed);
~ammannSound = ~ammannSonify.value(~ammannMusic);
~ammannSound.play;
~ammannTranscribe.value(~ammannMusic);
~visualize.value(~ammann.value(200, 200), 0, 0, name: "ammann")
s.record(~dir +/+ ".." +/+ "recs" +/+ "ammann.wav", duration: (30 * 60));
~ammannMusicFile = File((~dir +/+ "music_data" +/+ "ammann.txt").standardizePath, "w");
~ammannMusicFile.write(~ammannMusic.asCompileString);
~ammannMusicFile.close


~kariTiling = ~kari_culik.value(500, 500, 0, 0, true);
~kariCreateSynths.value;
~kariMusic = ~kariMusicify.value(~kariTiling, 30, 30, 320, 320, ~seed);
~kariSound = ~kariSonify.value(~kariMusic);
~kariSound.play;
~kariTranscribe.value(~kariMusic);
~visualize.value(~kari_culik.value(200, 200, 0, 5, true), 0, 0, scale: 1, name: "kari");
s.record(~dir +/+ ".." +/+ "recs" +/+ "kari_culik.wav", duration: (30 * 60));
~kariMusicFile = File((~dir +/+ "music_data" +/+ "kari.txt").standardizePath, "w");
~kariMusicFile.write(~kariMusic.asCompileString);
~kariMusicFile.close


~jaendelTiling = ~jaendel.value(14, 0, 0, 0);
~jaendelCreateSynths.value;
~jaendelMusic = ~jaendelMusicify.value(~jaendelTiling, 15, 15, 548 * 2 * 1, 64 * 1);
~jaendelSound = ~jaendelSonify.value(~jaendelMusic, 0, 1, 55);
~jaendelSound.play;
~jaendelTranscribe.value(~jaendelMusic);
~visualize.value(~jaendelTiling, 0, 0, name: "jaendel");
s.record(~dir +/+ ".." +/+ "recs" +/+ "jaendel_rao.wav", duration: 30 * 60);
~jaendelMusicFile = File((~dir +/+ "music_data" +/+ "jaendel.txt").standardizePath, "w");
~jaendelMusicFile.write(~jaendelMusic.asCompileString);
~jaendelMusicFile.close


//~~~~~~~~~crypto visualizer code
//check to make sure that jaendel is offset by at least 13
(
~genVisualCrypto = {arg dir,
	seed = 11735,
	genTilings = false,
	genSecrets = true,
	genOTP = true,
	genShadowArray0 = true,
	genShadowArray1 = true,
	genIMGData = true,
	writeIMGs = true,
	visualize = false;

	var path, bergerTiling, robinsonTiling, penroseTiling, ammannTiling, kariTiling, jaendelTiling, tilings, offsets,
	secrets, otp, shadowArray0, shadowArray1, shadowImg0, shadowImg1;

	path = dir +/+ ".." +/+ "visualizations" +/+ seed;
	File.mkdir(path);

	if(genTilings, {
		var texts, file;
		//for final tilings

		bergerTiling = ~berger.value(500, 500, true);
		robinsonTiling = ~robinson.value(8);
		penroseTiling = ~penrose.value(140, 5, ~seed);
		ammannTiling = ~ammann.value(500, 140);
		kariTiling = ~kari_culik.value(500, 500, 0, 0, true);
		jaendelTiling = ~jaendel.value(13, 0, 0, 0);

		//array form
		texts = ["from oracles ", "subsets of infinite orders ", "on undecidable questions "];
		tilings = [
			[jaendelTiling, kariTiling, texts[0]],
			[ammannTiling, texts[1], penroseTiling],
			[texts[2], robinsonTiling, bergerTiling]
		];

		//write tiling data
		file = File((path +/+ "tiling_data_with_text.txt").standardizePath, "w");
		file.write(tilings.asCompileString);
		file.close;
	}, {
		//read tilings
		tilings = File.readAllString((path +/+ "tiling_data_with_text.txt").standardizePath);
		tilings = tilings.interpret;
	});
	"tilings generated".postln;

	if(genSecrets, {
		var file;
		offsets = [
			[[15, 15], [30, 30], [0, 0]],
			[[0, 0], [0, 0], [0, 0]],
			[[0, 0], [0, 0], [0, 0]]
		];
		//gen secrets
		secrets = ~genSecrets.value(tilings, offsets);

		//write secrets
		file = File((path +/+ "/secrets.txt").standardizePath, "w");
		file.write(secrets.asCompileString);
		file.close
	}, {
		//read secrets
		secrets = File.readAllString((path +/+ "/secrets.txt").standardizePath);
		secrets = secrets.interpret
	});
	"secrets generated".postln;

	if(genOTP, {
		var file;
		//gen on time pad
		otp = ~genOneTimePad.value(secrets[0][0].size, secrets[0][0][0].size);

		//write otp
		file = File((path +/+ "/otp.txt").standardizePath, "w");
		file.write(otp.asCompileString);
		file.close
	}, {
		//read otp
		otp = File.readAllString((path +/+ "/otp.txt").standardizePath);
		otp = otp.interpret
	});
	"otp generated".postln;

	if(genShadowArray0, {
		var file;
		//gen first shadow array
		shadowArray0 = ~genShadowArray0.value(secrets, otp);

		//write first shadow array
		file = File((path +/+ "/shadow_data_0.txt".standardizePath), "w");
		file.write(shadowArray0.asCompileString);
		file.close
	}, {
		//read first shadow array
		shadowArray0 = File.readAllString((path +/+ "/shadow_data_0.txt").standardizePath).interpret;
	});
	"shadow0 generated".postln;

	if(genShadowArray1, {
		var file;
		//gen second shadow array
		shadowArray1 = ~genShadowArray1.value(otp, shadowArray0.size, shadowArray0.size);

		//write second shadow array
		file = File((path +/+ "/shadow_data_1.txt".standardizePath), "w");
		file.write(shadowArray1.asCompileString);
		file.close
	}, {
		//read second shadow array
		shadowArray1 = File.readAllString((path +/+ "/shadow_data_1.txt").standardizePath).interpret;
	});
	"shadow1 generated".postln;

	if(genIMGData, {
		shadowImg0 = ~genShadowImage.value(shadowArray0);
		shadowImg1 = ~genShadowImage.value(shadowArray1);
	});

	if(writeIMGs, {
		shadowImg0.write((path +/+  "/shadow_img_0_final.png").standardizePath , quality: 100);
		shadowImg1.write((path +/+ "/shadow_img_1_final.png").standardizePath , quality: 100);
	});
	"imgs generated".postln;

	if(visualize, {
		~visualize.value([shadowImg0, shadowImg1]);
	});

};
)

(
~genVisualCrypto.value(
	dir: ~dir,
	seed: ~seed,
	genTilings: false,
	genSecrets: false,
	genOTP: false,
	genShadowArray0: false,
	genShadowArray1: true,
	genIMGData: true,
	writeIMGs: true,
	visualize: false
)
)

(
~genVisualCrypto.value(
	dir: ~dir,
	seed: ~seed,
	genTilings: true,
	genSecrets: true,
	genOTP: true,
	genShadowArray0: true,
	genShadowArray1: true,
	genIMGData: true,
	writeIMGs: true,
	visualize: false
)
)

(
~genVisualCrypto.value(
	dir: ~dir,
	seed: ~seed,
	genTilings: false,
	genSecrets: false,
	genOTP: false,
	genShadowArray0: false,
	genShadowArray1: true,
	genIMGData: true,
	writeIMGs: true,
	visualize: false
)
)

~ammannTiling.flat.asSet.postln

///////////////////////// older code for examples
~visualize.value(~berger.value(5, 5), 0, 0, scale: 6, name: "berger")
~visualize.value(~berger.value(20, 15), 0, 0, scale: 6, name: "berger")
~visualize.value(~berger.value(48, 36), 0, 0, scale: 10, name: "berger")
~visualize.value(~berger.value(48 * 2, 36 * 2), 0, 0, scale: 5, name: "berger")

~visualize.value(~robinsonTiling, 0, 0, 48, 36, scale: 10, name: "robinson");
~visualize.value(~robinsonTiling, 0, 0, 48 * 2, 36 * 2, scale: 5, name: "robinson");

~visualize.value(~penrose.value(50), 0, 0, 48, 36, scale: 10, name: "penrose");
~visualize.value(~penrose.value(100), 0, 0, 48 * 2, 36 * 2, scale: 5, name: "penrose");

~visualize.value(~ammann.value(200, 100), 0, 0, 48, 36, scale: 10, name: "ammann")
~visualize.value(~ammann.value(200, 100), 0, 0, 48 * 2, 36 * 2, scale: 5, name: "ammann")

~visualize.value(~kari_culik.value(36, 48, 0, 5, true), 0, 0, scale: 10, name: "kari");
~visualize.value(~kari_culik.value(36 * 2, 48 * 2, 0, 5, true), 0, 0, scale: 5, name: "kari");

~visualize.value(~jaendel.value(8, 2, 0, 0), 20, 20, 48, 36, scale: 10, name: "jaendel");
~visualize.value(~jaendel.value(8, 2, 0, 0), 20, 20, 48 * 2, 36 * 2, scale: 5, name: "jaendel");


///////////////////////// length calculations
1356 / 60
//approx. 22.5
//6 sections

436 * 4 / 60
//approx 19 - 29
//10 sections

614 * 2 / 60
//approx 20.5
//20 sections

1385 / 60
//approx 23
//20 sections

915 / 50
//approx 18 to 22
//10 sections

270 * 4 / 90
//approx 12
//8 sections
