content/media/webaudio/test/test_periodicWave.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/webaudio/test/test_periodicWave.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,94 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <title>Test the PeriodicWave interface</title>
     1.8 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     1.9 +  <script type="text/javascript" src="webaudio.js"></script>
    1.10 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.11 +</head>
    1.12 +<body>
    1.13 +<pre id="test">
    1.14 +<script class="testbody" type="text/javascript">
    1.15 +
    1.16 +SimpleTest.waitForExplicitFinish();
    1.17 +
    1.18 +// real and imag are used in separate PeriodicWaves to make their peak values
    1.19 +// easy to determine.
    1.20 +const realMax = 99;
    1.21 +var real = new Float32Array(realMax + 1);
    1.22 +real[1] = 2.0; // fundamental
    1.23 +real[realMax] = 3.0;
    1.24 +const realPeak = real[1] + real[realMax];
    1.25 +const realFundamental = 19.0;
    1.26 +var imag = new Float32Array(4);
    1.27 +imag[0] = 6.0; // should be ignored.
    1.28 +imag[3] = 0.5;
    1.29 +const imagPeak = imag[3];
    1.30 +const imagFundamental = 551.0;
    1.31 +
    1.32 +const testLength = 4096;
    1.33 +
    1.34 +addLoadEvent(function() {
    1.35 +  var ac = new AudioContext();
    1.36 +  ac.createPeriodicWave(new Float32Array(4096), new Float32Array(4096));
    1.37 +  expectException(function() {
    1.38 +    ac.createPeriodicWave(new Float32Array(512), imag);
    1.39 +  }, DOMException.NOT_SUPPORTED_ERR);
    1.40 +  expectException(function() {
    1.41 +    ac.createPeriodicWave(new Float32Array(0), new Float32Array(0));
    1.42 +  }, DOMException.NOT_SUPPORTED_ERR);
    1.43 +  expectException(function() {
    1.44 +    ac.createPeriodicWave(new Float32Array(4097), new Float32Array(4097));
    1.45 +  }, DOMException.NOT_SUPPORTED_ERR);
    1.46 +
    1.47 +  runTest();
    1.48 +});
    1.49 +
    1.50 +var gTest = {
    1.51 +  createGraph: function(context) {
    1.52 +    var merger = context.createChannelMerger();
    1.53 +
    1.54 +    var osc0 = context.createOscillator();
    1.55 +    var osc1 = context.createOscillator();
    1.56 +
    1.57 +    osc0.setPeriodicWave(context.
    1.58 +                         createPeriodicWave(real,
    1.59 +                                            new Float32Array(real.length)));
    1.60 +    osc1.setPeriodicWave(context.
    1.61 +                         createPeriodicWave(new Float32Array(imag.length),
    1.62 +                                            imag));
    1.63 +
    1.64 +    osc0.frequency.value = realFundamental;
    1.65 +    osc1.frequency.value = imagFundamental;
    1.66 +
    1.67 +    osc0.start();
    1.68 +    osc1.start();
    1.69 +
    1.70 +    osc0.connect(merger, 0, 0);
    1.71 +    osc1.connect(merger, 0, 1);
    1.72 +
    1.73 +    return merger;
    1.74 +  },
    1.75 +  createExpectedBuffers: function(context) {
    1.76 +    var buffer = context.createBuffer(2, testLength, context.sampleRate);
    1.77 +
    1.78 +    for (var i = 0; i < buffer.length; ++i) {
    1.79 +
    1.80 +      buffer.getChannelData(0)[i] = 1.0 / realPeak *
    1.81 +        (real[1] * Math.cos(2 * Math.PI * realFundamental * i /
    1.82 +                            context.sampleRate) +
    1.83 +         real[realMax] * Math.cos(2 * Math.PI * realMax * realFundamental * i /
    1.84 +                            context.sampleRate));
    1.85 +
    1.86 +      buffer.getChannelData(1)[i] = 1.0 / imagPeak *
    1.87 +         imag[3] * Math.sin(2 * Math.PI * 3 * imagFundamental * i /
    1.88 +                            context.sampleRate);
    1.89 +    }
    1.90 +    return buffer;
    1.91 +  },
    1.92 +};
    1.93 +
    1.94 +</script>
    1.95 +</pre>
    1.96 +</body>
    1.97 +</html>

mercurial