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