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 processing 
6     * 
7     * interpreting a melody in abc notation will amount to flashing 
8     * clolored rectanglegs for prescribed durations, while sounding the 
9     *  pitch! the color of the rectangle will correspond to pitch class. The duration will 
10    * correspond the the duration of the note. 
11    * 
12    * for this first version of the program the duration will be held at constant at 1 beat. 
13    * 
14    * three sorts of images will appear oon the screen, the  chromesthetic 
15    *  output, a text input box, and an error message box. Simplicitly 
16    * of design is rendered by permiting only one box to be on the screen at a time. 
17    * 
18    * ABC represents notes in manner consistent with these examples: 
19    * C, D, E, C D E c d  e 
20    * 
21    * Google ABC music representation if you want to know morea bout it. 
22    */
23   
24   package chromesthesia2;
25   
26   
27   import painter.SPainter;
28   
29   import javax.swing.*;
30   import java.util.Scanner;
31   
32   public class Chromesthesia {
33   
34       //Infrustructure for the program -- LAUNCHING A "GRAPHICS" THREAD
35   
36       public static void main(String[] args) {
37           SwingUtilities.invokeLater(new ThreadForGUI());
38       }
39   
40       private static class ThreadForGUI implements Runnable {
41           @Override
42           public void run() {
43               new Chromesthesia();
44           }
45       }
46   
47       public Chromesthesia() {
48           interpreter();
49       }
50   
51       //FEATURED VARIABLES
52   
53       private static SPainter miro;
54       private static Pitch[] pitches;
55   
56       //THE INTERPRETER
57   
58       public static void interpreter() {
59           initialization(); //miro and pitches
60   
61           while (true) {
62               String input = getInput();
63               if (input.equalsIgnoreCase("EXIT")){
64                   break;
65               } else if (input.equalsIgnoreCase("AGAIN")){
66                   try {
67                       playMelody(input, pitches);
68                   } catch ( Exception ex) {
69                       showErrorMessage(ex.toString());
70                   }
71               } else {
72                   try {
73                       playMelody(input, pitches);
74                   } catch ( Exception ex) {
75                       showErrorMessage(ex.toString());
76                   }
77               }
78           }
79   
80           cleanup(); // miro has to go
81       }
82   
83       //METHODS PRETAINING TO THE CHROMESTHETIC PITCHES
84   
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  
132      private static Pitch find(String token, Pitch[] pitches) throws Exception {
133          for (int i = 0; i<pitches.length; i = i+1) {
134              Pitch pitch = pitches[i];
135              if ( pitch.abcName().equals(token) ) {
136                  return pitch;
137              }
138          }
139          throw new Exception("### PITCH " + token + " NOT FOUND");
140      }
141  
142      private static void display(Pitch[] pitches) {
143          for ( int i = 0; i< pitches.length; i = i+1 ) {
144              System.out.println(pitches[i].toString());
145          }
146      }
147  
148      private static void playMelody(String input, Pitch[] pitches) throws Exception {
149          Scanner scanner = new Scanner(input);
150          while ( scanner.hasNext() ) {
151              String token = scanner.next();
152              String pitchName;
153              String duration = "";
154              if ( token.indexOf(",") < 0 ) {
155                  pitchName = token.substring(0,1);
156                  duration = token.substring(1);
157              } else {
158                  pitchName = token.substring(0,2);
159                  duration = token.substring(2);
160              }
161              if ( duration.length() == 0 ) { duration = "1"; }
162              Pitch pitch = find(pitchName,pitches);
163              pitch.play(duration);
164          }
165      }
166  
167      //INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
168  
169      static private void showErrorMessage(String message) {
170          miro.setVisible(false);
171          JOptionPane.showMessageDialog(null, message);
172      }
173  
174      private static void initialization() {
175          //ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
176          miro = new SPainter("Chromesthesia", 500,500);
177          miro.setVisible(false);
178          miro.setBrushWidth(7);
179          //ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS
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 ... AGAIN    ";
187          String input = JOptionPane.showInputDialog(null,label);
188          miro.setVisible(true);
189          if ( input == null) { input = ""; }
190          return input;
191      }
192  
193      static private void cleanup() {
194          System.exit(0);
195      }
196  }
197  
198