summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-pannernode-interface/panner-rolloff-clamping.html
blob: 387f87301092ebd725ed240cc70f0df7ec69a5ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!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>