1 /* For this first version of the program, the duration will be held 2 *constant at 1 beat. 3 *Three sorts of images will appear on the screen, 4 *the chromesthetic output box, a text input box, 5 *and an error message box. 6 *Simplicity of design is rendered by permitting only one box to be on the screen at a time. 7 * ABC represents notes in a manner consistent with these examples: 8 * C, D, E, C D E c d e 9 *Google ABC music representation if you would like to know more about it. 10 */ 11 12 13 14 15 package chromesthesia0; 16 17 import java.util.Scanner; 18 import javax.swing.JOptionPane; 19 import javax.swing.SwingUtilities; 20 import painter.SPainter; 21 22 public class Chromesthesia { 23 // INFRASTRUCTURE FOR THE PROGRAM -- LAUNCHING A "GRAPHICS" THREAD 24 public static void main(String[] args) { 25 SwingUtilities.invokeLater(new ThreadForGUI()); 26 } 27 private static class ThreadForGUI implements Runnable { 28 @Override 29 public void run() { 30 new Chromesthesia(); 31 } 32 } 33 public Chromesthesia() { 34 interpreter(); 35 } 36 37 38 //Featured Variables 39 private static SPainter miro; 40 private static Pitch[] pitches; 41 // THE INTERPRETER 42 public static void interpreter() { 43 initialization(); // miro and pitches 44 while ( true ) { 45 String input = getInput(); 46 if (input.equalsIgnoreCase("EXIT")) { 47 break; 48 } else { 49 try { 50 playMelody(input, pitches); 51 } catch (Exception ex) { 52 showErrorMessage(ex.toString()); 53 } 54 } 55 } 56 cleanup(); // miro has to go 57 } 58 // Methods Pertaining To The Chromesthetic Pitches 59 private static Pitch[] establishPitches(SPainter painter) { 60 Pitch[] pitches = new Pitch[9]; 61 Pitch pitchMiddleC = new Pitch("C",painter); 62 pitches[0] = pitchMiddleC; 63 Pitch pitchLowC = new Pitch("C,",painter); 64 pitches[1] = pitchLowC; 65 Pitch pitchHighC = new Pitch("c",painter); 66 pitches[2] = pitchHighC; 67 Pitch pitchMiddleD = new Pitch("D",painter); 68 pitches[3] = pitchMiddleD; 69 Pitch pitchLowD = new Pitch("D,",painter); 70 pitches[4] = pitchLowD; 71 Pitch pitchHighD = new Pitch("d",painter); 72 pitches[5] = pitchHighD; 73 Pitch pitchMiddleE = new Pitch("E",painter); 74 pitches[6] = pitchMiddleE; 75 Pitch pitchLowE = new Pitch("E,",painter); 76 pitches[7] = pitchLowE; 77 Pitch pitchHighE = new Pitch("e",painter); 78 pitches[8] = pitchHighE; 79 return pitches; 80 } 81 private static Pitch find(String token, Pitch[] pitches) throws Exception { 82 for (int i = 0; i < pitches.length; i = i + 1) { 83 Pitch pitch = pitches[i]; 84 if (pitch.abcName().equals(token)) { 85 return pitch; 86 } 87 } 88 throw new Exception("### PITCH " + token + " NOT FOUND"); 89 } 90 private static void display(Pitch[] pitches){ 91 for (int i = 0; i < pitches.length; i = i + 1) { 92 System.out.println(pitches[i].toString()); 93 } 94 } 95 private static void playMelody(String input, Pitch[] pitches) throws Exception { 96 Scanner scanner = new Scanner(input); 97 while ( scanner.hasNext() ) { 98 String token = scanner.next(); 99 Pitch pitch = find(token,pitches); 100 pitch.play("1"); 101 } 102 } 103 // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING 104 static private void showErrorMessage(String message) { 105 miro.setVisible(false); 106 JOptionPane.showMessageDialog(null, message); 107 } 108 private static void initialization() { 109 // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH 110 miro = new SPainter("Chromesthesia",500,500); 111 miro.setVisible(false); 112 miro.setBrushWidth(7); 113 // ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS 114 pitches = establishPitches(miro); 115 display(pitches); 116 } 117 private static String getInput() { 118 miro.setVisible(false); 119 String label = "Please enter a melody in ABC notation, or EXIT ... "; 120 String input = JOptionPane.showInputDialog(null,label); 121 miro.setVisible(true); 122 if ( input == null ) { input = ""; } 123 return input; 124 } 125 static private void cleanup() { 126 System.exit(0); 127 } 128 } 129 130 131