/Users/lyon/j4p/src/sound/SynthesizerController.java

1    package sound; 
2     
3    import gui.run.RunSlider; 
4    import gui.run.RunRadio; 
5    import gui.run.RunButton; 
6     
7    import javax.swing.*; 
8    import java.awt.*; 
9    import java.awt.event.WindowAdapter; 
10   import java.awt.event.WindowEvent; 
11    
12   public class SynthesizerController extends JFrame { 
13       OscillatorModel om = new OscillatorModel(); 
14       public SynthesizerController() { 
15           Container synthesizerFrame = initPanels(); 
16           handleCloseEvent(); 
17           setSize(700, 400); 
18           show(); 
19       } 
20    
21       private void handleCloseEvent() { 
22           addWindowListener( 
23                   new WindowAdapter() { 
24                       public void windowClosing(WindowEvent e) { 
25                           System.exit(0); 
26                       } 
27                   } 
28           ); 
29       } 
30    
31       private int runRadioState = 1; 
32    
33       private JPanel getRadioButtonPanel() { 
34           JPanel jp = new JPanel(); 
35           jp.setLayout(new GridLayout(1, 0)); 
36           ButtonGroup bg = new ButtonGroup(); 
37           addRadioButton(bg, jp, "a", 1); 
38           addRadioButton(bg, jp, "b", 2); 
39           addRadioButton(bg, jp, "c", 3); 
40           addRadioButton(bg, jp, "d", 4); 
41           return jp; 
42       } 
43    
44       private void addRadioButton(ButtonGroup bg, 
45                                   JPanel jp, 
46                                   String label, 
47                                   final int state) { 
48           RunRadio rr2 = new RunRadio(label) { 
49               public void run() { 
50                   runRadioState = state; 
51               } 
52           }; 
53           bg.add(rr2); 
54           jp.add(rr2); 
55       } 
56    
57       private Container initPanels() { 
58           Container c = getContentPane(); 
59           c.setLayout(new FlowLayout()); 
60           c.add(getHarmonicSliderPanel(harmonicAmplitudes)); 
61           c.add(getFrequencySliderPanel()); 
62           c.add(getVolumeSliderPanel()); 
63           c.add(getRadioButtonPanel()); 
64           c.add(getButtonControllerPanel()); 
65           c.add(getWaveformPanel()); 
66           return c; 
67       } 
68       private JPanel getWaveformPanel() { 
69           JPanel jp = new JPanel(); 
70           jp.setLayout(new GridLayout(1,0)); 
71           Voice v[] = om.getVoices(); 
72           for (int i=0; i < v.length; i++) { 
73               OscopePanel op = new OscopePanel( 
74    
75                                           v[i].getVoicedWave()); 
76               op.setLabelsVisible(false); 
77               jp.add(op); 
78           } 
79           OscopePanel op = new OscopePanel(om.getVoicedWave()); 
80           op.setLabelsVisible(false); 
81           jp.add(op); 
82           return jp; 
83       } 
84       private void play() { 
85           om.play(); 
86       } 
87       private void graph() { 
88           // do the view; 
89       } 
90       private JPanel getButtonControllerPanel() { 
91           JPanel jp = new JPanel(); 
92           jp.setLayout(new FlowLayout()); 
93           jp.add(new RunButton("play"){ 
94               public void run() { 
95                 play(); 
96               } 
97           }); 
98           jp.add(new RunButton("graph"){ 
99               public void run() { 
100                graph(); 
101              } 
102          }); 
103          return jp; 
104      } 
105   
106      private double frequencies[] = new double[4]; 
107   
108      private JPanel getFrequencySliderPanel() { 
109          JPanel controlPanel = new JPanel(new GridLayout(0, 1)); 
110          JLabel label = new JLabel("Frequency", JLabel.CENTER); 
111          controlPanel.add(label); 
112          for (int i = 0; i < 4; i++) { 
113              setUpFrequencySlider(i, controlPanel); 
114          } 
115          return controlPanel; 
116      } 
117   
118      private void setUpFrequencySlider(final int frequencyNumber, 
119                                        JPanel controlPanel) { 
120          RunSlider rs = new RunSlider(SwingConstants.HORIZONTAL) { 
121              public void run() { 
122                  System.out.println("Frequency Slider #" + 
123                          frequencyNumber + " = " + getValue()); 
124                  frequencies[frequencyNumber] = getValue(); 
125              } 
126          }; 
127          String label1 = "#" + frequencyNumber; 
128          rs.setName("A"); 
129          rs.setToolTipText(label1); 
130          rs.setToolTipText(label1); 
131          controlPanel.add(rs); 
132      } 
133   
134      private JPanel getVolumeSliderPanel() { 
135          JPanel controlPanel = new JPanel(new GridLayout(0, 1)); 
136          JLabel label = new JLabel("Volume", JLabel.CENTER); 
137          controlPanel.add(label); 
138          RunSlider rs = new RunSlider(SwingConstants.HORIZONTAL) { 
139              public void run() { 
140                  System.out.println("Volume = " + getValue()); 
141              } 
142          }; 
143          controlPanel.add(rs); 
144   
145          return controlPanel; 
146      } 
147   
148      private static JPanel getHarmonicSliderPanel(double[] harmonicAmplitudes1) { 
149          JPanel labeledPanel = new JPanel(new BorderLayout()); 
150          JPanel controlPanel = new JPanel(new GridLayout(1, 0)); 
151          JLabel label = new JLabel("Harmonics", JLabel.CENTER); 
152          label.setAlignmentY(JLabel.CENTER_ALIGNMENT); 
153          labeledPanel.add(label, BorderLayout.NORTH); 
154          labeledPanel.add(controlPanel, BorderLayout.CENTER); 
155          for (int i = 0; i < 7; i++) 
156              addSlider(harmonicAmplitudes1, controlPanel, SwingConstants.VERTICAL, i); 
157          return labeledPanel; 
158      } 
159   
160      private double harmonicAmplitudes[] = new double[7]; 
161   
162      public static void addSlider(final double[] harmonicAmplitudes1, 
163                                   Container c, 
164                                   int orientation, 
165                                   final int harmonicNumber) { 
166          RunSlider rs = new RunSlider(orientation) { 
167              public void run() { 
168                  System.out.println( 
169                          "Harmonic Slider #" + 
170                          harmonicNumber + " = " + getValue()); 
171                  harmonicAmplitudes1[harmonicNumber] = getValue(); 
172              } 
173          }; 
174          String label = "harmonic:" + harmonicNumber; 
175          rs.setToolTipText(label); 
176          rs.setToolTipText(label); 
177          c.add(rs); 
178      } 
179   
180   
181      public static void main(String args[]) { 
182          SynthesizerController app = new SynthesizerController(); 
183   
184   
185      } 
186  } 
187   
188