summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-audioworklet-interface/audioworkletnode-disconnected-input.https.html
blob: c58502af01192c13884f303b8345a017af9ad3a9 (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
<!DOCTYPE html>
<html>
  <head>
    <title>
      Test AudioWorkletNode's Disconnected Input Array Length
    </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>
  </head>
  <body>
    <script id="layout-test-code">
      let audit = Audit.createTaskRunner();

      // Arbitrary numbers used to align the test with render quantum boundary.
      // The sample rate is a power of two to eliminate roundoff in computing
      // the suspend time needed for the test.
      let sampleRate = 16384;
      let renderLength = 8 * RENDER_QUANTUM_FRAMES;
      let context;

      let filePath = 'processors/input-length-processor.js';

      let testChannelValues = [1, 2, 3];

      // Creates a 3-channel buffer and play with BufferSourceNode. The source
      // goes through a bypass AudioWorkletNode (gain value of 1).
      audit.define(
          {
            label: 'test',
            description:
                'Input array length should be zero for disconnected input'
          },
          (task, should) => {
            context = new OfflineAudioContext({
              numberOfChannels: 1,
              length: renderLength,
              sampleRate: sampleRate
            });

            context.audioWorklet.addModule(filePath).then(() => {
              let sourceNode = new ConstantSourceNode(context);
              let workletNode =
                  new AudioWorkletNode(context, 'input-length-processor');

              workletNode.connect(context.destination);

              // Connect the source now.
              let connectFrame = RENDER_QUANTUM_FRAMES;

              context.suspend(connectFrame / sampleRate)
                  .then(() => {
                    sourceNode.connect(workletNode);
                  })
                  .then(() => context.resume());
              ;

              // Then disconnect the source after a few renders
              let disconnectFrame = 3 * RENDER_QUANTUM_FRAMES;
              context.suspend(disconnectFrame / sampleRate)
                  .then(() => {
                    sourceNode.disconnect(workletNode);
                  })
                  .then(() => context.resume());

              sourceNode.start();
              context.startRendering()
                  .then(resultBuffer => {
                    let data = resultBuffer.getChannelData(0);

                    should(
                        data.slice(0, connectFrame),
                        'Before connecting the source: Input array length')
                        .beConstantValueOf(0);

                    // Find where the output is no longer 0.
                    let nonZeroIndex = data.findIndex(x => x > 0);
                    should(nonZeroIndex, 'First non-zero output')
                        .beEqualTo(connectFrame);

                    should(
                        data.slice(
                            nonZeroIndex,
                            nonZeroIndex + (disconnectFrame - connectFrame)),
                        'While source is connected: Input array length')
                        .beConstantValueOf(RENDER_QUANTUM_FRAMES);
                    should(
                        data.slice(disconnectFrame),
                        'After disconnecting the source: Input array length')
                        .beConstantValueOf(0);
                  })
                  .then(() => task.done());
            });
          });

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