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