summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-audioparam-interface/k-rate-panner-connections.html
blob: 001cf63bd36dccea41112f865f6b0747f8168906 (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
<!doctype html>
<html>
  <head>
    <title>
      k-rate AudioParams with inputs for PannerNode
    </title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="/webaudio/resources/audit.js"></script>
    <script src="/webaudio/resources/audit-util.js"></script>
    </title>
  </head>

  <body>
    <script>
      let audit = Audit.createTaskRunner();

      audit.define(
          {label: 'Panner x', description: 'k-rate input'},
          async (task, should) => {
            await testPannerParams(should, {param: 'positionX'});
            task.done();
          });

      audit.define(
          {label: 'Panner y', description: 'k-rate input'},
          async (task, should) => {
            await testPannerParams(should, {param: 'positionY'});
            task.done();
          });

      audit.define(
          {label: 'Panner z', description: 'k-rate input'},
          async (task, should) => {
            await testPannerParams(should, {param: 'positionZ'});
            task.done();
          });

      audit.define(
          {label: 'Listener x', description: 'k-rate input'},
          async (task, should) => {
            await testListenerParams(should, {param: 'positionX'});
            task.done();
          });

      audit.define(
          {label: 'Listener y', description: 'k-rate input'},
          async (task, should) => {
            await testListenerParams(should, {param: 'positionY'});
            task.done();
          });

      audit.define(
          {label: 'Listener z', description: 'k-rate input'},
          async (task, should) => {
            await testListenerParams(should, {param: 'positionZ'});
            task.done();
          });

      audit.run();

      async function testPannerParams(should, options) {
        // Arbitrary sample rate and duration.
        const sampleRate = 8000;
        const testFrames = 5 * RENDER_QUANTUM_FRAMES;
        let testDuration = testFrames / sampleRate;
        // Four channels needed because the first two are for the output of
        // the reference panner, and the next two are for the test panner.
        let context = new OfflineAudioContext({
          numberOfChannels: 4,
          sampleRate: sampleRate,
          length: testDuration * sampleRate
        });

        let merger = new ChannelMergerNode(
            context, {numberOfInputs: context.destination.channelCount});
        merger.connect(context.destination);

        // Create a stereo source out of two mono sources
        let src0 = new ConstantSourceNode(context, {offset: 1});
        let src1 = new ConstantSourceNode(context, {offset: 2});
        let src = new ChannelMergerNode(context, {numberOfInputs: 2});
        src0.connect(src, 0, 0);
        src1.connect(src, 0, 1);

        let finalPosition = 100;

        // Reference panner node with k-rate AudioParam automations.  The
        // output of this panner is the reference output.
        let refNode = new PannerNode(context);
        // Initialize the panner location to somewhat arbitrary values.
        refNode.positionX.value = 1;
        refNode.positionY.value = 50;
        refNode.positionZ.value = -25;

        // Set the AudioParam under test with the appropriate automations.
        refNode[options.param].automationRate = 'k-rate';
        refNode[options.param].setValueAtTime(1, 0);
        refNode[options.param].linearRampToValueAtTime(
            finalPosition, testDuration);
        let refSplit = new ChannelSplitterNode(context, {numberOfOutputs: 2});

        // Test panner node with k-rate AudioParam with inputs.
        let tstNode = new PannerNode(context);
        tstNode.positionX.value = 1;
        tstNode.positionY.value = 50;
        tstNode.positionZ.value = -25;
        tstNode[options.param].value = 0;
        tstNode[options.param].automationRate = 'k-rate';
        let tstSplit = new ChannelSplitterNode(context, {numberOfOutputs: 2});

        // The input to the AudioParam.  It must have the same automation
        // sequence as used by refNode.  And must be a-rate to demonstrate
        // the k-rate effect of the AudioParam.
        let mod = new ConstantSourceNode(context, {offset: 0});
        mod.offset.setValueAtTime(1, 0);
        mod.offset.linearRampToValueAtTime(finalPosition, testDuration);

        mod.connect(tstNode[options.param]);

        src.connect(refNode).connect(refSplit);
        src.connect(tstNode).connect(tstSplit);

        refSplit.connect(merger, 0, 0);
        refSplit.connect(merger, 1, 1);
        tstSplit.connect(merger, 0, 2);
        tstSplit.connect(merger, 1, 3);

        mod.start();
        src0.start();
        src1.start();

        const buffer = await context.startRendering();
        let expected0 = buffer.getChannelData(0);
        let expected1 = buffer.getChannelData(1);
        let actual0 = buffer.getChannelData(2);
        let actual1 = buffer.getChannelData(3);

        should(expected0, `Panner: ${options.param}: Expected output channel 0`)
            .notBeConstantValueOf(expected0[0]);
        should(expected1, `${options.param}: Expected output channel 1`)
            .notBeConstantValueOf(expected1[0]);

        // Verify output is a stair step because positionX is k-rate,
        // and no other AudioParam is changing.

        for (let k = 0; k < testFrames; k += RENDER_QUANTUM_FRAMES) {
          should(
              actual0.slice(k, k + RENDER_QUANTUM_FRAMES),
              `Panner: ${options.param}: Channel 0 output[${k}, ${
                  k + RENDER_QUANTUM_FRAMES - 1}]`)
              .beConstantValueOf(actual0[k]);
        }

        for (let k = 0; k < testFrames; k += RENDER_QUANTUM_FRAMES) {
          should(
              actual1.slice(k, k + RENDER_QUANTUM_FRAMES),
              `Panner: ${options.param}: Channel 1 output[${k}, ${
                  k + RENDER_QUANTUM_FRAMES - 1}]`)
              .beConstantValueOf(actual1[k]);
        }

        should(actual0, `Panner: ${options.param}: Actual output channel 0`)
            .beCloseToArray(expected0, {absoluteThreshold: 0});
        should(actual1, `Panner: ${options.param}: Actual output channel 1`)
            .beCloseToArray(expected1, {absoluteThreshold: 0});
      }

      async function testListenerParams(should, options) {
        // Arbitrary sample rate and duration.
        const sampleRate = 8000;
        const testFrames = 5 * RENDER_QUANTUM_FRAMES;
        let testDuration = testFrames / sampleRate;
        // Four channels needed because the first two are for the output of
        // the reference panner, and the next two are for the test panner.
        let context = new OfflineAudioContext({
          numberOfChannels: 2,
          sampleRate: sampleRate,
          length: testDuration * sampleRate
        });

        // Create a stereo source out of two mono sources
        let src0 = new ConstantSourceNode(context, {offset: 1});
        let src1 = new ConstantSourceNode(context, {offset: 2});
        let src = new ChannelMergerNode(context, {numberOfInputs: 2});
        src0.connect(src, 0, 0);
        src1.connect(src, 0, 1);

        let finalPosition = 100;

        // Reference panner node with k-rate AudioParam automations.  The
        // output of this panner is the reference output.
        let panner = new PannerNode(context);
        panner.positionX.value = 10;
        panner.positionY.value = 50;
        panner.positionZ.value = -25;

        src.connect(panner);

        let mod = new ConstantSourceNode(context, {offset: 0});
        mod.offset.setValueAtTime(1, 0);
        mod.offset.linearRampToValueAtTime(finalPosition, testDuration);

        context.listener[options.param].automationRate = 'k-rate';
        mod.connect(context.listener[options.param]);

        panner.connect(context.destination);

        src0.start();
        src1.start();
        mod.start();

        const buffer = await context.startRendering();
        let c0 = buffer.getChannelData(0);
        let c1 = buffer.getChannelData(1);

        // Verify output is a stair step because positionX is k-rate,
        // and no other AudioParam is changing.

        for (let k = 0; k < testFrames; k += RENDER_QUANTUM_FRAMES) {
          should(
              c0.slice(k, k + RENDER_QUANTUM_FRAMES),
              `Listener: ${options.param}: Channel 0 output[${k}, ${
                  k + RENDER_QUANTUM_FRAMES - 1}]`)
              .beConstantValueOf(c0[k]);
        }

        for (let k = 0; k < testFrames; k += RENDER_QUANTUM_FRAMES) {
          should(
              c1.slice(k, k + RENDER_QUANTUM_FRAMES),
              `Listener: ${options.param}: Channel 1 output[${k}, ${
                  k + RENDER_QUANTUM_FRAMES - 1}]`)
              .beConstantValueOf(c1[k]);
        }
      }
    </script>
  </body>
</html>