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 chromesthesia0; 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 //FEATURED VARIABLES 51 52 private static SPainter miro; 53 private static Pitch[] pitches; 54 //THE INTERPRETER 55 public static void interpreter() { 56 initialization(); //miro and pitches 57 while (true){ 58 String input = getInput(); 59 if (input.equalsIgnoreCase("EXIT")) { 60 break; 61 }else{ 62 try { 63 playMelody(input, pitches); 64 }catch(Exception ex) { 65 showErrorMessage(ex.toString()); 66 } 67 } 68 } 69 cleanup(); //miro has to go 70 } 71 //METHODS PERTAINING TO THE CHROMESTHETIC PITCHES 72 private static Pitch[] establishPitches(SPainter painter) { 73 Pitch[] pitches = new Pitch[9]; 74 Pitch pitchMiddleC = new Pitch("C",painter); 75 pitches[0] = pitchMiddleC; 76 Pitch pitchLowC = new Pitch("C,",painter); 77 pitches[1] = pitchLowC; 78 Pitch pitchHighC = new Pitch("c", painter); 79 pitches[2] = pitchHighC; 80 Pitch pitchMiddleD = new Pitch("D",painter); 81 pitches[3] = pitchMiddleD; 82 Pitch pitchLowD = new Pitch("D,",painter); 83 pitches[4]=pitchLowD; 84 Pitch pitchHighD = new Pitch("d", painter); 85 pitches[5] = pitchHighD; 86 Pitch pitchMiddleE = new Pitch("E", painter); 87 pitches[6]=pitchMiddleE; 88 Pitch pitchLowE = new Pitch("E,", painter); 89 pitches[7]=pitchLowE; 90 Pitch pitchHighE = new Pitch("e",painter); 91 pitches[8]=pitchHighE; 92 return pitches; 93 } 94 private static Pitch find(String token, Pitch[] pitches) throws Exception { 95 for (int i = 0; i < pitches.length; i = i + 1) { 96 Pitch pitch = pitches[i]; 97 if (pitch.abcName().equals(token)) { 98 return pitch; 99 } 100 } 101 throw new Exception("### PITCH " + token + "NOT FOUND"); 102 } 103 private static void display(Pitch[] pitches){ 104 for (int i = 0; i < pitches.length; i = i + 1){ 105 System.out.println(pitches[i].toString()); 106 } 107 108 } 109 private static void playMelody(String input, Pitch[] pitches) throws Exception{ 110 Scanner scanner = new Scanner(input); 111 while (scanner.hasNext()){ 112 String token = scanner.next(); 113 Pitch pitch = find(token,pitches); 114 pitch.play("1"); 115 } 116 } 117 //INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING 118 static private void showErrorMessage(String message){ 119 miro.setVisible(false); 120 JOptionPane.showMessageDialog(null,message); 121 } 122 private static void initialization(){ 123 //ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH 124 miro = new SPainter("Chromesthesia",500,500); 125 miro.setVisible(false); 126 miro.setBrushWidth(7); 127 //ESTABLISH THE CHROMESTITC PITCH CLASS OBJECTS 128 pitches = establishPitches(miro); 129 display(pitches); 130 } 131 private static String getInput() { 132 miro.setVisible(false); 133 String label = "please enter a melody in ABC notation, or EXIT ... "; 134 String input = JOptionPane.showInputDialog(null,label); 135 miro.setVisible(true); 136 if(input == null){input = "";} 137 return input; 138 } 139 static private void cleanup() { 140 System.exit(0); 141 } 142 }