1 package chromesthesia0; 2 3 4 import java.util.Scanner; 5 import javax.swing.JOptionPane; 6 import javax.swing.SwingUtilities; 7 8 import chromesthesia0.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 chromesthesia0.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 chromesthesia0.Pitch[] establishPitches(SPainter painter) { 61 chromesthesia0.Pitch[] pitches = new chromesthesia0.Pitch[9]; 62 chromesthesia0.Pitch pitchMiddleC = new chromesthesia0.Pitch("C", painter); 63 pitches[0] = pitchMiddleC; 64 chromesthesia0.Pitch pitchLowC = new chromesthesia0.Pitch("C,", painter); 65 pitches[1] = pitchLowC; 66 chromesthesia0.Pitch pitchHighC = new chromesthesia0.Pitch("c", painter); 67 pitches[2] = pitchHighC; 68 chromesthesia0.Pitch pitchMiddleD = new chromesthesia0.Pitch("D", painter); 69 pitches[3] = pitchMiddleD; 70 chromesthesia0.Pitch pitchLowD = new chromesthesia0.Pitch("D,", painter); 71 pitches[4] = pitchLowD; 72 chromesthesia0.Pitch pitchHighD = new chromesthesia0.Pitch("d", painter); 73 pitches[5] = pitchHighD; 74 chromesthesia0.Pitch pitchMiddleE = new chromesthesia0.Pitch("E", painter); 75 pitches[6] = pitchMiddleE; 76 chromesthesia0.Pitch pitchLowE = new chromesthesia0.Pitch("E,", painter); 77 pitches[7] = pitchLowE; 78 chromesthesia0.Pitch pitchHighE = new chromesthesia0.Pitch("e", painter); 79 pitches[8] = pitchHighE; 80 return pitches; 81 82 } 83 84 private static chromesthesia0.Pitch find(String token, chromesthesia0.Pitch[] pitches) throws Exception { 85 for (int i = 0; i < pitches.length; i = i + 1) { 86 chromesthesia0.Pitch pitch = pitches[i]; 87 if (pitch.abcName().equals(token)) { 88 return pitch; 89 } 90 } 91 throw new Exception("### PITCH " + token + " NOT FOUND"); 92 } 93 94 private static void display(chromesthesia0.Pitch[] pitches) { 95 for (int i = 0; i < pitches.length; i = i + 1) { 96 System.out.println(pitches[i].toString()); 97 } 98 } 99 100 private static void playMelody(String input, Pitch[] pitches) throws Exception { 101 Scanner scanner = new Scanner(input); 102 while (scanner.hasNext()) { 103 String token = scanner.next(); 104 Pitch pitch = find(token, pitches); 105 pitch.play("1"); 106 } 107 } 108 109 110 // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING 111 112 static private void showErrorMessage(String message) { 113 miro.setVisible(false); 114 JOptionPane.showMessageDialog(null, message); 115 } 116 117 private static void initialization() { 118 // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH 119 miro = new SPainter("Chromesthesia", 500, 500); 120 miro.setVisible(false); 121 miro.setBrushWidth(7); 122 // ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS 123 pitches = establishPitches(miro); 124 display(pitches); 125 } 126 127 private static String getInput() { 128 miro.setVisible(false); 129 String label = "Please enter a melody in ABC notation, or EXIT ... "; 130 String input = JOptionPane.showInputDialog(null, label); 131 miro.setVisible(true); 132 if (input == null) { 133 input = ""; 134 } 135 return input; 136 } 137 138 static private void cleanup() { 139 System.exit(0); 140 } 141 142 } 143 144