summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-audiobuffersourcenode-interface/active-processing.https.html
blob: 7b8c1295c6948c01ce018765b50b084d4200b144 (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 Active Processing for AudioBufferSourceNode
    </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 audit = Audit.createTaskRunner();

      // Arbitrary sample rate. And we only new a few blocks for rendering to
      // see if things are working.
      let sampleRate = 8000;
      let renderLength = 10 * RENDER_QUANTUM_FRAMES;

      // Offline context used for the tests.
      let context;

      // Number of channels for the AudioBufferSource. Fairly arbitrary, but
      // should be more than 2.
      let numberOfChannels = 7;

      // Number of frames in the AudioBuffer.  Fairly arbitrary, but should
      // probablybe more than one render quantum and significantly less than
      // |renderLength|.
      let bufferFrames = 131;

      let filePath =
          '../the-audioworklet-interface/processors/input-count-processor.js';

      audit.define('Setup graph', (task, should) => {
        context =
            new OfflineAudioContext(numberOfChannels, renderLength, sampleRate);

        should(
            context.audioWorklet.addModule(filePath).then(() => {
              let buffer = new AudioBuffer({
                numberOfChannels: numberOfChannels,
                length: bufferFrames,
                sampleRate: context.sampleRate
              });

              src = new AudioBufferSourceNode(context, {buffer: buffer});
              let counter = new AudioWorkletNode(context, 'counter');

              src.connect(counter).connect(context.destination);
              src.start();
            }),
            'AudioWorklet and graph construction')
            .beResolved()
            .then(() => task.done());
      });

      audit.define('verify count change', (task, should) => {
        context.startRendering()
            .then(renderedBuffer => {
              let output = renderedBuffer.getChannelData(0);

              // Find the first time the number of channels changes to 0.
              let countChangeIndex = output.findIndex(x => x == 0);

              // Verify that the count did change.  If it didn't there's a bug
              // in the implementation, or it takes longer than the render
              // length to change.  For the latter case, increase the render
              // length, but it can't be arbitrarily large.  The change needs to
              // happen at some reasonable time after the source stops.
              should(countChangeIndex >= 0, 'Number of channels changed')
                  .beTrue();
              should(
                  countChangeIndex, 'Index where input channel count changed')
                  .beLessThanOrEqualTo(renderLength);

              // Verify the number of channels at the beginning matches the
              // number of channels in the AudioBuffer.
              should(
                  output.slice(0, countChangeIndex),
                  `Number of channels in input[0:${countChangeIndex - 1}]`)
                  .beConstantValueOf(numberOfChannels);

              // Verify that after the source has stopped, the number of
              // channels is 0.
              should(
                  output.slice(countChangeIndex),
                  `Number of channels in input[${countChangeIndex}:]`)
                  .beConstantValueOf(0);
            })
            .then(() => task.done());
      });

      audit.run();
    </script>

  </body>
</html>