1 package chromesthesia2; 2 import java.util.Scanner; 3 import javax.swing.JOptionPane; 4 import javax.swing.SwingUtilities; 5 import painter.SPainter; 6 public class Chromesthesia 7 { 8 public static void main(String[] args) 9 { 10 SwingUtilities.invokeLater(new ThreadForGUI()); 11 } 12 private static class ThreadForGUI implements Runnable 13 { 14 @Override 15 public void run() 16 { 17 new Chromesthesia(); 18 } 19 } 20 public Chromesthesia() 21 { 22 interpreter(); 23 } 24 private static SPainter miro; 25 private static Pitch[] pitches; 26 public static void interpreter() 27 { 28 initialization(); 29 String previousMelody = " "; 30 while (true) 31 { 32 String input = getInput(); 33 if (input.equalsIgnoreCase("EXIT")) 34 { 35 break; 36 } 37 else if(input.equalsIgnoreCase("AGAIN")) 38 { 39 if (previousMelody != " ") 40 { 41 try 42 { 43 playMelody(previousMelody, pitches); 44 } 45 catch (Exception ex) 46 { 47 showErrorMessage(ex.toString()); 48 } 49 } 50 else 51 { 52 showErrorMessage("There was no melody"); 53 } 54 55 } 56 else previousMelody = input; 57 try 58 { 59 playMelody(input, pitches); 60 } 61 catch(Exception ex) 62 { 63 showErrorMessage(ex.toString()); 64 } 65 66 } 67 cleanup(); 68 } 69 70 private static Pitch[] establishPitches(SPainter painter) 71 { 72 Pitch[] pitches = new Pitch[21]; 73 Pitch pitchMiddleC = new Pitch("C,", painter); 74 pitches[0] = pitchMiddleC; 75 Pitch pitchLowC = new Pitch("C", painter); 76 pitches[1] = pitchLowC; 77 Pitch pitchHighC = new Pitch("c", painter); 78 pitches[2] = pitchHighC; 79 Pitch pitchMiddleD = new Pitch("D", painter); 80 pitches[3] = pitchMiddleD; 81 Pitch pitchLowD = new Pitch("D,", painter); 82 pitches[4] = pitchLowD; 83 Pitch pitchHighD = new Pitch("d", painter); 84 pitches[5] = pitchHighD; 85 Pitch pitchMiddleE = new Pitch("E", painter); 86 pitches[6] = pitchMiddleE; 87 Pitch pitchLowE = new Pitch("E,", painter); 88 pitches[7] = pitchLowE; 89 Pitch pitchHighE = new Pitch("e", painter); 90 pitches[8] = pitchHighE; 91 Pitch pitchLowF = new Pitch("F,", painter); 92 pitches[9] = pitchLowF; 93 Pitch pitchMiddleF = new Pitch("F", painter); 94 pitches[10] = pitchMiddleF; 95 Pitch pitchHighF = new Pitch("f", painter); 96 pitches[11] = pitchHighF; 97 Pitch pitchLowG = new Pitch("G,", painter); 98 pitches[12] = pitchLowG; 99 Pitch pitchMiddleG = new Pitch("G", painter); 100 pitches[13] = pitchMiddleG; 101 Pitch pitchHighG = new Pitch("g", painter); 102 pitches[14] = pitchHighG; 103 Pitch pitchLowA = new Pitch("A,", painter); 104 pitches[15] = pitchLowA; 105 Pitch pitchMiddleA = new Pitch("A", painter); 106 pitches[16] = pitchMiddleA; 107 Pitch pitchHighA = new Pitch("a", painter); 108 pitches[17] = pitchHighA; 109 Pitch pitchLowB = new Pitch("B,", painter); 110 pitches[18] = pitchLowB; 111 Pitch pitchMiddleB = new Pitch("B", painter); 112 pitches[19] = pitchMiddleB; 113 Pitch pitchHighB = new Pitch("b", painter); 114 pitches[20] = pitchHighB; 115 return pitches; 116 } 117 118 private static Pitch find(String token, Pitch[] pitches) throws Exception 119 { 120 for (int i = 0; i < pitches.length; i = i + 1) 121 { 122 Pitch pitch = pitches[i]; 123 if (pitch.abcName().equals(token)) 124 { 125 return pitch; 126 } 127 } 128 throw new Exception("### PITCH " + token + "NOT FOUND"); 129 } 130 131 private static void display(Pitch[] pitches) 132 { 133 for (int i = 0; i < pitches.length; i = i + 1) 134 { 135 System.out.println(pitches[i].toString()); 136 } 137 } 138 139 private static void playMelody(String input, Pitch[] pitches) throws Exception 140 { 141 Scanner scanner = new Scanner(input); 142 while (scanner.hasNext()) 143 { 144 String token = scanner.next(); 145 String pitchName; 146 String duration = ""; 147 if(token.indexOf(",") < 0) 148 { 149 pitchName = token.substring(0, 1); 150 duration = token.substring(1); 151 } 152 else 153 { 154 pitchName = token.substring(0, 2); 155 duration = token.substring(2); 156 } 157 if(duration.length() == 0) {duration = "1";} 158 Pitch pitch = find(pitchName,pitches); 159 pitch.play(duration); 160 } 161 } 162 163 static private void showErrorMessage(String message) 164 { 165 miro.setVisible(false); 166 JOptionPane.showMessageDialog(null, message); 167 } 168 169 private static void initialization() 170 { 171 miro = new SPainter("Chromesthesia", 500, 500); 172 miro.setVisible(false); 173 miro.setBrushWidth(7); 174 pitches = establishPitches(miro); 175 display(pitches); 176 } 177 178 private static String getInput() { 179 miro.setVisible(false); 180 String label = "Please enter a melody in ABC notation, or Exit or Again... "; 181 String input = JOptionPane.showInputDialog(null, label); 182 miro.setVisible(true); 183 if (input == null) { 184 input = ""; 185 } 186 return input; 187 } 188 static private void cleanup() 189 { 190 System.exit(0); 191 } 192 } 193