/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package tonerow; /** * * @author ljp50 */ import java.io.*; import java.util.*; import java.time.*; public class ToneRowMain { protected static String[] noteNames = new String[] { "c", "c#", "d", "d#", "e", "f", "f#", "g", "g#", "a", "a#", "b" }; // instance variable, protected static String inputFileName = "ToneRowsIn.txt"; // instance variable, protected static String outputFileName = "ToneRowMatrix.txt"; // instance variable, protected static String cycleSeparator = "====== ****** ======"; protected static String oHeading = "O ===>"; protected static String rFooting = "<=== R"; protected static String[] iLeftMatrix = new String[] { "I", "|", "|", "|", "v", " ", " ", " ", " ", " ", " ", " " }; protected static String[] riRightMatrix = new String[] { " ", " ", " ", " ", " ", " ", " ", "^", "|", "|", "|", "RI" }; // is visible for other methods in same class // and for subclasses. /** * @param args the command line arguments */ public static void main(String[] args) { String[] originalRowStringOp33a = new String[] { "a#", "f", "c", "b", "f#", "d#", "c#", "a", "g", "e", "d", "g#" }; String[] originalRowString = new String[] { "b", "c#", "f#", "a#", "f", "d#", "a", "g#", "g", "e", "c", "d" }; // prepare print write for file output long startTime = Instant.now().toEpochMilli(); PrintWriter fileOut = null; String currentPrintLine = null; try { File outputFile = new File(outputFileName); fileOut = new PrintWriter(outputFile); System.out.println("fileout printwriter started"); } catch (IOException e) { System.out.println("ToneRowMatrix.txt not found"); } // read input file with rows and titles BufferedReader fileInReader = null; String oneInputLine = null; try { fileInReader = new BufferedReader(new FileReader(inputFileName)); oneInputLine = fileInReader.readLine(); while (oneInputLine != null) { System.out.println(oneInputLine); String[] firstSplitInputLine = oneInputLine.split("\\$"); System.out.println("split one a = " + firstSplitInputLine[0] + " b = " + firstSplitInputLine[1]); String[] secondSplitInputLine = firstSplitInputLine[0].toLowerCase().split(" "); CreateOneMatrix(secondSplitInputLine, firstSplitInputLine[1], fileOut); oneInputLine = fileInReader.readLine(); } fileInReader.close(); } catch (IOException e) { System.out.println("ToneRowsIn.txt not found"); } // print execution time in sysout currentPrintLine = " "; fileOut.println(currentPrintLine); fileOut.println(currentPrintLine); fileOut.println(currentPrintLine); fileOut.println(currentPrintLine); long endTime = Instant.now().toEpochMilli(); long timeElapsed = endTime - startTime; double secondsElapsed = timeElapsed / 1000.0; currentPrintLine = "Execution time: " + secondsElapsed + " seconds"; System.out.println(currentPrintLine); fileOut.println(currentPrintLine); // end of main class fileOut.close(); System.out.println("end main class tonerow println"); } public static void CreateOneMatrix(String originalRowStringIn[], String titleOfRow, PrintWriter fileOut) { String currentPrintLine = " "; String[] originalRowString = new String[12]; for (int i = 0; i < 12; i++) { originalRowString[i] = originalRowStringIn[i]; } int originalRowInt[] = new int[12]; int originalRowIntervalToNextNote[] = new int[12]; int invertedRowInt[] = new int[12]; int toneRowMatrix[][] = new int[12][12]; // TODO code application logic here System.out.println(Arrays.toString(noteNames)); for (int i = 0; i < noteNames.length; i++) { System.out.print(noteNames[i] + " "); } System.out.println("end"); for (int i = 0; i < noteNames.length; i++) { System.out.print(String.format("%s ", noteNames[i])); } System.out.println("end"); for (int i = 0; i < noteNames.length; i++) { System.out.print(padRightSpaces(noteNames[i], 3)); } System.out.println("end"); // // mark begining of composition // System.out.println(" "); fileOut.println(" "); fileOut.println(" "); fileOut.println(" "); System.out.println(cycleSeparator); currentPrintLine = "processing composition " + titleOfRow; System.out.println(currentPrintLine); System.out.println(cycleSeparator); System.out.println(" "); // dump input row to sysout System.out.print("originalRow with ints = "); for (int i = 0; i < originalRowString.length; i++) { originalRowInt[i] = IntFromNoteString(originalRowString[i]); System.out.print(originalRowString[i] + "=" + Integer.toString(originalRowInt[i]) + " "); } System.out.println("end"); System.out.print("intervals originalRow = "); for (int i = 0; i < originalRowString.length - 1; i++) { originalRowIntervalToNextNote[i] = IntFromNoteString(originalRowString[i + 1]) - IntFromNoteString(originalRowString[i]); System.out.print( originalRowString[i] + "=>" + originalRowString[i + 1] + "~t=" + Integer.toString(originalRowIntervalToNextNote[i]) + " " ); } System.out.println("end"); System.out.println("construct original row from ints"); for (int i = 0; i < originalRowString.length - 1; i++) { String currentNoteNumber = Integer.toString(originalRowInt[i]); String currentNote = originalRowString[i]; String currentIntervalToNextNote = Integer.toString(originalRowIntervalToNextNote[i]); String nextNote = nextNoteFromIntInterval(originalRowInt[i], originalRowIntervalToNextNote[i]); String nextNoteNumber = Integer.toString(IntFromNoteString(nextNote)); System.out.println( "currentNote = " + currentNote + "=" + currentNoteNumber + " IntervalToNext = " + currentIntervalToNextNote + " NextNote = " + nextNote + "=" + nextNoteNumber ); } System.out.println("construct inverted row from ints"); invertedRowInt[0] = originalRowInt[0]; for (int i = 0; i < originalRowString.length - 1; i++) { int currentNoteNumberInt = invertedRowInt[i]; String currentNoteNumber = Integer.toString(currentNoteNumberInt); String currentNote = noteNames[currentNoteNumberInt]; String currentIntervalToNextNote = Integer.toString(originalRowIntervalToNextNote[i]); String nextInvertedNote = nextInvertedNoteFromIntInterval(currentNoteNumberInt, originalRowIntervalToNextNote[i]); String nextNoteNumber = Integer.toString(IntFromNoteString(nextInvertedNote)); invertedRowInt[i + 1] = IntFromNoteString(nextInvertedNote); System.out.println( "cureentNote = " + currentNote + "=" + currentNoteNumber + " IntervalToNext = " + currentIntervalToNextNote + " NextInvertedNote = " + nextInvertedNote + "=" + nextNoteNumber ); } // build top row of matrx, original tone row for (int i = 0; i < originalRowInt.length; i++) { toneRowMatrix[i][11] = originalRowInt[i]; } // Build full chart // By running inversions in columns // starting with the second row for (int i = 0; i < originalRowInt.length; i++) { for (int j = originalRowInt.length - 1; j > 0; j--) { int currentNoteNumberInt = toneRowMatrix[i][j]; String nextInvertedNote = nextInvertedNoteFromIntInterval(currentNoteNumberInt, originalRowIntervalToNextNote[11 - j]); int nextNoteNumberInt = IntFromNoteString(nextInvertedNote); toneRowMatrix[i][j - 1] = IntFromNoteString(nextInvertedNote); } } // print the matrix header numbers System.out.print(" "); for (int i = 0; i < originalRowInt.length; i++) { System.out.print("{" + padRightSpaces(Integer.toString(i), 2) + "} "); } System.out.println(" end row"); System.out.println("begin print matrix integers"); // print the matrix integers for (int j = originalRowInt.length - 1; j > -1; j--) { System.out.print("{" + padRightSpaces(Integer.toString(j), 2) + "} "); for (int i = 0; i < originalRowInt.length; i++) { String currentNoteName = noteNames[toneRowMatrix[i][j]]; int currentNoteInt = toneRowMatrix[i][j]; String currentNoteNamePadded = padRightSpaces(currentNoteName, 5); String currentNoteIntPadded = padRightSpaces(Integer.toString(currentNoteInt), 5); System.out.print(currentNoteIntPadded); } System.out.println("end row"); } System.out.println("end print matrix integers"); // *** System.out.println("begin print matrix strings"); // print the matrix header numbers currentPrintLine = " "; // fileOut.print(currentPrintLine); System.out.print(currentPrintLine); for (int i = 0; i < originalRowInt.length; i++) { currentPrintLine = "{" + padRightSpaces(Integer.toString(i), 2) + "} "; // fileOut.print("currentPrintLine); // fileOut.print(" "); System.out.print(currentPrintLine); } currentPrintLine = " end row"; fileOut.println(""); // fileOut.println(currentPrintLine); System.out.println(currentPrintLine); // print the matrix strings for (int j = originalRowInt.length - 1; j > -1; j--) { currentPrintLine = "{" + padRightSpaces(Integer.toString(j), 2) + "} "; // fileOut.print(currentPrintLine); // fileOut.print(" "); System.out.print(currentPrintLine); for (int i = 0; i < originalRowInt.length; i++) { String currentNoteName = noteNames[toneRowMatrix[i][j]]; int currentNoteInt = toneRowMatrix[i][j]; String currentNoteNamePadded = padRightSpaces(currentNoteName, 5); String currentNoteIntPadded = padRightSpaces(Integer.toString(currentNoteInt), 5); currentPrintLine = currentNoteNamePadded; // fileOut.print(currentPrintLine); System.out.print(currentPrintLine); } currentPrintLine = "end row"; // fileOut.println(currentPrintLine); //fileOut.println(""); System.out.println(currentPrintLine); } System.out.println("end print matrix strings"); System.out.println("begin print matrix strings with 1-12 with full display format"); // print composition title currentPrintLine = " "; System.out.println(currentPrintLine); fileOut.print(currentPrintLine); System.out.print(currentPrintLine); System.out.println(titleOfRow); fileOut.println(titleOfRow); currentPrintLine = " "; fileOut.println(currentPrintLine); System.out.println(currentPrintLine); // print O header currentPrintLine = " "; fileOut.print(currentPrintLine); System.out.print(currentPrintLine); currentPrintLine = oHeading; fileOut.println(currentPrintLine); System.out.println(currentPrintLine); // print the matrix header numbers currentPrintLine = " "; fileOut.print(currentPrintLine); System.out.print(currentPrintLine); for (int i = 0; i < originalRowInt.length; i++) { currentPrintLine = " " + padRightSpaces(Integer.toString(i + 1), 2) + " "; fileOut.print(currentPrintLine); // fileOut.print(" "); System.out.print(currentPrintLine); } currentPrintLine = " "; fileOut.println(""); // fileOut.println(currentPrintLine); System.out.println(currentPrintLine); // print the matrix strings and 1-12 integers for (int j = originalRowInt.length - 1; j > -1; j--) { // Insert left I arrow matrix currentPrintLine = " " + padRightSpaces(iLeftMatrix[11 - j], 2); fileOut.print(currentPrintLine); System.out.print(currentPrintLine); // insert left number currentPrintLine = " " + padRightSpaces(Integer.toString(12 - j), 2) + " "; fileOut.print(currentPrintLine); System.out.print(currentPrintLine); for (int i = 0; i < originalRowInt.length; i++) { String currentNoteName = noteNames[toneRowMatrix[i][j]]; int currentNoteInt = toneRowMatrix[i][j]; String currentNoteNamePadded = padRightSpaces(currentNoteName, 5); String currentNoteIntPadded = padRightSpaces(Integer.toString(currentNoteInt), 5); currentPrintLine = currentNoteNamePadded; fileOut.print(currentPrintLine.toUpperCase()); System.out.print(currentPrintLine); } // end number is inverted for ri currentPrintLine = padRightSpaces(Integer.toString(j + 1), 3); fileOut.print(currentPrintLine); // fileOut.print(" "); System.out.print(currentPrintLine); // Insert right ri arrow matrix currentPrintLine = " " + padRightSpaces(riRightMatrix[11 - j], 2); fileOut.print(currentPrintLine); System.out.print(currentPrintLine); // fileOut.println(currentPrintLine); currentPrintLine = " "; fileOut.println(currentPrintLine); System.out.println(currentPrintLine); } // print the matrix footer numbers (inverse) currentPrintLine = " "; fileOut.print(currentPrintLine); System.out.print(currentPrintLine); for (int i = 0; i < originalRowInt.length; i++) { currentPrintLine = " " + padRightSpaces(Integer.toString(12 - i), 2) + " "; fileOut.print(currentPrintLine); // fileOut.print(" "); System.out.print(currentPrintLine); } currentPrintLine = " "; fileOut.println(currentPrintLine); System.out.println(currentPrintLine); // fileOut.println(currentPrintLine); // System.out.println(currentPrintLine); // print r footer currentPrintLine = " "; fileOut.print(currentPrintLine); System.out.print(currentPrintLine); currentPrintLine = rFooting; fileOut.println(currentPrintLine); System.out.println(currentPrintLine); System.out.println("end print matrix strings 1-12"); // // mark end of composition // System.out.println(" "); System.out.println(cycleSeparator); currentPrintLine = "end composition " + titleOfRow; System.out.println(currentPrintLine); System.out.println(cycleSeparator); System.out.println(" "); return; } public static String padRightSpaces(String strin, int len) { int lenstr = strin.length(); String outString = strin; for (int i = lenstr; i < len; i++) { outString = outString + " "; } return outString; } public static int IntFromNoteString(String origNote) { int location = 99; for (int i = 0; i < noteNames.length; i++) { if (noteNames[i].equals(origNote)) { location = i; } } return location; } public static String nextNoteFromIntInterval(int origNoteInt, int interval) { String nextNote = noteNames[(origNoteInt + interval + 12) % 12]; return nextNote; } public static String nextInvertedNoteFromIntInterval(int origNoteInt, int interval) { String nextNote = noteNames[(origNoteInt - interval + 12) % 12]; return nextNote; } }