(
~ammannTranscribe = {arg seqs;
	var dir, basePath, subTiling, tileMap, tiles, genTileMap, odds, harms, amps;

	//thisThread.randSeed = 100;

	//dir = thisProcess.nowExecutingPath.dirname;
	//basePath = dir +/+ ".." +/+ "score" +/+ "lilypond_v2.24_update" +/+ "ammann_distributed";
	basePath = "/home/mwinter/Portfolio/a_history_of_the_domino_problem/a_history_of_the_domino_problem_source/score/lilypond_v2.24_update/ammann_distributed";

	seqs.do({arg part, p;

		var lilyFile, lilyString, lastNote, lastTimeSig, lastClef, lastDynamic, beatCountTotal;

		//create file
		lilyFile = File(basePath +/+ "includes" +/+ "ammann_" ++ "part_" ++ p.asString ++ ".ly".standardizePath,"w");

		//start lilypond directives
		lilyString = "";
		lilyString = lilyString ++ "{\n\\set tupletFullLength = ##t\n"; //"\\set Score.markFormatter = #format-mark-box-numbers\n";
		//lilyString = lilyString ++ "{\n";
		//lilyString = lilyString + "\\tempo 4 = 60 \n";
		lilyString = lilyString ++ "\n\\tempo \\markup {
       \\concat {
         \\smaller \\general-align #Y #DOWN
         \\note #\"4\" #1 \\normal-text \" ≈ 60\"
}}\n";

		lastNote = "r";
		lastTimeSig = "";
		lastClef = "\\clef treble ";
		lastDynamic = "";
		beatCountTotal = 0;
		part.size.do({arg nIndex;
			var dirs, del, dur, sustain, harm, amp, quant, note, beatCount, durUnit, measureDur, timeSig, clef, delayGroup, tuplet, dynamic, octFlag = false;
			dirs = part[nIndex];
			del = dirs[0][0];
			dur = dirs[0][1];
			sustain = dirs[1, 1]; //not used
			harm = dirs[2][1];
			amp = dirs[3][1];
			quant = dirs[4];

			/*
			if(amp != 0, {
				note = ["c", "cis", "d", "dis", "e", "f", "fis", "g", "gis", "a", "ais", "b"][((55 * harm).cpsmidi % 12).trunc.asInteger];
				note = note ++ [",,", ",", "", "'", "''"][((55 * if(harm >= 8, {harm / 2}, {harm})).cpsmidi / 12).trunc.asInteger - 2];
			}, {note = "r"});
			*/



			if(amp != 0, {
				note = ["c", "cih", "cis", "deh", "d", "dih", "dis", "eeh", "e", "eih", "f", "fih", "fis", "geh", "g", "gih", "gis", "aeh", "a", "aih", "ais", "beh", "b", "bih"][((55 * harm).cpsmidi % 12).round(0.5) * 2];
				note = note ++ [",,", ",", "", "'", "''", "'''", "''''"][((55 * if(harm >= 8, {harm /* / 2 */}, {harm})).cpsmidi / 12).trunc.asInteger - 2];
			}, {note = "r"});



			/*
			if((amp == 0) || (harm < 8) ,  {note = "r"}, {
				note = ["c", "cih", "cis", "deh", "d", "dih", "dis", "eeh", "e", "eih", "f", "fih", "fis", "geh", "g", "gih", "gis", "aeh", "a", "aih", "ais", "beh", "b", "bih"][((55 * harm).cpsmidi % 12).round(0.5) * 2];
				//if(p == 4, {(harm + " sdfsdfsd").postln});
				note = note ++ [",,", ",", "", "'", "''", "'''", "''''"][((55 * if(harm >= 8, {harm /* / 2 */}, {harm})).cpsmidi / 12).trunc.asInteger - 2];
			});
			*/


			//tmp note vals
			//note = ["c'", "d'", "e'", "f'", "g'", "a'", "b'", "c''", "d''", "e''", "f''", "g''", "a''", "b''", "c'''", "d'''"][harm];

			beatCount = 0;
			durUnit = 0.25;
			measureDur = del + dur;

			//put in a tie if there is a delay
			if((del > 0) && (nIndex > 0) && (lastNote != "r"), {lilyString = lilyString ++ "~ "});
			if(lilyString.keep(-4) == "} ~ ", {lilyString = lilyString.drop(-4) ++ "~ } "});

			//double bar
			if(((nIndex % 32) == 0) && (nIndex != 0), {lilyString = lilyString ++ "\\bar \"||\" "});
			//bar check
			if(nIndex != 0, {lilyString = lilyString ++ " | " ++ "%{ noteIndex: " ++ nIndex.asString ++ " beat: " ++ beatCountTotal ++ " %} "});
			//rehearsal mark
			if((nIndex % 32) == 0, {lilyString = lilyString ++ "\\mark \\default "});

			//set time signature for each measure
			timeSig = "\\numericTimeSignature \\time " ++ if((measureDur / 0.5 % 2) == 0, {measureDur.trunc.asInteger.asString ++ "/4 "},
				{(measureDur / 0.5).trunc.asInteger.asString ++ "/8 "});
			if(lastTimeSig != timeSig, {lilyString = lilyString ++ timeSig}, {lilyString = lilyString ++ "%{" ++ timeSig ++ "%} "});
			lastTimeSig = timeSig;


			tuplet = case
			{quant == 0.25} {[1/4, " "]}
			{quant == (1/3)} {[1/6, "3\/2 "]}
			{quant == (1/5)} {[1/5, "5\/4 "]};

			//put any quarter notes held from the last measure
			if(del.trunc.asInteger >= 1, {
				beatCount = beatCount + del.trunc.asInteger;
				lilyString = lilyString ++ del.trunc.asInteger.collect({arg index; lastNote ++ "4 " ++
					if(((del.frac / quant) >= 1) && (lastNote != "r"), {"~ "}, {""})}).join
			});

			//transition from last note to new note
			if((del.frac / quant) >= 1, {var lastNoteFrac, noteFrac;

				beatCount = beatCount + 1;
				//only start a tuplet if necessary
				if(quant != 0.25, {lilyString = lilyString ++ "\\tuplet " ++ tuplet[1] + " { "});
				//end of last note
				lastNoteFrac = switch((del.frac / tuplet[0]).round.asInteger, 1, {"16 "}, 2, {"8 "}, 3, {"8. "}, 4, {"4 "});
				lilyString = lilyString ++ lastNote ++ lastNoteFrac;

				lilyString = lilyString ++ "%{notechange%} ";
				//put clef if necessary
				if(amp != 0, {
					clef = if((55 * harm).cpsmidi >= 57, {"\\clef treble "}, {"\\clef bass "});
					if(lastClef != clef, {lilyString = lilyString + clef});
					lastClef = clef;
				});

				//beginning of new note
				noteFrac = switch(((if(beatCount > measureDur, {measureDur.frac}, {1}) - del.frac) / tuplet[0]).round.asInteger, 1, {"16 "}, 2, {"8 "}, 3, {"8. "}, 4, {"4 "});

				dynamic = if(amp == 0.06, {"\\mf "}, {"\\p "});

				lilyString = lilyString ++ note ++ noteFrac ++
				if((note != "r"), {if(harm >= 8, {"upp "}, {"down "})}, {""}) ++
				if((lastDynamic != dynamic) && (note != "r"), {lastDynamic = dynamic; dynamic}, {""}) ++
				if((((measureDur - beatCount) / 0.5).trunc.asInteger > 0) && (note != "r"), {"~ "}, {""});

				if(quant != 0.25, {lilyString = lilyString ++ " } "});
				octFlag = false;
			}, {

				lilyString = lilyString ++ "%{notechange%} ";

				//put clef if necessary
				if(amp != 0, {
					clef = if((55 * harm).cpsmidi >= 57, {"\\clef treble "}, {"\\clef bass "});
					if(lastClef != clef, {lilyString = lilyString + clef});
					lastClef = clef;
				});
				octFlag = true;

			});

			dynamic = if(amp == 0.06, {"\\mf "}, {"\\p "});
			lilyString = lilyString ++ (measureDur - beatCount).trunc.asInteger.collect({arg b; beatCount = beatCount + 1; note ++ "4 " ++
				if(octFlag && (b == 0) && (note != "r"), {if(harm >= 8, {"upp "}, {"down "})}, {""}) ++
				if((lastDynamic != dynamic) && (note != "r"), {lastDynamic = dynamic; dynamic}, {""}) ++
				if(((measureDur - beatCount) > 0) && (note != "r"), {"~ "}, {""});
			}).join;
			if((measureDur - beatCount) > 0, {
				lilyString = lilyString ++ ((measureDur - beatCount).frac / 0.5).trunc.asInteger.collect({note ++ "8 "}).join});


			beatCountTotal = beatCountTotal + measureDur;
			lastNote = note

		});

		lilyString.findRegexp(
			"([a-g]|[a-g]+is|[a-g]+ih|[a-g]+eh|r)(,,|,|'|''|)4 (upp |down |)(\\\\mf |\\\\p |)(~ )*" ++
			"([a-g]|[a-g]+is|[a-g]+ih|[a-g]+eh|r)(,,|,|'|''|)4 (\\\\mf |\\\\p |)(~ )*" ++
			"([a-g]|[a-g]+is|[a-g]+ih|[a-g]+eh|r)(,,|,|'|''|)4 (\\\\mf |\\\\p |)(~ )*" ++
			"([a-g]|[a-g]+is|[a-g]+ih|[a-g]+eh|r)(,,|,|'|''|)4").clump(16).do({arg match;
			if(
				[
					match[1][1] ++ match[2][1],
					match[6][1] ++ match[7][1],
					match[10][1] ++ match[11][1],
					match[14][1] ++ match[15][1]
				].asSet.size == 1, {lilyString = lilyString.replace(match[0][1], match[1][1] ++ match[2][1] ++ "1" ++ match[3][1] ++ match[4][1])
			})
		});

		lilyString.findRegexp(
			"([a-g]|[a-g]+is|[a-g]+ih|[a-g]+eh|r)(,,|,|'|''|)4 (upp |down |)(\\\\mf |\\\\p |)(~ )*" ++
			"([a-g]|[a-g]+is|[a-g]+ih|[a-g]+eh|r)(,,|,|'|''|)4 (\\\\mf |\\\\p |)(~ )*" ++
			"([a-g]|[a-g]+is|[a-g]+ih|[a-g]+eh|r)(,,|,|'|''|)4").clump(12).do({arg match;
			if(
				[

					match[1][1] ++ match[2][1],
					match[6][1] ++ match[7][1],
					match[10][1] ++ match[11][1]
				].asSet.size == 1, {lilyString = lilyString.replace(match[0][1], match[1][1] ++ match[2][1] ++ "2." ++ match[3][1] ++ match[4][1])
			})
		});

		lilyString.findRegexp(
			"([a-g]|[a-g]+is|[a-g]+ih|[a-g]+eh|r)(,,|,|'|''|)4 (upp |down |)(\\\\mf |\\\\p |)(~ )*" ++
			"([a-g]|[a-g]+is|[a-g]+ih|[a-g]+eh|r)(,,|,|'|''|)4").clump(8).do({arg match;
			if(
				[

					match[1][1] ++ match[2][1],
					match[6][1] ++ match[7][1]
				].asSet.size == 1, {lilyString = lilyString.replace(match[0][1], match[1][1] ++ match[2][1] ++ "2" ++ match[3][1] ++ match[4][1])
			})
		});


		lilyString.findRegexp(
			"([a-g]|[a-g]+is|[a-g]+ih|[a-g]+eh|r)(,,|,|'|''|)8 (upp |down |)(\\\\mf |\\\\p |)(~ )*" ++
			"([a-g]|[a-g]+is|[a-g]+ih|[a-g]+eh|r)(,,|,|'|''|)8").clump(8).do({arg match;
			if(
				[

					match[1][1] ++ match[2][1],
					match[6][1] ++ match[7][1]
				].asSet.size == 1, {lilyString = lilyString.replace(match[0][1], match[1][1] ++ match[2][1] ++ "4" ++ match[3][1] ++ match[4][1])
			})
		});

		lilyString.findRegexp(
			"([a-g]|[a-g]+is|[a-g]+ih|[a-g]+eh|r)(,,|,|'|''|)4 (upp |down |)(\\\\mf |\\\\p |)(~ )*" ++
			"([a-g]|[a-g]+is|[a-g]+ih|[a-g]+eh|r)(,,|,|'|''|)8 ").clump(8).do({arg match;
			if(
				[
					match[1][1] ++ match[2][1],
					match[6][1] ++ match[7][1]
				].asSet.size == 1, {lilyString = lilyString.replace(match[0][1], match[1][1] ++ match[2][1] ++ "4. " ++ match[3][1] ++ match[4][1])
			})
		});

		lilyString.findRegexp(
			"([a-g]|[a-g]+is|[a-g]+ih|[a-g]+eh|r)(,,|,|'|''|)8 (upp |down |)(\\\\mf |\\\\p |)(~ )*" ++
			"([a-g]|[a-g]+is|[a-g]+ih|[a-g]+eh|r)(,,|,|'|''|)4 ").clump(8).do({arg match;
			if(
				[

					match[1][1] ++ match[2][1],
					match[6][1] ++ match[7][1]
				].asSet.size == 1, {lilyString = lilyString.replace(match[0][1], match[1][1] ++ match[2][1] ++ "4. " ++ match[3][1] ++ match[4][1])
			})
		});


		//rest consolidation for half parts
		lilyString.findRegexp("\\\\tuplet\\h*\\d/\\d\\h*{\\h*r\\d.?\\h*%{notechange%}\\h*r\\d.?\\h*}").do({arg match;
			lilyString = lilyString.replace(match[1], "r4")
		});

		lilyString.findRegexp("\\\\time\\h*\\d/\\d\\h*%?}?\\h*((%{notechange%})?\\h*r\\d.?\\h*)*\\|").do({arg match;
			if(match[1].contains("time"), {
				var repString = "", drop = 10, sig = "";
				case
				{match[1].contains("time 1/4")} {repString =  "r4 | "}
				{match[1].contains("time 2/4")} {repString = "r2 | "}
				{match[1].contains("time 3/4")} {repString = "r2. | "}
				{match[1].contains("time 4/4")} {repString = "r1 | "}
				{match[1].contains("time 5/4")} {repString = "r2 r2. | "}
				{match[1].contains("time 6/4")} {repString = "r1. | "}
				{match[1].contains("time 7/4")} {repString = "r1 r2. | "}
				{match[1].contains("time 8/4")} {repString = "r1 r1 | "}
				{match[1].contains("time 9/4")} {repString = "r2. r2. r2. | "}
				{match[1].contains("time 10/4")} {repString = "r1 r1 r2 | "}
				//{match[1].contains("time 11/4")} {repString = "r1 r1 r2. | "}
				{match[1].contains("time 3/8")} {repString = "r4. | "}
				{match[1].contains("time 5/8")} {repString = "r4. r4 | "}
				{match[1].contains("time 7/8")} {repString = "r2 r4. | "}
				{match[1].contains("time 9/8")} {repString = "r4. r4. r4.| "};

				if(match[1].contains("10/4") || match[1].contains("11/4"), {drop = 11});
				if(match[1].findRegexp("\\d/\\d %}").size > 0, {drop = drop + 3});
				if(match[1].findRegexp("\\d\\d/\\d %}").size > 0, {drop = drop + 1});

				//if(match[1].contains("11"), {
				//	[match[1], match[1].drop(drop), repString].postln;
				//});

				lilyString = lilyString.replace(match[1].drop(drop), " \\hideNotes "  ++ repString ++ " \\unHideNotes ")

				//sig = match[1].findRegexp("\\d/\\d")[0][1];
				//sig.postln;
				//lilyString = lilyString.replace(match[1].drop(drop), (" R1*" ++ sig ++ " | ").postln)
			})
		});


		//lilyString = lilyString.replace("upp ", "^\\markup{\\bold{\\larger{↑}}}").replace("down", "^\\markup{\\bold{\\larger{↓}}}");
		lilyString = lilyString.replace("upp ", "").replace("down", "");

		lilyString = lilyString.replace("\\note #\"4\" #1 \\normal-text \" ≈ 60\"", "\\note {4} #1 \\normal-text \" ≈ 60\"");


		//write file
		lilyString = lilyString ++ "\\bar \"|.\"\n}";
		lilyFile.write(lilyString);
		lilyFile.close;
	});

};

~ammannTranscribe.value(~ammannMusic)
)
