1 package Chromesthesia2; 2 3 import Chromesthesia2.Pitch; 4 import painter.SPainter; 5 import javax.swing.*; 6 import java.util.Scanner; 7 8 public class Chromesthesia { 9 public static void main(String[] args) { 10 SwingUtilities.invokeLater(new ThreadForGUI()); 11 } 12 13 private static class ThreadForGUI implements Runnable { 14 @Override 15 public void run() { 16 new Chromesthesia(); 17 } 18 } 19 20 public Chromesthesia() { 21 interpreter(); 22 } 23 24 private static SPainter miro; 25 private static Pitch[] pitches; 26 27 public static void interpreter() { 28 29 initialization(); 30 String note = "none"; 31 while (true) { 32 String input = getInput(); 33 if (input.equalsIgnoreCase("EXIT")) { 34 break; 35 } else if(input.equalsIgnoreCase("AGAIN")) { 36 try { 37 playMelody(note,pitches); 38 } catch (Exception ex) { 39 showErrorMessage(ex.toString()); 40 } 41 } 42 else { 43 note = new String(input); 44 System.out.println("New Note: " + note); 45 try { 46 playMelody(note, pitches); 47 } catch (Exception ex) { 48 showErrorMessage(ex.toString()); 49 } 50 } 51 } 52 cleanup(); 53 } 54 //METHODS PERTAINING TO THE CHROMESTHETIC PITCHES 55 56 private static Pitch[] establishesPitches(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 101 return pitches; 102 } 103 104 private static Pitch find(String token, Pitch[] pitches) throws Exception { 105 for(int i = 0; i < pitches.length; i = i + 1) { 106 Pitch pitch = pitches[i]; 107 if (pitch.abcName().equals(token)) { 108 return pitch; 109 } 110 } 111 throw new Exception("### PITCH " + token + " NOT FOUND"); 112 } 113 114 private static void display(Pitch[] pitches) { 115 for (int i = 0; i<pitches.length;i = i + 1) { 116 System.out.println(pitches[i].toString()); 117 } 118 } 119 120 private static void playMelody(String input, Pitch[] pitches) throws Exception { 121 Scanner scanner = new Scanner(input); 122 while (scanner.hasNext()) { 123 String token = scanner.next(); 124 String pitchName; 125 String duration = ""; 126 if( token.indexOf(",") < 0) { 127 pitchName = token.substring(0,1); 128 duration = token.substring(1); 129 } else { 130 pitchName = token.substring(0,2); 131 duration = token.substring(2); 132 } 133 if (duration.length() == 0) { duration = "1";} 134 Pitch pitch = find(pitchName,pitches); 135 pitch.play(duration); 136 } 137 } 138 139 //INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING 140 141 static private void showErrorMessage(String message) { 142 miro.setVisible(false); 143 JOptionPane.showMessageDialog(null, message); 144 } 145 146 private static void initialization() { 147 //ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH 148 miro = new SPainter("Chromesthesia", 500,500); 149 miro.setVisible(false); 150 miro.setBrushWidth(7); 151 //ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS 152 pitches = establishesPitches(miro); 153 display(pitches); 154 } 155 private static String getInput() { 156 miro.setVisible(false); 157 String label = "Please enter a melody in ABC notation, or EXIT ..."; 158 String input = JOptionPane.showInputDialog(null,label); 159 miro.setVisible(true); 160 if(input == null) { input = "";} 161 return input; 162 } 163 164 static private void cleanup() { 165 System.exit(0); 166 } 167 }