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