TestProcessControlLogic.java revision 8729:0242fce0f717
1/*
2 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25 @summary Test SoftLowFrequencyOscillator processControlLogic method */
26
27import com.sun.media.sound.AudioSynthesizerPropertyInfo;
28import com.sun.media.sound.SoftLowFrequencyOscillator;
29import com.sun.media.sound.SoftSynthesizer;
30
31public class TestProcessControlLogic {
32
33    private static float control_rate = 147f;
34    private static SoftSynthesizer synth = new SoftSynthesizer();
35    private static SoftLowFrequencyOscillator lfo = new SoftLowFrequencyOscillator();
36
37    private static void testLFO(boolean shared, int instance, float freq, float delay,
38            float delay2) throws Exception {
39        SoftLowFrequencyOscillator lfo =
40            shared?TestProcessControlLogic.lfo:new SoftLowFrequencyOscillator();
41        lfo.reset();
42        double[] lfo_freq = lfo.get(instance, "freq");
43        double[] lfo_delay = lfo.get(instance, "delay");
44        double[] lfo_delay2 = lfo.get(instance, "delay2");
45        double[] lfo_output = lfo.get(instance, null);
46        lfo_freq[0] = freq;
47        lfo_delay[0] = delay;
48        lfo_delay2[0] = delay2;
49        lfo.init(synth);
50
51        // For delayCount amount time, the output LFO should be 0.5
52        int delayCount = (int) ((Math.pow(2, delay / 1200.0) * control_rate));
53        delayCount += (int) ((delay2 * control_rate) / 1000.0);
54        for (int i = 0; i < delayCount; i++) {
55            if (Math.abs(0.5 - lfo_output[0]) > 0.000001)
56                throw new Exception("Incorrect LFO output ("
57                        +"0.5 != "+lfo_output[0]+")!");
58            lfo.processControlLogic();
59        }
60
61        // After the delay the LFO should start oscillate
62        // Let make sure output is accurate enough
63        double p_step = (440.0 / control_rate)
64        * Math.exp((freq - 6900.0) * (Math.log(2) / 1200.0));
65        double p = 0;
66        for (int i = 0; i < 30; i++) {
67            p += p_step;
68            double predicted_output = 0.5 + Math.sin(p * 2 * Math.PI) * 0.5;
69            if (Math.abs(predicted_output - lfo_output[0]) > 0.001)
70                throw new Exception("Incorrect LFO output ("
71                        +predicted_output+" != "+lfo_output[0]+")!");
72            lfo.processControlLogic();
73        }
74
75    }
76
77    public static void main(String[] args) throws Exception {
78
79        // Get default control rate from synthesizer
80        AudioSynthesizerPropertyInfo[] p = synth.getPropertyInfo(null);
81        for (int i = 0; i < p.length; i++) {
82            if (p[i].name.equals("control rate")) {
83                control_rate = ((Float) p[i].value).floatValue();
84                break;
85            }
86        }
87
88        // Test LFO under various configurations
89        for (int instance = 0; instance < 3; instance++)
90            for (int d1 = -3000; d1 < 0; d1 += 1000)
91                for (int d2 = 0; d2 < 5000; d2 += 1000)
92                    for (int fr = -1000; fr < 1000; fr += 100) {
93                        testLFO(true, instance,
94                                (fr == -1000) ? Float.NEGATIVE_INFINITY : fr,
95                                (d1 == -3000) ? Float.NEGATIVE_INFINITY : d1,
96                                d2);
97                        testLFO(false, instance,
98                                (fr == -1000) ? Float.NEGATIVE_INFINITY : fr,
99                                (d1 == -3000) ? Float.NEGATIVE_INFINITY : d1,
100                                d2);
101                    }
102
103    }
104}
105