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