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 procesing. 6 * 7 * Interpreting a melody in ABC notation will amount to flashing 8 * colored rectangles for prescribed durations, while sounding 9 * the pitch! The color of the rectangle wil correspond to pitch class. 10 * The duration will correspond to the duration of the note. 11 * 12 * For this first version of the program, the duration will be held 13 * constant at 1 beat. 14 * 15 * Three sorts of images will appear on the screen, the chromesthetic 16 * output box, a text input box, and a error message box. Simplicity 17 * of design is rendered by permitting only one box to be on the screen 18 * at a time. 19 * 20 * ABC represents notes in a manner consistent with these examples: 21 * C, D, E, C D E c d e 22 * 23 */ 24 package chromesthesia2; 25 26 import javax.swing.*; 27 import painter.SPainter; 28 import java.util.Scanner; 29 import javax.swing.SwingUtilities; 30 31 public class Chromesthesia { 32 33 public static void main(String[] args){ 34 SwingUtilities.invokeLater(new ThreadForGUI()); 35 } 36 private static class ThreadForGUI implements Runnable{ 37 public void run(){ 38 new Chromesthesia(); 39 } 40 } 41 public Chromesthesia(){ 42 interpreter(); 43 } 44 45 private static SPainter miro; 46 private static Pitch[] pitches; 47 48 public static void interpreter(){ 49 intialization(); 50 51 String getInput = ""; 52 53 while(true){ 54 String input = getInput(); 55 if (input.equalsIgnoreCase("EXIT")) { 56 break; 57 } else if (input.equalsIgnoreCase("AGAIN")) { 58 try{ 59 playMelody(input,pitches); 60 }catch (Exception ex){ 61 showErrorMessage(ex.toString()); 62 } 63 } else { 64 getInput= input; 65 try { 66 playMelody(getInput, pitches); 67 }catch (Exception ex) { 68 showErrorMessage(ex.toString()); 69 } 70 } 71 } 72 cleanup(); 73 } 74 private static Pitch[] establishPitches(SPainter painter){ 75 Pitch[] pitches = new Pitch[9]; 76 Pitch pitchMiddleC = new Pitch("C", painter); 77 pitches[0] = pitchMiddleC; 78 Pitch pitchLowC = new Pitch("C," , painter); 79 pitches[1] = pitchLowC; 80 Pitch pitchHighC = new Pitch("c", painter); 81 pitches[2] = pitchHighC; 82 Pitch pitchMiddleD = new Pitch("D", painter); 83 pitches[3] = pitchMiddleD; 84 Pitch pitchLowD = new Pitch("D," ,painter); 85 pitches[4] = pitchLowD; 86 Pitch pitchHighD = new Pitch("d", painter); 87 pitches[5] = pitchHighD; 88 Pitch pitchMiddleE = new Pitch("E", painter); 89 pitches[6] = pitchMiddleE; 90 Pitch pitchLowE = new Pitch("E,", painter); 91 pitches[7] = pitchLowE; 92 Pitch pitchHighE = new Pitch("e", painter); 93 pitches[8] = pitchHighE; 94 return pitches; 95 } 96 97 private static Pitch find(String token, Pitch[] pitches) throws Exception{ 98 for (int i = 0; i < pitches.length; i = i + 1){ 99 Pitch pitch = pitches[i]; 100 if( pitch.abcName().equals(token)){ 101 return pitch; 102 } 103 } 104 throw new Exception("### PITCH " + token + "NOT FOUND"); 105 } 106 private static void display(Pitch[] pitches){ 107 for( int i = 0; i < pitches.length; i = i +1){ 108 System.out.println(pitches[i].toString()); 109 } 110 } 111 private static void playMelody(String input, Pitch[] pitches) throws Exception{ 112 Scanner scanner = new Scanner(input); 113 while( scanner.hasNext()){ 114 String token = scanner.next(); 115 Pitch pitch = find(token, pitches); 116 pitch.play("1"); 117 } 118 } 119 static private void showErrorMessage(String message){ 120 miro.setVisible(false); 121 JOptionPane.showMessageDialog(null, message); 122 } 123 124 private static void intialization(){ 125 126 miro = new SPainter("Chromesthesia", 500, 500); 127 miro.setVisible(false); 128 miro.setBrushWidth(7); 129 130 pitches = establishPitches(miro); 131 display(pitches); 132 } 133 134 private static String getInput(){ 135 miro.setVisible(false); 136 String label = "Please enter a melody in ABC notation, or EXIT ... "; 137 String input = JOptionPane.showInputDialog(null, label); 138 miro.setVisible(true); 139 if (input == null ) {input = "";} 140 return input; 141 } 142 static private void cleanup(){ 143 System.exit(0); 144 } 145 } 146