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