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