1 /* 2 * This program interprets melodic lines given in ABC notation 3 * as a chromesthete might. 4 * 5 * A Pitch class will be defined, and will take center stage in 6 * 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 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 th 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 19 * screen 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 25 package chromesthesia0; 26 27 import java.util.Scanner; 28 import javax.swing.JOptionPane; 29 import javax.swing.SwingUtilities; 30 import painter.SPainter; 31 32 public class Chromesthesia { 33 34 //INFRASTRUCTURE FOR THE PROGRAM -- LAUNCHING A "GRAPHICS" THREAD 35 36 public static void main (String[] args) { 37 SwingUtilities.invokeLater(new ThreadForGUI()); 38 } 39 40 private static class ThreadForGUI implements Runnable { 41 @Override 42 public void run () { 43 new Chromesthesia(); 44 } 45 } 46 47 public Chromesthesia() { 48 interpreter(); 49 } 50 51 //FEATURED VARIABLS 52 53 private static SPainter miro; 54 private static Pitch[] pitches; 55 56 //THE INTERPRETER 57 58 public static void interpreter() { 59 60 initialization(); //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 CHOMESTHETIC PITCHES 79 private static Pitch[] establishPitches(SPainter painter) { 80 Pitch[] pitches = new Pitch[9]; 81 82 Pitch pitchMiddleC = new Pitch("C", painter); 83 pitches[0] = pitchMiddleC; 84 85 Pitch pitchLowC = new Pitch("C,", painter); 86 pitches[1] = pitchLowC; 87 88 Pitch pitchHighC = new Pitch("c", painter); 89 pitches[2] = pitchHighC; 90 91 Pitch pitchMiddleD = new Pitch("D", painter); 92 pitches[3] = pitchMiddleD; 93 94 Pitch pitchLowD = new Pitch("D,", painter); 95 pitches[4] = pitchLowD; 96 97 Pitch pitchHighD = new Pitch("d", painter); 98 pitches[5] = pitchHighD; 99 100 Pitch pitchMiddleE = new Pitch("E", painter); 101 pitches[6] = pitchMiddleE; 102 103 Pitch pitchLowE = new Pitch("E,", painter); 104 pitches[7] = pitchLowE; 105 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 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 static private void showErrorMessage(String message) { 139 miro.setVisible(false); 140 JOptionPane.showMessageDialog(null, message); 141 } 142 143 private static void initialization() { 144 //ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH 145 miro = new SPainter ("Chromesthesia", 500, 500); 146 miro.setVisible(false); 147 miro.setBrushWidth(7); 148 //ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS 149 pitches = establishPitches(miro); 150 display(pitches); 151 } 152 153 private static String getInput() { 154 miro.setVisible(false); 155 String label = "Please enter a melody in ABC notation, or EXIT ... "; 156 String input = JOptionPane.showInputDialog(null, label); 157 miro.setVisible(true); 158 if (input == null) {input = "";} 159 return input; 160 } 161 162 static private void cleanup() { 163 System.exit(0); 164 } 165 166 }