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 these examples: 
22    * C, D, E, C D E c d e 
23    * 
24    * Google ABC music representation if you would like to know more about it. 
25    */
26   
27   
28   package chromesthesia2;
29   
30   
31   import chromesthesia2.Pitch;
32   import painter.SPainter;
33   
34   import javax.swing.*;
35   import java.util.Scanner;
36   
37   public class Chromesthesia {
38       //infrastructure for the program -- launching a "Graphics" thread
39   
40       public static void main(String[] args){
41           SwingUtilities.invokeLater(new ThreadForGUI());
42       }
43   
44       private static class ThreadForGUI implements Runnable{
45           @Override
46           public void run(){
47               new Chromesthesia();
48           }
49       }
50   
51       public Chromesthesia(){
52           interpreter();
53       }
54   
55       //featured variables
56   
57       private static SPainter miro;
58       private static Pitch[] pitches;
59   
60       //the interpreter
61   
62       public static void interpreter(){
63           String previousState = " ";
64   
65           initialization(); //miro and pitchers
66   
67           while(true){
68               String input = getInput();
69   
70   
71   
72   
73               if(input.equalsIgnoreCase("again")){
74                   if (previousState != " "){
75                       try{
76                           playMelody(previousState,pitches);
77                       }catch (Exception ex) {
78                           showErrorMessage(ex.toString());
79                       }
80                   } else{
81                       showErrorMessage("There is no previous state");
82                   }
83   
84               }
85               else if(input.equalsIgnoreCase("EXIT")){
86                   break;
87               } else {
88                   previousState = input;
89   
90                   try{
91                       playMelody(input,pitches);
92                   }catch (Exception ex){
93                       showErrorMessage(ex.toString());
94                   }
95               }
96           }
97           cleanup(); //miro has to go
98   
99       }
100      //methods pertaining to the chromesthetic pitches
101  
102      private static Pitch[] establishPitches(SPainter painter){
103          Pitch[] pitches = new Pitch[21];
104          Pitch pitchMiddleC = new Pitch("C", painter);
105          pitches[0] = pitchMiddleC;
106          Pitch pitchLowC = new Pitch("C,",painter);
107          pitches[1] = pitchLowC;
108          Pitch pitchHighC = new Pitch("c",painter);
109          pitches[2] = pitchHighC;
110          Pitch pitchMiddleD = new Pitch("D",painter);
111          pitches[3] = pitchMiddleD;
112          Pitch pitchLowD = new Pitch("D,",painter);
113          pitches[4] = pitchLowD;
114          Pitch pitchHighD = new Pitch("d",painter);
115          pitches[5] = pitchHighD;
116          Pitch pitchMiddleE = new Pitch("E",painter);
117          pitches[6] = pitchMiddleE;
118          Pitch pitchLowE = new Pitch("E,",painter);
119          pitches[7] = pitchLowE;
120          Pitch pitchHighE = new Pitch("e",painter);
121          pitches[8] = pitchHighE;
122          Pitch pitchMiddleF = new Pitch("F",painter);
123          pitches[9] = pitchMiddleF;
124          Pitch pitchLowF = new Pitch("F,",painter);
125          pitches[10] = pitchLowF;
126          Pitch pitchHighF = new Pitch("f",painter);
127          pitches[11] = pitchHighF;
128          Pitch pitchMiddleG = new Pitch("G",painter);
129          pitches[12] = pitchMiddleG;
130          Pitch pitchLowG = new Pitch("G,",painter);
131          pitches[13] = pitchLowG;
132          Pitch pitchHighG = new Pitch("g",painter);
133          pitches[14] = pitchHighG;
134          Pitch pitchMiddleA = new Pitch("A",painter);
135          pitches[15] = pitchMiddleA;
136          Pitch pitchLowA = new Pitch("A,",painter);
137          pitches[16] = pitchLowA;
138          Pitch pitchHighA = new Pitch("a",painter);
139          pitches[17] = pitchHighA;
140          Pitch pitchMiddleB = new Pitch("B",painter);
141          pitches[18] = pitchMiddleB;
142          Pitch pitchLowB = new Pitch("B,",painter);
143          pitches[19] = pitchLowB;
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      private static void playMelody(String input, Pitch[] pitches) throws Exception {
165          Scanner scanner = new Scanner(input);
166  
167          while ( scanner.hasNext() ) {
168              String token = scanner.next();
169              String pitchName;
170              String duration = " ";
171  
172              if( token.indexOf(",") < 0){
173                  pitchName = token.substring(0,1);
174                  duration = token.substring(1);
175              } else {
176                  pitchName = token.substring(0,2);
177                  duration = token.substring(2);
178              }
179              if (duration.length()==0) {
180                  duration = "1";
181              }
182              Pitch pitch = find(pitchName,pitches);
183              pitch.play(duration);
184          }
185      }
186      // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
187      static private void showErrorMessage(String message) {
188          miro.setVisible(false);
189          JOptionPane.showMessageDialog(null, message);
190      }
191      private static void initialization() {
192          // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
193          miro = new SPainter("Chromesthesia",500,500);
194          miro.setVisible(false);
195          miro.setBrushWidth(7);
196          // ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS
197          pitches = establishPitches(miro);
198          display(pitches);
199      }
200      private static String getInput() {
201          miro.setVisible(false);
202          String label = "Please enter a melody in ABC notation, or EXIT ...     ";
203          String input1 = JOptionPane.showInputDialog(null,label);
204          miro.setVisible(true);
205          if ( input1 == null ) { input1 = ""; }
206          return input1;
207      }
208      static private void cleanup() {
209          System.exit(0);
210      }
211  
212  }
213