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