Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/sound/midi/Gervill/SoftLowFrequencyOscillator/TestProcessControlLogic.java
38859 views
/*1* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@summary Test SoftLowFrequencyOscillator processControlLogic method */2526import com.sun.media.sound.AudioSynthesizerPropertyInfo;27import com.sun.media.sound.SoftLowFrequencyOscillator;28import com.sun.media.sound.SoftSynthesizer;2930public class TestProcessControlLogic {3132private static float control_rate = 147f;33private static SoftSynthesizer synth = new SoftSynthesizer();34private static SoftLowFrequencyOscillator lfo = new SoftLowFrequencyOscillator();3536private static void testLFO(boolean shared, int instance, float freq, float delay,37float delay2) throws Exception {38SoftLowFrequencyOscillator lfo =39shared?TestProcessControlLogic.lfo:new SoftLowFrequencyOscillator();40lfo.reset();41double[] lfo_freq = lfo.get(instance, "freq");42double[] lfo_delay = lfo.get(instance, "delay");43double[] lfo_delay2 = lfo.get(instance, "delay2");44double[] lfo_output = lfo.get(instance, null);45lfo_freq[0] = freq;46lfo_delay[0] = delay;47lfo_delay2[0] = delay2;48lfo.init(synth);4950// For delayCount amount time, the output LFO should be 0.551int delayCount = (int) ((Math.pow(2, delay / 1200.0) * control_rate));52delayCount += (int) ((delay2 * control_rate) / 1000.0);53for (int i = 0; i < delayCount; i++) {54if (Math.abs(0.5 - lfo_output[0]) > 0.000001)55throw new Exception("Incorrect LFO output ("56+"0.5 != "+lfo_output[0]+")!");57lfo.processControlLogic();58}5960// After the delay the LFO should start oscillate61// Let make sure output is accurate enough62double p_step = (440.0 / control_rate)63* Math.exp((freq - 6900.0) * (Math.log(2) / 1200.0));64double p = 0;65for (int i = 0; i < 30; i++) {66p += p_step;67double predicted_output = 0.5 + Math.sin(p * 2 * Math.PI) * 0.5;68if (Math.abs(predicted_output - lfo_output[0]) > 0.001)69throw new Exception("Incorrect LFO output ("70+predicted_output+" != "+lfo_output[0]+")!");71lfo.processControlLogic();72}7374}7576public static void main(String[] args) throws Exception {7778// Get default control rate from synthesizer79AudioSynthesizerPropertyInfo[] p = synth.getPropertyInfo(null);80for (int i = 0; i < p.length; i++) {81if (p[i].name.equals("control rate")) {82control_rate = ((Float) p[i].value).floatValue();83break;84}85}8687// Test LFO under various configurations88for (int instance = 0; instance < 3; instance++)89for (int d1 = -3000; d1 < 0; d1 += 1000)90for (int d2 = 0; d2 < 5000; d2 += 1000)91for (int fr = -1000; fr < 1000; fr += 100) {92testLFO(true, instance,93(fr == -1000) ? Float.NEGATIVE_INFINITY : fr,94(d1 == -3000) ? Float.NEGATIVE_INFINITY : d1,95d2);96testLFO(false, instance,97(fr == -1000) ? Float.NEGATIVE_INFINITY : fr,98(d1 == -3000) ? Float.NEGATIVE_INFINITY : d1,99d2);100}101102}103}104105106