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