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