summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-channelmergernode-interface/audiochannelmerger-disconnect.html
blob: ad74d5e00457e6ccd1b165940ecad140a423d247 (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
<!DOCTYPE html>
<html>
  <head>
    <title>
      audiochannelmerger-disconnect.html
    </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 id="layout-test-code">
      let renderQuantum = 128;

      let numberOfChannels = 2;
      let sampleRate = 44100;
      let renderDuration = 0.5;
      let disconnectTime = 0.5 * renderDuration;

      let audit = Audit.createTaskRunner();

      // Task: Check if the merger outputs a silent channel when an input is
      // disconnected.
      audit.define('silent-disconnect', (task, should) => {
        let context = new OfflineAudioContext(
            numberOfChannels, renderDuration * sampleRate, sampleRate);
        let merger = context.createChannelMerger();
        let source1 = context.createBufferSource();
        let source2 = context.createBufferSource();

        // Create and assign a constant buffer.
        let bufferDCOffset = createConstantBuffer(context, 1, 1);
        source1.buffer = source2.buffer = bufferDCOffset;
        source1.loop = source2.loop = true;

        // Connect the output of source into the 4th input of merger. The merger
        // should produce 6 channel output.
        source1.connect(merger, 0, 0);
        source2.connect(merger, 0, 1);
        merger.connect(context.destination);
        source1.start();
        source2.start();

        // Schedule the disconnection of |source2| at the half of render
        // duration.
        context.suspend(disconnectTime).then(function() {
          source2.disconnect();
          context.resume();
        });

        context.startRendering()
            .then(function(buffer) {
              // The entire first channel of the output should be 1.
              should(buffer.getChannelData(0), 'Channel #0')
                  .beConstantValueOf(1);

              // Calculate the first zero index in the second channel.
              let channel1 = buffer.getChannelData(1);
              let disconnectIndex = disconnectTime * sampleRate;
              disconnectIndex = renderQuantum *
                  Math.floor(
                      (disconnectIndex + renderQuantum - 1) / renderQuantum);
              let firstZeroIndex = channel1.findIndex(function(element, index) {
                if (element === 0)
                  return index;
              });

              // The second channel should contain 1, and 0 after the
              // disconnection.
              should(channel1, 'Channel #1').containValues([1, 0]);
              should(
                  firstZeroIndex, 'The index of first zero in the channel #1')
                  .beEqualTo(disconnectIndex);
            })
            .then(() => task.done());
      });

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