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