summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-pannernode-interface/panner-automation-basic.html
blob: 5c3df0e6fdf85f9cf33314c0748e72b515f40e6f (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<!DOCTYPE html>
<html>
  <head>
    <title>
      Test Basic PannerNode with Automation Position Properties
    </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>
    <script src="../../resources/panner-formulas.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      let sampleRate = 48000;

      // These tests are quite slow, so don't run for many frames.  256 frames
      // should be enough to demonstrate that automations are working.
      let renderFrames = 256;
      let renderDuration = renderFrames / sampleRate;

      let audit = Audit.createTaskRunner();

      // Array of tests for setting the panner positions.  These tests basically
      // verify that the position setters for the panner and listener are
      // working correctly.
      let testConfig = [
        {
          setter: 'positionX',
        },
        {
          setter: 'positionY',
        },
        {
          setter: 'positionZ',
        }
      ];

      // Create tests for the panner position setters.  Both mono and steroe
      // sources are tested.
      for (let k = 0; k < testConfig.length; ++k) {
        let config = testConfig[k];
        // Function to create the test to define the test.
        let tester = function(config, channelCount) {
          return (task, should) => {
            let nodes = createGraph(channelCount);
            let {context, source, panner} = nodes;

            let message = channelCount == 1 ? 'Mono' : 'Stereo';
            message += ' panner.' + config.setter;

            testPositionSetter(should, {
              nodes: nodes,
              pannerSetter: panner[config.setter],
              message: message
            }).then(() => task.done());
          }
        };

        audit.define('Stereo panner.' + config.setter, tester(config, 2));
        audit.define('Mono panner.' + config.setter, tester(config, 1));
      }

      // Create tests for the listener position setters.  Both mono and steroe
      // sources are tested.
      for (let k = 0; k < testConfig.length; ++k) {
        let config = testConfig[k];
        // Function to create the test to define the test.
        let tester = function(config, channelCount) {
          return (task, should) => {
            let nodes = createGraph(channelCount);
            let {context, source, panner} = nodes;

            let message = channelCount == 1 ? 'Mono' : 'Stereo';
            message += ' listener.' + config.setter;

            // Some relatively arbitrary (non-default) position for the source
            // location.
            panner.setPosition(1, 0, 1);

            testPositionSetter(should, {
              nodes: nodes,
              pannerSetter: context.listener[config.setter],
              message: message
            }).then(() => task.done());
          }
        };

        audit.define('Stereo listener.' + config.setter, tester(config, 2));
        audit.define('Mono listener.' + config.setter, tester(config, 1));
      }

      // Test setPosition method.
      audit.define('setPosition', (task, should) => {
        let {context, panner, source} = createGraph(2);

        // Initialize source position (values don't really matter).
        panner.setPosition(1, 1, 1);

        // After some (unimportant) time, move the panner to a (any) new
        // location.
        let suspendFrame = 128;
        context.suspend(suspendFrame / sampleRate)
            .then(function() {
              panner.setPosition(-100, 2000, 8000);
            })
            .then(context.resume.bind(context));

        context.startRendering()
            .then(function(resultBuffer) {
              verifyPannerOutputChanged(
                  should, resultBuffer,
                  {message: 'setPosition', suspendFrame: suspendFrame});
            })
            .then(() => task.done());
      });

      audit.define('orientation setter', (task, should) => {
        let {context, panner, source} = createGraph(2);

        // For orientation to matter, we need to make the source directional,
        // and also move away from the listener (because the default location is
        // 0,0,0).
        panner.setPosition(0, 0, 1);
        panner.coneInnerAngle = 0;
        panner.coneOuterAngle = 360;
        panner.coneOuterGain = .001;

        // After some (unimportant) time, change the panner orientation to a new
        // orientation.  The only constraint is that the orientation changes
        // from before.
        let suspendFrame = 128;
        context.suspend(suspendFrame / sampleRate)
            .then(function() {
              panner.orientationX.value = -100;
              panner.orientationY.value = 2000;
              panner.orientationZ.value = 8000;
            })
            .then(context.resume.bind(context));

        context.startRendering()
            .then(function(resultBuffer) {
              verifyPannerOutputChanged(should, resultBuffer, {
                message: 'panner.orientation{XYZ}',
                suspendFrame: suspendFrame
              });
            })
            .then(() => task.done());
      });

      audit.define('forward setter', (task, should) => {
        let {context, panner, source} = createGraph(2);

        // For orientation to matter, we need to make the source directional,
        // and also move away from the listener (because the default location is
        // 0,0,0).
        panner.setPosition(0, 0, 1);
        panner.coneInnerAngle = 0;
        panner.coneOuterAngle = 360;
        panner.coneOuterGain = .001;

        // After some (unimportant) time, change the panner orientation to a new
        // orientation.  The only constraint is that the orientation changes
        // from before.
        let suspendFrame = 128;
        context.suspend(suspendFrame / sampleRate)
            .then(function() {
              context.listener.forwardX.value = -100;
              context.listener.forwardY.value = 2000;
              context.listener.forwardZ.value = 8000;
            })
            .then(context.resume.bind(context));

        context.startRendering()
            .then(function(resultBuffer) {
              verifyPannerOutputChanged(should, resultBuffer, {
                message: 'listener.forward{XYZ}',
                suspendFrame: suspendFrame
              });
            })
            .then(() => task.done());
      });

      audit.define('up setter', (task, should) => {
        let {context, panner, source} = createGraph(2);

        // For orientation to matter, we need to make the source directional,
        // and also move away from the listener (because the default location is
        // 0,0,0).
        panner.setPosition(0, 0, 1);
        panner.coneInnerAngle = 0;
        panner.coneOuterAngle = 360;
        panner.coneOuterGain = .001;
        panner.setPosition(1, 0, 1);

        // After some (unimportant) time, change the panner orientation to a new
        // orientation.  The only constraint is that the orientation changes
        // from before.
        let suspendFrame = 128;
        context.suspend(suspendFrame / sampleRate)
            .then(function() {
              context.listener.upX.value = 100;
              context.listener.upY.value = 100;
              context.listener.upZ.value = 100;
              ;
            })
            .then(context.resume.bind(context));

        context.startRendering()
            .then(function(resultBuffer) {
              verifyPannerOutputChanged(
                  should, resultBuffer,
                  {message: 'listener.up{XYZ}', suspendFrame: suspendFrame});
            })
            .then(() => task.done());
      });

      audit.run();

      function createGraph(channelCount) {
        let context = new OfflineAudioContext(2, renderFrames, sampleRate);
        let panner = context.createPanner();
        let source = context.createBufferSource();
        source.buffer =
            createConstantBuffer(context, 1, channelCount == 1 ? 1 : [1, 2]);
        source.loop = true;

        source.connect(panner);
        panner.connect(context.destination);

        source.start();
        return {context: context, source: source, panner: panner};
      }

      function testPositionSetter(should, options) {
        let {nodes, pannerSetter, message} = options;

        let {context, source, panner} = nodes;

        // Set panner x position. (Value doesn't matter);
        pannerSetter.value = 1;

        // Wait a bit and set a new position.  (Actual time and position doesn't
        // matter).
        let suspendFrame = 128;
        context.suspend(suspendFrame / sampleRate)
            .then(function() {
              pannerSetter.value = 10000;
            })
            .then(context.resume.bind(context));

        return context.startRendering().then(function(resultBuffer) {
          verifyPannerOutputChanged(
              should, resultBuffer,
              {message: message, suspendFrame: suspendFrame});
        });
      }

      function verifyPannerOutputChanged(should, resultBuffer, options) {
        let {message, suspendFrame} = options;
        // Verify that the first part of output is constant. (Doesn't matter
        // what.)
        let data0 = resultBuffer.getChannelData(0);
        let data1 = resultBuffer.getChannelData(1);

        let middle = '[0, ' + suspendFrame + ') ';
        should(
            data0.slice(0, suspendFrame),
            message + '.value frame ' + middle + 'channel 0')
            .beConstantValueOf(data0[0]);
        should(
            data1.slice(0, suspendFrame),
            message + '.value frame ' + middle + 'channel 1')
            .beConstantValueOf(data1[0]);

        // The rest after suspendTime should be constant and different from the
        // first part.
        middle = '[' + suspendFrame + ', ' + renderFrames + ') ';
        should(
            data0.slice(suspendFrame),
            message + '.value frame ' + middle + 'channel 0')
            .beConstantValueOf(data0[suspendFrame]);
        should(
            data1.slice(suspendFrame),
            message + '.value frame ' + middle + 'channel 1')
            .beConstantValueOf(data1[suspendFrame]);
        should(
            data0[suspendFrame],
            message + ': Output at frame ' + suspendFrame + ' channel 0')
            .notBeEqualTo(data0[0]);
        should(
            data1[suspendFrame],
            message + ': Output at frame ' + suspendFrame + ' channel 1')
            .notBeEqualTo(data1[0]);
      }
    </script>
  </body>
</html>