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