1 2 3 /* 4 *This program interprets melodic lines given in ABC notation as a chromesthete might. 5 * 6 * A pitch will be defined, and will take center stage in the 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 the 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 chromesthesia0; 28 29 30 import painter.SPainter; 31 import java.util.Scanner; 32 import javax.swing.SwingUtilities; 33 import javax.swing.JOptionPane; 34 35 36 public class Chromesthesia { 37 //INFRASTRUCTURE FOR THE PROGRAM -- LAUNCHING A "GRAPHICS" THREAD 38 39 public static void main(String[] args) { 40 SwingUtilities.invokeLater(new ThreadForGUI()); 41 } 42 43 private static class ThreadForGUI implements Runnable { 44 @Override 45 public void run() { 46 47 new 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 initialization(); //miro and pitches 64 65 while (true) { 66 String input = getInput(); 67 68 if (input.equalsIgnoreCase("EXIT")) { 69 break; 70 71 } else { 72 try { 73 playMelody(input, pitches); 74 } catch (Exception ex) { 75 showErrorMessage(ex.toString()); 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[9]; 86 Pitch pitchMiddleC = new Pitch("C",painter); 87 pitches[0] = pitchMiddleC; 88 89 Pitch pitchLowC = new Pitch("C,",painter); 90 pitches[1] = pitchLowC; 91 Pitch pitchHighC = new Pitch("c",painter); 92 pitches[2] = pitchHighC; 93 94 Pitch pitchMiddleD = new Pitch("D",painter); 95 pitches[3] = pitchMiddleD; 96 Pitch pitchLowD = new Pitch("D,",painter); 97 pitches[4] = pitchLowD; 98 99 Pitch pitchHighD = new Pitch("d",painter); 100 pitches[5] = pitchHighD; 101 Pitch pitchMiddleE = new Pitch("E",painter); 102 pitches[6] = pitchMiddleE; 103 104 Pitch pitchLowE = new Pitch("E,",painter); 105 pitches[7] = pitchLowE; 106 Pitch pitchHighE = new Pitch("e",painter); 107 pitches[8] = pitchHighE; 108 109 return pitches; 110 } 111 112 private static Pitch find(String token, Pitch[] pitches) throws Exception { 113 for ( int i = 0; i < pitches.length; i = i + 1 ) { 114 115 Pitch pitch = pitches[i]; 116 if ( pitch.abcName().equals(token) ) { 117 118 return pitch; 119 } 120 121 } 122 123 throw new Exception("### PITCH " + token + " NOT FOUND"); 124 } 125 126 private static void display(Pitch[] pitches) { 127 for ( int i = 0; i < pitches.length; i = i + 1 ) { 128 129 System.out.println(pitches[i].toString()); 130 } 131 132 } 133 134 private static void playMelody(String input, Pitch[] pitches) throws Exception { 135 Scanner scanner = new Scanner(input); 136 while ( scanner.hasNext() ) { 137 138 String token = scanner.next(); 139 Pitch pitch = find(token,pitches); 140 pitch.play("1"); 141 142 } 143 144 } 145 146 // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING 147 148 static private void showErrorMessage(String message) { 149 miro.setVisible(false); 150 JOptionPane.showMessageDialog(null, message); 151 152 } 153 154 private static void initialization() { 155 // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH 156 miro = new SPainter("Chromesthesia",500,500); 157 miro.setVisible(false); 158 miro.setBrushWidth(7); 159 // ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS 160 pitches = establishPitches(miro); 161 display(pitches); 162 163 } 164 165 private static String getInput() { 166 miro.setVisible(false); 167 String label = "Please enter a melody in ABC notation, or EXIT ... "; 168 String input = JOptionPane.showInputDialog(null,label); 169 miro.setVisible(true); 170 if ( input == null ) { input = ""; } 171 return input; 172 173 } 174 175 static private void cleanup() { 176 System.exit(0); 177 178 } 179 180 }