jdk/test/javax/sound/midi/Gervill/SoftLowFrequencyOscillator/TestProcessControlLogic.java
author serb
Tue, 12 Nov 2013 20:24:25 +0400
changeset 21596 0e3a39f29dbc
parent 5506 202f599c92aa
child 23010 6dadb192ad81
permissions -rw-r--r--
8027696: Incorrect copyright header in the tests Reviewed-by: alanb, malenkov, mullan
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
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2657
diff changeset
     2
 * Copyright (c) 2009, 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
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    25
 @summary Test SoftLowFrequencyOscillator processControlLogic method */
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    26
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    27
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
    28
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
    29
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
    30
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    31
public class TestProcessControlLogic {
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
    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
    34
    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
    35
    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
    36
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    37
    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
    38
            float delay2) throws Exception {
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    39
        SoftLowFrequencyOscillator lfo =
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    40
            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
    41
        lfo.reset();
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    42
        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
    43
        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
    44
        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
    45
        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
    46
        lfo_freq[0] = freq;
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    47
        lfo_delay[0] = delay;
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    48
        lfo_delay2[0] = delay2;
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    49
        lfo.init(synth);
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    50
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    51
        // 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
    52
        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
    53
        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
    54
        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
    55
            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
    56
                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
    57
                        +"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
    58
            lfo.processControlLogic();
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    59
        }
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    60
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    61
        // 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
    62
        // 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
    63
        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
    64
        * 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
    65
        double p = 0;
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    66
        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
    67
            p += p_step;
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    68
            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
    69
            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
    70
                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
    71
                        +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
    72
            lfo.processControlLogic();
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    73
        }
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    74
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
    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
    78
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    79
        // 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
    80
        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
    81
        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
    82
            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
    83
                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
    84
                break;
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    85
            }
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    86
        }
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
        // 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
    89
        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
    90
            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
    91
                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
    92
                    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
    93
                        testLFO(true, instance,
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    94
                                (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
    95
                                (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
    96
                                d2);
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    97
                        testLFO(false, instance,
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
    98
                                (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
    99
                                (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
   100
                                d2);
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
   101
                    }
bc5c66dd4730 6823446: Gervill SoftLowFrequencyOscillator fails when freq is set to 0 cent or 8.1758 Hz.
kalli
parents:
diff changeset
   102
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
}