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