/home/sjenks/NetBeansProjects/CS1/src/chromesthesia0/Chromesthesia.java
  1 
  2 /*
  3  * This porgram interprest melodic lines given in ABC notati nas a chromesthete might.
  4  * 
  5  * A Pitch class will be defined, and will take center stage in processing.
  6  * 
  7  * INterpreting a molody in aBC notaiton will amount ot flashing colored rectangles
  8  * for prescrived durations, while sounding the Pitch! The color of the rectangle
  9  * will correspond to pitch class. The duration will correspond to the duration of the note.
 10  *
 11  * For this first version of the program, the duratin will be held constand at 1 beat.
 12  * 
 13  * Threee sorts of images will appear on the screen, the chromestheic output box,
 14  * a text input box, and an error message box. Simplicity of design is rendered
 15  * by premitting only one box to be on the screen at a time. 
 16  * 
 17  * ABC represents notes in a manner consistend with these examples,
 18  * C, D, E, C D E c d e
 19  * 
 20  * Google ABC music represtnation if you would like to know more about it. 
 21  */
 22 package chromesthesia0;
 23 
 24 import java.util.Scanner;
 25 import javax.swing.JOptionPane;
 26 import javax.swing.SwingUtilities;
 27 import painter.SPainter;
 28 
 29 
 30 /**
 31  *
 32  * @author sjenks
 33  */
 34 public class Chromesthesia {
 35     
 36     //INFRASTRUCTURE FOR THE PORGRAM -- LAUGHING A 'GRAPHICS" THREAD
 37 
 38     /**
 39      * @param args the command line arguments
 40      */
 41     public static void main(String[] args) {
 42         SwingUtilities.invokeLater(new ThreadForGUI());
 43         
 44     }
 45 
 46 
 47 
 48 
 49     private static class ThreadForGUI implements Runnable {
 50         @Override
 51         public void run() {
 52             new Chromesthesia();
 53         }
 54     }
 55     
 56     public Chromesthesia(){
 57         interpreter();
 58     }
 59     
 60         //FEATURE VARIABLES
 61         
 62         private static SPainter miro;
 63         public static Pitch[] pitches;
 64         
 65         // THE INTERPRETER
 66         
 67         public static void interpreter(){
 68             initialization(); //miro and pitches
 69             
 70             while ( true ) { 
 71                 String input = getInput();
 72                 if ( input.equalsIgnoreCase("EXIT")){
 73                     break;
 74                 } else { 
 75                     try { 
 76                         playMelody(input,pitches);
 77                     }catch (Exception ex){
 78                         showErrorMessage(ex.toString());
 79                         
 80                     }
 81                 }
 82             }
 83             
 84             cleanup(); //miro has to go
 85             
 86         }
 87         
 88         //METHODS PRETAINING TO THE CHROMESTHETIC PITCHES
 89         
 90         private static Pitch[] establishPitches (SPainter painter){
 91             Pitch[] pitches = new Pitch [9];
 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             return pitches;
111     }
112         private static Pitch find (String token, Pitch[] pitches) throws Exception {
113             for ( int i = 0; i < pitches.length; i = i + 1 ){ 
114                 Pitch pitch = pitches[i];
115                 if ( pitch.abcName().equals(token)){
116                     return pitch;
117                 }
118             }
119             throw new Exception ("### PITCH" + token + "NOT FOUND");
120         }
121         
122         private static void display (Pitch[] pitches){ 
123             for ( int i = 0; i < pitches.length; i = i+1){
124                 System.out.println(pitches[i].toString());
125             }
126         }
127         private static void playMelody(String input, Pitch[] pitches) throws Exception {
128             Scanner scanner = new Scanner(input);
129             miro.pause();
130             while ( scanner.hasNext()){
131                 String token = scanner.next();
132                 Pitch pitch = find (token,pitches);
133                 pitch.play ("1");
134             }
135     }
136         //INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
137         
138         private static void showErrorMessage(String message) {
139         miro.setVisible (false);
140         JOptionPane.showMessageDialog(null, message);
141     }
142         
143         private static void initialization() {
144         //ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
145         miro = new SPainter ("Chromesthesia", 500,500);
146         miro.setVisible (false);
147         miro.setBrushWidth(7);
148         //ESTABLISH THE CHROMESTITIC PICH CLASS OBJECTS
149         pitches = establishPitches(miro);
150         display (pitches);
151     }
152         private static String getInput() {
153         miro.setVisible (false);
154         String label = "Please enter a melody in ABC notation, or EXIT...   ";
155         String input = JOptionPane.showInputDialog(null,label);
156         miro.setVisible(true);
157         if (input == null ) {input = "";}
158         return input;
159     }
160         static private void cleanup(){
161             System.exit(0);
162             
163         }
164         
165 }
166