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