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