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