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