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