tor-browser

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

simple-input-output.html (3059B)


      1 <!DOCTYPE html>
      2 <html>
      3 <head>
      4  <title>Test ScriptProcessorNode</title>
      5  <script src="/resources/testharness.js"></script>
      6  <script src="/resources/testharnessreport.js"></script>
      7  <script src="/webaudio/resources/audit-util.js"></script>
      8 </head>
      9 <body>
     10  <script>
     11    // Arbitrary sample rate
     12    const sampleRate = 48000;
     13 
     14    promise_test(async (t) => {
     15      // Two channels for testing.  Channel 0 is the output of the
     16      // scriptProcessor.  Channel 1 is the oscillator so we can compare
     17      // the outputs.
     18      const context = new OfflineAudioContext({
     19        numberOfChannels: 2,
     20        length: sampleRate,
     21        sampleRate: sampleRate});
     22 
     23      const merger = new ChannelMergerNode(
     24          context, {numberOfChannels: context.destination.channelCount});
     25      merger.connect(context.destination);
     26 
     27      const src = new OscillatorNode(context);
     28 
     29      // Arbitrary buffer size for the ScriptProcessorNode.  Don't use 0;
     30      // we need to know the actual size to know the latency of the node
     31      // (easily).
     32      const spnSize = 512;
     33      const spn = context.createScriptProcessor(spnSize, 1, 1);
     34 
     35      // Arrange for the ScriptProcessor to add |offset| to the input.
     36      const offset = 1;
     37      spn.onaudioprocess = (event) => {
     38        const input = event.inputBuffer.getChannelData(0);
     39        const output = event.outputBuffer.getChannelData(0);
     40        for (let k = 0; k < output.length; ++k) {
     41          output[k] = input[k] + offset;
     42        }
     43      };
     44 
     45      src.connect(spn).connect(merger, 0, 0);
     46      src.connect(merger, 0, 1);
     47 
     48      // Start and stop the source.  The stop time is fairly arbitrary,
     49      // but use a render quantum boundary for simplicity.
     50      const stopFrame = RENDER_QUANTUM_FRAMES;
     51      src.start();
     52      src.stop(stopFrame / context.sampleRate);
     53 
     54      const buffer = await context.startRendering();
     55      const ch0 = buffer.getChannelData(0);
     56      const ch1 = buffer.getChannelData(1);
     57 
     58      const shifted = ch1.slice(0, stopFrame).map((x) => x + offset);
     59 
     60      // SPN has a basic latency of 2*|spnSize| fraems, so the
     61      // beginning is silent.
     62      assert_array_equals(
     63          ch0.slice(0, 2 * spnSize - 1),
     64          new Float32Array(2 * spnSize - 1),
     65          `ScriptProcessor output[0:${2 * spnSize - 1}]`);
     66 
     67      // For the middle section (after adding latency), the output
     68      // should be the source shifted by |offset|.
     69      assert_array_equals(
     70          ch0.slice(2 * spnSize, 2 * spnSize + stopFrame),
     71          new Float32Array(shifted),
     72          `ScriptProcessor output[${2 * spnSize}:` +
     73              `${2 * spnSize + stopFrame - 1}]`);
     74 
     75      // Output should be constant after the source has stopped.
     76      // Include the latency introduced by the node.
     77      assert_array_equals(
     78        ch0.slice(2 * spnSize + stopFrame),
     79        new Float32Array(ch0.length - (2 * spnSize + stopFrame)).fill(offset),
     80        `ScriptProcessor output[${2 * spnSize + stopFrame}:]`);
     81    }, 'ScriptProcessor with stopped input source');
     82  </script>
     83 </body>
     84 </html>