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