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