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