tor-browser

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

test_scriptProcessorNodePassThrough.html (3971B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <title>Test ScriptProcessorNode with passthrough</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 // We do not use our generic graph test framework here because
     14 // the testing logic here is sort of complicated, and would
     15 // not be easy to map to OfflineAudioContext, as ScriptProcessorNodes
     16 // can experience delays.
     17 
     18 SimpleTest.waitForExplicitFinish();
     19 addLoadEvent(function() {
     20  var context = new AudioContext();
     21  var buffer = null;
     22 
     23  var sourceSP = context.createScriptProcessor(2048);
     24  sourceSP.addEventListener("audioprocess", function(e) {
     25    // generate the audio
     26    for (var i = 0; i < 2048; ++i) {
     27      // Make sure our first sample won't be zero
     28      e.outputBuffer.getChannelData(0)[i] = Math.sin(440 * 2 * Math.PI * (i + 1) / context.sampleRate);
     29      e.outputBuffer.getChannelData(1)[i] = Math.sin(880 * 2 * Math.PI * (i + 1) / context.sampleRate);
     30    }
     31    // Remember our generated audio
     32    buffer = e.outputBuffer;
     33 
     34    sourceSP.removeEventListener("audioprocess", arguments.callee);
     35  });
     36 
     37  function findFirstNonZeroSample(buffer) {
     38    for (var i = 0; i < buffer.length; ++i) {
     39      if (buffer.getChannelData(0)[i] != 0) {
     40        return i;
     41      }
     42    }
     43    return buffer.length;
     44  }
     45 
     46  var sp = context.createScriptProcessor(2048);
     47  sourceSP.connect(sp);
     48 
     49  var spWrapped = SpecialPowers.wrap(sp);
     50  ok("passThrough" in spWrapped, "ScriptProcessorNode should support the passThrough API");
     51  spWrapped.passThrough = true;
     52 
     53  sp.onaudioprocess = function() {
     54    ok(false, "The audioprocess event must never be dispatched on the passthrough ScriptProcessorNode");
     55  };
     56 
     57  var sp2 = context.createScriptProcessor(2048);
     58  sp.connect(sp2);
     59  sp2.connect(context.destination);
     60 
     61  var emptyBuffer = context.createBuffer(1, 2048, context.sampleRate);
     62 
     63  sp2.onaudioprocess = function(e) {
     64    // Because of the initial latency added by the second script processor node,
     65    // we will never see any generated audio frames in the first callback.
     66    compareChannels(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0));
     67    compareChannels(e.inputBuffer.getChannelData(1), emptyBuffer.getChannelData(0));
     68 
     69    sp2.onaudioprocess = function(e) {
     70      var firstNonZero = findFirstNonZeroSample(e.inputBuffer);
     71      ok(firstNonZero <= 2048, "First non-zero sample within range");
     72 
     73      compareChannels(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0), firstNonZero);
     74      compareChannels(e.inputBuffer.getChannelData(1), emptyBuffer.getChannelData(0), firstNonZero);
     75      compareChannels(e.inputBuffer.getChannelData(0), buffer.getChannelData(0), 2048 - firstNonZero, firstNonZero, 0);
     76      compareChannels(e.inputBuffer.getChannelData(1), buffer.getChannelData(1), 2048 - firstNonZero, firstNonZero, 0);
     77 
     78      if (firstNonZero == 0) {
     79        // If we did not experience any delays, the test is done!
     80        sp2.onaudioprocess = null;
     81 
     82        SimpleTest.finish();
     83      } else if (firstNonZero != 2048) {
     84        // In case we just saw a zero buffer this time, wait one more round
     85        sp2.onaudioprocess = function(e) {
     86          compareChannels(e.inputBuffer.getChannelData(0), buffer.getChannelData(0), firstNonZero, 0, 2048 - firstNonZero);
     87          compareChannels(e.inputBuffer.getChannelData(1), buffer.getChannelData(1), firstNonZero, 0, 2048 - firstNonZero);
     88          compareChannels(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0), undefined, firstNonZero);
     89          compareChannels(e.inputBuffer.getChannelData(1), emptyBuffer.getChannelData(0), undefined, firstNonZero);
     90 
     91          sp2.onaudioprocess = null;
     92 
     93          SimpleTest.finish();
     94        };
     95      }
     96    };
     97  };
     98 });
     99 
    100 </script>
    101 </pre>
    102 </body>
    103 </html>