tor-browser

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

audioworkletnode-channel-count.https.html (3039B)


      1 <!DOCTYPE html>
      2 <html>
      3  <head>
      4    <title>
      5      Test AudioWorkletNode's dynamic channel count feature
      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      const sampleRate = RENDER_QUANTUM_FRAMES * 100;
     15      const renderLength = RENDER_QUANTUM_FRAMES * 2;
     16      const filePath = 'processors/gain-processor.js';
     17 
     18      const testChannelValues = [1, 2, 3];
     19 
     20      // Creates a 3-channel source and play with ConstantSourceNode.
     21      // The source goes through a bypass AudioWorkletNode (gain value of 1).
     22      // Verifies if the rendered buffer has all zero for the first half
     23      // (before 128 samples) and the expected values for the second half.
     24      promise_test(async () => {
     25        const context = new OfflineAudioContext(
     26            testChannelValues.length,
     27            renderLength,
     28            sampleRate);
     29 
     30        // Explicitly sets the destination channelCountMode and
     31        // channelInterpretation to make sure the result does no mixing.
     32        context.channelCountMode = 'explicit';
     33        context.channelInterpretation = 'discrete';
     34 
     35        await context.audioWorklet.addModule(filePath);
     36 
     37        const gainWorkletNode = new AudioWorkletNode(context, 'gain');
     38        gainWorkletNode.parameters.get('gain').value = 1.0;
     39 
     40        // Create a ChannelMergerNode to combine multiple ConstantSourceNodes
     41        const merger = new ChannelMergerNode(
     42            context, {numberOfInputs: testChannelValues.length});
     43        merger.connect(gainWorkletNode);
     44        gainWorkletNode.connect(context.destination);
     45 
     46        // Create a ConstantSourceNode for each channel with its
     47        // corresponding value
     48        const constantSources = testChannelValues.map((value, index) => {
     49          const constantSource =
     50              new ConstantSourceNode(context, {offset: value});
     51          constantSource.connect(merger, 0, index);
     52          return constantSource;
     53        });
     54 
     55        // Suspend the context at 128 sample frames and start the sources.
     56        context.suspend(RENDER_QUANTUM_FRAMES/sampleRate).then(() => {
     57          constantSources.forEach(source => source.start());
     58          context.resume();
     59        });
     60 
     61        const renderedBuffer = await context.startRendering();
     62 
     63        testChannelValues.forEach((value, index) => {
     64          const channelData = renderedBuffer.getChannelData(index);
     65 
     66          assert_constant_value(
     67              channelData.subarray(0, RENDER_QUANTUM_FRAMES),
     68              0,
     69              'First half of Channel #' + index);
     70 
     71          assert_constant_value(
     72              channelData.subarray(RENDER_QUANTUM_FRAMES, renderLength),
     73              value,
     74              'Second half of Channel #' + index);
     75        });
     76      }, 'setup-buffer-and-worklet-verify-rendered-buffer');
     77    </script>
     78  </body>
     79 </html>