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