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