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 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 chromesthesia2;
27   
28   import painter.SPainter;
29   import javax.swing.JOptionPane;
30   import javax.swing.SwingUtilities;
31   import java.util.Scanner;
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   
48       public Chromesthesia() {
49           interpreter();
50       }
51   
52       // FEATURED VARIABLES
53   
54       private static SPainter miro;
55       private static Pitch[] pitches;
56   
57       // THE INTERPRETER
58   
59       public static void interpreter() {
60   
61           initialization(); // miro and pitches
62           String previousInput = "";
63           String currentInput = "";
64           while ( true ) {
65               String input = getInput();
66               if ( input.equalsIgnoreCase("EXIT") ) {
67                   break;
68               } else if( input.equalsIgnoreCase("AGAIN") ) {
69                   previousInput = currentInput;
70                   try {
71                       playMelody(currentInput, pitches);
72                   } catch (Exception ex) {
73                       showErrorMessage(ex.toString());
74                   }
75               } else {
76                   previousInput = currentInput;
77                   currentInput = input;
78                   try {
79                       playMelody(currentInput,pitches);
80                   } catch (Exception ex) {
81                       showErrorMessage(ex.toString());
82                   }
83               }
84           }
85   
86           cleanup(); // miro has to go
87   
88       }
89   
90       // METHODS PERTAINING TO THE CHROMESTHETIC PITCHES
91   
92       private static Pitch[] establishPitches(SPainter painter) {
93           Pitch[] pitches = new Pitch[21];
94           Pitch pitchMiddleC = new Pitch("C",painter);
95           pitches[0] = pitchMiddleC;
96           Pitch pitchLowC = new Pitch("C,",painter);
97           pitches[1] = pitchLowC;
98           Pitch pitchHighC = new Pitch("c",painter);
99           pitches[2] = pitchHighC;
100          Pitch pitchMiddleD = new Pitch("D",painter);
101          pitches[3] = pitchMiddleD;
102          Pitch pitchLowD = new Pitch("D,",painter);
103          pitches[4] = pitchLowD;
104          Pitch pitchHighD = new Pitch("d",painter);
105          pitches[5] = pitchHighD;
106          Pitch pitchMiddleE = new Pitch("E",painter);
107          pitches[6] = pitchMiddleE;
108          Pitch pitchLowE = new Pitch("E,",painter);
109          pitches[7] = pitchLowE;
110          Pitch pitchHighE = new Pitch("e",painter);
111          pitches[8] = pitchHighE;
112          Pitch pitchMiddleF = new Pitch("F",painter);
113          pitches[9] = pitchMiddleF;
114          Pitch pitchLowF = new Pitch("F,",painter);
115          pitches[10] = pitchLowF;
116          Pitch pitchHighF = new Pitch("f",painter);
117          pitches[11] = pitchHighF;
118          Pitch pitchMiddleG = new Pitch("G",painter);
119          pitches[12] = pitchMiddleG;
120          Pitch pitchLowG = new Pitch("G,",painter);
121          pitches[13] = pitchLowG;
122          Pitch pitchHighG = new Pitch("g",painter);
123          pitches[14] = pitchHighG;
124          Pitch pitchMiddleA = new Pitch("A",painter);
125          pitches[15] = pitchMiddleA;
126          Pitch pitchLowA = new Pitch("A,",painter);
127          pitches[16] = pitchLowA;
128          Pitch pitchHighA = new Pitch("a",painter);
129          pitches[17] = pitchHighA;
130          Pitch pitchMiddleB = new Pitch("B",painter);
131          pitches[18] = pitchMiddleB;
132          Pitch pitchLowB = new Pitch("B,",painter);
133          pitches[19] = pitchLowB;
134          Pitch pitchHighB = new Pitch("b",painter);
135          pitches[20] = pitchHighB;
136          return pitches;
137      }
138  
139      private static Pitch find(String token, Pitch[] pitches) throws Exception {
140          for ( int i = 0; i < pitches.length; i = i + 1) {
141              Pitch pitch = pitches[i];
142              if ( pitch.abcName().equals(token) ) {
143                  return pitch;
144              }
145          }
146          throw new Exception("### PITCH " + token + " NOT FOUND");
147      }
148  
149      private static void display(Pitch[] pitches) {
150          for ( int i = 0; i < pitches.length; i = i + 1) {
151              System.out.println(pitches[1].toString());
152          }
153      }
154  
155      private static void playMelody(String input, Pitch[] pitches) throws Exception {
156          Scanner scanner = new Scanner(input);
157          while ( scanner.hasNext() ) {
158              String token = scanner.next();
159              String pitchName;
160              String duration = "";
161              if ( token.indexOf(",") < 0 ) {
162                  pitchName = token.substring(0,1);
163                  duration = token.substring(1);
164              } else {
165                  pitchName = token.substring(0,2);
166                  duration = token.substring(2);
167              }
168              if ( duration.length() == 0 ) { duration = "1"; }
169              Pitch pitch = find(pitchName,pitches);
170              pitch.play(duration);
171          }
172      }
173  
174      // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
175  
176      static private void showErrorMessage(String message) {
177          miro.setVisible(false);
178          JOptionPane.showMessageDialog(null, message);
179      }
180  
181      private static void initialization() {
182          // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
183          miro = new SPainter("Chromesthesia",500,500);
184          miro.setVisible(false);
185          miro.setBrushWidth(7);
186          // ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS
187          pitches = establishPitches(miro);
188          display(pitches);
189      }
190  
191      private static String getInput() {
192          miro.setVisible(false);
193          String label = "Please enter a melody in ABC notation, or EXIT...    ";
194          String input = JOptionPane.showInputDialog(null, label);
195          miro.setVisible(true);
196          if (input == null) { input = ""; }
197          return input;
198      }
199  
200      static private void cleanup() {
201          System.exit(0);
202      }
203  
204  }
205