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