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