tor-browser

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

test_scriptProcessorNode.html (5449B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <title>Test ScriptProcessorNode</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  expectException(function() {
     38    context.createScriptProcessor(1);
     39  }, DOMException.INDEX_SIZE_ERR);
     40  expectException(function() {
     41    context.createScriptProcessor(2);
     42  }, DOMException.INDEX_SIZE_ERR);
     43  expectException(function() {
     44    context.createScriptProcessor(128);
     45  }, DOMException.INDEX_SIZE_ERR);
     46  expectException(function() {
     47    context.createScriptProcessor(255);
     48  }, DOMException.INDEX_SIZE_ERR);
     49 
     50  is(sourceSP.channelCount, 2, "script processor node has 2 input channels by default");
     51  is(sourceSP.channelCountMode, "explicit", "Correct channelCountMode for the script processor node");
     52  is(sourceSP.channelInterpretation, "speakers", "Correct channelCountInterpretation for the script processor node");
     53 
     54  function findFirstNonZeroSample(buffer) {
     55    for (var i = 0; i < buffer.length; ++i) {
     56      if (buffer.getChannelData(0)[i] != 0) {
     57        return i;
     58      }
     59    }
     60    return buffer.length;
     61  }
     62 
     63  var sp = context.createScriptProcessor(2048);
     64  sourceSP.connect(sp);
     65  sp.connect(context.destination);
     66  var lastPlaybackTime = 0;
     67 
     68  var emptyBuffer = context.createBuffer(1, 2048, context.sampleRate);
     69 
     70  function checkAudioProcessingEvent(e) {
     71    is(e.target, sp, "Correct event target");
     72    ok(e.playbackTime > lastPlaybackTime, "playbackTime correctly set");
     73    lastPlaybackTime = e.playbackTime;
     74    is(e.inputBuffer.numberOfChannels, 2, "Correct number of channels for the input buffer");
     75    is(e.inputBuffer.length, 2048, "Correct length for the input buffer");
     76    is(e.inputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the input buffer");
     77    is(e.outputBuffer.numberOfChannels, 2, "Correct number of channels for the output buffer");
     78    is(e.outputBuffer.length, 2048, "Correct length for the output buffer");
     79    is(e.outputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the output buffer");
     80 
     81    compareChannels(e.outputBuffer.getChannelData(0), emptyBuffer.getChannelData(0));
     82    compareChannels(e.outputBuffer.getChannelData(1), emptyBuffer.getChannelData(0));
     83  }
     84 
     85  sp.onaudioprocess = function(e) {
     86    isnot(buffer, null, "The audioprocess handler for sourceSP must be run at this point");
     87    checkAudioProcessingEvent(e);
     88 
     89    // Because of the initial latency added by the second script processor node,
     90    // we will never see any generated audio frames in the first callback.
     91    compareChannels(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0));
     92    compareChannels(e.inputBuffer.getChannelData(1), emptyBuffer.getChannelData(0));
     93 
     94    sp.onaudioprocess = function(e) {
     95      checkAudioProcessingEvent(e);
     96 
     97      var firstNonZero = findFirstNonZeroSample(e.inputBuffer);
     98      ok(firstNonZero <= 2048, "First non-zero sample within range");
     99 
    100      compareChannels(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0), firstNonZero);
    101      compareChannels(e.inputBuffer.getChannelData(1), emptyBuffer.getChannelData(0), firstNonZero);
    102      compareChannels(e.inputBuffer.getChannelData(0), buffer.getChannelData(0), 2048 - firstNonZero, firstNonZero, 0);
    103      compareChannels(e.inputBuffer.getChannelData(1), buffer.getChannelData(1), 2048 - firstNonZero, firstNonZero, 0);
    104 
    105      if (firstNonZero == 0) {
    106        // If we did not experience any delays, the test is done!
    107        sp.onaudioprocess = null;
    108 
    109        SimpleTest.finish();
    110      } else if (firstNonZero != 2048) {
    111        // In case we just saw a zero buffer this time, wait one more round
    112        sp.onaudioprocess = function(e) {
    113          checkAudioProcessingEvent(e);
    114 
    115          compareChannels(e.inputBuffer.getChannelData(0), buffer.getChannelData(0), firstNonZero, 0, 2048 - firstNonZero);
    116          compareChannels(e.inputBuffer.getChannelData(1), buffer.getChannelData(1), firstNonZero, 0, 2048 - firstNonZero);
    117          compareChannels(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0), undefined, firstNonZero);
    118          compareChannels(e.inputBuffer.getChannelData(1), emptyBuffer.getChannelData(0), undefined, firstNonZero);
    119 
    120          sp.onaudioprocess = null;
    121 
    122          SimpleTest.finish();
    123        };
    124      }
    125    };
    126  };
    127 });
    128 
    129 </script>
    130 </pre>
    131 </body>
    132 </html>