1 package chromesthesia2; 2 3 4 import painter.SPainter; 5 6 import javax.swing.*; 7 import java.util.Scanner; 8 9 public class Chromesthesia2 { 10 // INFRASTRUCTURE FOR THE PROGRAM -- LAUNCHING A "GRAPHICS" THREAD 11 12 public static void main(String[] args) { 13 SwingUtilities.invokeLater(new ThreadForGUI()); 14 } 15 16 private static class ThreadForGUI implements Runnable { 17 @Override 18 public void run() { 19 new Chromesthesia2(); 20 } 21 } 22 23 public Chromesthesia2() { 24 interpreter(); 25 } 26 27 // FEATURED VARIABLES 28 private static SPainter miro; 29 private static Pitch2[] pitch2s; 30 31 // THE INTERPRETER 32 public static void interpreter() { 33 initialization(); // miro and pitches 34 String i = ""; 35 while ( true ) { 36 String input = getInput(); 37 if (input.equalsIgnoreCase("EXIT")) { 38 break; 39 } else if (input.equalsIgnoreCase("AGAIN") ){ 40 try { 41 playMelody(i, pitch2s); 42 } catch (Exception ex) { 43 showErrorMessage(ex.toString()); 44 } 45 }else { 46 try { 47 playMelody(input, pitch2s); 48 i= input; 49 } catch (Exception ex) { 50 showErrorMessage(ex.toString()); 51 } 52 53 } 54 } 55 cleanup(); // miro has to go 56 57 } 58 59 // METHODS PERTAINING TO THE CHROMESTHETIC PITCHES 60 private static Pitch2[] establishPitches(SPainter painter) { 61 Pitch2[] pitch2s = new Pitch2[21]; 62 Pitch2 pitch2MiddleC = new Pitch2("C",painter); 63 pitch2s[0] = pitch2MiddleC; 64 Pitch2 pitch2LowC = new Pitch2("C,",painter); 65 pitch2s[1] = pitch2LowC; 66 Pitch2 pitch2HighC = new Pitch2("c",painter); 67 pitch2s[2] = pitch2HighC; 68 Pitch2 pitch2MiddleD = new Pitch2("D",painter); 69 pitch2s[3] = pitch2MiddleD; 70 Pitch2 pitch2LowD = new Pitch2("D,",painter); 71 pitch2s[4] = pitch2LowD; 72 Pitch2 pitch2HighD = new Pitch2("d",painter); 73 pitch2s[5] = pitch2HighD; 74 Pitch2 pitch2MiddleE = new Pitch2("E",painter); 75 pitch2s[6] = pitch2MiddleE; 76 Pitch2 pitch2LowE = new Pitch2("E,",painter); 77 pitch2s[7] = pitch2LowE; 78 Pitch2 pitch2HighE = new Pitch2("e",painter); 79 pitch2s[8] = pitch2HighE; 80 Pitch2 pitch2MiddleF = new Pitch2("F",painter); 81 pitch2s[9] = pitch2MiddleF; 82 Pitch2 pitch2LowF = new Pitch2("F,",painter ); 83 pitch2s[10] = pitch2LowF; 84 Pitch2 pitch2HighF = new Pitch2("f",painter ); 85 pitch2s[11] = pitch2HighF; 86 Pitch2 pitch2MiddleG = new Pitch2("G",painter); 87 pitch2s[12] = pitch2MiddleG; 88 Pitch2 pitch2LowG = new Pitch2("G,",painter ); 89 pitch2s[13] = pitch2LowG; 90 Pitch2 pitch2HighG = new Pitch2("g",painter ); 91 pitch2s[14] = pitch2HighG; 92 Pitch2 pitch2MiddleA = new Pitch2("A",painter); 93 pitch2s[15] = pitch2MiddleA; 94 Pitch2 pitch2LowA = new Pitch2("A,",painter ); 95 pitch2s[16] = pitch2LowA; 96 Pitch2 pitch2HighA = new Pitch2("a",painter ); 97 pitch2s[17] = pitch2HighA; 98 Pitch2 pitch2MiddleB = new Pitch2("B",painter); 99 pitch2s[18] = pitch2MiddleB; 100 Pitch2 pitch2LowB = new Pitch2("B,",painter ); 101 pitch2s[19] = pitch2LowB; 102 Pitch2 pitch2HighB = new Pitch2("b",painter ); 103 pitch2s[20] = pitch2HighB; 104 105 return pitch2s; 106 } 107 108 private static Pitch2 find(String token, Pitch2[] pitch1s) throws Exception { 109 for (int i = 0; i < pitch1s.length; i = i + 1 ) { 110 Pitch2 pitch2 = pitch1s[i]; 111 if ( pitch2.abcName().equals(token) ) { 112 return pitch2; 113 } 114 } 115 throw new Exception("### PITCH " + token + " NOT FOUND"); 116 } 117 private static void display(Pitch2[] pitch2s) { 118 for (int i = 0; i < pitch2s.length; i = i + 1) { 119 System.out.println(pitch2s[i].toString()); 120 } 121 } 122 private static void playMelody(String input, Pitch2[] pitch1s) throws Exception { 123 Scanner scanner = new Scanner(input); 124 while ( scanner.hasNext() ) { 125 String token = scanner.next(); 126 String pitchName; 127 String duration = " "; 128 if (token.indexOf(",")< 0){ 129 pitchName = token.substring(0,1); 130 duration = token.substring(1); 131 }else { 132 pitchName = token.substring(0,2); 133 duration = token.substring(2); 134 } 135 if (duration.length() == 0){duration = "1"; } 136 Pitch2 pitch = find(pitchName, pitch2s); 137 pitch.play(duration); 138 } 139 // Pitch2 pitch2 = find(token, pitch1s); 140 // pitch2.play("1"); 141 } 142 143 144 // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING 145 static private void showErrorMessage(String message) { 146 miro.setVisible(false); 147 JOptionPane.showMessageDialog(null, message); 148 } 149 150 private static void initialization() { 151 // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH 152 miro = new SPainter("Chromesthesia",500,500); 153 miro.setVisible(false); 154 miro.setBrushWidth(7); 155 // ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS 156 pitch2s = establishPitches(miro); 157 display(pitch2s); 158 } 159 private static String getInput() { 160 miro.setVisible(false); 161 String label = "Please enter a melody in ABC notation, or EXIT ... "; 162 String input = JOptionPane.showInputDialog(null,label); 163 miro.setVisible(true); 164 if ( input == null ) { input = ""; } 165 return input; 166 } 167 static private void cleanup() { 168 System.exit(0); 169 } 170 } 171 172 173 174 175 176