TestProcessControlLogic.java revision 8729:0242fce0f717
11590Srgrimes/*
21590Srgrimes * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
31590Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
41590Srgrimes *
51590Srgrimes * This code is free software; you can redistribute it and/or modify it
61590Srgrimes * under the terms of the GNU General Public License version 2 only, as
71590Srgrimes * published by the Free Software Foundation.
81590Srgrimes *
91590Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
101590Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
111590Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
121590Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
131590Srgrimes * accompanied this code).
141590Srgrimes *
151590Srgrimes * You should have received a copy of the GNU General Public License version
161590Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
171590Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
181590Srgrimes *
191590Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
201590Srgrimes * or visit www.oracle.com if you need additional information or have any
211590Srgrimes * questions.
221590Srgrimes */
231590Srgrimes
241590Srgrimes/* @test
251590Srgrimes @summary Test SoftLowFrequencyOscillator processControlLogic method */
261590Srgrimes
271590Srgrimesimport com.sun.media.sound.AudioSynthesizerPropertyInfo;
281590Srgrimesimport com.sun.media.sound.SoftLowFrequencyOscillator;
291590Srgrimesimport com.sun.media.sound.SoftSynthesizer;
301590Srgrimes
3128454Scharnierpublic class TestProcessControlLogic {
321590Srgrimes
331590Srgrimes    private static float control_rate = 147f;
341590Srgrimes    private static SoftSynthesizer synth = new SoftSynthesizer();
351590Srgrimes    private static SoftLowFrequencyOscillator lfo = new SoftLowFrequencyOscillator();
361590Srgrimes
3728454Scharnier    private static void testLFO(boolean shared, int instance, float freq, float delay,
381590Srgrimes            float delay2) throws Exception {
3928454Scharnier        SoftLowFrequencyOscillator lfo =
4028454Scharnier            shared?TestProcessControlLogic.lfo:new SoftLowFrequencyOscillator();
4150477Speter        lfo.reset();
421590Srgrimes        double[] lfo_freq = lfo.get(instance, "freq");
431590Srgrimes        double[] lfo_delay = lfo.get(instance, "delay");
4428454Scharnier        double[] lfo_delay2 = lfo.get(instance, "delay2");
45132858Stjr        double[] lfo_output = lfo.get(instance, null);
461590Srgrimes        lfo_freq[0] = freq;
4728454Scharnier        lfo_delay[0] = delay;
4828454Scharnier        lfo_delay2[0] = delay2;
4928454Scharnier        lfo.init(synth);
5028454Scharnier
51132858Stjr        // For delayCount amount time, the output LFO should be 0.5
52200462Sdelphij        int delayCount = (int) ((Math.pow(2, delay / 1200.0) * control_rate));
531590Srgrimes        delayCount += (int) ((delay2 * control_rate) / 1000.0);
541590Srgrimes        for (int i = 0; i < delayCount; i++) {
551590Srgrimes            if (Math.abs(0.5 - lfo_output[0]) > 0.000001)
561590Srgrimes                throw new Exception("Incorrect LFO output ("
571590Srgrimes                        +"0.5 != "+lfo_output[0]+")!");
581590Srgrimes            lfo.processControlLogic();
591590Srgrimes        }
601590Srgrimes
611590Srgrimes        // After the delay the LFO should start oscillate
621590Srgrimes        // Let make sure output is accurate enough
631590Srgrimes        double p_step = (440.0 / control_rate)
641590Srgrimes        * Math.exp((freq - 6900.0) * (Math.log(2) / 1200.0));
651590Srgrimes        double p = 0;
661590Srgrimes        for (int i = 0; i < 30; i++) {
671590Srgrimes            p += p_step;
681590Srgrimes            double predicted_output = 0.5 + Math.sin(p * 2 * Math.PI) * 0.5;
69227190Sed            if (Math.abs(predicted_output - lfo_output[0]) > 0.001)
70227190Sed                throw new Exception("Incorrect LFO output ("
7187303Sdwmalone                        +predicted_output+" != "+lfo_output[0]+")!");
721590Srgrimes            lfo.processControlLogic();
731590Srgrimes        }
741590Srgrimes
751590Srgrimes    }
761590Srgrimes
77132858Stjr    public static void main(String[] args) throws Exception {
78132858Stjr
791590Srgrimes        // Get default control rate from synthesizer
801590Srgrimes        AudioSynthesizerPropertyInfo[] p = synth.getPropertyInfo(null);
81227190Sed        for (int i = 0; i < p.length; i++) {
82227190Sed            if (p[i].name.equals("control rate")) {
83227190Sed                control_rate = ((Float) p[i].value).floatValue();
84227190Sed                break;
85227190Sed            }
86227190Sed        }
871590Srgrimes
8892922Simp        // Test LFO under various configurations
89227190Sed        for (int instance = 0; instance < 3; instance++)
90227190Sed            for (int d1 = -3000; d1 < 0; d1 += 1000)
91227190Sed                for (int d2 = 0; d2 < 5000; d2 += 1000)
92227190Sed                    for (int fr = -1000; fr < 1000; fr += 100) {
93227190Sed                        testLFO(true, instance,
94227190Sed                                (fr == -1000) ? Float.NEGATIVE_INFINITY : fr,
95227190Sed                                (d1 == -3000) ? Float.NEGATIVE_INFINITY : d1,
96227190Sed                                d2);
97227190Sed                        testLFO(false, instance,
98227190Sed                                (fr == -1000) ? Float.NEGATIVE_INFINITY : fr,
99227190Sed                                (d1 == -3000) ? Float.NEGATIVE_INFINITY : d1,
10028454Scharnier                                d2);
1011590Srgrimes                    }
1021590Srgrimes
10328789Scharnier    }
104102944Sdwmalone}
1051590Srgrimes