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