1 package chromesthesia0; 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[9]; 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 return pitches; 71 } 72 73 private static void playMelody(String input, Pitch[] pitches) throws Exception { 74 Scanner scanner = new Scanner(input); 75 while (scanner.hasNext() ) { 76 String token = scanner.next(); 77 Pitch pitch = find(token, pitches); 78 pitch.play("1"); 79 } 80 } 81 82 private static Pitch find(String token, Pitch[] pitches) throws Exception { 83 for (int i = 0; i < pitches.length; i = i + 1){ 84 Pitch pitch = pitches[i]; 85 if (pitch.abcName().equals(token)){ 86 return pitch; 87 } 88 } 89 throw new Exception("### PITCH" + token + "NOT FOUND"); 90 } 91 92 static private void initialization() { 93 miro = new SPainter("Chromesthesia", 500, 500); 94 miro.setVisible(false); 95 miro.setBrushWidth(7); 96 pitches = establishPitches(miro); 97 display(pitches); 98 } 99 100 private static void display(Pitch[] pitches) { 101 for ( int i = 0; i <pitches.length; i = i + 1){ 102 System.out.println(pitches[i].toString()); 103 } 104 } 105 106 private static String getInput() { 107 miro.setVisible(false); 108 String label = "Please enter a melody in ABC notation or EXIT..."; 109 String input = JOptionPane.showInputDialog(null, label); 110 miro.setVisible(true); 111 if ( input == null ) { input = ""; } 112 return input; 113 } 114 115 static private void cleanup() { 116 System.exit(0); 117 } 118 119 }