Pitch.java
1    /* 
2     * This program will have specific color to the lab manual and have 
3     * different notes like 1/2 or 2 or 2/3 and 3 
4     * 
5     * This program interprets melodic lines given in ABC notation as a 
6     * chromesthete might. 
7     * 
8     * A Pitch class will be defined, and will take center stage in the 
9     * processing. 
10    * 
11    * Interpreting a melody in ABC notation will amount to flashing 
12    * colored rectangles for prescribed durations, while sounding 
13    * the pitch! The color of the rectangle will correspond to pitch 
14    * class. The duration will correspond to the duration of the note. 
15    * 
16    * For this first version of the program, the duration will be held 
17    * constant at 1 beat. 
18    * 
19    * Three sorts of images will appear on the screen, the chromesthetic 
20    * output box, a text input box, and an error message box. Simplicity 
21    * of design is rendered by permitting only one box to be on the screen 
22    * at a time. 
23    * 
24    * ABC represents notes in a manner consistent with these examples: 
25    * C, D, E, C D E c d e 
26    * 
27    * Google ABC music representation if you would like to know more about it. 
28    */
29   package chromesthesia2;
30   import java.awt.Color;
31   import note.SNote;
32   import painter.SPainter;
33   import shapes.SRectangle;
34   
35   
36   
37   public class Pitch {
38       // INSTANCE VARIABLES
39       private String abcName;
40       private SPainter painter;
41       private SRectangle box;
42       private SNote note;
43       private Color color;
44   
45       public Pitch(String abcName, SPainter painter) {
46           this.abcName = abcName;
47           this.painter = painter;
48           this.box = new SRectangle(painter.painterHeight - 50, painter.painterWidth - 50);
49           this.note = createNoteForThisPitch(abcName);
50           this.color = getPitchClassColor(abcName.substring(0, 1).toUpperCase());
51       }
52   
53       public String toString() {
54           return "[ " + abcName + " | " + note.degree() + " | " + color + " ]";
55       }
56   
57       public String abcName() {
58           return abcName;
59       }
60   
61       private SNote createNoteForThisPitch(String abcPitchClassName) {
62           SNote note = new SNote();
63           if (abcPitchClassName.equals("C")) {
64   // nothing to do
65           } else if (abcPitchClassName.equals("C,")) {
66               note.lp(7);
67           } else if (abcPitchClassName.equals("c")) {
68               note.rp(7);
69           } else if (abcPitchClassName.equals("D")) {
70               note.rp(1);
71           } else if (abcPitchClassName.equals("D,")) {
72               note.lp(6);
73           } else if (abcPitchClassName.equals("d")) {
74               note.rp(8);
75           } else if (abcPitchClassName.equals("E")) {
76               note.rp(2);
77           } else if (abcPitchClassName.equals("E,")) {
78               note.lp(5);
79           } else if (abcPitchClassName.equals("e")) {
80               note.rp(9);
81           } else if (abcPitchClassName.equals("F")) {
82               note.rp(3);
83           } else if (abcPitchClassName.equals("F,")) {
84               note.lp(4);
85           } else if (abcPitchClassName.equals("f")) {
86               note.rp(10);
87           } else if (abcPitchClassName.equals("G")) {
88               note.rp(4);
89           } else if (abcPitchClassName.equals("G,")) {
90               note.lp(3);
91           } else if (abcPitchClassName.equals("g")) {
92               note.rp(11);
93           } else if (abcPitchClassName.equals("A")) {
94               note.rp(5);
95           } else if (abcPitchClassName.equals("A,")) {
96               note.lp(2);
97           } else if (abcPitchClassName.equals("a")) {
98               note.rp(12);
99           } else if (abcPitchClassName.equals("B")) {
100              note.rp(6);
101          } else if (abcPitchClassName.equals("B,")) {
102              note.lp(1);
103          } else if (abcPitchClassName.equals("b")) {
104              note.rp(13);
105          }
106          return note;
107      }
108  
109      private Color getPitchClassColor(String letter) {
110          if (letter.equals("A")) {
111              return new Color(0, 0, 225);
112          } else if (letter.equals("B")) {
113              return new Color(0, 255, 0);
114          } else if (letter.equals("C")) {
115              return new Color(127, 0, 127);
116          } else if (letter.equals("D")) {
117              return new Color(255, 225, 0);
118          } else if (letter.equals("E")) {
119              return new Color(225, 0, 0);
120          } else if (letter.equals("F")) {
121              return new Color(225, 127, 0);
122          } else if (letter.equals("G")) {
123              return new Color(0, 255, 225);
124          } else {
125              return Color.BLACK;
126          }
127      }
128  
129      public void play(String d) {
130          painter.setColor(color);
131          painter.paint(box);
132          painter.setColor(randomColor());
133          painter.draw(box);
134          if (d.equals("1")) {
135              note.play();
136          } else if (d.equals("2")) {
137              note.x2();
138              note.play();
139              note.s2();
140          } else if (d.equals("1/2")) {
141              note.s2();
142              note.play();
143              note.x2();
144          }else if (d.equals("3")) {
145              note.x3();
146              note.play();
147              note.s3();
148          }else if (d.equals("1/3")) {
149              note.s3();
150              note.play();
151              note.x3();
152          }else if (d.equals("2/3")){
153              note.x2();
154              note.s3();
155              note.play();
156              note.s2();
157              note.x3();
158          }
159      }
160      private static Color randomColor() {
161          int rv = (int)(Math.random()*256);
162          int gv = (int)(Math.random()*256);
163          int bv = (int)(Math.random()*256);
164          return new Color(rv,gv,bv);
165      }
166  }