content/media/webaudio/test/test_delayNodeCycles.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/webaudio/test/test_delayNodeCycles.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,170 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <title>Test the support of cycles.</title>
     1.8 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     1.9 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.10 +</head>
    1.11 +<body>
    1.12 +<pre id="test">
    1.13 +<script src="webaudio.js" type="text/javascript"></script>
    1.14 +<script class="testbody" type="text/javascript">
    1.15 +
    1.16 +SimpleTest.waitForExplicitFinish();
    1.17 +
    1.18 +addLoadEvent(function() {
    1.19 +  function getSineBuffer(ctx) {
    1.20 +    var buffer = ctx.createBuffer(1, 2048, ctx.sampleRate);
    1.21 +    var b = buffer.getChannelData(0);
    1.22 +    for (var i = 0; i < 2048; i++) {
    1.23 +      b[i] = Math.sin(440 * 2 * Math.PI * i / ctx.sampleRate);
    1.24 +    }
    1.25 +    return buffer;
    1.26 +  }
    1.27 +
    1.28 +  function createAndPlayWithCycleAndDelayNode(ctx) {
    1.29 +    var source = ctx.createBufferSource();
    1.30 +    source.loop = true;
    1.31 +    source.buffer = getSineBuffer(ctx);
    1.32 +
    1.33 +    var gain = ctx.createGain();
    1.34 +    var delay = ctx.createDelay();
    1.35 +    delay.delayTime = 0.5;
    1.36 +
    1.37 +    source.connect(gain);
    1.38 +    gain.connect(delay);
    1.39 +    delay.connect(ctx.destination);
    1.40 +    // cycle
    1.41 +    delay.connect(gain);
    1.42 +
    1.43 +    source.start(0);
    1.44 +  }
    1.45 +
    1.46 +  function createAndPlayWithCycleAndDelayNodeButNullDelayTime(ctx) {
    1.47 +    var source = ctx.createBufferSource();
    1.48 +    source.loop = true;
    1.49 +    source.buffer = getSineBuffer(ctx);
    1.50 +
    1.51 +    var gain = ctx.createGain();
    1.52 +    var delay = ctx.createDelay();
    1.53 +    delay.delayTime = 0.0;
    1.54 +
    1.55 +    source.connect(gain);
    1.56 +    gain.connect(delay);
    1.57 +    delay.connect(ctx.destination);
    1.58 +    // cycle
    1.59 +    delay.connect(gain);
    1.60 +
    1.61 +    source.start(0);
    1.62 +  }
    1.63 +
    1.64 +  function createAndPlayWithCycleAndNoDelayNode(ctx) {
    1.65 +    var source = ctx.createBufferSource();
    1.66 +    source.loop = true;
    1.67 +    source.buffer = getSineBuffer(ctx);
    1.68 +
    1.69 +    var gain = ctx.createGain();
    1.70 +    var gain2 = ctx.createGain();
    1.71 +
    1.72 +    source.connect(gain);
    1.73 +    gain.connect(gain2);
    1.74 +    // cycle
    1.75 +    gain2.connect(gain);
    1.76 +    gain2.connect(ctx.destination);
    1.77 +
    1.78 +    source.start(0);
    1.79 +  }
    1.80 +
    1.81 +  function createAndPlayWithCycleAndNoDelayNodeInCycle(ctx) {
    1.82 +    var source = ctx.createBufferSource();
    1.83 +    source.loop = true;
    1.84 +    source.buffer = getSineBuffer(ctx);
    1.85 +
    1.86 +    var delay = ctx.createDelay();
    1.87 +    var gain = ctx.createGain();
    1.88 +    var gain2 = ctx.createGain();
    1.89 +
    1.90 +    // Their is a cycle, a delay, but the delay is not in the cycle.
    1.91 +    source.connect(delay);
    1.92 +    delay.connect(gain);
    1.93 +    gain.connect(gain2);
    1.94 +    // cycle
    1.95 +    gain2.connect(gain);
    1.96 +    gain2.connect(ctx.destination);
    1.97 +
    1.98 +    source.start(0);
    1.99 +  }
   1.100 +
   1.101 +  var remainingTests = 0;
   1.102 +  function finish() {
   1.103 +    if (--remainingTests == 0) {
   1.104 +      SimpleTest.finish();
   1.105 +    }
   1.106 +  }
   1.107 +
   1.108 +  function getOfflineContext(oncomplete) {
   1.109 +    var ctx = new OfflineAudioContext(1, 48000, 48000);
   1.110 +    ctx.oncomplete = oncomplete;
   1.111 +    return ctx;
   1.112 +  }
   1.113 +
   1.114 +  function checkSilentBuffer(e) {
   1.115 +    var buffer = e.renderedBuffer.getChannelData(0);
   1.116 +    for (var i = 0; i < buffer.length; i++) {
   1.117 +      if (buffer[i] != 0.0) {
   1.118 +        ok(false, "buffer should be silent.");
   1.119 +        finish();
   1.120 +        return;
   1.121 +      }
   1.122 +    }
   1.123 +    ok(true, "buffer should be silent.");
   1.124 +    finish();
   1.125 +  }
   1.126 +
   1.127 +  function checkNoisyBuffer(e) {
   1.128 +    var buffer = e.renderedBuffer.getChannelData(0);
   1.129 +    for (var i = 0; i < buffer.length; i++) {
   1.130 +      if (buffer[i] != 0.0) {
   1.131 +        ok(true, "buffer should be noisy.");
   1.132 +        finish();
   1.133 +        return true;
   1.134 +      }
   1.135 +    }
   1.136 +    ok(false, "buffer should be noisy.");
   1.137 +    finish();
   1.138 +    return false;
   1.139 +  }
   1.140 +
   1.141 +  function expectSilentOutput(f) {
   1.142 +    remainingTests++;
   1.143 +    var ctx = getOfflineContext(checkSilentBuffer);
   1.144 +    f(ctx);
   1.145 +    ctx.startRendering();
   1.146 +  }
   1.147 +
   1.148 +  function expectNoisyOutput(f) {
   1.149 +    remainingTests++;
   1.150 +    var ctx = getOfflineContext(checkNoisyBuffer);
   1.151 +    f(ctx);
   1.152 +    ctx.startRendering();
   1.153 +  }
   1.154 +
   1.155 +  // This is trying to make a graph with a cycle and no DelayNode in the graph.
   1.156 +  // The cycle subgraph should be muted, in this graph the output should be silent.
   1.157 +  expectSilentOutput(createAndPlayWithCycleAndNoDelayNode);
   1.158 +  // This is trying to make a graph with a cycle and a DelayNode in the graph, but
   1.159 +  // not part of the cycle.
   1.160 +  // The cycle subgraph should be muted, in this graph the output should be silent.
   1.161 +  expectSilentOutput(createAndPlayWithCycleAndNoDelayNodeInCycle);
   1.162 +  // Those are making legal graphs, with at least one DelayNode in the cycle.
   1.163 +  // There should be some non-silent output.
   1.164 +  expectNoisyOutput(createAndPlayWithCycleAndDelayNode);
   1.165 +  // DelayNode.delayTime will be clamped to 128/ctx.sampleRate.
   1.166 +  // There should be some non-silent output.
   1.167 +  expectNoisyOutput(createAndPlayWithCycleAndDelayNodeButNullDelayTime);
   1.168 +});
   1.169 +
   1.170 +</script>
   1.171 +</pre>
   1.172 +</body>
   1.173 +</html>

mercurial