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 duration, while sounding 10 * the pitch! The color of the rectangle will correspond to 11 * the pitch class. The duration will correspond to the duration 12 * of the note. 13 * 14 * For this first version of the program, the duration will be held 15 * constant at 1 beat. 16 * 17 * Three sorts of images will appear on the screen, 18 * the chromesthetic output box, a text input box, 19 * and an error message box. Simplicity of design 20 * is rendered by permitting only one box to be on the screen 21 * at at a time. 22 * 23 * ABC Represents notes in a manner consistent with these examples: 24 * C D E C D E ,cde 25 * 26 * google ABC Music representation for more information 27 */ 28 29 package chromesthesia2; 30 31 import java.util.Scanner; 32 import javax.swing.JOptionPane; 33 import javax.swing.SwingUtilities; 34 import painter.SPainter; 35 36 public class Chromesthesia { 37 //INFRASTRUCTURE FOR THE PRORGRAM -- LAUNCHING A "GRAPHICS" THREAD 38 public static void main(String[] args) { 39 SwingUtilities.invokeLater(new ThreadForGUI()); 40 } 41 private static class ThreadForGUI implements Runnable { 42 @Override 43 public void run(){ 44 new Chromesthesia(); 45 } 46 } 47 public Chromesthesia(){ 48 interpreter(); 49 } 50 51 //FEATURED VARIABLES 52 private static SPainter miro; 53 private static Pitch[] pitches; 54 55 //THE INTERPRETER 56 public static void interpreter() { 57 initialization(); //miro and pitches 58 String input2 = ""; 59 while (true){ 60 String input = getInput(); 61 if (input.equalsIgnoreCase("EXIT")) { 62 break; 63 }else if (input.equalsIgnoreCase("AGAIN")){ 64 65 try{playMelody(input2,pitches); 66 }catch(Exception ex) { 67 showErrorMessage(ex.toString()); 68 } 69 } 70 else 71 try { 72 playMelody(input, pitches); 73 input2 = input; 74 }catch(Exception ex) { 75 showErrorMessage(ex.toString()); 76 } 77 78 } 79 80 cleanup(); //miro has to go 81 } 82 //METHODS PERTAINING TO THE CHROMESTHETIC PITCHES 83 private static Pitch[] establishPitches(SPainter painter) { 84 Pitch[] pitches = new Pitch[21]; 85 Pitch pitchMiddleC = new Pitch("C",painter); 86 pitches[0] = pitchMiddleC; 87 Pitch pitchLowC = new Pitch("C,",painter); 88 pitches[1] = pitchLowC; 89 Pitch pitchHighC = new Pitch("c", painter); 90 pitches[2] = pitchHighC; 91 Pitch pitchMiddleD = new Pitch("D",painter); 92 pitches[3] = pitchMiddleD; 93 Pitch pitchLowD = new Pitch("D,",painter); 94 pitches[4]=pitchLowD; 95 Pitch pitchHighD = new Pitch("d", painter); 96 pitches[5] = pitchHighD; 97 Pitch pitchMiddleE = new Pitch("E", painter); 98 pitches[6]=pitchMiddleE; 99 Pitch pitchLowE = new Pitch("E,", painter); 100 pitches[7]=pitchLowE; 101 Pitch pitchHighE = new Pitch("e",painter); 102 pitches[8]=pitchHighE; 103 Pitch pitchMiddleF = new Pitch("F", painter); 104 pitches[9] = pitchMiddleF; 105 Pitch pitchLowF = new Pitch("F,", painter); 106 pitches[10] = pitchLowF; 107 Pitch pitchHighF = new Pitch("f", painter); 108 pitches[11] = pitchHighF; 109 Pitch pitchMiddleG = new Pitch("G",painter); 110 pitches[12] = pitchMiddleG; 111 Pitch pitchLowG = new Pitch("G,", painter); 112 pitches[13] = pitchLowG; 113 Pitch pitchHighG = new Pitch("g", painter); 114 pitches[14] = pitchHighG; 115 Pitch pitchMiddleA = new Pitch("A", painter); 116 pitches[15] = pitchMiddleA; 117 Pitch pitchLowA = new Pitch("A,", painter); 118 pitches[16]=pitchLowA; 119 Pitch pitchHighA = new Pitch("a",painter); 120 pitches[17]=pitchHighA; 121 Pitch pitchMiddleB = new Pitch("B", painter); 122 pitches[18] = pitchMiddleB; 123 Pitch pitchLowB = new Pitch("B,",painter); 124 pitches[19] = pitchLowB; 125 Pitch pitchHighB = new Pitch("b", painter); 126 pitches[20] = pitchHighB; 127 128 return pitches; 129 } 130 private static Pitch find(String token, Pitch[] pitches) throws Exception { 131 for (int i = 0; i < pitches.length; i = i + 1) { 132 Pitch pitch = pitches[i]; 133 if (pitch.abcName().equals(token)) { 134 return pitch; 135 } 136 } 137 throw new Exception("### PITCH " + token + "NOT FOUND"); 138 } 139 private static void display(Pitch[] pitches){ 140 for (int i = 0; i < pitches.length; i = i + 1){ 141 System.out.println(pitches[i].toString()); 142 } 143 144 } 145 private static void playMelody(String input, Pitch[] pitches) throws Exception{ 146 Scanner scanner = new Scanner(input); 147 while (scanner.hasNext()) { 148 String token = scanner.next(); 149 String pitchName; 150 String duration = ""; 151 if (token.indexOf(",") < 0) { 152 pitchName = token.substring(0, 1); 153 duration = token.substring(1); 154 } else { 155 pitchName = token.substring(0, 2); 156 duration = token.substring(2); 157 } 158 if( duration.length() == 0) {duration = "1";} 159 Pitch pitch = find(pitchName,pitches); 160 pitch.play(duration); 161 } 162 } 163 //INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING 164 static private void showErrorMessage(String message){ 165 miro.setVisible(false); 166 JOptionPane.showMessageDialog(null,message); 167 } 168 private static void initialization(){ 169 //ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH 170 miro = new SPainter("Chromesthesia",500,500); 171 miro.setVisible(false); 172 miro.setBrushWidth(7); 173 //ESTABLISH THE CHROMESTITC PITCH CLASS OBJECTS 174 pitches = establishPitches(miro); 175 display(pitches); 176 } 177 private static String getInput() { 178 miro.setVisible(false); 179 String label = "please enter a melody in ABC notation, or EXIT, or AGAIN! ... "; 180 String input = JOptionPane.showInputDialog(null,label); 181 miro.setVisible(true); 182 if(input == null){input = "";} 183 return input; 184 } 185 static private void cleanup() { 186 System.exit(0); 187 } 188 }