Chromesthesia.java
1    package chromesthesia2;
2    
3    import painter.SPainter;
4    
5    import javax.swing.*;
6    import java.util.Scanner;
7    
8    public class Chromesthesia {
9    
10       //INFRASTRUCTURE FOR THE PROGRAM -- LAUNCHING A "GRAPHICS" THREAD
11   
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 Chromesthesia();
20           }
21       }
22   
23       public Chromesthesia() {
24           interpreter();
25       }
26   
27       //FEATURED VARIABLES
28       private static SPainter miro;
29       private static Pitch[] pitches;
30   
31       //THE INTERPRETER
32   
33       public static void interpreter(){
34   
35           initialization(); //miro and pitches
36           String previous = "";
37   
38           while (true) {
39               String input = getInput();
40               if(input.equalsIgnoreCase("EXIT")) {
41                   break;
42               } else if (input.equalsIgnoreCase("AGAIN")) {
43                   try{
44                       playMelody(previous, pitches);
45                   }
46                   catch (Exception ex) {
47                       ex.printStackTrace();
48                   }
49               }
50               else {
51                   try {
52                       playMelody(input, pitches);
53                   } catch (Exception ex) {
54                       showErrorMessage(ex.toString());
55                   }
56   
57                   if (!previous.equalsIgnoreCase("AGAIN")) {
58                       previous = input;
59                   }
60               }
61           }
62           cleanup(); //miro has to go
63       }
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          return pitches;
114      }
115  
116      private static Pitch find(String token, Pitch[] pitches) throws Exception {
117          for (int i = 0; i < pitches.length; i++) {
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      private static void display(Pitch[] pitches) {
127          for (int i = 0; i < pitches.length; i++){
128              System.out.println(pitches[i].toString());
129          }
130      }
131  
132      private static void playMelody(String input, Pitch[] pitches) throws Exception {
133          Scanner scanner = new Scanner(input);
134          while (scanner.hasNext()) {
135              String token = scanner.next();
136              String pitchName;
137              String duration;
138              if (token.indexOf(",") < 0) {
139                  pitchName = token.substring(0,1);
140                  duration = token.substring(1);
141              } else {
142                  pitchName = token.substring(0,2);
143                  duration = token.substring(2);
144              }
145              if (duration.length() == 0) {
146                  duration = "1";
147              }
148              Pitch pitch = find(pitchName, pitches);
149              pitch.play(duration);
150          }
151      }
152  
153  
154      //INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
155  
156      static private void showErrorMessage(String message) {
157          miro.setVisible(false);
158          JOptionPane.showInputDialog(null, message);
159      }
160  
161      static private void initialization() {
162          //ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
163          miro = new SPainter("Chromesthesia", 500,500);
164          miro.setVisible(false);
165          miro.setBrushWidth(7);
166  
167          //ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS
168          pitches = establishPitches(miro);
169          display(pitches);
170      }
171  
172      private static String getInput() {
173          miro.setVisible(false);
174          String label = "Please enter a melody in ABC notation, or EXIT...";
175          String input = JOptionPane.showInputDialog(null,label);
176          miro.setVisible(true);
177          if (input == null) {input = "";}
178          return input;
179      }
180  
181      static private void cleanup() {
182          System.exit(0);
183      }
184  }
185  
186