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