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