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