1 package chromesthesia0; 2 3 import java.util.Scanner; 4 import javax.swing.JOptionPane; 5 import javax.swing.SwingUtilities; 6 import painter.SPainter; 7 8 public class Chromesthesiav0 { 9 public static void main(String[] args) { 10 SwingUtilities.invokeLater(new ThreadForGUI()); 11 } 12 private static class ThreadForGUI implements Runnable { 13 @Override 14 public void run() { 15 new Chromesthesiav0(); 16 } 17 } 18 public Chromesthesiav0() { 19 interpreter(); 20 } 21 private static SPainter miro; 22 private static Pitchv0[] pitches; 23 public static void interpreter() { 24 initialization(); 25 while ( true ) { 26 String input = getInput(); 27 if ( input.equalsIgnoreCase("EXIT") ) { 28 break; 29 } else { 30 try { 31 playMelody(input,pitches); 32 } catch (Exception ex) { 33 showErrorMessage(ex.toString()); 34 } 35 } 36 } 37 cleanup(); 38 } 39 private static Pitchv0[] establishPitches(SPainter painter) { 40 Pitchv0[] pitches = new Pitchv0[9]; 41 Pitchv0 pitchMiddleC = new Pitchv0("C",painter); 42 pitches[0] = pitchMiddleC; 43 Pitchv0 pitchLowC = new Pitchv0("C,",painter); 44 pitches[1] = pitchLowC; 45 Pitchv0 pitchHighC = new Pitchv0("c",painter); 46 pitches[2] = pitchHighC; 47 Pitchv0 pitchMiddleD = new Pitchv0("D",painter); 48 pitches[3] = pitchMiddleD; 49 Pitchv0 pitchLowD = new Pitchv0("D,",painter); 50 pitches[4] = pitchLowD; 51 Pitchv0 pitchHighD = new Pitchv0("d",painter); 52 pitches[5] = pitchHighD; 53 Pitchv0 pitchMiddleE = new Pitchv0("E",painter); 54 pitches[6] = pitchMiddleE; 55 Pitchv0 pitchLowE = new Pitchv0("E,",painter); 56 pitches[7] = pitchLowE; 57 Pitchv0 pitchHighE = new Pitchv0("e",painter); 58 pitches[8] = pitchHighE; 59 return pitches; 60 } 61 private static Pitchv0 find(String token, Pitchv0[] pitches) throws Exception { 62 for ( int i = 0; i < pitches.length; i = i + 1 ) { 63 Pitchv0 pitch = pitches[i]; 64 if ( pitch.abcName().equals(token) ) { 65 return pitch; 66 } 67 } 68 throw new Exception("### PITCH " + token + " NOT FOUND"); 69 } 70 private static void display(Pitchv0[] pitches) { 71 for ( int i = 0; i < pitches.length; i = i + 1 ) { 72 System.out.println(pitches[i].toString()); 73 } 74 } 75 private static void playMelody(String input, Pitchv0[] pitches) throws Exception { 76 Scanner scanner = new Scanner(input); 77 while ( scanner.hasNext() ) { 78 String token = scanner.next(); 79 Pitchv0 pitch = find(token,pitches); 80 pitch.play("1"); 81 } 82 } 83 static private void showErrorMessage(String message) { 84 miro.setVisible(false); 85 JOptionPane.showMessageDialog(null, message); 86 } 87 private static void initialization() { 88 miro = new SPainter("Chromesthesia",500,500); 89 miro.setVisible(false); 90 miro.setBrushWidth(7); 91 pitches = establishPitches(miro); 92 display(pitches); 93 } 94 private static String getInput() { 95 miro.setVisible(false); 96 String label = "Please enter a melody in ABC notation, or EXIT ... "; 97 String input = JOptionPane.showInputDialog(null,label); 98 miro.setVisible(true); 99 if ( input == null ) { input = ""; } 100 return input; 101 } 102 static private void cleanup() { 103 System.exit(0); 104 } 105 }