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