tor-browser

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

simple-input-output.https.html (2529B)


      1 <!DOCTYPE html>
      2 <html>
      3  <head>
      4    <title>
      5      Test Simple AudioWorklet I/O
      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      // Arbitrary sample rate
     15      const sampleRate = 48000;
     16 
     17      // The offset to be applied by the worklet to its inputs.
     18      const offset = 1;
     19 
     20      // Location of the worklet's code
     21      const filePath = 'processors/add-offset.js';
     22 
     23      promise_test(async () => {
     24        // Two channels for testing.  Channel 0 is the output of the
     25        // AudioWorklet.  Channel 1 is the oscillator so we can compare
     26        // the outputs.
     27        const context = new OfflineAudioContext(
     28            {numberOfChannels: 2, length: sampleRate, sampleRate: sampleRate});
     29 
     30        // Load up the code for the worklet.
     31        await context.audioWorklet.addModule(filePath);
     32 
     33        const merger = new ChannelMergerNode(
     34            context, {numberOfChannels: context.destination.channelCount});
     35        merger.connect(context.destination);
     36 
     37        const src = new OscillatorNode(context);
     38 
     39        const worklet = new AudioWorkletNode(
     40            context, 'add-offset-processor',
     41            {processorOptions: {offset: offset}});
     42 
     43        src.connect(worklet).connect(merger, 0, 0);
     44        src.connect(merger, 0, 1);
     45 
     46        // Start and stop the source.  The stop time is fairly arbitrary,
     47        // but use a render quantum boundary for simplicity.
     48        const stopFrame = RENDER_QUANTUM_FRAMES;
     49        src.start();
     50        src.stop(stopFrame / context.sampleRate);
     51 
     52        const resultBuffer = await context.startRendering();
     53        const ch0 = resultBuffer.getChannelData(0);
     54        const ch1 = resultBuffer.getChannelData(1);
     55 
     56        const shifted = ch1.slice(0, stopFrame).map(x => x + offset);
     57 
     58        // The initial part of the output should be the oscillator
     59        // shifted by |offset|.
     60        assert_array_equal_within_eps(
     61            ch0.slice(0, stopFrame),
     62            shifted,
     63            {absoluteThreshold: 0},
     64            `AudioWorklet output[0:${stopFrame - 1}]`);
     65 
     66        // Output should be constant after the source has stopped.
     67        assert_array_equals(
     68            ch0.slice(stopFrame),
     69            new Float32Array(ch0.slice(stopFrame).length).fill(offset),
     70            `AudioWorklet output[${stopFrame}:]`);
     71      }, 'Simple AudioWorklet I/O');
     72    </script>
     73  </body>
     74 </html>