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 chromesthesia0;
28   
29   import painter.SPainter;
30   
31   import javax.swing.*;
32   import java.util.Scanner;
33   public class Chromesthesia {
34   
35          //INFRASTRUCTURE FOR THE PROGRAM --- LAUNCHING A "GRAPHICS" THREAD
36   
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          //THE INTERPRETER
55   
56       public static void interpreter() {
57   
58           initialization(); //miro and pitches
59   
60           while (true) {
61               String input = getInput();
62               if(input.equalsIgnoreCase("EXIT")) {
63                   break;
64               }else {
65                   try {
66                       playMelody(input,pitches);
67                   }catch (Exception ex) {
68                       showErrorMessage(ex.toString());
69                   }
70               }
71           }
72   
73       cleanup(); //miro has to go
74       }
75   
76          // METHODS PERTAINING TO THE CHROMESTHETIC PITCHES
77       private static Pitch[] establishPitches(SPainter painter) {
78           Pitch[] pitches = new Pitch[9];
79           Pitch pitchMiddleC = new Pitch("C", painter);
80           pitches[0] = pitchMiddleC;
81           Pitch pitchLowC = new Pitch("C,", painter);
82           pitches[1] = pitchLowC;
83           Pitch pitchHighC = new Pitch("c", painter);
84           pitches[2] = pitchHighC;
85           Pitch pitchMiddleD = new Pitch("D", painter);
86           pitches[3] = pitchMiddleD;
87           Pitch pitchLowD = new Pitch("D,", painter);
88           pitches[4] = pitchLowD;
89           Pitch pitchHighD = new Pitch("d", painter);
90           pitches[5] = pitchHighD;
91           Pitch pitchMiddleE = new Pitch("E", painter);
92           pitches[6] = pitchMiddleE;
93           Pitch pitchLowE = new Pitch("E,", painter);
94           pitches[7] = pitchLowE;
95           Pitch pitchHighE = new Pitch("e", painter);
96           pitches[8] = pitchHighE;
97           return pitches;
98       }
99   
100      private static Pitch find(String token, Pitch[] pitches) throws Exception {
101          for (int i = 0; i < pitches.length; i = i + 1) {
102              Pitch pitch = pitches[i];
103              if (pitch.abcName().equals(token)) {
104                  return pitch;
105              }
106          }
107          throw new Exception("### PITCH" + token + "NOT FOUND");
108      }
109      private static void display (Pitch[] pitches) {
110          for (int i = 0; i < pitches.length; i = i + 1) {
111              System.out.println(pitches[i].toString());
112          }
113      }
114      private static void playMelody(String input, Pitch[] pitches) throws Exception {
115          Scanner scanner = new Scanner(input);
116          while (scanner.hasNext()) {
117              String token = scanner.next();
118              Pitch pitch = find(token,pitches);
119              pitch.play("1");
120          }
121      }
122      //INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
123  
124      static private void showErrorMessage(String message) {
125          miro.setVisible(false);
126          JOptionPane.showMessageDialog(null, message);
127      }
128      private static void initialization() {
129          //ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
130          miro = new SPainter("Chromesthesia",500,500);
131          miro.setVisible(false);
132          miro.setBrushWidth(7);
133          //ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS
134          pitches = establishPitches(miro);
135          display(pitches);
136      }
137  
138      private static String getInput() {
139          miro.setVisible(false);
140          String label = "Please enter a melody in ABC notation, or EXIT...  ";
141          String input = JOptionPane.showInputDialog(null, label);
142          miro.setVisible(true);
143          if (input == null) {
144              input = "";
145          }
146          return input;
147  
148      }
149      static private void cleanup() {
150            System.exit(0);
151      }
152  }
153  
154