1 /* 2 * This program interprets melodic lines given in ABC notation as a 3 * chromesthete might. 4 * 5 * A Pitch class will be defined, and will take center stage in the 6 * processing. 7 * 8 * Interpreting a melody in ABC notation will amount to flashing 9 * colored rectangles for prescribed durations, while sounding 10 * the pitch! The color of the rectangle will correspond to pitch 11 * class. The duration will correspond to the duration of the note. 12 * For this first version of the program, the duration will be held 13 * constant at 1 beat. 14 * 15 * Three sorts of images will appear on the screen, the chromesthetic 16 * output box, a text input box, and an error message box. Simplicity 17 * of design is rendered by permitting only one box to be on the screen 18 * at a time. 19 * 20 * ABC represents notes in a manner consistent with these examples: 21 * C, D, E, C D E c d e 22 * 23 * Google ABC music representation if you would like to know more aAbout it. 24 */ 25 package chromesthesia2; 26 27 import chromesthesia1.Pitch; 28 import painter.SPainter; 29 30 import javax.swing.*; 31 import java.util.Scanner; 32 33 public class Chromesthesia { 34 // FEATURED VARIABLES 35 private static SPainter miro; 36 private static Pitch[] pitches; 37 38 public Chromesthesia() { 39 interpreter(); 40 } 41 42 // INFRASTRUCTURE FOR THE PROGRAM -- LAUNCHING A "GRAPHICS" THREAD 43 public static void main(String[] args) { 44 SwingUtilities.invokeLater(new ThreadForGUI()); 45 } 46 47 // THE INTERPRETER 48 public static void interpreter() { 49 initialization(); //micro and pitches. 50 String previousInput = ""; 51 String currentInput = ""; 52 while (true) { 53 String input = getInput(); 54 if (input.equalsIgnoreCase("EXIT")) { 55 break; 56 } else if (input.equalsIgnoreCase("AGAIN")) { 57 previousInput = currentInput; 58 try { 59 playMelody(currentInput, pitches); 60 } catch (Exception ex) { 61 showErrorMessage(ex.toString()); 62 } 63 } else { 64 previousInput = currentInput; 65 currentInput = input; 66 try { 67 playMelody(currentInput, pitches); 68 } catch (Exception ex) { 69 showErrorMessage(ex.toString()); 70 } 71 } 72 } 73 cleanup(); //micro has to go 74 75 } 76 77 // METHODS PERTAINING TO THE CHROMESTHETIC PITCHES 78 private static Pitch[] establishPitches(SPainter painter) { 79 int asMuchPitchesAsWeNeed = 21; 80 Pitch[] pitches = new Pitch[asMuchPitchesAsWeNeed]; 81 82 Pitch pitchMiddleC = new Pitch("C", painter); 83 pitches[0] = pitchMiddleC; 84 Pitch pitchLowC = new Pitch("C,", painter); 85 pitches[1] = pitchLowC; 86 Pitch pitchHighC = new Pitch("c", painter); 87 pitches[2] = pitchHighC; 88 89 Pitch pitchMiddleD = new Pitch("D", painter); 90 pitches[3] = pitchMiddleD; 91 Pitch pitchLowD = new Pitch("D,", painter); 92 pitches[4] = pitchLowD; 93 Pitch pitchHighD = new Pitch("d", painter); 94 pitches[5] = pitchHighD; 95 96 Pitch pitchMiddleE = new Pitch("E", painter); 97 pitches[6] = pitchMiddleE; 98 Pitch pitchLowE = new Pitch("E,", painter); 99 pitches[7] = pitchLowE; 100 Pitch pitchHighE = new Pitch("e", painter); 101 pitches[8] = pitchHighE; 102 103 Pitch pitchMiddleF = new Pitch("F", painter); 104 pitches[9] = pitchMiddleF; 105 Pitch pitchLowF = new Pitch("F,", painter); 106 pitches[10] = pitchLowF; 107 Pitch pitchHighF = new Pitch("f", painter); 108 pitches[11] = pitchHighF; 109 110 Pitch pitchMiddleG = new Pitch("G", painter); 111 pitches[12] = pitchMiddleG; 112 Pitch pitchLowG = new Pitch("G,", painter); 113 pitches[13] = pitchLowG; 114 Pitch pitchHighG = new Pitch("g", painter); 115 pitches[14] = pitchHighG; 116 117 Pitch pitchMiddleA = new Pitch("A", painter); 118 pitches[15] = pitchMiddleA; 119 Pitch pitchLowA = new Pitch("A,", painter); 120 pitches[16] = pitchLowA; 121 Pitch pitchHighA = new Pitch("a", painter); 122 pitches[17] = pitchHighA; 123 124 Pitch pitchMiddleB = new Pitch("B", painter); 125 pitches[18] = pitchMiddleB; 126 Pitch pitchLowB = new Pitch("B,", painter); 127 pitches[19] = pitchLowB; 128 Pitch pitchHighB = new Pitch("b", painter); 129 pitches[20] = pitchHighB; 130 131 return pitches; 132 } 133 134 private static Pitch find(String token, Pitch[] pitches) throws Exception { 135 for (int i = 0; i < pitches.length; i = i + 1) { 136 Pitch pitch = pitches[i]; 137 if (pitch.abcName().equals(token)) { 138 return pitch; 139 } 140 } 141 throw new Exception("### PITCH " + token + " NOT FOUND"); 142 } 143 144 private static void display(Pitch[] pitches) { 145 for (int i = 0; i < pitches.length; i = i + 1) { 146 System.out.println(pitches[i].toString()); 147 } 148 } 149 150 private static void playMelody(String input, Pitch[] pitches) throws Exception { 151 Scanner scanner = new Scanner(input); 152 while (scanner.hasNext()) { 153 String token = scanner.next(); 154 String pitchName; 155 String duration = ""; 156 if (token.indexOf(",") < 0) { 157 pitchName = token.substring(0, 1); 158 duration = token.substring(1); 159 } else { 160 pitchName = token.substring(0, 2); 161 duration = token.substring(2); 162 } 163 if (duration.length() == 0) { 164 duration = "1"; 165 } 166 Pitch pitch = find(pitchName, pitches); 167 pitch.play(duration); 168 } 169 } 170 171 // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING 172 static private void showErrorMessage(String message) { 173 miro.setVisible(false); 174 JOptionPane.showMessageDialog(null, message); 175 } 176 177 private static void initialization() { 178 // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH 179 miro = new SPainter("Chromesthesia", 500, 500); 180 miro.setVisible(false); 181 miro.setBrushWidth(7); 182 // ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS 183 pitches = establishPitches(miro); 184 display(pitches); 185 } 186 187 private static String getInput() { 188 miro.setVisible(false); 189 String label = "Please enter a melody in ABC notation, or EXIT ... "; 190 String input = JOptionPane.showInputDialog(null, label); 191 miro.setVisible(true); 192 if (input == null) { 193 input = ""; 194 } 195 return input; 196 } 197 198 static private void cleanup() { 199 System.exit(0); 200 } 201 202 private static class ThreadForGUI implements Runnable { 203 @Override 204 public void run() { 205 new Chromesthesia(); 206 } 207 } 208 }