tor-browser

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

panner-automation-position.html (10418B)


      1 <!DOCTYPE html>
      2 <html>
      3  <head>
      4    <title>
      5      Test Automation of PannerNode Positions
      6    </title>
      7    <script src="/resources/testharness.js"></script>
      8    <script src="/resources/testharnessreport.js"></script>
      9    <script src="../../resources/audit-util.js"></script>
     10    <script src="../../resources/audit.js"></script>
     11    <script src="../../resources/panner-formulas.js"></script>
     12  </head>
     13  <body>
     14    <script id="layout-test-code">
     15      let sampleRate = 48000;
     16      // These tests are quite slow, so don't run for many frames.  256 frames
     17      // should be enough to demonstrate that automations are working.
     18      let renderFrames = 256;
     19      let renderDuration = renderFrames / sampleRate;
     20 
     21      let context;
     22      let panner;
     23 
     24      let audit = Audit.createTaskRunner();
     25 
     26      // Set of tests for the panner node with automations applied to the
     27      // position of the source.
     28      let testConfigs = [
     29        {
     30          // Distance model parameters for the panner
     31          distanceModel: {model: 'inverse', rolloff: 1},
     32          // Initial location of the source
     33          startPosition: [0, 0, 1],
     34          // Final position of the source.  For this test, we only want to move
     35          // on the z axis which
     36          // doesn't change the azimuth angle.
     37          endPosition: [0, 0, 10000],
     38        },
     39        {
     40          distanceModel: {model: 'inverse', rolloff: 1},
     41          startPosition: [0, 0, 1],
     42          // An essentially random end position, but it should be such that
     43          // azimuth angle changes as
     44          // we move from the start to the end.
     45          endPosition: [20000, 30000, 10000],
     46          errorThreshold: [
     47            {
     48              // Error threshold for 1-channel case
     49              relativeThreshold: 4.8124e-7
     50            },
     51            {
     52              // Error threshold for 2-channel case
     53              relativeThreshold: 4.3267e-7
     54            }
     55          ],
     56        },
     57        {
     58          distanceModel: {model: 'exponential', rolloff: 1.5},
     59          startPosition: [0, 0, 1],
     60          endPosition: [20000, 30000, 10000],
     61          errorThreshold:
     62              [{relativeThreshold: 5.0783e-7}, {relativeThreshold: 5.2180e-7}]
     63        },
     64        {
     65          distanceModel: {model: 'linear', rolloff: 1},
     66          startPosition: [0, 0, 1],
     67          endPosition: [20000, 30000, 10000],
     68          errorThreshold: [
     69            {relativeThreshold: 6.5324e-6}, {relativeThreshold: 6.5756e-6}
     70          ]
     71        }
     72      ];
     73 
     74      for (let k = 0; k < testConfigs.length; ++k) {
     75        let config = testConfigs[k];
     76        let tester = function(c, channelCount) {
     77          return (task, should) => {
     78            runTest(should, c, channelCount).then(() => task.done());
     79          }
     80        };
     81 
     82        let baseTestName = config.distanceModel.model +
     83            ' rolloff: ' + config.distanceModel.rolloff;
     84 
     85        // Define tasks for both 1-channel and 2-channel
     86        audit.define(k + ': 1-channel ' + baseTestName, tester(config, 1));
     87        audit.define(k + ': 2-channel ' + baseTestName, tester(config, 2));
     88      }
     89 
     90      audit.run();
     91 
     92      function runTest(should, options, channelCount) {
     93        // Output has 5 channels: channels 0 and 1 are for the stereo output of
     94        // the panner node. Channels 2-5 are the for automation of the x,y,z
     95        // coordinate so that we have actual coordinates used for the panner
     96        // automation.
     97        context = new OfflineAudioContext(5, renderFrames, sampleRate);
     98 
     99        // Stereo source for the panner.
    100        let source = context.createBufferSource();
    101        source.buffer = createConstantBuffer(
    102            context, renderFrames, channelCount == 1 ? 1 : [1, 2]);
    103 
    104        panner = context.createPanner();
    105        panner.distanceModel = options.distanceModel.model;
    106        panner.rolloffFactor = options.distanceModel.rolloff;
    107        panner.panningModel = 'equalpower';
    108 
    109        // Source and gain node for the z-coordinate calculation.
    110        let dist = context.createBufferSource();
    111        dist.buffer = createConstantBuffer(context, 1, 1);
    112        dist.loop = true;
    113        let gainX = context.createGain();
    114        let gainY = context.createGain();
    115        let gainZ = context.createGain();
    116        dist.connect(gainX);
    117        dist.connect(gainY);
    118        dist.connect(gainZ);
    119 
    120        // Set the gain automation to match the z-coordinate automation of the
    121        // panner.
    122 
    123        // End the automation some time before the end of the rendering so we
    124        // can verify that automation has the correct end time and value.
    125        let endAutomationTime = 0.75 * renderDuration;
    126 
    127        gainX.gain.setValueAtTime(options.startPosition[0], 0);
    128        gainX.gain.linearRampToValueAtTime(
    129            options.endPosition[0], endAutomationTime);
    130        gainY.gain.setValueAtTime(options.startPosition[1], 0);
    131        gainY.gain.linearRampToValueAtTime(
    132            options.endPosition[1], endAutomationTime);
    133        gainZ.gain.setValueAtTime(options.startPosition[2], 0);
    134        gainZ.gain.linearRampToValueAtTime(
    135            options.endPosition[2], endAutomationTime);
    136 
    137        dist.start();
    138 
    139        // Splitter and merger to map the panner output and the z-coordinate
    140        // automation to the correct channels in the destination.
    141        let splitter = context.createChannelSplitter(2);
    142        let merger = context.createChannelMerger(5);
    143 
    144        source.connect(panner);
    145        // Split the output of the panner to separate channels
    146        panner.connect(splitter);
    147 
    148        // Merge the panner outputs and the z-coordinate output to the correct
    149        // destination channels.
    150        splitter.connect(merger, 0, 0);
    151        splitter.connect(merger, 1, 1);
    152        gainX.connect(merger, 0, 2);
    153        gainY.connect(merger, 0, 3);
    154        gainZ.connect(merger, 0, 4);
    155 
    156        merger.connect(context.destination);
    157 
    158        // Initialize starting point of the panner.
    159        panner.positionX.setValueAtTime(options.startPosition[0], 0);
    160        panner.positionY.setValueAtTime(options.startPosition[1], 0);
    161        panner.positionZ.setValueAtTime(options.startPosition[2], 0);
    162 
    163        // Automate z coordinate to move away from the listener
    164        panner.positionX.linearRampToValueAtTime(
    165            options.endPosition[0], 0.75 * renderDuration);
    166        panner.positionY.linearRampToValueAtTime(
    167            options.endPosition[1], 0.75 * renderDuration);
    168        panner.positionZ.linearRampToValueAtTime(
    169            options.endPosition[2], 0.75 * renderDuration);
    170 
    171        source.start();
    172 
    173        // Go!
    174        return context.startRendering().then(function(renderedBuffer) {
    175          // Get the panner outputs
    176          let data0 = renderedBuffer.getChannelData(0);
    177          let data1 = renderedBuffer.getChannelData(1);
    178          let xcoord = renderedBuffer.getChannelData(2);
    179          let ycoord = renderedBuffer.getChannelData(3);
    180          let zcoord = renderedBuffer.getChannelData(4);
    181 
    182          // We're doing a linear ramp on the Z axis with the equalpower panner,
    183          // so the equalpower panning gain remains constant.  We only need to
    184          // model the distance effect.
    185 
    186          // Compute the distance gain
    187          let distanceGain = new Float32Array(xcoord.length);
    188          ;
    189 
    190          if (panner.distanceModel === 'inverse') {
    191            for (let k = 0; k < distanceGain.length; ++k) {
    192              distanceGain[k] =
    193                  inverseDistance(panner, xcoord[k], ycoord[k], zcoord[k])
    194            }
    195          } else if (panner.distanceModel === 'linear') {
    196            for (let k = 0; k < distanceGain.length; ++k) {
    197              distanceGain[k] =
    198                  linearDistance(panner, xcoord[k], ycoord[k], zcoord[k])
    199            }
    200          } else if (panner.distanceModel === 'exponential') {
    201            for (let k = 0; k < distanceGain.length; ++k) {
    202              distanceGain[k] =
    203                  exponentialDistance(panner, xcoord[k], ycoord[k], zcoord[k])
    204            }
    205          }
    206 
    207          // Compute the expected result.  Since we're on the z-axis, the left
    208          // and right channels pass through the equalpower panner unchanged.
    209          // Only need to apply the distance gain.
    210          let buffer0 = source.buffer.getChannelData(0);
    211          let buffer1 =
    212              channelCount == 2 ? source.buffer.getChannelData(1) : buffer0;
    213 
    214          let azimuth = new Float32Array(buffer0.length);
    215 
    216          for (let k = 0; k < data0.length; ++k) {
    217            azimuth[k] = calculateAzimuth(
    218                [xcoord[k], ycoord[k], zcoord[k]],
    219                [
    220                  context.listener.positionX.value,
    221                  context.listener.positionY.value,
    222                  context.listener.positionZ.value
    223                ],
    224                [
    225                  context.listener.forwardX.value,
    226                  context.listener.forwardY.value,
    227                  context.listener.forwardZ.value
    228                ],
    229                [
    230                  context.listener.upX.value, context.listener.upY.value,
    231                  context.listener.upZ.value
    232                ]);
    233          }
    234 
    235          let expected = applyPanner(azimuth, buffer0, buffer1, channelCount);
    236          let expected0 = expected.left;
    237          let expected1 = expected.right;
    238 
    239          for (let k = 0; k < expected0.length; ++k) {
    240            expected0[k] *= distanceGain[k];
    241            expected1[k] *= distanceGain[k];
    242          }
    243 
    244          let info = options.distanceModel.model +
    245              ', rolloff: ' + options.distanceModel.rolloff;
    246          let prefix = channelCount + '-channel ' +
    247              '[' + options.startPosition[0] + ', ' + options.startPosition[1] +
    248              ', ' + options.startPosition[2] + '] -> [' +
    249              options.endPosition[0] + ', ' + options.endPosition[1] + ', ' +
    250              options.endPosition[2] + ']: ';
    251 
    252          let errorThreshold = 0;
    253 
    254          if (options.errorThreshold)
    255            errorThreshold = options.errorThreshold[channelCount - 1]
    256 
    257            should(data0, prefix + 'distanceModel: ' + info + ', left channel')
    258                .beCloseToArray(expected0, {absoluteThreshold: errorThreshold});
    259          should(data1, prefix + 'distanceModel: ' + info + ', right channel')
    260              .beCloseToArray(expected1, {absoluteThreshold: errorThreshold});
    261        });
    262      }
    263    </script>
    264  </body>
    265 </html>