tor-browser

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

audioworkletnode-disconnected-input.https.html (3430B)


      1 <!DOCTYPE html>
      2 <html>
      3  <head>
      4    <title>
      5      Test AudioWorkletNode's Disconnected Input Array Length
      6    </title>
      7    <script src="/resources/testharness.js"></script>
      8    <script src="/resources/testharnessreport.js"></script>
      9    <script src="/webaudio/resources/audit-util.js"></script>
     10  </head>
     11  <body>
     12    <script>
     13      // Arbitrary numbers used to align the test with render quantum boundary.
     14      // The sample rate is a power of two to eliminate roundoff in computing
     15      // the suspend time needed for the test.
     16      const sampleRate = 16384;
     17      const renderLength = 8 * RENDER_QUANTUM_FRAMES;
     18      const filePath = 'processors/input-length-processor.js';
     19 
     20      // Creates a 3-channel buffer and play with BufferSourceNode. The source
     21      // goes through a bypass AudioWorkletNode (gain value of 1).
     22      promise_test(async () => {
     23        const context = new OfflineAudioContext({
     24          numberOfChannels: 1,
     25          length: renderLength,
     26          sampleRate: sampleRate
     27        });
     28 
     29        await context.audioWorklet.addModule(filePath);
     30 
     31        const sourceNode = new ConstantSourceNode(context);
     32        const workletNode =
     33            new AudioWorkletNode(context, 'input-length-processor');
     34 
     35        workletNode.connect(context.destination);
     36 
     37        // Connect the source after one render quantum.
     38        const connectFrame = RENDER_QUANTUM_FRAMES;
     39        context.suspend(connectFrame / sampleRate).then(() => {
     40          sourceNode.connect(workletNode);
     41          return context.resume();
     42        });
     43        // Disconnect the source after three render quanta.
     44        const disconnectFrame = 3 * RENDER_QUANTUM_FRAMES;
     45        context.suspend(disconnectFrame / sampleRate).then(() => {
     46          sourceNode.disconnect(workletNode);
     47          return context.resume();
     48        });
     49 
     50        sourceNode.start();
     51        const resultBuffer = await context.startRendering();
     52        const data = resultBuffer.getChannelData(0);
     53 
     54        // Before connecting the source: input array length should be 0.
     55        assert_array_equals(
     56            data.subarray(0, connectFrame),
     57            new Float32Array(connectFrame),
     58            'Before connecting the source: Input array length should be 0');
     59 
     60        // Find where the output is no longer 0.
     61        const nonZeroIndex = data.findIndex(x => x > 0);
     62        assert_equals(
     63            nonZeroIndex,
     64            connectFrame,
     65            'First non-zero output should occur exactly at connectFrame');
     66 
     67        // While source is connected: Input array length
     68        // should be RENDER_QUANTUM_FRAMES
     69        {
     70          const expectedLength = disconnectFrame - connectFrame;
     71          const expected =
     72              new Float32Array(expectedLength).fill(RENDER_QUANTUM_FRAMES);
     73          assert_array_equals(
     74              data.subarray(connectFrame, disconnectFrame), expected,
     75              'While source is connected: Input array length');
     76        }
     77 
     78        // After disconnecting the source: Input array length should be zero
     79        {
     80          const expectedLength = data.length - disconnectFrame;
     81          const expected = new Float32Array(expectedLength);
     82          assert_array_equals(
     83              data.subarray(disconnectFrame), expected,
     84              'After disconnecting the source: Input array length');
     85        }
     86      }, 'Input array length should be zero for disconnected input');
     87    </script>
     88  </body>
     89 </html>