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