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

1    package sound; 
2     
3    import gui.ClosableJFrame; 
4    import gui.run.RunButton; 
5    import gui.run.RunSlider; 
6    import math.transforms.FFT1d; 
7     
8    import javax.swing.*; 
9    import java.awt.*; 
10    
11   // For the final which is due next week... 
12   // Use the 4 voice synthesizer to generated a wave form. 
13   // Play the waveform through the speakers. 
14   // Plot the waveform using the oscopePanel. 
15   // Show a panel with the Power Spectrial Density of the waveform. 
16   // Come to class. 
17   // Set up your final. 
18   // Demo to the class. 
19   // Hand in the printout, with floppy. 
20   // Go home! 
21   // Just like what I did just now, but use 4 voices. Each 
22   // is controlled with 7 sliders. You add the output of the 4 
23   // voices togther. 
24   /** 
25    * Created by 
26    * User: lyon 
27    * Date: Nov 23, 2003 
28    * Time: 1:35:35 PM 
29    * 
30    */ 
31    
32   /** 
33    * Created by IntelliJ IDEA. 
34    * User: dlyon 
35    * Date: Nov 17, 2003 
36    * Time: 7:44:50 PM 
37    * To change this template use Options | File Templates. 
38    */ 
39    
40   //Sliders on the left--Oscope on the right 
41   //Play button in the south 
42   //single voice to play 
43    
44    
45   public class VoicePanel extends JPanel { 
46       private int frequency = 440; 
47       private double harmonics[] = { 
48           1, 0, 0, 0, 0, 0, 0 
49       }; 
50       OscopePanel op = new OscopePanel(); 
51       FFT1d fft = new FFT1d(); 
52       private Voice v = new Voice(frequency); 
53       Oscillator o = new Oscillator(440, 80); 
54       Audio a = new Audio(o.getSineWave()); 
55       OscopePanel spectrumAnalyzer = 
56               FFT1d.getSpectrumPanel(o.getSineWave(), "PSD"); 
57    
58       ClosableJFrame cf = new ClosableJFrame("OscilatorFrame"); 
59       JLabel frequencyLabel = new JLabel("400 Hz"); 
60    
61       public Voice getVoice() { 
62           return v; 
63       } 
64    
65       public int getFrequency() { 
66           return frequency; 
67       } 
68    
69       public static void main(String args[]) { 
70           //VoicePanel vp = new VoicePanel(); 
71           //vp.initTestFrame(); 
72           testGetVoicePanels(); 
73       } 
74    
75       private RunSlider getHarmonicSlider(final int i) { 
76           RunSlider rs = new RunSlider(RunSlider.HORIZONTAL) { 
77               public void run() { 
78                   harmonics[i] = getValue() / 100.0; 
79               } 
80           }; 
81           rs.setValue(0); 
82           return rs; 
83       } 
84    
85    
86       private RunSlider getFrequencySlider() { 
87           return new RunSlider(RunSlider.HORIZONTAL) { 
88               public void run() { 
89                   setFrequency(10 * getValue()); 
90               } 
91           }; 
92       } 
93    
94       public RunButton getPlayButton() { 
95           return new RunButton("play") { 
96               public void run() { 
97                   play(); 
98               } 
99           }; 
100      } 
101   
102      public VoicePanel() { 
103          addComponents(); 
104      } 
105   
106   
107      private void addComponents() { 
108          setLayout(new GridLayout(0, 1)); 
109          add(getLabeledFrequencySlider()); 
110          for (int i = 0; i < 7; i++) 
111              add(getLabeledSlider(i)); 
112          add(getPlayButton()); 
113      } 
114   
115      private JPanel getLabeledFrequencySlider() { 
116          JPanel jp = new JPanel(); 
117          jp.setLayout(new BorderLayout()); 
118          jp.add(frequencyLabel, BorderLayout.WEST); 
119          jp.add(getFrequencySlider(), BorderLayout.CENTER); 
120          return jp; 
121      } 
122   
123      private JPanel getLabeledSlider(int i) { 
124          JPanel jp = new JPanel(); 
125          jp.setLayout(new BorderLayout()); 
126          jp.add(new JLabel("s" + i), BorderLayout.WEST); 
127          jp.add(getHarmonicSlider(i), BorderLayout.CENTER); 
128          return jp; 
129      } 
130   
131      public static void log(double a[]) { 
132          for (int i = 0; i < a.length; i++) 
133              a[i] = Math.log(a[i]); 
134      } 
135   
136      public void play() { 
137          v.setFrequency(frequency); 
138          v.setHarmonic(harmonics); 
139          v.play(); 
140          op.setData(v.getVoicedWave()); 
141          double[] psd = FFT1d.getPsd(v.getVoicedWave()); 
142          spectrumAnalyzer.setData( 
143                  psd); 
144          op.repaint(); 
145          spectrumAnalyzer.repaint(); 
146      } 
147   
148      public void setFrequency(int f) { 
149          frequency = f; 
150          frequencyLabel.setText(f + " Hz"); 
151      } 
152   
153      public static void testGetVoicePanel() { 
154          ClosableJFrame cf = new ClosableJFrame(); 
155          Voices voices = new Voices(); 
156          Container c = cf.getContentPane(); 
157          c.setLayout(new BorderLayout()); 
158          c.add(getVoicePanel(voices), BorderLayout.CENTER); 
159          c.setSize(400, 400); 
160          cf.show(); 
161      } 
162   
163      public static void testGetVoicePanels() { 
164          ClosableJFrame cf = new ClosableJFrame(); 
165          Container c = cf.getContentPane(); 
166          c.setLayout(new BorderLayout()); 
167          c.add(getVoicePanels(), BorderLayout.CENTER); 
168          c.setSize(400, 400); 
169          cf.show(); 
170      } 
171        public static JPanel getVoicePanel(Voices voices) { 
172          JPanel c = new JPanel(); 
173          c.setLayout(new GridLayout(1, 0)); 
174          VoicePanel vp = new VoicePanel(); 
175          Voice v = vp.getVoice(); 
176          voices.add(v); 
177          OscopePanel op = vp.op; 
178          Oscillator o = vp.o; 
179          OscopePanel spectrumAnalyzer = 
180                  vp.spectrumAnalyzer; 
181          c.add(vp); 
182          c.add(op); 
183          c.add(spectrumAnalyzer); 
184          return c; 
185      } 
186      public static JPanel getCompositeVoicePanel( 
187              final Voices voices) { 
188          JPanel jp = new JPanel(); 
189          jp.setLayout(new GridLayout(1, 0)); 
190          OscopePanel op = new OscopePanel( 
191                  voices.getVoicedWaveForm()); 
192          OscopePanel spectrumAnalyzer = 
193                  new OscopePanel(voices.getVoicedWaveForm()); 
194          jp.add(op); 
195          jp.add(spectrumAnalyzer); 
196          jp.add(new VoicesRunButton(voices,op,spectrumAnalyzer)); 
197          return jp; 
198      } 
199   
200      public static JPanel getVoicePanels() { 
201          Voices voices = new Voices(); 
202          JTabbedPane jtp = new JTabbedPane(); 
203          JPanel v1 = getVoicePanel(voices); 
204          jtp.add("voice1", v1); 
205          JPanel v2 = getVoicePanel(voices); 
206          jtp.add("voice2", v2); 
207          JPanel v3 = getVoicePanel(voices); 
208          jtp.add("voice3", v3); 
209          JPanel v4 = getVoicePanel(voices); 
210          jtp.add("voice4", v4); 
211          jtp.add("composite voice", 
212                  getCompositeVoicePanel(voices)); 
213          JPanel jp = new JPanel(); 
214          jp.setLayout(new BorderLayout()); 
215          jp.add(jtp, BorderLayout.CENTER); 
216          return jp; 
217      } 
218   
219      public class VoicePlusPanel { 
220          private JPanel jpanel; 
221          private Voice voice; 
222   
223          VoicePlusPanel(JPanel _jp, Voice _v) { 
224              jpanel = _jp; 
225              voice = _v; 
226          } 
227   
228          public JPanel getJpanel() { 
229              return jpanel; 
230          } 
231   
232          public Voice getVoice() { 
233              return voice; 
234          } 
235      } 
236   
237   
238   
239      private void initTestFrame() { 
240          Container c = cf.getContentPane(); 
241          c.setLayout(new GridLayout(1, 0)); 
242          c.add(this); 
243          c.add(op); 
244          c.add(spectrumAnalyzer); 
245          cf.setSize(600, 400); 
246          cf.setVisible(true); 
247      } 
248   
249      private static class VoicesRunButton extends RunButton { 
250          private final Voices voices; 
251          private final OscopePanel op; 
252          private final OscopePanel psd; 
253   
254          public VoicesRunButton(Voices voices, OscopePanel op, 
255                                 OscopePanel psd) { 
256              super("Play"); 
257              this.voices = voices; 
258              this.op = op; 
259              this.psd = psd; 
260          } 
261   
262          public void run(){ 
263              voices.play(); 
264              op.setData(voices.getVoicedWaveForm()); 
265              double psdVals[] = 
266                      FFT1d.getPsd(voices.getVoicedWaveForm()); 
267              psd.setData(psdVals); 
268              psd.repaint(); 
269              op.repaint(); 
270          } 
271      } 
272  } 
273   
274   
275