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