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    * For this first version of the program, the duration will be held 
14    * constant at 1 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    * Google ABC music representation if you would like to know more about it. 
25    */
26   
27   
28   package chromesthesia1;
29   
30   
31   import chromesthesia1.Pitch;
32   import painter.SPainter;
33   
34   import javax.swing.*;
35   import java.util.Scanner;
36   
37   public class Chromesthesia {
38       //infrastructure for the program -- launching a "Graphics" thread
39   
40       public static void main(String[] args){
41           SwingUtilities.invokeLater(new ThreadForGUI());
42       }
43   
44       private static class ThreadForGUI implements Runnable{
45           @Override
46           public void run(){
47               new Chromesthesia();
48           }
49       }
50   
51       public Chromesthesia(){
52           interpreter();
53       }
54   
55       //featured variables
56   
57       private static SPainter miro;
58       private static Pitch[] pitches;
59   
60       //the interpreter
61   
62       public static void interpreter(){
63   
64           initialization(); //miro and pitchers
65   
66           while(true){
67               String input = getInput();
68   
69               if(input.equalsIgnoreCase("EXIT")){
70                   break;
71               } else {
72                   try{
73                       playMelody(input,pitches);
74                   }catch (Exception ex){
75                       showErrorMessage(ex.toString());
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      private static void playMelody(String input, Pitch[] pitches) throws Exception {
147          Scanner scanner = new Scanner(input);
148          while ( scanner.hasNext() ) {
149              String token = scanner.next();
150              Pitch pitch = find(token,pitches);
151              pitch.play("1");
152          } }
153      // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
154      static private void showErrorMessage(String message) {
155          miro.setVisible(false);
156          JOptionPane.showMessageDialog(null, message);
157      }
158      private static void initialization() {
159          // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
160          miro = new SPainter("Chromesthesia",500,500);
161          miro.setVisible(false);
162          miro.setBrushWidth(7);
163          // ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS
164          pitches = establishPitches(miro);
165          display(pitches);
166      }
167      private static String getInput() {
168          miro.setVisible(false);
169          String label = "Please enter a melody in ABC notation, or EXIT ...     ";
170          String input1 = JOptionPane.showInputDialog(null,label);
171          miro.setVisible(true);
172          if ( input1 == null ) { input1 = ""; }
173          return input1;
174      }
175      static private void cleanup() {
176          System.exit(0);
177      }
178  
179  }
180