summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-audioparam-interface/nan-param.html
blob: e9b8f0accbd1b0359275615f3ef12bd7e9317c4f (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
<!doctype html>
<html>
  <head>
    <title>Test Flushing of NaN to Zero in AudioParams</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>
      let audit = Audit.createTaskRunner();

      // See
      // https://webaudio.github.io/web-audio-api/#computation-of-value.
      //
      // The computed value must replace NaN values in the output with
      // the default value of the param.
      audit.define('AudioParam NaN', async (task, should) => {
        // For testing, we only need a small number of frames; and
        // a low sample rate is perfectly fine.  Use two channels.
        // The first channel is for the AudioParam output.  The
        // second channel is for the AudioParam input.
        let context = new OfflineAudioContext(
            {numberOfChannels: 2, length: 256, sampleRate: 8192});
        let merger = new ChannelMergerNode(
            context, {numberOfInputs: context.destination.channelCount});
        merger.connect(context.destination);

        // A constant source with a huge value.
        let mod = new ConstantSourceNode(context, {offset: 1e30});

        // Gain nodes with a huge positive gain and huge negative
        // gain.  Combined with the huge offset in |mod|, the
        // output of the gain nodes are +Infinity and -Infinity.
        let gainPos = new GainNode(context, {gain: 1e30});
        let gainNeg = new GainNode(context, {gain: -1e30});

        mod.connect(gainPos);
        mod.connect(gainNeg);

        // Connect these to the second merger channel. This is a
        // sanity check that the AudioParam input really is NaN.
        gainPos.connect(merger, 0, 1);
        gainNeg.connect(merger, 0, 1);

        // Source whose AudioParam is connected to the graph
        // that produces NaN values.  Use a non-default value offset
        // just in case something is wrong we get default for some
        // other reason.
        let src = new ConstantSourceNode(context, {offset: 100});

        gainPos.connect(src.offset);
        gainNeg.connect(src.offset);

        // AudioParam output goes to channel 1 of the destination.
        src.connect(merger, 0, 0);

        // Let's go!
        mod.start();
        src.start();

        let buffer = await context.startRendering();

        let input = buffer.getChannelData(1);
        let output = buffer.getChannelData(0);

        // Have to test manually for NaN values in the input because
        // NaN fails all comparisons.
        let isNaN = true;
        for (let k = 0; k < input.length; ++k) {
          if (!Number.isNaN(input[k])) {
            isNaN = false;
            break;
          }
        }

        should(isNaN, 'AudioParam input contains only NaN').beTrue();

        // Output of the AudioParam should have all NaN values
        // replaced by the default.
        should(output, 'AudioParam output')
            .beConstantValueOf(src.offset.defaultValue);

        task.done();
      });

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