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