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