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