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 the examples: 22 * C, D, E, C D E c d e 23 * 24 * 25 */ 26 package chromesthesia0; 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 61 initialization(); // miro and pitches 62 63 while ( true ) { 64 String input = getInput(); 65 if ( input.equalsIgnoreCase("EXIT") ) { 66 break; 67 } else { 68 try { 69 playMelody(input,pitches); 70 } catch (Exception ex) { 71 showErrorMessage(ex.toString()); 72 } 73 } 74 } 75 76 cleanup(); // miro has to go 77 } 78 79 // METHODS PERTAINING TO THE CHROMESTHETIC PITCHES 80 81 private static Pitch[] establishPitches(SPainter painter) { 82 Pitch[] pitches = new Pitch[9]; 83 Pitch pitchMiddleC = new Pitch("C",painter); 84 pitches[0] = pitchMiddleC; 85 86 Pitch pitchLowC = new Pitch("C,",painter); 87 pitches[1] = pitchLowC; 88 89 Pitch pitchHighC = new Pitch("c",painter); 90 pitches[2] = pitchHighC; 91 92 Pitch pitchMiddleD = new Pitch("D",painter); 93 pitches[3] = pitchMiddleD; 94 95 Pitch pitchLowD = new Pitch("D,",painter); 96 pitches[4] = pitchLowD; 97 98 Pitch pitchHighD = new Pitch("d",painter); 99 pitches[5] = pitchHighD; 100 101 Pitch pitchMiddleE = new Pitch("E",painter); 102 pitches[6] = pitchMiddleE; 103 104 Pitch pitchLowE = new Pitch("E,",painter); 105 pitches[7] = pitchLowE; 106 107 Pitch pitchHighE = new Pitch("e",painter); 108 pitches[8] = pitchHighE; 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 Pitch pitch = pitches[i]; 115 if ( pitch.abcName().equals(token)) { 116 return pitch; 117 } 118 } 119 throw new Exception("### PITCH " + token + "NOT FOUND"); 120 } 121 122 private static void display(Pitch[] pitches) { 123 for (int i = 0; i < pitches.length; i = i + 1) { 124 System.out.println(pitches[i].toString()); 125 } 126 } 127 128 private static void playMelody(String input, Pitch[] pitches) throws Exception { 129 Scanner scanner = new Scanner(input); 130 while (scanner.hasNext()) { 131 String token = scanner.next(); 132 Pitch pitch = find(token,pitches); 133 pitch.play("1"); 134 } 135 } 136 137 // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING 138 139 static private void showErrorMessage(String message) { 140 miro.setVisible(false); 141 JOptionPane.showMessageDialog(null, message); 142 } 143 144 private static void initialization() { 145 // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH 146 miro = new SPainter("Chromesthesia",500,500); 147 miro.setVisible(false); 148 miro.setBrushWidth(7); 149 // ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS 150 pitches = establishPitches(miro); 151 display(pitches); 152 } 153 154 private static String getInput() { 155 miro.setVisible(false); 156 String label = "Please enter a melody in ABC notation, or EXIT ... "; 157 String input = JOptionPane.showInputDialog(null, label); 158 miro.setVisible(true); 159 if (input == null) { input = ""; } 160 return input; 161 } 162 163 static private void cleanup() { 164 System.exit(0); 165 } 166 } 167