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