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