tor-browser

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

k-rate-audioworklet.https.html (1903B)


      1 <!DOCTYPE html>
      2 <html>
      3  <head>
      4    <title>Test k-rate AudioParam of AudioWorkletNode</title>
      5    <script src="/resources/testharness.js"></script>
      6    <script src="/resources/testharnessreport.js"></script>
      7  </head>
      8 
      9  <body>
     10    <script>
     11      // Use the worklet gain node to test k-rate parameters.
     12      const filePath =
     13          '../the-audioworklet-interface/processors/gain-processor.js';
     14 
     15      // Context for testing
     16      let context;
     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        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 
     40        // Automate the gain
     41        kRateParam.setValueAtTime(0, 0);
     42        kRateParam.linearRampToValueAtTime(
     43            10, context.length / context.sampleRate);
     44 
     45        src.start();
     46 
     47        const audioBuffer = await context.startRendering();
     48        const output = audioBuffer.getChannelData(0);
     49 
     50        // Verify that the output from the worklet is step-wise
     51        // constant.
     52        for (let k = 0; k < output.length; k += 128) {
     53          assert_array_equals(
     54              output.slice(k, k + 128),
     55              new Float32Array(128).fill(output[k]),
     56              `k-rate output [${k}: ${k + 127}]`);
     57        }
     58      }, 'AudioWorklet k-rate AudioParam');
     59    </script>
     60  </body>
     61 </html>