Chromesthesia.java
1    /* 
2     * This program interprets melodic lines given in ABC notation as a chromesthete might. 
3     * 
4     * A Pitch class will be defined, and will take center stage in the processing. 
5     * 
6     * interpreting a melody in ABC notation will amount to flashing colored rectangles for 
7     * prescribed durations, while sounding the pitch! The color of the rectangle will correspond 
8     * to pitch class The duration will correspond to the duration of the note. 
9     * 
10    * For this second version of the program, the duration will be held constant at 1 beat. 
11    * 
12    * Three sorts of images will appear on the screen, the chromesthetic output box, a text input 
13    * box, and an error message box. Simplicity of design is rendered by permitting only one box 
14    * to be on the screen at a time. 
15    * 
16    * ABC represents notes in a manner consistent with these examples: 
17    * C, D, E, C D E c d e 
18    * 
19    * Google ABC music representation if you would like to know more about it. 
20    */
21   
22   package chromesthesia1;
23   
24   import painter.SPainter;
25   import javax.swing.JOptionPane;
26   import javax.swing.SwingUtilities;
27   import java.util.Scanner;
28   
29   public class Chromesthesia {
30   
31       //INFRASTRUCTURE FOR THE PROGRAM - LAUNCHING A "GRAPHICS" THREAD
32       public static void main(String[] args) {
33           SwingUtilities.invokeLater(new chromesthesia1.Chromesthesia.ThreadForGUI());
34       }
35   
36       private static class ThreadForGUI implements Runnable {
37           @Override
38           public void run() {
39               new chromesthesia1.Chromesthesia();
40           }
41       }
42   
43       public Chromesthesia() {
44           interpreter();
45       }
46   
47       //FEATURED VARIABLES
48       private static SPainter miro;
49       private static Pitch[] pitches;
50   
51       //THE INTERPRETER
52       public static void interpreter() {
53           initialization(); //miro and pitches
54   
55           while (true) {
56               String input = getInput();
57               if (input.equalsIgnoreCase("EXIT")) {
58                   break;
59               } else {
60                   try {
61                       playMelody(input, pitches);
62                   } catch (Exception ex) {
63                       showErrorMessage(ex.toString());
64                   }
65               }
66           }
67           cleanup(); //miro has to go
68       }
69   
70       //METHODS PERTAINING TO THE CHROMESTHETIC PITCHES
71       private static Pitch[] establishPitches(SPainter painter) {
72           Pitch[] pitches = new Pitch[21];
73   
74           Pitch pitchMiddleC = new Pitch("C", painter);
75           pitches[0] = pitchMiddleC;
76   
77           Pitch pitchLowC = new Pitch("C,", painter);
78           pitches[1] = pitchLowC;
79   
80           Pitch pitchHighC = new Pitch("c", painter);
81           pitches[2] = pitchHighC;
82   
83           Pitch pitchMiddleD = new Pitch("D", painter);
84           pitches[3] = pitchMiddleD;
85   
86           Pitch pitchLowD = new Pitch("D,", painter);
87           pitches[4] = pitchLowD;
88   
89           Pitch pitchHighD = new Pitch("d", painter);
90           pitches[5] = pitchHighD;
91   
92           Pitch pitchMiddleE = new Pitch("E", painter);
93           pitches[6] = pitchMiddleE;
94   
95           Pitch pitchLowE = new Pitch("E,", painter);
96           pitches[7] = pitchLowE;
97   
98           Pitch pitchHighE = new Pitch("e", painter);
99           pitches[8] = pitchHighE;
100  
101          Pitch pitchMiddleF = new Pitch("F", painter);
102          pitches[9] = pitchMiddleF;
103  
104          Pitch pitchLowF = new Pitch("F,", painter);
105          pitches[10] = pitchLowF;
106  
107          Pitch pitchHighF = new Pitch("f", painter);
108          pitches[11] = pitchHighF;
109  
110          Pitch pitchMiddleG = new Pitch("G", painter);
111          pitches[12] = pitchMiddleG;
112  
113          Pitch pitchLowG = new Pitch("G,", painter);
114          pitches[13] = pitchLowG;
115  
116          Pitch pitchHighG = new Pitch("g", painter);
117          pitches[14] = pitchHighG;
118  
119          Pitch pitchMiddleA = new Pitch("A", painter);
120          pitches[15] = pitchMiddleA;
121  
122          Pitch pitchLowA = new Pitch("A,", painter);
123          pitches[16] = pitchLowA;
124  
125          Pitch pitchHighA = new Pitch("a", painter);
126          pitches[17] = pitchHighA;
127  
128          Pitch pitchMiddleB = new Pitch("B", painter);
129          pitches[18] = pitchMiddleB;
130  
131          Pitch pitchLowB = new Pitch("B,", painter);
132          pitches[19] = pitchLowB;
133  
134          Pitch pitchHighB = new Pitch("b", painter);
135          pitches[20] = pitchHighB;
136  
137          return pitches;
138      }
139  
140      private static Pitch find(String token, Pitch[] pitches) throws Exception{
141          for (int i = 0; i < pitches.length; i++) {
142              Pitch pitch = pitches[i];
143              if (pitch.abcName().equals(token)) {
144                  return pitch;
145              }
146          }
147          throw new Exception("### PITCH " + token + " NOT FOUND");
148      }
149  
150      private static void display(Pitch[] pitches) {
151          for (int i = 0; i < pitches.length; i++) {
152              System.out.println(pitches[i].toString());
153          }
154      }
155  
156      private static void playMelody(String input, Pitch[] pitches) throws Exception {
157          Scanner scanner = new Scanner(input);
158          while (scanner.hasNext()) {
159              String token = scanner.next();
160              Pitch pitch = find(token, pitches);
161              pitch.play("1");
162          }
163      }
164  
165      //INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
166      static private void showErrorMessage(String message) {
167          miro.setVisible(false);
168          JOptionPane.showMessageDialog(null, message);
169      }
170  
171      static private void initialization() {
172          //ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
173          miro = new SPainter("Chromesthesia1", 500, 500);
174          miro.setVisible(false);
175          miro.setBrushWidth(7);
176          //ESTABLISH THE CHROMESTHETIC PITCH CLASS OBJECTS
177          pitches= establishPitches(miro);
178          display(pitches);
179      }
180  
181      private static String getInput() {
182          miro.setVisible(false);
183          String label = "Please enter a melody in ABC notation, or EXIT ...";
184          String input = JOptionPane.showInputDialog(null, label);
185          miro.setVisible(true);
186          if (input == null) { input = ""; }
187          return input;
188      }
189  
190      static private void cleanup() {
191          System.exit(0);
192      }
193  
194  }
195