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