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