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