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("please enter input"); 48 } 49 } else if (input.equalsIgnoreCase("EXIT")) { 50 break; 51 } else { 52 earlierMelody = input; 53 54 55 try { 56 playMelody(input, pitches); 57 } catch (Exception ex) { 58 showErrorMessage(ex.toString()); 59 } 60 } 61 } 62 cleanup(); //miro has to go 63 } 64 65 66 //Methods pertaining to the chromesthetic pitches 67 68 private static Pitch [] establishPitches(SPainter painter){ 69 Pitch[]pitches= new Pitch[21]; 70 Pitch pitchMiddleC= new Pitch("C", painter); 71 pitches[0]=pitchMiddleC; 72 Pitch pitchLowC= new Pitch("C,", painter); 73 pitches[1]=pitchLowC; 74 Pitch pitchHighC= new Pitch("c", painter); 75 pitches[2]=pitchHighC; 76 Pitch pitchMiddleD= new Pitch("D", painter); 77 pitches[3]=pitchMiddleD; 78 Pitch pitchLowD= new Pitch("D,", painter); 79 pitches[4]=pitchLowD; 80 Pitch pitchHighD= new Pitch("d", painter); 81 pitches[5]=pitchHighD; 82 Pitch pitchMiddleE= new Pitch("E", painter); 83 pitches[6]=pitchMiddleE; 84 Pitch pitchLowE= new Pitch("E,", painter); 85 pitches[7]=pitchLowE; 86 Pitch pitchHighE= new Pitch("e", painter); 87 pitches[8] = pitchHighE; 88 Pitch pitchMiddleF= new Pitch("F", painter); 89 pitches[9]=pitchMiddleF; 90 Pitch pitchLowF= new Pitch("F,", painter); 91 pitches[10]=pitchLowF; 92 Pitch pitchHighF= new Pitch("f", painter); 93 pitches[11] = pitchHighF; 94 Pitch pitchMiddleG= new Pitch("G", painter); 95 pitches[12]=pitchMiddleG; 96 Pitch pitchLowG= new Pitch("G,", painter); 97 pitches[13]=pitchLowG; 98 Pitch pitchHighG= new Pitch("g", painter); 99 pitches[14] = pitchHighG; 100 Pitch pitchMiddleA= new Pitch("A", painter); 101 pitches[15]=pitchMiddleA; 102 Pitch pitchLowA= new Pitch("A,", painter); 103 pitches[16]=pitchLowA; 104 Pitch pitchHighA= new Pitch("a", painter); 105 pitches[17] = pitchHighA; 106 Pitch pitchMiddleB= new Pitch("B", painter); 107 pitches[18]=pitchMiddleB; 108 Pitch pitchLowB= new Pitch("B,", painter); 109 pitches[19]=pitchLowB; 110 Pitch pitchHighB= new Pitch("b", painter); 111 pitches[20] = pitchHighB; 112 113 return pitches; 114 } 115 private static Pitch find(String token, Pitch[]pitches)throws Exception{ 116 for (int i=0; i<pitches.length; i=i+1){ 117 Pitch pitch= pitches[i]; 118 if (pitch.abcName().equals(token)){ 119 return pitch; 120 } 121 } 122 throw new Exception("### PITCH" + token+ "NOT FOUND"); 123 } 124 125 126 private static void playMelody(String input, Pitch[] pitches) throws Exception{ 127 Scanner scanner= new Scanner(input); 128 while (scanner.hasNext()){ 129 String token= scanner.next(); 130 String pitchName; 131 String duration= " "; 132 if (token.indexOf(",")<0){ 133 pitchName=token.substring(0,1); 134 duration=token.substring(1); 135 }else { 136 pitchName=token.substring(0,2); 137 duration=token.substring(2); 138 } 139 if (duration.length()==0) 140 {duration= "1";} 141 Pitch pitch= find(pitchName, pitches); 142 pitch.play(duration); 143 144 145 } 146 } 147 private static void display(Pitch[] pitches) { 148 for (int i = 0; i < pitches.length; i = i + 1) { 149 System.out.println(pitches[i].toString()); 150 } 151 } 152 //INITIALIZATION, CLEAN UP, GETTING INPUT, ERROR MESSAGING 153 static private void showErrorMessage(String message){ 154 miro.setVisible(false); 155 JOptionPane.showMessageDialog(null, message); 156 } 157 private static void intialization() { 158 //ESTABLISH A PAINTER AND GIVE A SUBSTANTIAL BRUSH WIDTH 159 miro= new SPainter("Chromesthesia", 500, 500); 160 miro.setBrushWidth(7); 161 //ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS 162 pitches= establishPitches(miro); 163 display(pitches); 164 } 165 private static String getInput() { 166 miro.setVisible(false); 167 String label= "Please enter a melody in ABC notation, or AGAIN to replay it or EXIT.. " ; 168 String input= JOptionPane.showInputDialog(null, label); 169 170 miro.setVisible(true); 171 if (input==null){input=""; } 172 return input; 173 } 174 private static void cleanup() { 175 System.exit(0); 176 } 177 178 }