Chromesthesia.java
1    /*This program interprets melodic lines given in ABC notation as a 
2     * chromesthete might. 
3     * 
4     * A Pitch class will be defined, and will take center stage in the 
5     * processing. 
6     * 
7     * Interpreting a melody in ABC notation will amount to flashing 
8     * colored rectangles for prescribed durations, while sounding 
9     * the pitch! The color of the rectangle will correspond to pitch 
10    * class. The duration will correspond to the duration of the note. 
11    * 
12    * For this first version of the program, the duration will be held 
13    * 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 
18    * at a time. 
19    * 
20    * ABC represents notes in a manner consistent with these examples: 
21    * A,B,C,D,E,F,G, A B C D E F G a b c d e f g 
22    * 
23    * Google ABC music representation if you would like to know more about it 
24    */
25   
26   package chromesthesia1;
27   
28   import painter.SPainter;
29   
30   import javax.swing.*;
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   
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       //FEATURED VARIABLES
54   
55       private static SPainter miro;
56       private static Pitch[] pitches;
57   
58       //THE INTERPRETER
59   
60       public static void interpreter() {
61           initialization(); //miro and pitches
62           while (true) {
63               String input = getInput();
64               if(input.equalsIgnoreCase("EXIT")) {
65                   break;
66               }else {
67                   try {
68                       playMelody(input,pitches);
69                   }catch (Exception ex) {
70                       showErrorMessage(ex.toString());
71                   }
72               }
73           }
74   
75           cleanup(); //miro has to go
76       }
77   
78       // METHODS PERTAINING TO THE CHROMESTHETIC PITCHES
79   
80       private static Pitch[] establishPitches(SPainter painter) {
81           Pitch[] pitches = new Pitch[21];
82           Pitch pitchMiddleC = new Pitch("C", painter);
83           pitches[0] = pitchMiddleC;
84           Pitch pitchLowC = new Pitch("C,", painter);
85           pitches[1] = pitchLowC;
86           Pitch pitchHighC = new Pitch("c", painter);
87           pitches[2] = pitchHighC;
88           Pitch pitchMiddleD = new Pitch("D", painter);
89           pitches[3] = pitchMiddleD;
90           Pitch pitchLowD = new Pitch("D,", painter);
91           pitches[4] = pitchLowD;
92           Pitch pitchHighD = new Pitch("d", painter);
93           pitches[5] = pitchHighD;
94           Pitch pitchMiddleE = new Pitch("E", painter);
95           pitches[6] = pitchMiddleE;
96           Pitch pitchLowE = new Pitch("E,", painter);
97           pitches[7] = pitchLowE;
98           Pitch pitchHighE = new Pitch("e", painter);
99           pitches[8] = pitchHighE;
100          //ADDED NOTES F,G,A,B
101          Pitch pitchMiddleF = new Pitch("F", painter);
102          pitches[9] = pitchMiddleF;
103          Pitch pitchLowF = new Pitch("F,", painter);
104          pitches[10] = pitchLowF;
105          Pitch pitchHighF = new Pitch("f", painter);
106          pitches[11] = pitchHighF;
107          Pitch pitchMiddleG = new Pitch("G", painter);
108          pitches[12] = pitchMiddleG;
109          Pitch pitchLowG = new Pitch("G,", painter);
110          pitches[13] = pitchLowG;
111          Pitch pitchHighG = new Pitch("g", painter);
112          pitches[14] = pitchHighG;
113          Pitch pitchMiddleA = new Pitch("A", painter);
114          pitches[15] = pitchMiddleA;
115          Pitch pitchLowA = new Pitch("A,", painter);
116          pitches[16] = pitchLowA;
117          Pitch pitchHighA = new Pitch("a", painter);
118          pitches[17] = pitchHighA;
119          Pitch pitchMiddleB = new Pitch("B", painter);
120          pitches[18] = pitchMiddleB;
121          Pitch pitchLowB = new Pitch("B,", painter);
122          pitches[19] = pitchLowB;
123          Pitch pitchHighB = new Pitch("b", painter);
124          pitches[20] = pitchHighB;
125          return pitches;
126      }
127      private static Pitch find(String token, Pitch[] pitches) throws Exception {
128          for (int i = 0; i < pitches.length; i = i + 1) {
129              Pitch pitch = pitches[i];
130              if (pitch.abcName().equals(token)) {
131                  return pitch;
132              }
133          }
134          throw new Exception("### PITCH" + token + "NOT FOUND");
135      }
136      private static void display (Pitch[] pitches) {
137          for (int i = 0; i < pitches.length; i = i + 1) {
138              System.out.println(pitches[i].toString());
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              Pitch pitch = find(token,pitches);
146              pitch.play("1");
147          }
148      }
149      //INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
150  
151      static private void showErrorMessage(String message) {
152          miro.setVisible(false);
153          JOptionPane.showMessageDialog(null, message);
154      }
155      private static void initialization() {
156          //ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
157          miro = new SPainter("Chromesthesia",500,500);
158          miro.setVisible(false);
159          miro.setBrushWidth(7);
160          //ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS
161          pitches = establishPitches(miro);
162          display(pitches);
163      }
164      private static String getInput() {
165          miro.setVisible(false);
166          String label = "Please enter a melody in ABC notation, or EXIT...  ";
167          String input = JOptionPane.showInputDialog(null, label);
168          miro.setVisible(true);
169          if (input == null) {
170              input = "";
171          }
172          return input;
173  
174      }
175      static private void cleanup() {
176          System.exit(0);
177      }
178  }
179  
180