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