<!DOCTYPE html>
<html>
  <head>
    <title>
      Test Clamping of PannerNode rolloffFactor
    </title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="../../resources/audit-util.js"></script>
    <script src="../../resources/audit.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      // Fairly arbitrary sample rate and render frames.
      let sampleRate = 16000;
      let renderFrames = 2048;

      let audit = Audit.createTaskRunner();

      audit.define(
          {
            label: 'linear-clamp-high',
            description: 'rolloffFactor clamping for linear distance model'
          },
          (task, should) => {
            runTest(should, {
              distanceModel: 'linear',
              // Fairly arbitrary value outside the nominal range
              rolloffFactor: 2,
              clampedRolloff: 1
            }).then(() => task.done());
          });

      // Test clamping of the rolloffFactor.  The test is done by comparing the
      // output of a panner with the rolloffFactor set outside the nominal range
      // against the output of a panner with the rolloffFactor clamped to the
      // nominal range.  The outputs should be the same.
      //
      // The |options| dictionary should contain the members
      //   distanceModel  - The distance model to use for the panners
      //   rolloffFactor  - The desired rolloffFactor.  Should be outside the
      //                    nominal range of the distance model.
      //   clampedRolloff - The rolloffFactor (above) clamped to the nominal
      //                    range for the given distance model.
      function runTest(should, options) {
        // Offline context with two channels.  The first channel is the panner
        // node under test.  The second channel is the reference panner node.
        let context = new OfflineAudioContext(2, renderFrames, sampleRate);

        // The source for the panner nodes.  This is fairly arbitrary.
        let src = new OscillatorNode(context, {type: 'sawtooth'});

        // Create the test panner with the specified rolloff factor.  The
        // position is fairly arbitrary, but something that is not the default
        // is good to show the distance model had some effect.
        let pannerTest = new PannerNode(context, {
          rolloffFactor: options.rolloffFactor,
          distanceModel: options.distanceModel,
          positionX: 5000
        });

        // Create the reference panner with the rolloff factor clamped to the
        // appropriate limit.
        let pannerRef = new PannerNode(context, {
          rolloffFactor: options.clampedRolloff,
          distanceModel: options.distanceModel,
          positionX: 5000
        });


        // Connect the source to the panners to the destination appropriately.
        let merger = new ChannelMergerNode(context, {numberOfInputs: 2});


        src.connect(pannerTest).connect(merger, 0, 0);
        src.connect(pannerRef).connect(merger, 0, 1);

        merger.connect(context.destination);

        src.start();

        return context.startRendering().then(function(resultBuffer) {
          // The two channels should be the same due to the clamping.  Verify
          // that they are the same.
          let actual = resultBuffer.getChannelData(0);
          let expected = resultBuffer.getChannelData(1);

          let message = 'Panner distanceModel: "' + options.distanceModel +
              '", rolloffFactor: ' + options.rolloffFactor;

          should(actual, message).beEqualToArray(expected);
        });
      }

      audit.run();
    </script>
  </body>
</html>
