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

1    /** 
2     * Created by IntelliJ IDEA. 
3     * User: dlyon 
4     * Date: Nov 17, 2003 
5     * Time: 7:44:50 PM 
6     * To change this template use Options | File Templates. 
7     */ 
8    package sound; 
9     
10    
11   import gui.ClosableJFrame; 
12   import gui.run.RunButton; 
13   import gui.run.RunSlider; 
14    
15   import javax.swing.*; 
16   import java.awt.*; 
17    
18   public class OscillatorPanel extends JPanel { 
19       private int frequency = 440; 
20       private int duration = 1000; 
21       private Oscillator os = new Oscillator(frequency, duration); 
22       OscopePanel osp = new OscopePanel(); 
23    
24       public int getFrequency() { 
25           return frequency; 
26       } 
27    
28       public static void main(String args[]) { 
29           ClosableJFrame cf = new ClosableJFrame("OscilatorFrame"); 
30           Container c = cf.getContentPane(); 
31           c.setLayout(new GridLayout(1,0)); 
32           c.add(new OscillatorPanel()); 
33           c.add(new OscillatorPanel()); 
34           c.add(new OscillatorPanel()); 
35           c.add(new OscillatorPanel()); 
36           cf.setSize(550, 400); 
37           cf.show(); 
38    
39       } 
40    
41       private void setDuration(int d) { 
42           duration = d; 
43       } 
44    
45       private RunSlider getFrequencySlider() { 
46           return new RunSlider(RunSlider.HORIZONTAL) { 
47               public void run() { 
48                   setFrequency(10 * getValue()); 
49                   playSineWave(); 
50               } 
51           }; 
52       } 
53    
54       private RunSlider getDurationSlider() { 
55           return new RunSlider(RunSlider.HORIZONTAL) { 
56               public void run() { 
57                   setDuration(100 * getValue()); 
58               } 
59           }; 
60       } 
61    
62       public JPanel getButtonControlPanel() { 
63           JPanel jp = new JPanel(); 
64           jp.setLayout(new FlowLayout()); 
65           jp.add(getPlaySawWaveButton()); 
66           jp.add(getPlaySquareWaveButton()); 
67           jp.add(getPlayTriangleWaveButton()); 
68           jp.add(getPlaySineWaveButton()); 
69           return jp; 
70       } 
71    
72       public RunButton getPlayTriangleWaveButton() { 
73           return new RunButton("triangle") { 
74               public void run() { 
75                   playTriangleWave(); 
76               } 
77           }; 
78       } 
79    
80       public RunButton getPlaySineWaveButton() { 
81           return new RunButton("sine") { 
82               public void run() { 
83                   playSineWave(); 
84               } 
85           }; 
86       } 
87    
88       public RunButton getPlaySawWaveButton() { 
89           return new RunButton("saw") { 
90               public void run() { 
91                   playSawWave(); 
92               } 
93           }; 
94       } 
95    
96       public RunButton getPlaySquareWaveButton() { 
97           return new RunButton("square") { 
98               public void run() { 
99                   playSquareWave(); 
100              } 
101          }; 
102      } 
103   
104      public OscillatorPanel() { 
105          setLayout(new BorderLayout()); 
106          add(getControlPanel(), BorderLayout.NORTH); 
107   
108          add(getOscopePanel(), BorderLayout.CENTER); 
109          add(getButtonPanel(), BorderLayout.SOUTH); 
110      } 
111     private JPanel getOscopePanel() { 
112          JPanel jp = new JPanel(); 
113          jp.setLayout(new GridLayout()); 
114          jp.add(osp); 
115          return jp; 
116      } 
117      private JPanel getControlPanel() { 
118          JPanel jp = new JPanel(); 
119          jp.setLayout(new GridLayout(2,0)); 
120          jp.add(getFrequencySlider()); 
121          jp.add(getDurationSlider()); 
122          return jp; 
123      } 
124   
125      private JPanel getButtonPanel() { 
126          JPanel jp = new JPanel(); 
127          jp.setLayout(new GridLayout(2,0)); 
128          jp.add(getPlaySawWaveButton()); 
129          jp.add(getPlaySquareWaveButton()); 
130          jp.add(getPlayTriangleWaveButton()); 
131          jp.add(getPlaySineWaveButton()); 
132          return jp; 
133      } 
134   
135   
136      public void playSquareWave() { 
137          os = new Oscillator(frequency, duration); 
138          UlawCodec ulc = new UlawCodec(os.getSquareWave()); 
139          playAndUpdateDisplay(ulc); 
140      } 
141   
142      public void playSawWave() { 
143          os = new Oscillator(frequency, duration); 
144          UlawCodec ulc = new UlawCodec(os.getSawWave()); 
145          playAndUpdateDisplay(ulc); 
146      } 
147   
148      public void playTriangleWave() { 
149          os = new Oscillator(frequency, duration); 
150          UlawCodec ulc = new UlawCodec(os.getTriangleWave()); 
151          playAndUpdateDisplay(ulc); 
152      } 
153   
154      public void playSineWave() { 
155          os = new Oscillator(frequency, duration); 
156          UlawCodec ulc = new UlawCodec(os.getSineWave()); 
157          playAndUpdateDisplay(ulc); 
158      } 
159   
160      private void playAndUpdateDisplay(UlawCodec ulc) { 
161          osp.setData(ulc.getDoubleArray()); 
162          osp.repaint(500); 
163          ulc.play(); 
164      } 
165   
166      public void setFrequency(int f) { 
167          frequency = f; 
168      } 
169   
170  } 
171