1 /* 2 * this program features colorful effects with sounds, which means each note have it owns color 3 */ 4 package chromesthesia2; 5 6 import painter.SPainter; 7 8 import javax.swing.*; 9 import java.awt.*; 10 import java.util.Scanner; 11 12 public class Chromesthesia { 13 public static void main(String[] args) { 14 SwingUtilities.invokeLater(new ThreadForGUI()); 15 } 16 17 private static class ThreadForGUI implements Runnable { 18 19 @Override 20 public void run() { 21 new Chromesthesia(); 22 } 23 } 24 public Chromesthesia() { 25 interpreter(); 26 } 27 //Featured Variables 28 private static SPainter miro; 29 private static Pitch[] pitches; 30 31 //The Interpreter 32 public static void interpreter() { 33 String earlierMelody = " "; 34 intialization(); //miro and pitches 35 36 while (true) { 37 String input = getInput(); 38 39 if (input.equalsIgnoreCase("AGAIN")) { 40 if (earlierMelody != " ") 41 try { 42 playMelody(earlierMelody, pitches); 43 } catch (Exception ex) { 44 showErrorMessage(ex.toString()); 45 } 46 else { 47 showErrorMessage("input required"); 48 } 49 } 50 else if (input.equalsIgnoreCase("EXIT")) { 51 break; 52 } 53 else { 54 earlierMelody=input; 55 } 56 57 try { 58 playMelody(input, pitches); 59 } catch (Exception ex) { 60 showErrorMessage(ex.toString()); 61 } 62 } 63 cleanup(); //miro has to go 64 } 65 66 67 //Methods pertaining to the chromesthetic pitches 68 69 private static Pitch [] establishPitches(SPainter painter){ 70 Pitch[]pitches= new Pitch[21]; 71 Pitch pitchMiddleC= new Pitch("C", painter); 72 pitches[0]=pitchMiddleC; 73 Pitch pitchLowC= new Pitch("C,", painter); 74 pitches[1]=pitchLowC; 75 Pitch pitchHighC= new Pitch("c", painter); 76 pitches[2]=pitchHighC; 77 Pitch pitchMiddleD= new Pitch("D", painter); 78 pitches[3]=pitchMiddleD; 79 Pitch pitchLowD= new Pitch("D,", painter); 80 pitches[4]=pitchLowD; 81 Pitch pitchHighD= new Pitch("d", painter); 82 pitches[5]=pitchHighD; 83 Pitch pitchMiddleE= new Pitch("E", painter); 84 pitches[6]=pitchMiddleE; 85 Pitch pitchLowE= new Pitch("E,", painter); 86 pitches[7]=pitchLowE; 87 Pitch pitchHighE= new Pitch("e", painter); 88 pitches[8] = pitchHighE; 89 Pitch pitchMiddleF= new Pitch("F", painter); 90 pitches[9]=pitchMiddleF; 91 Pitch pitchLowF= new Pitch("F,", painter); 92 pitches[10]=pitchLowF; 93 Pitch pitchHighF= new Pitch("f", painter); 94 pitches[11] = pitchHighF; 95 Pitch pitchMiddleG= new Pitch("G", painter); 96 pitches[12]=pitchMiddleG; 97 Pitch pitchLowG= new Pitch("G,", painter); 98 pitches[13]=pitchLowG; 99 Pitch pitchHighG= new Pitch("g", painter); 100 pitches[14] = pitchHighG; 101 Pitch pitchMiddleA= new Pitch("A", painter); 102 pitches[15]=pitchMiddleA; 103 Pitch pitchLowA= new Pitch("A,", painter); 104 pitches[16]=pitchLowA; 105 Pitch pitchHighA= new Pitch("a", painter); 106 pitches[17] = pitchHighA; 107 Pitch pitchMiddleB= new Pitch("B", painter); 108 pitches[18]=pitchMiddleB; 109 Pitch pitchLowB= new Pitch("B,", painter); 110 pitches[19]=pitchLowB; 111 Pitch pitchHighB= new Pitch("b", painter); 112 pitches[20] = pitchHighB; 113 114 return pitches; 115 } 116 private static Pitch find(String token, Pitch[]pitches)throws Exception{ 117 for (int i=0; i<pitches.length; i=i+1){ 118 Pitch pitch= pitches[i]; 119 if (pitch.abcName().equals(token)){ 120 return pitch; 121 } 122 } 123 throw new Exception("### PITCH" + token+ "NOT FOUND"); 124 } 125 126 127 private static void playMelody(String input, Pitch[] pitches) throws Exception{ 128 Scanner scanner= new Scanner(input); 129 while (scanner.hasNext()){ 130 String token= scanner.next(); 131 String pitchName; 132 String duration= " "; 133 if (token.indexOf(",")<0){ 134 pitchName=token.substring(0,1); 135 duration=token.substring(1); 136 }else { 137 pitchName=token.substring(0,2); 138 duration=token.substring(2); 139 } 140 if (duration.length()==0) 141 {duration= "1";} 142 Pitch pitch= find(pitchName, pitches); 143 pitch.play(duration); 144 145 146 } 147 } 148 private static void display(Pitch[] pitches) { 149 for (int i = 0; i < pitches.length; i = i + 1) { 150 System.out.println(pitches[i].toString()); 151 } 152 } 153 //INITIALIZATION, CLEAN UP, GETTING INPUT, ERROR MESSAGING 154 static private void showErrorMessage(String message){ 155 miro.setVisible(false); 156 JOptionPane.showMessageDialog(null, message); 157 } 158 private static void intialization() { 159 //ESTABLISH A PAINTER AND GIVE A SUBSTANTIAL BRUSH WIDTH 160 miro= new SPainter("Chromesthesia", 500, 500); 161 miro.setBrushWidth(7); 162 //ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS 163 pitches= establishPitches(miro); 164 display(pitches); 165 } 166 private static String getInput() { 167 miro.setVisible(false); 168 String label= "Please enter a melody in ABC notation, or AGAIN to replay or EXIT.. " ; 169 String input= JOptionPane.showInputDialog(null, label); 170 171 miro.setVisible(true); 172 if (input==null){input=""; } 173 return input; 174 } 175 private static void cleanup() { 176 System.exit(0); 177 } 178 179 } 180