tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_delayNodeCycles.html (4306B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <title>Test the support of cycles.</title>
      5  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6  <script type="text/javascript" src="webaudio.js"></script>
      7  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      8 </head>
      9 <body>
     10 <pre id="test">
     11 <script class="testbody" type="text/javascript">
     12 
     13 SimpleTest.waitForExplicitFinish();
     14 
     15 const sampleRate = 48000;
     16 const inputLength = 2048;
     17 
     18 addLoadEvent(function() {
     19  function addSine(b) {
     20    for (var i = 0; i < b.length; i++) {
     21      b[i] += Math.sin(440 * 2 * Math.PI * i / sampleRate);
     22    }
     23  }
     24 
     25  function getSineBuffer(ctx) {
     26    var buffer = ctx.createBuffer(1, inputLength, ctx.sampleRate);
     27    addSine(buffer.getChannelData(0));
     28    return buffer;
     29  }
     30 
     31  function createAndPlayWithCycleAndDelayNode(ctx, delayFrames) {
     32    var source = ctx.createBufferSource();
     33    source.buffer = getSineBuffer(ctx);
     34 
     35    var gain = ctx.createGain();
     36    var delay = ctx.createDelay();
     37    delay.delayTime.value = delayFrames/ctx.sampleRate;
     38 
     39    source.connect(gain);
     40    gain.connect(delay);
     41    delay.connect(ctx.destination);
     42    // cycle
     43    delay.connect(gain);
     44 
     45    source.start(0);
     46  }
     47 
     48  function createAndPlayWithCycleAndNoDelayNode(ctx) {
     49    var source = ctx.createBufferSource();
     50    source.loop = true;
     51    source.buffer = getSineBuffer(ctx);
     52 
     53    var gain = ctx.createGain();
     54    var gain2 = ctx.createGain();
     55 
     56    source.connect(gain);
     57    gain.connect(gain2);
     58    // cycle
     59    gain2.connect(gain);
     60    gain2.connect(ctx.destination);
     61 
     62    source.start(0);
     63  }
     64 
     65  function createAndPlayWithCycleAndNoDelayNodeInCycle(ctx) {
     66    var source = ctx.createBufferSource();
     67    source.loop = true;
     68    source.buffer = getSineBuffer(ctx);
     69 
     70    var delay = ctx.createDelay();
     71    var gain = ctx.createGain();
     72    var gain2 = ctx.createGain();
     73 
     74    // Their is a cycle, a delay, but the delay is not in the cycle.
     75    source.connect(delay);
     76    delay.connect(gain);
     77    gain.connect(gain2);
     78    // cycle
     79    gain2.connect(gain);
     80    gain2.connect(ctx.destination);
     81 
     82    source.start(0);
     83  }
     84 
     85  var remainingTests = 0;
     86  function finish() {
     87    if (--remainingTests == 0) {
     88      SimpleTest.finish();
     89    }
     90  }
     91 
     92  function getOfflineContext(oncomplete) {
     93    var ctx = new OfflineAudioContext(1, sampleRate, sampleRate);
     94    ctx.oncomplete = oncomplete;
     95    return ctx;
     96  }
     97 
     98  function checkSilentBuffer(e) {
     99    var buffer = e.renderedBuffer.getChannelData(0);
    100    for (var i = 0; i < buffer.length; i++) {
    101      if (buffer[i] != 0.0) {
    102        ok(false, "buffer should be silent.");
    103        finish();
    104        return;
    105      }
    106    }
    107    ok(true, "buffer should be silent.");
    108    finish();
    109  }
    110 
    111  function checkNoisyBuffer(e, aDelayFrames) {
    112    let delayFrames = Math.max(128, aDelayFrames);
    113 
    114    var expected = new Float32Array(e.renderedBuffer.length);
    115    for (var i = delayFrames; i < expected.length; i += delayFrames) {
    116      addSine(expected.subarray(i, i + inputLength));
    117    }
    118 
    119    compareChannels(e.renderedBuffer.getChannelData(0), expected);
    120    finish();
    121  }
    122 
    123  function expectSilentOutput(f) {
    124    remainingTests++;
    125    var ctx = getOfflineContext(checkSilentBuffer);
    126    f(ctx);
    127    ctx.startRendering();
    128  }
    129 
    130  function expectNoisyOutput(delayFrames) {
    131    remainingTests++;
    132    var ctx = getOfflineContext();
    133    ctx.oncomplete = function(e) { checkNoisyBuffer(e, delayFrames); };
    134    createAndPlayWithCycleAndDelayNode(ctx, delayFrames);
    135    ctx.startRendering();
    136  }
    137 
    138  // This is trying to make a graph with a cycle and no DelayNode in the graph.
    139  // The cycle subgraph should be muted, in this graph the output should be silent.
    140  expectSilentOutput(createAndPlayWithCycleAndNoDelayNode);
    141  // This is trying to make a graph with a cycle and a DelayNode in the graph, but
    142  // not part of the cycle.
    143  // The cycle subgraph should be muted, in this graph the output should be silent.
    144  expectSilentOutput(createAndPlayWithCycleAndNoDelayNodeInCycle);
    145  // Those are making legal graphs, with at least one DelayNode in the cycle.
    146  // There should be some non-silent output.
    147  expectNoisyOutput(sampleRate/4);
    148  // DelayNode.delayTime will be clamped to 128/ctx.sampleRate.
    149  // There should be some non-silent output.
    150  expectNoisyOutput(0);
    151 });
    152 
    153 </script>
    154 </pre>
    155 </body>
    156 </html>