/home/ssingh6/NetBeansProjects/CS1/src/chromesthesia0/Chromesthesia.java
  1 /*
  2  * To change this license header, choose License Headers in Project Properties.
  3  * To change this template file, choose Tools | Templates
  4  * and open the template in the editor.
  5  */
  6 package chromesthesia0;
  7 
  8 import java.util.Scanner;
  9 import javax.swing.JOptionPane;
 10 import javax.swing.SwingUtilities;
 11 import painter.SPainter;
 12 
 13 /**
 14  *
 15  * @author ssingh6
 16  */
 17 public class Chromesthesia {
 18 
 19     /**
 20      * @param args the command line arguments
 21      */
 22     public static void main(String[] args) {
 23       SwingUtilities.invokeLater(new ThreadForGUI());
 24       }
 25   
 26      private static class ThreadForGUI implements Runnable {
 27           @Override
 28           public void run() {
 29               new Chromesthesia();
 30           }
 31      }
 32      
 33       public Chromesthesia() {
 34         interpreter();
 35       }
 36       
 37      // FEATURED VARIABLES
 38       
 39      private static SPainter miro;
 40       private static Pitch[] pitches;
 41       private static String melody;
 42      
 43      // THE INTERPRETER
 44       
 45       public static void interpreter() {
 46           initialization(); // miro and pitches
 47           
 48           while ( true ) {
 49               String input = getInput();
 50               if ( input.equalsIgnoreCase("EXIT") ) {
 51                   break;
 52              } else if ( input.equalsIgnoreCase("AGAIN") ) {
 53                   if ( melody == null ) {
 54                      miro.setVisible(false);
 55                       showErrorMessage("### NO PREVIOUS MELODY FOUND");
 56                   } else {
 57                       try {
 58                           playMelody(melody,pitches);
 59                       } catch (Exception ex) {
 60                           showErrorMessage(ex.toString());
 61                       }
 62                   }
 63              } else {
 64                   try {
 65                       melody = input;
 66                       playMelody(melody,pitches);
 67                   } catch (Exception ex) {
 68                       showErrorMessage(ex.toString());
 69                   }
 70              }
 71           }
 72          
 73          cleanup(); // miro has to go
 74           
 75       }
 76       
 77       // METHODS PERTAINING TO THE CHROMESTHETIC PITCHES
 78       
 79       private static Pitch[] establishPitches(SPainter painter) {
 80           Pitch[] pitches = new Pitch[21];
 81           Pitch pitchMiddleC = new Pitch("C",painter);
 82          pitches[0] = pitchMiddleC;
 83          Pitch pitchLowC = new Pitch("C,",painter);
 84         pitches[1] = pitchLowC;
 85          Pitch pitchHighC = new Pitch("c",painter);
 86         pitches[2] = pitchHighC;
 87         Pitch pitchMiddleD = new Pitch("D",painter);
 88         pitches[3] = pitchMiddleD;
 89          Pitch pitchLowD = new Pitch("D,",painter);
 90         pitches[4] = pitchLowD;
 91          Pitch pitchHighD = new Pitch("d",painter);
 92          pitches[5] = pitchHighD;
 93          Pitch pitchMiddleE = new Pitch("E",painter);
 94          pitches[6] = pitchMiddleE;
 95          Pitch pitchLowE = new Pitch("E,",painter);
 96         pitches[7] = pitchLowE;
 97         Pitch pitchHighE = new Pitch("e",painter);
 98          pitches[8] = pitchHighE;
 99          Pitch pitchMiddleF = new Pitch("F",painter);
100         pitches[9] = pitchMiddleF;
101        Pitch pitchLowF = new Pitch("F,",painter);
102          pitches[10] = pitchLowF;
103          Pitch pitchHighF = new Pitch("f",painter);
104        pitches[11] = pitchHighF;
105          Pitch pitchMiddleG = new Pitch("G",painter);
106          pitches[12] = pitchMiddleG;
107          Pitch pitchLowG = new Pitch("G,",painter);
108          pitches[13] = pitchLowG;
109          Pitch pitchHighG = new Pitch("g",painter);
110          pitches[14] = pitchHighG;
111          Pitch pitchMiddleA = new Pitch("A",painter);
112         pitches[15] = pitchMiddleA;
113         Pitch pitchLowA = new Pitch("A,",painter);
114         pitches[16] = pitchLowA;
115          Pitch pitchHighA = new Pitch("a",painter);
116          pitches[17] = pitchHighA;
117          Pitch pitchMiddleB = new Pitch("B",painter);
118          pitches[18] = pitchMiddleB;
119          Pitch pitchLowB = new Pitch("B,",painter);
120          pitches[19] = pitchLowB;
121          Pitch pitchHighB = new Pitch("b",painter);
122          pitches[20] = pitchHighB;
123          return pitches;
124      }
125      
126    private static Pitch find(String token, Pitch[] pitches) throws Exception {
127          for ( int i = 0; i < pitches.length; i = i + 1 ) {
128              Pitch pitch = pitches[i];
129             if ( pitch.abcName().equals(token) ) {
130                  return pitch;
131              }
132          }
133        throw new Exception("### PITCH " + token + " NOT FOUND");
134      }
135     
136      private static void display(Pitch[] pitches) {
137          for ( int i = 0; i < pitches.length; i = i + 1 ) {
138              System.out.println(pitches[i].toString());
139          }
140     }
141     
142      private static void playMelody(String melody, Pitch[] pitches) throws Exception {
143          Scanner scanner = new Scanner(melody);
144          while ( scanner.hasNext() ) {
145             String token = scanner.next();
146              String pitchName;
147              String duration = "";
148              if ( token.indexOf(",") < 0 ) {
149                 pitchName = token.substring(0,1);
150                  duration = token.substring(1);
151             } else {
152                 pitchName = token.substring(0,2);               duration = token.substring(2);
153              }
154              if ( duration.length() == 0 ) { duration = "1"; }
155              Pitch pitch = find(pitchName,pitches);
156              pitch.play(duration);
157          }
158      }
159      
160     // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
161  
162     private static void showErrorMessage(String message) {
163          miro.setVisible(false);
164          JOptionPane.showMessageDialog(null,message);
165      }
166  
167      private static void initialization() {
168         // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
169          miro = new SPainter("Chromesthesia",500,500);
170          miro.setVisible(false);
171          miro.setBrushWidth(7);
172          // ESTABLISH THE CHROMESTHETIC PITCH CLASS OBJECTS
173          pitches = establishPitches(miro);
174          display(pitches);
175      }
176  
177      private static String getInput() {
178          miro.setVisible(false);
179         String label = "Please enter a melody in ABC notation, AGAIN to repeat a previous melody, or EXIT ... ";
180          String input = JOptionPane.showInputDialog(null,label);
181          miro.setVisible(true);
182          miro.pause(100);
183          if ( input == null ) { input = ""; }
184          return input;
185      }
186      
187     private static void cleanup() {
188          System.exit(0);
189     }
190 }
191 
192  
193