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