1 package chromesthesia0; 2 3 import painter.SPainter; 4 5 import javax.swing.*; 6 import java.util.Scanner; 7 8 public class Chromesthesia { 9 10 public static void main(String[] args) { 11 SwingUtilities.invokeLater(new ThreadForGUI()); 12 } 13 14 private static class ThreadForGUI implements Runnable { 15 @Override 16 public void run() { 17 new Chromesthesia(); 18 } 19 } 20 21 public Chromesthesia() { 22 interpreter(); 23 } 24 25 private static SPainter miro; 26 private static Pitch[] pitches; 27 28 public static void interpreter() { 29 initialization(); 30 while ( true ) { 31 String input = getInput(); 32 if (input.equalsIgnoreCase("EXIT")) { 33 break; 34 } else { 35 try { 36 playMelody(input,pitches); 37 } catch (Exception ex) { 38 showErrorMessage(ex.toString()); 39 } 40 } 41 } 42 43 cleanup(); 44 45 } 46 47 private static Pitch[] establishPitches(SPainter painter) { 48 Pitch[] pitches = new Pitch[9]; 49 Pitch pitchMiddleC = new Pitch("C", painter); 50 pitches[0] = pitchMiddleC; 51 Pitch pitchLowC = new Pitch("C,", painter); 52 pitches[1] = pitchLowC; 53 Pitch pitchHighC = new Pitch("c", painter); 54 pitches[2] = pitchHighC; 55 Pitch pitchMiddleD = new Pitch("D", painter); 56 pitches[3] = pitchMiddleD; 57 Pitch pitchLowD = new Pitch("D,", painter); 58 pitches[4] = pitchLowD; 59 Pitch pitchHighD = new Pitch("d", painter); 60 pitches[5] = pitchHighD; 61 Pitch pitchMiddleE = new Pitch("E", painter); 62 pitches[6] = pitchMiddleE; 63 Pitch pitchLowE = new Pitch("E,", painter); 64 pitches[7] = pitchLowE; 65 Pitch pitchHighE = new Pitch("e", painter); 66 pitches[8] = pitchHighE; 67 return pitches; 68 } 69 70 private static Pitch find(String token, Pitch[] pitches) throws Exception { 71 for(int i = 0; i < pitches.length; i++) { 72 Pitch pitch = pitches[i]; 73 if(pitch.abcName().equals(token) ) { 74 return pitch; 75 } 76 } 77 throw new Exception("### PITCH " + token + " NOT FOUND"); 78 } 79 80 private static void display(Pitch[] pitches) { 81 for(int i = 0; i < pitches.length; i++) { 82 System.out.println(pitches[i].toString()); 83 } 84 } 85 86 private static void playMelody(String input, Pitch[] pitches) throws Exception { 87 Scanner scanner = new Scanner(input); 88 while ( scanner.hasNext() ) { 89 String token = scanner.next(); 90 Pitch pitch = find(token,pitches); 91 pitch.play("1"); 92 } 93 } 94 95 // Initialization, Cleanup, Getting Input, Error Messaging 96 97 static private void showErrorMessage(String message) { 98 miro.setVisible(false); 99 JOptionPane.showInputDialog(null, message); 100 } 101 102 private static void initialization() { 103 //Establish the painter 104 miro = new SPainter("Chromesthesia",500,500); 105 miro.setVisible(false); 106 miro.setBrushWidth(7); 107 //Establish Pitch class objects 108 pitches = establishPitches(miro); 109 display(pitches); 110 } 111 112 private static String getInput() { 113 miro.setVisible(false); 114 String label = "Please enter a melody in ABC notation, or EXIT ... "; 115 String input = JOptionPane.showInputDialog(null, label); 116 miro.setVisible(true); 117 if ( input == null) { input = "";} 118 return input; 119 } 120 121 static private void cleanup() { 122 System.exit(0); 123 } 124 125 } 126