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