/home/sjenks/NetBeansProjects/CS1/src/chromesthesia2/Chromesthesia.java
  1 /*
  2  * This porgram interprest melodic lines given in ABC notati nas a chromesthete might.
  3  * 
  4  * A Pitch class will be defined, and will take center stage in processing.
  5  * 
  6  * INterpreting a molody in aBC notaiton will amount ot flashing colored rectangles
  7  * for prescrived durations, while sounding the Pitch! The color of the rectangle
  8  * will correspond to pitch class. The duration will correspond to the duration of the note.
  9  *
 10  * For this third version of the program, the duratin will be held constand at 1 beat.
 11  * 
 12  * Three sorts of images will appear on the screen, the chromestheic output box,
 13  * a text input box, and an error message box. Simplicity of design is rendered
 14  * by premitting only one box to be on the screen at a time. 
 15  * 
 16  * ABC represents notes in a manner consistend with these examples,
 17  * C, D, E, F, G, A, B, C D E F G A B c d e f g a b 
 18  * 
 19  * Google ABC music represtnation if you would like to know more about it. 
 20  */
 21 package chromesthesia2;
 22 
 23 import java.util.Scanner;
 24 import javax.swing.JOptionPane;
 25 import javax.swing.SwingUtilities;
 26 import painter.SPainter;
 27 
 28 /**
 29  *
 30  * @author sjenks
 31  */
 32 public class Chromesthesia {
 33 
 34     //INFRASTRUCTURE FOR THE PORGRAM -- LAUGHING A 'GRAPHICS" THREAD
 35     /**
 36      * @param args the command line arguments
 37      */
 38     public static void main(String[] args) {
 39         SwingUtilities.invokeLater(new ThreadForGUI());
 40 
 41     }
 42 
 43     private static class ThreadForGUI implements Runnable {
 44 
 45         @Override
 46         public void run() {
 47             new Chromesthesia();
 48         }
 49     }
 50 
 51     public Chromesthesia() {
 52         interpreter();
 53     }
 54 
 55     //FEATURE VARIABLES
 56     private static SPainter miro;
 57     public static Pitch[] pitches;
 58 
 59     // THE INTERPRETER
 60     public static void interpreter() {
 61         initialization(); //miro and pitches
 62            String temp= "";
 63         while (true) {
 64             String input = getInput();
 65             if (input.equalsIgnoreCase("EXIT")) {
 66                 break;
 67             } else if (input.equalsIgnoreCase("AGAIN")){
 68                  try {
 69                     playMelody(temp, pitches);
 70                 } catch (Exception ex) {
 71                     showErrorMessage(ex.toString());
 72 
 73                 }
 74             }else {
 75                 try {
 76                     temp = input;
 77                     playMelody(input, pitches);
 78                 } catch (Exception ex) {
 79                     showErrorMessage(ex.toString());
 80 
 81                 }
 82             }
 83         }
 84 
 85         cleanup(); //miro has to go
 86 
 87     }
 88 
 89     //METHODS PRETAINING TO THE CHROMESTHETIC PITCHES
 90     private static Pitch[] establishPitches(SPainter painter) {
 91         Pitch[] pitches = new Pitch[21];
 92         Pitch pitchMiddleC = new Pitch("C", painter);
 93         pitches[0] = pitchMiddleC;
 94         Pitch pitchLowC = new Pitch("C,", painter);
 95         pitches[1] = pitchLowC;
 96         Pitch pitchHighC = new Pitch("c", painter);
 97         pitches[2] = pitchHighC;
 98         Pitch pitchMiddleD = new Pitch("D", painter);
 99         pitches[3] = pitchMiddleD;
100         Pitch pitchLowD = new Pitch("D,", painter);
101         pitches[4] = pitchLowD;
102         Pitch pitchHighD = new Pitch("d", painter);
103         pitches[5] = pitchHighD;
104         Pitch pitchMiddleE = new Pitch("E", painter);
105         pitches[6] = pitchMiddleE;
106         Pitch pitchLowE = new Pitch("E,", painter);
107         pitches[7] = pitchLowE;
108         Pitch pitchHighE = new Pitch("e", painter);
109         pitches[8] = pitchHighE;
110         Pitch pitchMiddleF = new Pitch("F", painter);
111         pitches[9] = pitchMiddleF;
112         Pitch pitchLowF = new Pitch("F,", painter);
113         pitches[10] = pitchLowF;
114         Pitch pitchHighF = new Pitch("f", painter);
115         pitches[11] = pitchHighF;
116         Pitch pitchMiddleG = new Pitch("G", painter);
117         pitches[12] = pitchMiddleG;
118         Pitch pitchLowG = new Pitch("G,", painter);
119         pitches[13] = pitchLowG;
120         Pitch pitchHighG = new Pitch("g", painter);
121         pitches[14] = pitchHighG;
122         Pitch pitchMiddleA = new Pitch("A", painter);
123         pitches[15] = pitchMiddleA;
124         Pitch pitchLowA = new Pitch("A,", painter);
125         pitches[16] = pitchLowA;
126         Pitch pitchHighA = new Pitch("a", painter);
127         pitches[17] = pitchHighA;
128         Pitch pitchMiddleB = new Pitch("B", painter);
129         pitches[18] = pitchMiddleB;
130         Pitch pitchLowB = new Pitch("B,", painter);
131         pitches[19] = pitchLowB;
132         Pitch pitchHighB = new Pitch("b", painter);
133         pitches[20] = pitchHighB;
134         return pitches;
135     }
136 
137     private static Pitch find(String token, Pitch[] pitches) throws Exception {
138         for (int i = 0; i < pitches.length; i = i + 1) {
139             Pitch pitch = pitches[i];
140             if (pitch.abcName().equals(token)) {
141                 return pitch;
142             }
143         }
144         throw new Exception("### PITCH" + token + "NOT FOUND");
145     }
146 
147     private static void display(Pitch[] pitches) {
148         for (int i = 0; i < pitches.length; i = i + 1) {
149             System.out.println(pitches[i].toString());
150         }
151     }
152 
153     private static void playMelody(String input, Pitch[] pitches) throws Exception {
154         Scanner scanner = new Scanner(input);
155         miro.pause();
156         while (scanner.hasNext()) {
157             String token = scanner.next();
158             String pitchName;
159             String duration = "";
160             //To look at in put like C or c
161             if ( token.indexOf (",")< 0){
162                 pitchName= token.substring(0,1);
163                 duration = token.substring(1);
164             }else{//To look at inupt like C,
165                 pitchName= token.substring(0,2);
166                 duration = token.substring(2);
167             }
168             if ( duration.length()== 0 ) {duration = "1";}
169             Pitch pitch = find(pitchName, pitches);
170             pitch.play(duration);
171         }
172     }
173     //INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
174 
175     private static void showErrorMessage(String message) {
176         miro.setVisible(false);
177         JOptionPane.showMessageDialog(null, message);
178     }
179 
180     private static void initialization() {
181         //ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
182         miro = new SPainter("Chromesthesia", 500, 500);
183         miro.setVisible(false);
184         miro.setBrushWidth(7);
185         //ESTABLISH THE CHROMESTITIC PICH CLASS OBJECTS
186         pitches = establishPitches(miro);
187         display(pitches);
188     }
189 
190     private static String getInput() {
191         miro.setVisible(false);
192         String label = "Please enter a melody in ABC notation, AGAIN, or EXIT...   ";
193         String input = JOptionPane.showInputDialog(null, label);
194         miro.setVisible(true);
195         if (input == null) {
196             input = "";
197         }
198         return input;
199     }
200 
201     static private void cleanup() {
202         System.exit(0);
203 
204     }
205 
206 }
207