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