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

1    package sound; 
2     
3    import sound.UlawCodec; 
4     
5    /** 
6     * Created by IntelliJ IDEA. 
7     * User: dlyon 
8     * Date: Nov 10, 2003 
9     * Time: 6:45:03 PM 
10    * To change this template use Options | File Templates. 
11    */ 
12    
13   public class OscillatorModel { 
14       private Voice v[] = { 
15           new Voice(550), 
16           new Voice(440), 
17           new Voice(330), 
18           new Voice(660) 
19       }; 
20      /** 
21       * All the computation is done here. 
22       * @return  a waveform for a 4 voice synthesizer 
23       */ 
24       public double[] getVoicedWave() { 
25           return add( 
26                   add( 
27                           add(v[0], v[1]), 
28                           v[2].getVoicedWave()), 
29                   v[3].getVoicedWave() 
30           ); 
31       } 
32       public Voice [] getVoices() { 
33           return v; 
34       } 
35    
36       public double[] add(Voice v1, Voice v2) { 
37           double a1[] = v1.getVoicedWave(); 
38           double a2[] = v2.getVoicedWave(); 
39           return add(a1, a2); 
40       } 
41    
42       public double[] add(double a[], double b[]) { 
43           for (int i = 0; i < a.length; i++) 
44               a[i] = a[i] + b[i]; 
45           return a; 
46       } 
47       /** 
48        * play the 4 voice sample. 
49        */ 
50       public void play() { 
51           UlawCodec ulc = new UlawCodec(getVoicedWave()); 
52           ulc.play(); 
53       } 
54       /** 
55        * 
56        * @param _v The voice we want to set. 
57        * @param i a number from 0,1,2 or 3 
58        */ 
59    
60       public void setVoice(Voice _v, int i) { 
61           if (i < 0 || i > 3) { 
62               System.out.println("i out of range, i=" + i); 
63               return; 
64           } 
65           v[i] = _v; 
66       } 
67    
68       public static void main(String args[]) { 
69           (new OscillatorModel()).play(); 
70       } 
71    
72   } 
73