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 chromesthesia2;
27   
28   import java.util.Scanner;
29   import javax.swing.JOptionPane;
30   import javax.swing.SwingUtilities;
31   
32   import chromesthesia1.Pitch;
33   import painter.SPainter;
34   
35   public class Chromesthesia {
36   
37       // INFRASTRUCTURE FOR THE PROGRAM -- LAUNCHING A "GRAPHICS" THREAD
38   
39       public static void main(String[] args) {
40           SwingUtilities.invokeLater(new chromesthesia2.Chromesthesia.ThreadForGUI());
41       }
42   
43       private static class ThreadForGUI implements Runnable {
44           @Override
45           public void run() {
46               new chromesthesia2.Chromesthesia();
47           }
48       }
49   
50       public Chromesthesia() {
51           interpreter();
52       }
53   
54       // FEATURED VARIABLES
55   
56       private static SPainter miro;
57       private static Pitch[] pitches;
58   
59       // THE INTERPRETER
60   
61       public static void interpreter() {
62   
63           initialization(); // miro and pitches
64   
65           while ( true ) {
66               String input = getInput();
67               if ( input.equalsIgnoreCase("EXIT") ) {
68                   break;
69               } else if (input.equalsIgnoreCase("AGAIN")) {
70                   try {
71                       playMelody(input,pitches);
72                   } catch (Exception ex) {
73                       showErrorMessage(ex.toString());
74                   }
75   
76               }
77               else {
78                   try {
79                       playMelody(input,pitches);
80                   } catch (Exception ex) {
81                       showErrorMessage(ex.toString());
82                   }
83               }
84           }
85   
86           cleanup(); // miro has to go
87       }
88   
89       // METHODS PERTAINING TO THE CHROMESTHETIC PITCHES
90   
91       private static Pitch[] establishPitches(SPainter painter) {
92           Pitch[] pitches = new Pitch[21];
93           Pitch pitchMiddleC = new Pitch("C",painter);
94           pitches[0] = pitchMiddleC;
95   
96           Pitch pitchLowC = new Pitch("C,",painter);
97           pitches[1] = pitchLowC;
98   
99           Pitch pitchHighC = new Pitch("c",painter);
100          pitches[2] = pitchHighC;
101  
102          Pitch pitchMiddleD = new Pitch("D",painter);
103          pitches[3] = pitchMiddleD;
104  
105          Pitch pitchLowD = new Pitch("D,",painter);
106          pitches[4] = pitchLowD;
107  
108          Pitch pitchHighD = new Pitch("d",painter);
109          pitches[5] = pitchHighD;
110  
111          Pitch pitchMiddleE = new Pitch("E",painter);
112          pitches[6] = pitchMiddleE;
113  
114          Pitch pitchLowE = new Pitch("E,",painter);
115          pitches[7] = pitchLowE;
116  
117          Pitch pitchHighE = new Pitch("e",painter);
118          pitches[8] = pitchHighE;
119  
120          Pitch pitchLowF = new Pitch("F,",painter);
121          pitches[9] = pitchLowF;
122  
123          Pitch pitchMiddleF = new Pitch("F",painter);
124          pitches[10] = pitchMiddleF;
125  
126          Pitch pitchHighF = new Pitch("f",painter);
127          pitches[11] = pitchHighF;
128  
129          Pitch pitchLowG = new Pitch("G,",painter);
130          pitches[12] = pitchLowG;
131  
132          Pitch pitchMiddleG = new Pitch("G",painter);
133          pitches[13] = pitchMiddleG;
134  
135          Pitch pitchHighG = new Pitch("g",painter);
136          pitches[14] = pitchHighG;
137  
138          Pitch pitchLowA = new Pitch("A,",painter);
139          pitches[15] = pitchLowA;
140  
141          Pitch pitchMiddleA = new Pitch("A",painter);
142          pitches[16] = pitchMiddleA;
143  
144          Pitch pitchHighA = new Pitch("a",painter);
145          pitches[17] = pitchHighA;
146  
147          Pitch pitchLowB = new Pitch("B,",painter);
148          pitches[18] = pitchLowB;
149  
150          Pitch pitchMiddleB = new Pitch("B",painter);
151          pitches[19] = pitchMiddleB;
152  
153          Pitch pitchHighB = new Pitch("b",painter);
154          pitches[20] = pitchHighB;
155          return pitches;
156      }
157  
158      private static Pitch find(String token, Pitch[] pitches) throws Exception {
159          for ( int i = 0; i < pitches.length; i = i + 1) {
160              Pitch pitch = pitches[i];
161              if ( pitch.abcName().equals(token)) {
162                  return pitch;
163              }
164          }
165          throw new Exception("### PITCH " + token + "NOT FOUND");
166      }
167  
168      private static void display(Pitch[] pitches) {
169          for (int i = 0; i < pitches.length; i = i + 1) {
170              System.out.println(pitches[i].toString());
171          }
172      }
173  
174      private static void playMelody(String input, Pitch[] pitches) throws Exception {
175          Scanner scanner = new Scanner(input);
176          while (scanner.hasNext()) {
177              String token = scanner.next();
178              String pitchName;
179              String duration = "";
180              if (token.indexOf(",") < 0) {
181                  pitchName = token.substring(0,1);
182                  duration = token.substring(1);
183              } else {
184                  pitchName = token.substring(0,2);
185                  duration = token.substring(2);
186              }
187              if (duration.length() == 0) { duration = "1"; }
188              Pitch pitch = find(pitchName,pitches);
189              pitch.play(duration);
190          }
191      }
192  
193      // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
194  
195      static private void showErrorMessage(String message) {
196          miro.setVisible(false);
197          JOptionPane.showMessageDialog(null, message);
198      }
199  
200      private static void initialization() {
201          // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
202          miro = new SPainter("Chromesthesia",500,500);
203          miro.setVisible(false);
204          miro.setBrushWidth(7);
205          // ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS
206          pitches = establishPitches(miro);
207          display(pitches);
208      }
209  
210      private static String getInput() {
211          miro.setVisible(false);
212          String label = "Please enter a melody in ABC notation, or EXIT or AGAIN ...      ";
213          String input = JOptionPane.showInputDialog(null, label);
214          miro.setVisible(true);
215          if (input == null) { input = ""; }
216          return input;
217      }
218  
219      static private void cleanup() {
220          System.exit(0);
221      }
222  }
223