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