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