summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-audioparam-interface/k-rate-biquad-connection.html
blob: ab9df8740fab30d0395750590f9832b65f257a92 (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
<!doctype html>
<html>
  <head>
    <title>Test k-rate AudioParam Inputs for BiquadFilterNode</title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="/webaudio/resources/audit-util.js"></script>
    <script src="/webaudio/resources/audit.js"></script>
  </head>

  <body>
    <script>
      // sampleRate and duration are fairly arbitrary.  We use low values to
      // limit the complexity of the test.
      let sampleRate = 8192;
      let testDuration = 0.5;

      let audit = Audit.createTaskRunner();

      audit.define(
          {label: 'Frequency AudioParam', description: 'k-rate input works'},
          async (task, should) => {
            // Test frequency AudioParam using a lowpass filter whose bandwidth
            // is initially larger than the oscillator frequency.  Then automate
            // the frequency to 0 so that the output of the filter is 0 (because
            // the cutoff is 0).
            let oscFrequency = 440;

            let options = {
              sampleRate: sampleRate,
              paramName: 'frequency',
              oscFrequency: oscFrequency,
              testDuration: testDuration,
              filterOptions: {type: 'lowpass', frequency: 0},
              autoStart:
                  {method: 'setValueAtTime', args: [2 * oscFrequency, 0]},
              autoEnd: {
                method: 'linearRampToValueAtTime',
                args: [0, testDuration / 4]
              }
            };

            let buffer = await doTest(should, options);
            let expected = buffer.getChannelData(0);
            let actual = buffer.getChannelData(1);
            let halfLength = expected.length / 2;

            // Sanity check.  The expected output should not be zero for
            // the first half, but should be zero for the second half
            // (because the filter bandwidth is exactly 0).
            const prefix = 'Expected k-rate frequency with automation';

            should(
                expected.slice(0, halfLength),
                `${prefix} output[0:${halfLength - 1}]`)
                .notBeConstantValueOf(0);
            should(
                expected.slice(expected.length),
                `${prefix} output[${halfLength}:]`)
                .beConstantValueOf(0);

            // Outputs should be the same.  Break the message into two
            // parts so we can see the expected outputs.
            checkForSameOutput(should, options.paramName, actual, expected);

            task.done();
          });

      audit.define(
          {label: 'Q AudioParam', description: 'k-rate input works'},
          async (task, should) => {
            // Test Q AudioParam.  Use a bandpass filter whose center frequency
            // is fairly far from the oscillator frequency.  Then start with a Q
            // value of 0 (so everything goes through) and then increase Q to
            // some large value such that the out-of-band signals are basically
            // cutoff.
            let frequency = 440;
            let oscFrequency = 4 * frequency;

            let options = {
              sampleRate: sampleRate,
              oscFrequency: oscFrequency,
              testDuration: testDuration,
              paramName: 'Q',
              filterOptions: {type: 'bandpass', frequency: frequency, Q: 0},
              autoStart: {method: 'setValueAtTime', args: [0, 0]},
              autoEnd: {
                method: 'linearRampToValueAtTime',
                args: [100, testDuration / 4]
              }
            };

            const buffer = await doTest(should, options);
            let expected = buffer.getChannelData(0);
            let actual = buffer.getChannelData(1);

            // Outputs should be the same
            checkForSameOutput(should, options.paramName, actual, expected);

            task.done();
          });

      audit.define(
          {label: 'Gain AudioParam', description: 'k-rate input works'},
          async (task, should) => {
            // Test gain AudioParam.  Use a peaking filter with a large Q so the
            // peak is narrow with a center frequency the same as the oscillator
            // frequency.  Start with a gain of 0 so everything goes through and
            // then ramp the gain down to -100 so that the oscillator is
            // filtered out.
            let oscFrequency = 4 * 440;

            let options = {
              sampleRate: sampleRate,
              oscFrequency: oscFrequency,
              testDuration: testDuration,
              paramName: 'gain',
              filterOptions:
                  {type: 'peaking', frequency: oscFrequency, Q: 100, gain: 0},
              autoStart: {method: 'setValueAtTime', args: [0, 0]},
              autoEnd: {
                method: 'linearRampToValueAtTime',
                args: [-100, testDuration / 4]
              }
            };

            const buffer = await doTest(should, options);
            let expected = buffer.getChannelData(0);
            let actual = buffer.getChannelData(1);

            // Outputs should be the same
            checkForSameOutput(should, options.paramName, actual, expected);

            task.done();
          });

      audit.define(
          {label: 'Detune AudioParam', description: 'k-rate input works'},
          async (task, should) => {
            // Test detune AudioParam.  The basic idea is the same as the
            // frequency test above, but insteda of automating the frequency, we
            // automate the detune value so that initially the filter cutuff is
            // unchanged and then changing the detune until the cutoff goes to 1
            // Hz, which would cause the oscillator to be filtered out.
            let oscFrequency = 440;
            let filterFrequency = 5 * oscFrequency;

            // For a detune value d, the computed frequency, fc, of the filter
            // is fc = f*2^(d/1200), where f is the frequency of the filter.  Or
            // d = 1200*log2(fc/f).  Compute the detune value to produce a final
            // cutoff frequency of 1 Hz.
            let detuneEnd = 1200 * Math.log2(1 / filterFrequency);

            let options = {
              sampleRate: sampleRate,
              oscFrequency: oscFrequency,
              testDuration: testDuration,
              paramName: 'detune',
              filterOptions: {
                type: 'lowpass',
                frequency: filterFrequency,
                detune: 0,
                gain: 0
              },
              autoStart: {method: 'setValueAtTime', args: [0, 0]},
              autoEnd: {
                method: 'linearRampToValueAtTime',
                args: [detuneEnd, testDuration / 4]
              }
            };

            const buffer = await doTest(should, options);
            let expected = buffer.getChannelData(0);
            let actual = buffer.getChannelData(1);

            // Outputs should be the same
            checkForSameOutput(should, options.paramName, actual, expected);

            task.done();
          });

      audit.define('All k-rate inputs', async (task, should) => {
        // Test the case where all AudioParams are set to k-rate with an input
        // to each AudioParam.  Similar to the above tests except all the params
        // are k-rate.
        let testFrames = testDuration * sampleRate;
        let context = new OfflineAudioContext(
            {numberOfChannels: 2, sampleRate: sampleRate, length: testFrames});

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

        let src = new OscillatorNode(context);

        // The peaking filter uses all four AudioParams, so this is the node to
        // test.
        let filterOptions =
            {type: 'peaking', frequency: 0, detune: 0, gain: 0, Q: 0};
        let refNode;
        should(
            () => refNode = new BiquadFilterNode(context, filterOptions),
            `Create: refNode = new BiquadFilterNode(context, ${
                JSON.stringify(filterOptions)})`)
            .notThrow();

        let tstNode;
        should(
            () => tstNode = new BiquadFilterNode(context, filterOptions),
            `Create: tstNode = new BiquadFilterNode(context, ${
                JSON.stringify(filterOptions)})`)
            .notThrow();
        ;

        // Make all the AudioParams k-rate.
        ['frequency', 'Q', 'gain', 'detune'].forEach(param => {
          should(
              () => refNode[param].automationRate = 'k-rate',
              `Set rate: refNode[${param}].automationRate = 'k-rate'`)
              .notThrow();
          should(
              () => tstNode[param].automationRate = 'k-rate',
              `Set rate: tstNode[${param}].automationRate = 'k-rate'`)
              .notThrow();
        });

        // One input for each AudioParam.
        let mod = {};
        ['frequency', 'Q', 'gain', 'detune'].forEach(param => {
          should(
              () => mod[param] = new ConstantSourceNode(context, {offset: 0}),
              `Create: mod[${
                  param}] = new ConstantSourceNode(context, {offset: 0})`)
              .notThrow();
          ;
          should(
              () => mod[param].offset.automationRate = 'a-rate',
              `Set rate: mod[${param}].offset.automationRate = 'a-rate'`)
              .notThrow();
        });

        // Set up automations for refNode.  We want to start the filter with
        // parameters that let the oscillator signal through more or less
        // untouched.  Then change the filter parameters to filter out the
        // oscillator.  What happens in between doesn't reall matter for this
        // test.  Hence, set the initial parameters with a center frequency well
        // above the oscillator and a Q and gain of 0 to pass everthing.
        [['frequency', [4 * src.frequency.value, 0]], ['Q', [0, 0]],
         ['gain', [0, 0]], ['detune', [4 * 1200, 0]]]
            .forEach(param => {
              should(
                  () => refNode[param[0]].setValueAtTime(...param[1]),
                  `Automate 0: refNode.${param[0]}.setValueAtTime(${
                      param[1][0]}, ${param[1][1]})`)
                  .notThrow();
              should(
                  () => mod[param[0]].offset.setValueAtTime(...param[1]),
                  `Automate 0: mod[${param[0]}].offset.setValueAtTime(${
                      param[1][0]}, ${param[1][1]})`)
                  .notThrow();
            });

        // Now move the filter frequency to the oscillator frequency with a high
        // Q and very low gain to remove the oscillator signal.
        [['frequency', [src.frequency.value, testDuration / 4]],
         ['Q', [40, testDuration / 4]], ['gain', [-100, testDuration / 4]], [
           'detune', [0, testDuration / 4]
         ]].forEach(param => {
          should(
              () => refNode[param[0]].linearRampToValueAtTime(...param[1]),
              `Automate 1: refNode[${param[0]}].linearRampToValueAtTime(${
                  param[1][0]}, ${param[1][1]})`)
              .notThrow();
          should(
              () => mod[param[0]].offset.linearRampToValueAtTime(...param[1]),
              `Automate 1: mod[${param[0]}].offset.linearRampToValueAtTime(${
                  param[1][0]}, ${param[1][1]})`)
              .notThrow();
        });

        // Connect everything
        src.connect(refNode).connect(merger, 0, 0);
        src.connect(tstNode).connect(merger, 0, 1);

        src.start();
        for (let param in mod) {
          should(
              () => mod[param].connect(tstNode[param]),
              `Connect: mod[${param}].connect(tstNode.${param})`)
              .notThrow();
        }

        for (let param in mod) {
          should(() => mod[param].start(), `Start: mod[${param}].start()`)
              .notThrow();
        }

        const buffer = await context.startRendering();
        let expected = buffer.getChannelData(0);
        let actual = buffer.getChannelData(1);

        // Sanity check that the output isn't all zeroes.
        should(actual, 'All k-rate AudioParams').notBeConstantValueOf(0);
        should(actual, 'All k-rate AudioParams').beCloseToArray(expected, {
          absoluteThreshold: 0
        });

        task.done();
      });

      audit.run();

      async function doTest(should, options) {
        // Test that a k-rate AudioParam with an input reads the input value and
        // is actually k-rate.
        //
        // A refNode is created with an automation timeline.  This is the
        // expected output.
        //
        // The testNode is the same, but it has a node connected to the k-rate
        // AudioParam.  The input to the node is an a-rate ConstantSourceNode
        // whose output is automated in exactly the same was as the refNode.  If
        // the test passes, the outputs of the two nodes MUST match exactly.

        // The options argument MUST contain the following members:
        //   sampleRate - the sample rate for the offline context
        //   testDuration - duration of the offline context, in sec.
        //   paramName  - the name of the AudioParam to be tested
        //   oscFrequency - frequency of oscillator source
        //   filterOptions - options used to construct the BiquadFilterNode
        //   autoStart     - information about how to start the automation
        //   autoEnd       - information about how to end the automation
        //
        //   The autoStart and autoEnd options are themselves dictionaries with
        //   the following required members:
        //     method - name of the automation method to be applied
        //     args   - array of arguments to be supplied to the method.
        let {
          sampleRate,
          paramName,
          oscFrequency,
          autoStart,
          autoEnd,
          testDuration,
          filterOptions
        } = options;

        let testFrames = testDuration * sampleRate;
        let context = new OfflineAudioContext(
            {numberOfChannels: 2, sampleRate: sampleRate, length: testFrames});

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

        // Any calls to |should| are meant to be informational so we can see
        // what nodes are created and the automations used.
        let src;

        // Create the source.
        should(
            () => {
              src = new OscillatorNode(context, {frequency: oscFrequency});
            },
            `${paramName}: new OscillatorNode(context, {frequency: ${
                oscFrequency}})`)
            .notThrow();

        // The refNode automates the AudioParam with k-rate automations, no
        // inputs.
        let refNode;
        should(
            () => {
              refNode = new BiquadFilterNode(context, filterOptions);
            },
            `Reference BiquadFilterNode(c, ${JSON.stringify(filterOptions)})`)
            .notThrow();

        refNode[paramName].automationRate = 'k-rate';

        // Set up automations for the reference node.
        should(
            () => {
              refNode[paramName][autoStart.method](...autoStart.args);
            },
            `refNode.${paramName}.${autoStart.method}(${autoStart.args})`)
            .notThrow();
        should(
            () => {
              refNode[paramName][autoEnd.method](...autoEnd.args);
            },
            `refNode.${paramName}.${autoEnd.method}.(${autoEnd.args})`)
            .notThrow();

        // The tstNode does the same automation, but it comes from the input
        // connected to the AudioParam.
        let tstNode;
        should(
            () => {
              tstNode = new BiquadFilterNode(context, filterOptions);
            },
            `Test BiquadFilterNode(context, ${JSON.stringify(filterOptions)})`)
            .notThrow();
        tstNode[paramName].automationRate = 'k-rate';

        // Create the input to the AudioParam of the test node.  The output of
        // this node MUST have the same set of automations as the reference
        // node, and MUST be a-rate to make sure we're handling k-rate inputs
        // correctly.
        let mod = new ConstantSourceNode(context);
        mod.offset.automationRate = 'a-rate';
        should(
            () => {
              mod.offset[autoStart.method](...autoStart.args);
            },
            `${paramName}: mod.offset.${autoStart.method}(${autoStart.args})`)
            .notThrow();
        should(
            () => {
              mod.offset[autoEnd.method](...autoEnd.args);
            },
            `${paramName}: mod.offset.${autoEnd.method}(${autoEnd.args})`)
            .notThrow();

        // Create graph
        mod.connect(tstNode[paramName]);
        src.connect(refNode).connect(merger, 0, 0);
        src.connect(tstNode).connect(merger, 0, 1);

        // Run!
        src.start();
        mod.start();
        return context.startRendering();
      }

      function checkForSameOutput(should, paramName, actual, expected) {
        let halfLength = expected.length / 2;

        // Outputs should be the same.  We break the check into halves so we can
        // see the expected outputs.  Mostly for a simple visual check that the
        // output from the second half is small because the tests generally try
        // to filter out the signal so that the last half of the output is
        // small.
        should(
            actual.slice(0, halfLength),
            `k-rate ${paramName} with input: output[0,${halfLength}]`)
            .beCloseToArray(
                expected.slice(0, halfLength), {absoluteThreshold: 0});
        should(
            actual.slice(halfLength),
            `k-rate ${paramName} with input: output[${halfLength}:]`)
            .beCloseToArray(expected.slice(halfLength), {absoluteThreshold: 0});
      }
    </script>
  </body>
</html>