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