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