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