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