Chromesthesia.java
1    /* 
2     * This program interprets melodic lines given in SBC notion as a 
3     * chromesthete might 
4     * 
5     * A pitch class will be defined and will take center stage in 
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 the pitch 
11    * class. The duration will correspond to the duration of the note. 
12    * 
13    * For the 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 representations if you would like to know more about it 
25    */
26   
27   
28   package Chromesthesia0;
29   
30   import java.util.Scanner;
31   import javax.swing.JOptionPane;
32   import javax.swing.SwingUtilities;
33   import painter.SPainter;
34   
35   public class Chromesthesia {
36       // INFRASTRUCTURE FOR THE PROGRAM -- LAUNCHING A "GRAPHICS" THREAD
37   
38       public static void main(String[] args){
39           SwingUtilities.invokeLater(new ThreadForGUI());
40       }
41   
42       private static class ThreadForGUI implements Runnable{
43           @Override
44           public void run(){
45               new Chromesthesia();
46           }
47       }
48   
49       public Chromesthesia(){
50           interpreter();
51       }
52   
53       // FEATURED VARIABLES
54   
55       private static SPainter miro;
56       private static Pitch[] pitches;
57   
58       // THE INTERPRETER
59   
60       public static void interpreter(){
61   
62           initialization(); // miro and pitches
63   
64           while(true){
65               String input = getInput();
66               if(input.equalsIgnoreCase("EXIT")){
67                   break;
68               }else{
69                   try{
70                       playMelody(input, pitches);
71                   } catch(Exception ex){
72                       showErrorMessage(ex.toString());
73                   }
74               }
75           }
76   
77           cleanup(); // miro has to go
78       }
79   
80       // METHODS PERTAINING TO THE CHROMESTHETIC PITCHES
81   
82       private static Pitch[] establishPitches(SPainter painter){
83           Pitch[] pitches = new Pitch[9];
84           Pitch pitchMiddleC = new Pitch("C", painter);
85           pitches[0] = pitchMiddleC;
86           Pitch pitchLowC = new Pitch("C,", painter);
87           pitches[1] = pitchLowC;
88           Pitch pitchHighC = new Pitch("c", painter);
89           pitches[2] = pitchHighC;
90           Pitch pitchMiddleD = new Pitch("D", painter);
91           pitches[3] = pitchMiddleD;
92           Pitch pitchLowD = new Pitch("D,", painter);
93           pitches[4] = pitchLowD;
94           Pitch pitchHighD = new Pitch("d", painter);
95           pitches[5] = pitchHighD;
96           Pitch pitchMiddleE = new Pitch("E", painter);
97           pitches[6] = pitchMiddleE;
98           Pitch pitchLowE = new Pitch("E,", painter);
99           pitches[7] = pitchLowE;
100          Pitch pitchHighE = new Pitch("e", painter);
101          pitches[8] = pitchHighE;
102          return pitches;
103      }
104  
105      private static Pitch find(String token, Pitch[] pitches) throws Exception{
106          for(int i = 0; i < pitches.length; i = i + 1){
107              Pitch pitch = pitches[i];
108              if(pitch.abcName().equals(token)){
109                  return pitch;
110              }
111          }
112          throw new Exception("### PITCH " + token + " NOT FOUND");
113      }
114  
115      private static void display(Pitch[] pitches){
116          for(int i = 0; i < pitches.length; i = i + 1){
117              System.out.println(pitches[i].toString());
118          }
119      }
120  
121      private static void playMelody(String input, Pitch[] pitches) throws Exception{
122          Scanner scanner = new Scanner(input);
123          while(scanner.hasNext()){
124              String token = scanner.next();
125              Pitch pitch = find(token, pitches);
126              pitch.play("1");
127          }
128      }
129  
130      // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
131      static private void showErrorMessage(String message){
132          miro.setVisible(false);
133          JOptionPane.showMessageDialog(null, message);
134      }
135  
136      private static void initialization(){
137          // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
138          miro = new SPainter("Chromesthesia", 500, 500);
139          miro.setVisible(false);
140          miro.setBrushWidth(7);
141          //ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS
142          pitches = establishPitches(miro);
143          display(pitches);
144      }
145  
146      private static String getInput(){
147          miro.setVisible(false);
148          String label = "Please enter a melody in ABC notation, or EXIT ...     ";
149          String input = JOptionPane.showInputDialog(null, label);
150          miro.setVisible(true);
151          if(input == null){input = "";}
152          return input;
153      }
154  
155      static private void cleanup(){
156          System.exit(0);
157      }
158  }
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174