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