/home/sjenks/NetBeansProjects/CS1/src/chromesthesia1/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 second  version of the program, the duratin will be held constand at 1 beat.
 11  * 
 12  * Threee 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 chromesthesia1;
 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 
 63         while (true) {
 64             String input = getInput();
 65             if (input.equalsIgnoreCase("EXIT")) {
 66                 break;
 67             } else {
 68                 try {
 69                     playMelody(input, pitches);
 70                 } catch (Exception ex) {
 71                     showErrorMessage(ex.toString());
 72 
 73                 }
 74             }
 75         }
 76 
 77         cleanup(); //miro has to go
 78 
 79     }
 80 
 81     //METHODS PRETAINING TO THE CHROMESTHETIC PITCHES
 82     private static Pitch[] establishPitches(SPainter painter) {
 83         Pitch[] pitches = new Pitch[21];
 84         Pitch pitchMiddleC = new Pitch("C", painter);
 85         pitches[0] = pitchMiddleC;
 86         Pitch pitchLowC = new Pitch("C,", painter);
 87         pitches[1] = pitchLowC;
 88         Pitch pitchHighC = new Pitch("c", painter);
 89         pitches[2] = pitchHighC;
 90         Pitch pitchMiddleD = new Pitch("D", painter);
 91         pitches[3] = pitchMiddleD;
 92         Pitch pitchLowD = new Pitch("D,", painter);
 93         pitches[4] = pitchLowD;
 94         Pitch pitchHighD = new Pitch("d", painter);
 95         pitches[5] = pitchHighD;
 96         Pitch pitchMiddleE = new Pitch("E", painter);
 97         pitches[6] = pitchMiddleE;
 98         Pitch pitchLowE = new Pitch("E,", painter);
 99         pitches[7] = pitchLowE;
100         Pitch pitchHighE = new Pitch("e", painter);
101         pitches[8] = pitchHighE;
102         Pitch pitchMiddleF = new Pitch("F", painter);
103         pitches[9] = pitchMiddleF;
104         Pitch pitchLowF = new Pitch("F,", painter);
105         pitches[10] = pitchLowF;
106         Pitch pitchHighF = new Pitch("f", painter);
107         pitches[11] = pitchHighF;
108         Pitch pitchMiddleG = new Pitch("G", painter);
109         pitches[12] = pitchMiddleG;
110         Pitch pitchLowG = new Pitch("G,", painter);
111         pitches[13] = pitchLowG;
112         Pitch pitchHighG = new Pitch("g", painter);
113         pitches[14] = pitchHighG;
114         Pitch pitchMiddleA = new Pitch("A", painter);
115         pitches[15] = pitchMiddleA;
116         Pitch pitchLowA = new Pitch("A,", painter);
117         pitches[16] = pitchLowA;
118         Pitch pitchHighA = new Pitch("a", painter);
119         pitches[17] = pitchHighA;
120         Pitch pitchMiddleB = new Pitch("B", painter);
121         pitches[18] = pitchMiddleB;
122         Pitch pitchLowB = new Pitch("B,", painter);
123         pitches[19] = pitchLowB;
124         Pitch pitchHighB = new Pitch("b", painter);
125         pitches[20] = pitchHighB;
126         return pitches;
127     }
128 
129     private static Pitch find(String token, Pitch[] pitches) throws Exception {
130         for (int i = 0; i < pitches.length; i = i + 1) {
131             Pitch pitch = pitches[i];
132             if (pitch.abcName().equals(token)) {
133                 return pitch;
134             }
135         }
136         throw new Exception("### PITCH" + token + "NOT FOUND");
137     }
138 
139     private static void display(Pitch[] pitches) {
140         for (int i = 0; i < pitches.length; i = i + 1) {
141             System.out.println(pitches[i].toString());
142         }
143     }
144 
145     private static void playMelody(String input, Pitch[] pitches) throws Exception {
146         Scanner scanner = new Scanner(input);
147         miro.pause();
148         while (scanner.hasNext()) {
149             String token = scanner.next();
150             Pitch pitch = find(token, pitches);
151             pitch.play("1");
152         }
153     }
154     //INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
155 
156     private static void showErrorMessage(String message) {
157         miro.setVisible(false);
158         JOptionPane.showMessageDialog(null, message);
159     }
160 
161     private static void initialization() {
162         //ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
163         miro = new SPainter("Chromesthesia", 500, 500);
164         miro.setVisible(false);
165         miro.setBrushWidth(7);
166         //ESTABLISH THE CHROMESTITIC PICH CLASS OBJECTS
167         pitches = establishPitches(miro);
168         display(pitches);
169     }
170 
171     private static String getInput() {
172         miro.setVisible(false);
173         String label = "Please enter a melody in ABC notation, or EXIT...   ";
174         String input = JOptionPane.showInputDialog(null, label);
175         miro.setVisible(true);
176         if (input == null) {
177             input = "";
178         }
179         return input;
180     }
181 
182     static private void cleanup() {
183         System.exit(0);
184 
185     }
186 
187 }
188