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 chromesthesia0; 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 while(true){ 52 String input = getInput(); 53 if (input.equalsIgnoreCase("EXIT")){ 54 break; 55 }else { 56 try{ 57 playMelody(input,pitches); 58 }catch (Exception ex){ 59 showErrorMessage(ex.toString()); 60 } 61 } 62 } 63 cleanup(); 64 } 65 private static Pitch[] establishPitches(SPainter painter){ 66 Pitch[] pitches = new Pitch[9]; 67 Pitch pitchMiddleC = new Pitch("C", painter); 68 pitches[0] = pitchMiddleC; 69 Pitch pitchLowC = new Pitch("C," , painter); 70 pitches[1] = pitchLowC; 71 Pitch pitchHighC = new Pitch("c", painter); 72 pitches[2] = pitchHighC; 73 Pitch pitchMiddleD = new Pitch("D", painter); 74 pitches[3] = pitchMiddleD; 75 Pitch pitchLowD = new Pitch("D," ,painter); 76 pitches[4] = pitchLowD; 77 Pitch pitchHighD = new Pitch("d", painter); 78 pitches[5] = pitchHighD; 79 Pitch pitchMiddleE = new Pitch("E", painter); 80 pitches[6] = pitchMiddleE; 81 Pitch pitchLowE = new Pitch("E,", painter); 82 pitches[7] = pitchLowE; 83 Pitch pitchHighE = new Pitch("e", painter); 84 pitches[8] = pitchHighE; 85 return pitches; 86 } 87 88 private static Pitch find(String token, Pitch[] pitches) throws Exception{ 89 for (int i = 0; i < pitches.length; i = i + 1){ 90 Pitch pitch = pitches[i]; 91 if( pitch.abcName().equals(token)){ 92 return pitch; 93 } 94 } 95 throw new Exception("### PITCH " + token + "NOT FOUND"); 96 } 97 private static void display(Pitch[] pitches){ 98 for( int i = 0; i < pitches.length; i = i +1){ 99 System.out.println(pitches[i].toString()); 100 } 101 } 102 private static void playMelody(String input, Pitch[] pitches) throws Exception{ 103 Scanner scanner = new Scanner(input); 104 while( scanner.hasNext()){ 105 String token = scanner.next(); 106 Pitch pitch = find(token, pitches); 107 pitch.play("1"); 108 } 109 } 110 static private void showErrorMessage(String message){ 111 miro.setVisible(false); 112 JOptionPane.showMessageDialog(null, message); 113 } 114 115 private static void intialization(){ 116 117 miro = new SPainter("Chromesthesia", 500, 500); 118 miro.setVisible(false); 119 miro.setBrushWidth(7); 120 121 pitches = establishPitches(miro); 122 display(pitches); 123 } 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 } 137