tor-browser

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

test_delayNodeTailWithDisconnect.html (2819B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <title>Test tail time lifetime of DelayNode after input is disconnected</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 // Web Audio doesn't provide a means to precisely time disconnect()s but we
     14 // can test that the output of delay nodes matches the output from their
     15 // sources before they are disconnected.
     16 
     17 SimpleTest.waitForExplicitFinish();
     18 
     19 const signalLength = 128;
     20 const bufferSize = 4096;
     21 const sourceCount = bufferSize / signalLength;
     22 // Delay should be long enough to allow CC to run
     23 var delayBufferCount = 20;
     24 const delayLength = delayBufferCount * bufferSize;
     25 
     26 var sourceOutput = new Float32Array(bufferSize);
     27 var delayOutputCount = 0;
     28 var sources = [];
     29 
     30 function onDelayOutput(e) {
     31  if (delayOutputCount < delayBufferCount) {
     32    delayOutputCount++;
     33    return;
     34  }
     35 
     36  compareChannels(e.inputBuffer.getChannelData(0), sourceOutput);
     37  e.target.onaudioprocess = null;
     38  SimpleTest.finish();
     39 }
     40 
     41 function onSourceOutput(e) {
     42  // Record the first buffer
     43  e.inputBuffer.copyFromChannel(sourceOutput, 0);
     44  e.target.onaudioprocess = null;
     45 }
     46 
     47 function disconnectSources() {
     48  for (var i = 0; i < sourceCount; ++i) {
     49    sources[i].disconnect();
     50  }
     51 
     52  SpecialPowers.forceGC();
     53  SpecialPowers.forceCC();
     54 }
     55 
     56 function startTest() {
     57  var ctx = new AudioContext();
     58 
     59  var sourceProcessor = ctx.createScriptProcessor(bufferSize, 1, 0);
     60  sourceProcessor.onaudioprocess = onSourceOutput;
     61  // Keep audioprocess events going after source disconnect.
     62  sourceProcessor.connect(ctx.destination);
     63 
     64  var delayProcessor = ctx.createScriptProcessor(bufferSize, 1, 0);
     65  delayProcessor.onaudioprocess = onDelayOutput;
     66 
     67  var delayDuration = delayLength / ctx.sampleRate;
     68  for (var i = 0; i < sourceCount; ++i) {
     69    var delay = ctx.createDelay(delayDuration);
     70    delay.delayTime.value = delayDuration;
     71    delay.connect(delayProcessor);
     72 
     73    var source = ctx.createOscillator();
     74    source.frequency.value = 440 + 10 * i
     75    source.start(i * signalLength / ctx.sampleRate);
     76    source.stop((i + 1) * signalLength / ctx.sampleRate);
     77    source.connect(delay);
     78    source.connect(sourceProcessor);
     79 
     80    sources[i] = source;
     81  }
     82 
     83  // Assuming the above Web Audio operations have already scheduled an event
     84  // to run in stable state and start the graph thread, schedule a subsequent
     85  // event to disconnect the sources, which will remove main thread connection
     86  // references before it knows the graph thread has started using the source
     87  // streams.
     88  SimpleTest.executeSoon(disconnectSources);
     89 };
     90 
     91 startTest();
     92 </script>
     93 </pre>
     94 </body>
     95 </html>