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