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