/Users/lyon/j4p/src/sound/Voice.java
|
1 package sound;
2
3
4 /**
5 * Created by IntelliJ IDEA.
6 * User: dlyon
7 * Date: Nov 10, 2003
8 * Time: 6:46:12 PM
9 * To change this template use Options | File Templates.
10 */
11
12 public class Voice {
13 private double harmonic[] = {
14 // 1,2,3,4,5,6,7 harmonics
15 // range from 0..1, each
16 1, 0, 1, 0, 1, 0, 1
17 };
18
19 public Voice(double _frequency) {
20 frequency = _frequency;
21 }
22
23 private double frequency = 440;
24
25 public double getFrequency() {
26 return frequency;
27 }
28
29 public void setFrequency(double _d) {
30 frequency = _d;
31 }
32
33 /**
34 * setHamonic is called every time a new
35 * voice is instanced, or the default squarewave
36 * will be played.
37 * @param _d
38 */
39 public void setHarmonic(double _d[]) {
40 harmonic = _d;
41 }
42
43 public double[] mult(double d[], double a) {
44 double c[] = new double[d.length];
45 for (int i = 0; i < d.length; i++)
46 c[i] = d[i] * a;
47 return c;
48 }
49
50 public double[] add(double a[], double b[]) {
51 double c[] = new double[a.length];
52 for (int i = 0; i < a.length; i++)
53 c[i] = a[i] + b[i];
54 return c;
55 }
56
57 /**
58 * This gives us all 7 harmonics together.
59 * @return
60 */
61 public double[] getVoicedWave() {
62 double h1[] = getVoicedWave(1);
63 for (int i = 2; i < 8; i++)
64 h1 = add(getVoicedWave(i), h1);
65 return h1;
66 }
67
68 /**
69 * Get the double precision voice where i is the
70 * harmonic number, starting at 1 and ending at 7.
71 * @param i
72 * @return
73 */
74 public double[] getVoicedWave(int i) {
75 int numberOfSamples = 16000;
76 if (i < 1 || i > 7)
77 System.out.println("i is out range! i=" + i);
78 Oscillator o = new Oscillator(i * frequency, numberOfSamples);
79 return mult(o.getSineWave(), harmonic[i - 1]);
80 }
81
82 public void play() {
83 UlawCodec ulc = new UlawCodec(getVoicedWave());
84 ulc.play();
85 }
86
87 public static void main(String args[]) {
88 Voice v = new Voice(440);
89 v.play();
90 }
91 }
92