package chromesthesia2; import java.util.Scanner; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; import painter.SPainter; public class Chromesthesia { // INFRASTRUCTURE FOR THE PROGRAM -- LAUNCHING A "GRAPHICS" THREAD public static void main(String[] args) { SwingUtilities.invokeLater(new ThreadForGUI()); } private static class ThreadForGUI implements Runnable { @Override public void run() { new Chromesthesia(); } } public Chromesthesia() { interpreter(); } // FEATURED VARIABLES private static SPainter miro; private static Pitch[] pitches; private static String data; // THE INTERPRETER public static void interpreter() { initialization(); // miro and pitches while (true) { String input = getInput(); if (input.equalsIgnoreCase("EXIT")) { break; } else if(input.equalsIgnoreCase("AGAIN")){ try { playMelody(data, pitches); } catch (Exception ex) { showErrorMessage(ex.toString()); } } else { try { data = input; playMelody(input, pitches); } catch (Exception ex) { showErrorMessage(ex.toString()); } } } cleanup(); // miro has to go } // METHODS PERTAINING TO THE CHROMESTHETIC PITCHES private static Pitch[] establishPitches(SPainter painter) { Pitch[] pitches = new Pitch[21]; Pitch pitchMiddleC = new Pitch("C", painter); Pitch pitchLowC = new Pitch("C,", painter); Pitch pitchHighC = new Pitch("c", painter); Pitch pitchMiddleD = new Pitch("D", painter); Pitch pitchLowD = new Pitch("D,", painter); Pitch pitchHighD = new Pitch("d", painter); Pitch pitchMiddleE = new Pitch("E", painter); Pitch pitchLowE = new Pitch("E,", painter); Pitch pitchHighE = new Pitch("e", painter); Pitch pitchMiddleF = new Pitch("F", painter); Pitch pitchLowF = new Pitch("F,", painter); Pitch pitchHighF = new Pitch("f", painter); Pitch pitchMiddleG = new Pitch("G", painter); Pitch pitchLowG = new Pitch("G,", painter); Pitch pitchHighG = new Pitch("g", painter); Pitch pitchMiddleA = new Pitch("A", painter); Pitch pitchLowA = new Pitch("A,", painter); Pitch pitchHighA = new Pitch("a", painter); Pitch pitchMiddleB = new Pitch("B", painter); Pitch pitchLowB = new Pitch("B,", painter); Pitch pitchHighB = new Pitch("b", painter); pitches[0] = pitchMiddleC; pitches[1] = pitchLowC; pitches[2] = pitchHighC; pitches[3] = pitchMiddleD; pitches[4] = pitchLowD; pitches[5] = pitchHighD; pitches[6] = pitchMiddleE; pitches[7] = pitchLowE; pitches[8] = pitchHighE; pitches[9] = pitchMiddleF; pitches[10] = pitchLowF; pitches[11] = pitchHighF; pitches[12] = pitchMiddleG; pitches[13] = pitchLowG; pitches[14] = pitchHighG; pitches[15] = pitchMiddleA; pitches[16] = pitchLowA; pitches[17] = pitchHighA; pitches[18] = pitchMiddleB; pitches[19] = pitchLowB; pitches[20] = pitchHighB; return pitches; } private static Pitch find(String token, Pitch[] pitches) throws Exception { for (int i = 0; i < pitches.length; i = i + 1) { Pitch pitch = pitches[i]; if (pitch.abcName().equals(token)) { return pitch; } } throw new Exception("### PITCH " + token + " NOT FOUND"); } private static void display(Pitch[] pitches) { for (int i = 0; i < pitches.length; i = i + 1) { System.out.println(pitches[i].toString()); } } private static void playMelody(String input, Pitch[] pitches) throws Exception { Scanner scanner = new Scanner(input); while (scanner.hasNext()) { String token = scanner.next(); String pitchName; String duration = ""; if (token.indexOf(",") < 0){ pitchName = token.substring(0,1); duration = token.substring(1); } else { pitchName = token.substring(0,2); duration = token.substring(2); } if (duration.length() == 0){ duration = "1";} Pitch pitch = find(pitchName,pitches); pitch.play(duration); } } private static void playMelodyAgain(String input, Pitch[] pitches) throws Exception { Scanner scanner = new Scanner(input); while (scanner.hasNext()) { String token = scanner.next(); String pitchName; String duration = ""; if (token.indexOf(",") < 0){ pitchName = token.substring(0,1); duration = token.substring(1); } else { pitchName = token.substring(0,2); duration = token.substring(2); } if (duration.length() == 0){ duration = "1";} Pitch pitch = find(pitchName,pitches); pitch.play(duration); } } // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING static private void showErrorMessage(String message) { miro.setVisible(false); JOptionPane.showMessageDialog(null, message); } private static void initialization() { // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH miro = new SPainter("Chromesthesia", 500, 500); miro.setVisible(false); miro.setBrushWidth(7); // ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS pitches = establishPitches(miro); display(pitches); } private static String getInput() { miro.setVisible(false); String label = "Please enter a melody in ABC notation, or EXIT ... "; String input = JOptionPane.showInputDialog(null, label); miro.setVisible(true); if (input == null) { input = ""; } return input; } static private void cleanup() { System.exit(0); } }