Path: blob/master/test/jdk/javax/sound/midi/Gervill/SoftLowFrequencyOscillator/TestProcessControlLogic.java
41159 views
/*1* Copyright (c) 2009, 2015, 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 method25@modules java.desktop/com.sun.media.sound26*/2728import com.sun.media.sound.AudioSynthesizerPropertyInfo;29import com.sun.media.sound.SoftLowFrequencyOscillator;30import com.sun.media.sound.SoftSynthesizer;3132public class TestProcessControlLogic {3334private static float control_rate = 147f;35private static SoftSynthesizer synth = new SoftSynthesizer();36private static SoftLowFrequencyOscillator lfo = new SoftLowFrequencyOscillator();3738private static void testLFO(boolean shared, int instance, float freq, float delay,39float delay2) throws Exception {40SoftLowFrequencyOscillator lfo =41shared?TestProcessControlLogic.lfo:new SoftLowFrequencyOscillator();42lfo.reset();43double[] lfo_freq = lfo.get(instance, "freq");44double[] lfo_delay = lfo.get(instance, "delay");45double[] lfo_delay2 = lfo.get(instance, "delay2");46double[] lfo_output = lfo.get(instance, null);47lfo_freq[0] = freq;48lfo_delay[0] = delay;49lfo_delay2[0] = delay2;50lfo.init(synth);5152// For delayCount amount time, the output LFO should be 0.553int delayCount = (int) ((Math.pow(2, delay / 1200.0) * control_rate));54delayCount += (int) ((delay2 * control_rate) / 1000.0);55for (int i = 0; i < delayCount; i++) {56if (Math.abs(0.5 - lfo_output[0]) > 0.000001)57throw new Exception("Incorrect LFO output ("58+"0.5 != "+lfo_output[0]+")!");59lfo.processControlLogic();60}6162// After the delay the LFO should start oscillate63// Let make sure output is accurate enough64double p_step = (440.0 / control_rate)65* Math.exp((freq - 6900.0) * (Math.log(2) / 1200.0));66double p = 0;67for (int i = 0; i < 30; i++) {68p += p_step;69double predicted_output = 0.5 + Math.sin(p * 2 * Math.PI) * 0.5;70if (Math.abs(predicted_output - lfo_output[0]) > 0.001)71throw new Exception("Incorrect LFO output ("72+predicted_output+" != "+lfo_output[0]+")!");73lfo.processControlLogic();74}7576}7778public static void main(String[] args) throws Exception {7980// Get default control rate from synthesizer81AudioSynthesizerPropertyInfo[] p = synth.getPropertyInfo(null);82for (int i = 0; i < p.length; i++) {83if (p[i].name.equals("control rate")) {84control_rate = ((Float) p[i].value).floatValue();85break;86}87}8889// Test LFO under various configurations90for (int instance = 0; instance < 3; instance++)91for (int d1 = -3000; d1 < 0; d1 += 1000)92for (int d2 = 0; d2 < 5000; d2 += 1000)93for (int fr = -1000; fr < 1000; fr += 100) {94testLFO(true, instance,95(fr == -1000) ? Float.NEGATIVE_INFINITY : fr,96(d1 == -3000) ? Float.NEGATIVE_INFINITY : d1,97d2);98testLFO(false, instance,99(fr == -1000) ? Float.NEGATIVE_INFINITY : fr,100(d1 == -3000) ? Float.NEGATIVE_INFINITY : d1,101d2);102}103104}105}106107108