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