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