Chromesthesia.java
1    /* 
2    * This program interprets melodic lines given in ABC notation as a 
3    * chromesthete might. 
4    * 
5    *A pitch class will be defined and will take center stage in the processing 
6    * 
7    * interpreting a melody in abc notation will amount to flashing 
8    * clolored rectanglegs for prescribed durations, while sounding the 
9    *  pitch! the color of the rectangle will correspond to pitch class. The duration will 
10   * correspond the the duration of the note. 
11   * 
12   * for this first version of the program the duration will be held at constant at 1 beat. 
13   * 
14   * three sorts of images will appear oon the screen, the  chromesthetic 
15   *  output, a text input box, and an error message box. Simplicitly 
16   * of design is rendered by permiting only one box to be on the screen at a time. 
17   * 
18   * ABC represents notes in manner consistent with these examples: 
19   * C, D, E, C D E c d  e 
20   * 
21   * Google ABC music representation if you want to know morea bout it. 
22    */
23   
24   package chromesthesia0;
25   
26   import painter.SPainter;
27   
28   import javax.swing.*;
29   import java.util.Scanner;
30   
31   public class Chromesthesia {
32   
33       //Infrustructure for the program -- LAUNCHING A "GRAPHICS" THREAD
34   
35       public static void main(String[] args) {
36           SwingUtilities.invokeLater(new ThreadForGUI());
37       }
38   
39       private static class ThreadForGUI implements Runnable {
40           @Override
41           public void run() {
42               new Chromesthesia();
43           }
44       }
45   
46       public Chromesthesia() {
47           interpreter();
48       }
49   
50       //FEATURED VARIABLES
51   
52       private static SPainter miro;
53       private static Pitch[] pitches;
54   
55       //THE INTERPRETER
56   
57       public static void interpreter() {
58           initialization(); //miro and pitches
59   
60           while (true) {
61               String input = getInput();
62               if (input.equalsIgnoreCase("EXIT")){
63                   break;
64               } else {
65                   try {
66                       playMelody(input, pitches);
67                   } catch ( Exception ex) {
68                       showErrorMessage(ex.toString());
69                   }
70               }
71           }
72   
73           cleanup(); // miro has to go
74       }
75   
76       //METHODS PRETAINING TO THE CHROMESTHETIC PITCHES
77   
78       private static Pitch[] establishPitches(SPainter painter) {
79           Pitch[] pitches = new Pitch[9];
80           Pitch pitchMiddleC = new Pitch("C",painter);
81           pitches[0] = pitchMiddleC;
82           Pitch pitchLowC = new Pitch("C,",painter);
83           pitches[1] = pitchLowC;
84           Pitch pitchHighC = new Pitch("c",painter);
85           pitches[2] = pitchHighC;
86           Pitch pitchMiddleD = new Pitch("D",painter);
87           pitches[3] = pitchMiddleD;
88           Pitch pitchLowD = new Pitch("D,",painter);
89           pitches[4] = pitchLowD;
90           Pitch pitchHighD = new Pitch("d",painter);
91           pitches[5] = pitchHighD;
92           Pitch pitchMiddleE = new Pitch("E",painter);
93           pitches[6] = pitchMiddleE;
94           Pitch pitchLowE = new Pitch("E,",painter);
95           pitches[7] = pitchLowE;
96           Pitch pitchHighE = new Pitch("e",painter);
97           pitches[8] = pitchHighE;
98           return pitches;
99       }
100  
101      private static Pitch find(String token, Pitch[] pitches) throws Exception {
102          for (int i = 0; i<pitches.length; i = i+1) {
103              Pitch pitch = pitches[i];
104              if ( pitch.abcName().equals(token) ) {
105                  return pitch;
106              }
107          }
108          throw new Exception("### PITCH " + token + " NOT FOUND");
109      }
110  
111      private static void display(Pitch[] pitches) {
112          for ( int i = 0; i< pitches.length; i = i+1 ) {
113              System.out.println(pitches[i].toString());
114          }
115      }
116  
117      private static void playMelody(String input, Pitch[] pitches) throws Exception {
118          Scanner scanner = new Scanner(input);
119          while ( scanner.hasNext() ) {
120              String token = scanner.next();
121              Pitch pitch = find(token,pitches);
122              pitch.play("1");
123          }
124      }
125  
126      //INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
127  
128       static private void showErrorMessage(String message) {
129          miro.setVisible(false);
130          JOptionPane.showMessageDialog(null, message);
131       }
132  
133       private static void initialization() {
134          //ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
135           miro = new SPainter("Chromesthesia", 500,500);
136           miro.setVisible(false);
137           miro.setBrushWidth(7);
138           //ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS
139           pitches = establishPitches(miro);
140           display(pitches);
141       }
142  
143       private static String getInput() {
144          miro.setVisible(false);
145          String label = "PLease enter a melody in ABC notation, or EXIT ...    ";
146          String input = JOptionPane.showInputDialog(null,label);
147          miro.setVisible(true);
148          if ( input == null) { input = ""; }
149          return input;
150       }
151  
152       static private void cleanup() {
153          System.exit(0);
154       }
155      }
156  
157