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