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