tor-browser

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

k-rate-audioworklet-connections.https.html (2289B)


      1 <!DOCTYPE html>
      2 <html>
      3  <head>
      4    <title>
      5      Test k-rate AudioParams with inputs for AudioWorkletNode
      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 
     12  <body>
     13    <script>
     14      // Use the worklet gain node to test k-rate parameters.
     15      const filePath =
     16          '../the-audioworklet-interface/processors/gain-processor.js';
     17 
     18      promise_test(async () => {
     19        // Arbitrary sample rate and duration.
     20        const sampleRate = 8000;
     21 
     22        // Only new a few render quanta to verify things are working.
     23        const testDuration = 4 * 128 / sampleRate;
     24 
     25        const context = new OfflineAudioContext({
     26          numberOfChannels: 3,
     27          sampleRate: sampleRate,
     28          length: testDuration * sampleRate,
     29        });
     30 
     31        await context.audioWorklet.addModule(filePath);
     32 
     33        const src = new ConstantSourceNode(context);
     34        const kRateNode = new AudioWorkletNode(context, 'gain');
     35        src.connect(kRateNode).connect(context.destination);
     36 
     37        const kRateParam = kRateNode.parameters.get('gain');
     38        kRateParam.automationRate = 'k-rate';
     39        kRateParam.value = 0;
     40 
     41        const mod = new ConstantSourceNode(context);
     42        mod.offset.setValueAtTime(0, 0);
     43        mod.offset.linearRampToValueAtTime(
     44            10, context.length / context.sampleRate);
     45        mod.connect(kRateParam);
     46 
     47        mod.start();
     48        src.start();
     49 
     50        const audioBuffer = await context.startRendering();
     51        const output = audioBuffer.getChannelData(0);
     52 
     53        // Verify that the output isn't constantly zero.
     54        const allZero = !output.some((value) => value !== 0);
     55        assert_false(allZero, `output should not be all zeros`);
     56 
     57        // Verify that the output from the worklet is step-wise
     58        // constant.
     59        for (let k = 0; k < output.length; k += 128) {
     60          const slice = output.slice(k, k + 128);
     61          const expected = new Float32Array(slice.length).fill(slice[0]);
     62          assert_array_equals(
     63              slice, expected, `k-rate output [${k}: ${k + 127}]`);
     64        }
     65      }, 'AudioWorklet k-rate AudioParam');
     66    </script>
     67  </body>
     68 </html>