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