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   package chromesthesia0;
28   
29   import java.util.Scanner;
30   import javax.swing.JOptionPane;
31   import javax.swing.SwingUtilities;
32   import painter.SPainter;
33   
34   public class Chromesthesia {
35   
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       public 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           cleanup(); // miro has to go
77       }
78   
79       private static Pitch[] establishPitches(SPainter painter) {
80           Pitch[] pitches = new Pitch[21];
81           Pitch pitchMiddleC = new Pitch("C", painter);
82           pitches[0] = pitchMiddleC;
83           Pitch pitchLowC = new Pitch("C,", painter);
84           pitches[1] = pitchLowC;
85           Pitch pitchHighC = new Pitch("c", painter);
86           pitches[2] = pitchHighC;
87           Pitch pitchMiddleD = new Pitch("D", painter);
88           pitches[3] = pitchMiddleD;
89           Pitch pitchLowD = new Pitch("D,", painter);
90           pitches[4] = pitchLowD;
91           Pitch pitchHighD = new Pitch("d", painter);
92           pitches[5] = pitchHighD;
93           Pitch pitchMiddleE = new Pitch("E", painter);
94           pitches[6] = pitchMiddleE;
95           Pitch pitchLowE = new Pitch("E,", painter);
96           pitches[7] = pitchLowE;
97           Pitch pitchHighE = new Pitch("e", painter);
98           pitches[8] = pitchHighE;
99           Pitch pitchMiddleF = new Pitch("F", painter);
100          pitches[9] = pitchMiddleF;
101          Pitch pitchLowF = new Pitch("F,", painter);
102          pitches[10] = pitchLowF;
103          Pitch pitchHighF = new Pitch("f", painter);
104          pitches[11] = pitchHighF;
105          Pitch pitchMiddleG = new Pitch("G", painter);
106          pitches[12] = pitchMiddleG;
107          Pitch pitchLowG = new Pitch("G,", painter);
108          pitches[13] = pitchLowG;
109          Pitch pitchHighG = new Pitch("g", painter);
110          pitches[14] = pitchHighG;
111          Pitch pitchMiddleA = new Pitch("A", painter);
112          pitches[15] = pitchMiddleA;
113          Pitch pitchLowA = new Pitch("A,", painter);
114          pitches[16] = pitchLowA;
115          Pitch pitchHighA = new Pitch("a", painter);
116          pitches[17] = pitchHighA;
117          Pitch pitchMiddleB = new Pitch("B", painter);
118          pitches[18] = pitchMiddleB;
119          Pitch pitchLowB = new Pitch("B,", painter);
120          pitches[19] = pitchLowB;
121          Pitch pitchHighB = new Pitch("b", painter);
122          pitches[20] = pitchHighB;
123          return pitches;
124      }
125  
126      private static Pitch find(String token, Pitch[] pitches) throws Exception {
127          for ( int i = 0; i < pitches.length; i = i + 1 ) {
128              Pitch pitch = pitches[i];
129              if ( pitch.abcName().equals(token)) {
130                  return pitch;
131              }
132          }
133          throw new Exception("### PITCH " + token + " NOT FOUND");
134      }
135  
136      private static void display(Pitch[] pitches) {
137          for ( int i = 0; i < pitches.length; i = i + 1 ) {
138              System.out.println(pitches[i].toString());
139          }
140      }
141  
142      private static void playMelody(String input, Pitch[] pitches) throws Exception {
143          Scanner scanner = new Scanner(input);
144          while ( scanner.hasNext() ) {
145              String token = scanner.next();
146              Pitch pitch = find(token,pitches);
147              pitch.play("1");
148          }
149      }
150  
151      // INITIALIZATION, CLEAN UP, GETTING INPUT, ERROR MESSAGING
152  
153      static private void showErrorMessage(String message) {
154          miro.setVisible(false);
155          JOptionPane.showMessageDialog(null,message);
156      }
157  
158      static private void initialization() {
159          // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
160          miro = new SPainter("Chromesthia", 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  
168      private static String getInput() {
169          miro.setVisible(false);
170          String label = "Please enter a melody in ABC notation, or EXIT ...    ";
171          String input = JOptionPane.showInputDialog(null, label);
172          miro.setVisible(true);
173          if ( input == null ) { input = ""; }
174          return input;
175      }
176  
177      static private void cleanup() {
178          System.exit(0);
179      }
180  
181  }
182