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 chromesthesia2;
22   
23   import chromesthesia2.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 chromesthesia2.Chromesthesia.ThreadForGUI());
33       }
34   
35       private static class ThreadForGUI implements Runnable{
36           @Override
37           public void run(){
38               new chromesthesia2.Chromesthesia();
39           }
40       }
41   
42       public Chromesthesia(){
43           interpreter();
44       }
45   
46       //FEATURED VARIABLES
47       private static SPainter miro;
48       private static chromesthesia2.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 chromesthesia2.Pitch[] establishPitches(SPainter painter){
72           chromesthesia2.Pitch[] pitches = new chromesthesia2.Pitch[21];
73           chromesthesia2.Pitch pitchMiddleC = new chromesthesia2.Pitch("C", painter);
74           pitches[0] = pitchMiddleC;
75           chromesthesia2.Pitch pitchLowC = new chromesthesia2.Pitch("C,",painter);
76           pitches[1] = pitchLowC;
77           chromesthesia2.Pitch pitchHighC = new chromesthesia2.Pitch("c",painter);
78           pitches[2] = pitchHighC;
79           chromesthesia2.Pitch pitchMiddleD = new chromesthesia2.Pitch("D",painter);
80           pitches[3] = pitchMiddleD;
81           chromesthesia2.Pitch pitchLowD = new chromesthesia2.Pitch("D,",painter);
82           pitches[4] = pitchLowD;
83           chromesthesia2.Pitch pitchHighD = new chromesthesia2.Pitch("d",painter);
84           pitches[5] = pitchHighD;
85           chromesthesia2.Pitch pitchMiddleE = new chromesthesia2.Pitch("E",painter);
86           pitches[6] = pitchMiddleE;
87           chromesthesia2.Pitch pitchLowE = new chromesthesia2.Pitch("E,",painter);
88           pitches[7] = pitchLowE;
89           chromesthesia2.Pitch pitchHighE = new chromesthesia2.Pitch("e",painter);
90           pitches[8] = pitchHighE;
91           chromesthesia2.Pitch pitchMiddleF = new chromesthesia2.Pitch("F",painter);
92           pitches[9] = pitchMiddleF;
93           chromesthesia2.Pitch pitchLowF = new chromesthesia2.Pitch("F,",painter);
94           pitches[10] = pitchLowF;
95           chromesthesia2.Pitch pitchHighF = new chromesthesia2.Pitch("f",painter);
96           pitches[11] = pitchHighF;
97           chromesthesia2.Pitch pitchMiddleG = new chromesthesia2.Pitch("G",painter);
98           pitches[12] = pitchMiddleG;
99           chromesthesia2.Pitch pitchLowG = new chromesthesia2.Pitch("G,",painter);
100          pitches[13] = pitchLowG;
101          chromesthesia2.Pitch pitchHighG = new chromesthesia2.Pitch("g",painter);
102          pitches[14] = pitchHighG;
103          chromesthesia2.Pitch pitchMiddleA = new chromesthesia2.Pitch("A",painter);
104          pitches[15] = pitchMiddleA;
105          chromesthesia2.Pitch pitchLowA = new chromesthesia2.Pitch("A,",painter);
106          pitches[16] = pitchLowA;
107          chromesthesia2.Pitch pitchHighA = new chromesthesia2.Pitch("a",painter);
108          pitches[17] = pitchHighA;
109          chromesthesia2.Pitch pitchMiddleB = new chromesthesia2.Pitch("B",painter);
110          pitches[18] = pitchMiddleB;
111          chromesthesia2.Pitch pitchLowB = new chromesthesia2.Pitch("B,",painter);
112          pitches[19] = pitchLowB;
113          chromesthesia2.Pitch pitchHighB = new chromesthesia2.Pitch("b",painter);
114          pitches[20] = pitchHighB;
115  
116          return pitches;
117      }
118  
119      private static chromesthesia2.Pitch find(String token, chromesthesia2.Pitch[] pitches) throws Exception{
120          for( int i = 0; i < pitches.length; i = i + 1){
121              chromesthesia2.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(chromesthesia2.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, chromesthesia2.Pitch[] pitches) throws Exception{
135          Scanner scanner = new Scanner(input);
136          while(scanner.hasNext()){
137              String token = scanner.next();
138              String pitchName;
139              String duration = "";
140              if(token.indexOf(",") < 0){
141                  pitchName = token.substring(0,1);
142                  duration = token.substring(1);
143              } else{
144                  pitchName = token.substring(0,2);
145                  duration = token.substring(2);
146              }
147              if(duration.length() == 0){duration = "1";}
148              Pitch pitch = find(pitchName,pitches);
149              pitch.play(duration);
150          }
151      }
152      //INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
153  
154      static private void showErrorMessage(String message){
155          miro.setVisible(false);
156          JOptionPane.showMessageDialog(null, message);
157      }
158  
159      private static void initialization(){
160          //ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
161          miro = new SPainter("Chromesthesia",500,500);
162          miro.setVisible(false);
163          miro.setBrushWidth(7);
164          //ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS
165          pitches = establishPitches(miro);
166          display(pitches);
167      }
168  
169      private static String getInput(){
170          miro.setVisible(false);
171          String label = "Please enter a melody in ABC notation, or EXIT...   ";
172          String input = JOptionPane.showInputDialog(null,label);
173          miro.setVisible(true);
174          if(input == null){input = "";}
175          return input;
176      }
177  
178      static private void cleanup(){
179          System.exit(0);
180      }
181  }
182