Chromesthesia.java
1    /* 
2     * This program is little different then the first one we have been 
3     * add few more note f g a b 
4     * This program interprets melodic lines given in ABC notation as a 
5     * chromesthete might. 
6     * 
7     * A Pitch class will be defined, and will take center stage in the 
8     * processing. 
9     * 
10    * Interpreting a melody in ABC notation will amount to flashing 
11    * colored rectangles for prescribed durations, while sounding 
12    * the pitch! The color of the rectangle will correspond to pitch 
13    * class. The duration will correspond to the duration of the note. 
14    * 
15    * For this first version of the program, the duration will be held 
16    * constant at 1 beat. 
17    * 
18    * Three sorts of images will appear on the screen, the chromesthetic 
19    * output box, a text input box, and an error message box. Simplicity 
20    * of design is rendered by permitting only one box to be on the screen 
21    * at a time. 
22    * 
23    * ABC represents notes in a manner consistent with these examples: 
24    * C, D, E, C D E c d e 
25    * 
26    * Google ABC music representation if you would like to know more about it. 
27    */
28   package chromesthesia1;
29   
30   import java.util.Scanner;
31   import javax.swing.JOptionPane;
32   import javax.swing.SwingUtilities;
33   import painter.SPainter;
34   
35   public class Chromesthesia {
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       // FEATURED VARIABLES
50       private static SPainter miro;
51       private static Pitch[] pitches;
52       // THE INTERPRETER
53       public static void interpreter() {
54           initialization(); // miro and pitches
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       // METHODS PERTAINING TO THE CHROMESTHETIC PITCHES
70       private static Pitch[] establishPitches(SPainter painter) {
71           Pitch[] pitches = new Pitch[21];
72           Pitch pitchMiddleA = new Pitch("A",painter);
73           pitches[0] = pitchMiddleA;
74           Pitch pitchLowA = new Pitch("A,",painter);
75           pitches[1] = pitchLowA;
76           Pitch pitchHigha = new Pitch("a",painter);
77           pitches[2] = pitchHigha;
78           Pitch pitchMiddleB = new Pitch("B",painter);
79           pitches[3] = pitchMiddleB;
80           Pitch pitchLowB = new Pitch("B,",painter);
81           pitches[4] = pitchLowB;
82           Pitch pitchHighb = new Pitch("b",painter);
83           pitches[5] = pitchHighb;
84           Pitch pitchMiddleC = new Pitch("C",painter);
85           pitches[6] = pitchMiddleC;
86           Pitch pitchLowC = new Pitch("C,",painter);
87           pitches[7] = pitchLowC;
88           Pitch pitchHighC = new Pitch("c",painter);
89           pitches[8] = pitchHighC;
90           Pitch pitchMiddleD = new Pitch("D",painter);
91           pitches[9] = pitchMiddleD;
92           Pitch pitchLowD = new Pitch("D,",painter);
93           pitches[10] = pitchLowD;
94           Pitch pitchHighD = new Pitch("d",painter);
95           pitches[11] = pitchHighD;
96           Pitch pitchMiddleE = new Pitch("E",painter);
97           pitches[12] = pitchMiddleE;
98           Pitch pitchLowE = new Pitch("E,",painter);
99           pitches[13] = pitchLowE;
100          Pitch pitchHighE = new Pitch("e",painter);
101          pitches[14] = pitchHighE;
102          Pitch pitchMiddleF = new Pitch("F",painter);
103          pitches[15] = pitchMiddleF;
104          Pitch pitchLowF = new Pitch("F,",painter);
105          pitches[16] = pitchLowF;
106          Pitch pitchHighf = new Pitch("f",painter);
107          pitches[17] = pitchHighf;
108          Pitch pitchMiddleG = new Pitch("G",painter);
109          pitches[18] = pitchMiddleG;
110          Pitch pitchLowG = new Pitch("G,",painter);
111          pitches[19] = pitchLowG;
112          Pitch pitchHighg = new Pitch("g",painter);
113          pitches[20] = pitchHighg;
114          return pitches;
115      }
116      private static Pitch find(String token, Pitch[] pitches) throws Exception {
117          for ( int i = 0; i < pitches.length; i = i + 1 ) {
118              Pitch pitch = pitches[i];
119              if ( pitch.abcName().equals(token) ) {
120                  return pitch;
121              }
122          }
123          throw new Exception("### PITCH " + token + " NOT FOUND");
124      }
125      private static void display(Pitch[] pitches) {
126          for ( int i = 0; i < pitches.length; i = i + 1 ) {
127              System.out.println(pitches[i].toString());
128          }
129      }
130      private static void playMelody(String input, Pitch[] pitches) throws Exception {
131          Scanner scanner = new Scanner(input);
132          while ( scanner.hasNext() ) {
133              String token = scanner.next();
134              Pitch pitch = find(token,pitches);
135              pitch.play("1");
136          }
137      }
138      // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
139      static private void showErrorMessage(String message) {
140          miro.setVisible(false);
141          JOptionPane.showMessageDialog(null, message);
142      }
143      private static void initialization() {
144  // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
145          miro = new SPainter("Chromesthesia",500,500);
146          miro.setVisible(false);
147          miro.setBrushWidth(7);
148  // ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS
149          pitches = establishPitches(miro);
150          display(pitches);
151      }
152      private static String getInput() {
153          miro.setVisible(false);
154          String label = "Please enter a melody in ABC notation, or EXIT ... ";
155          String input = JOptionPane.showInputDialog(null,label);
156          miro.setVisible(true);
157          if ( input == null ) { input = ""; }
158          return input;
159      }
160      static private void cleanup() {
161          System.exit(0);
162      }
163  }