summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-audioworklet-interface/audioworklet-audioparam-size.https.html
blob: 9578b268815a89d31457df8cdd7a088e9df13c7c (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
<!doctype html>
<html>
  <head>
    <title>
      Test AudioParam Array Size
    </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>
      let audit = Audit.createTaskRunner();
      let filePath = 'processors/param-size-processor.js';
      let context;

      // Use a power of two so there's no roundoff computing times from frames.
      let sampleRate = 16384;

      // Sets up AudioWorklet and OfflineAudioContext.
      audit.define('Initializing AudioWorklet and Context', (task, should) => {
        should(() => {
          context = new OfflineAudioContext(
              1, 10 * RENDER_QUANTUM_FRAMES, sampleRate);
        }, 'Creating offline context for testing').notThrow();

        should(
            context.audioWorklet.addModule(filePath), 'Creating test worklet')
            .beResolved()
            .then(() => {
              task.done();
            });
      });

      audit.define('Verify Size of AudioParam Arrays', (task, should) => {
        let node = new AudioWorkletNode(context, 'param-size');
        let nodeParam = node.parameters.get('param');

        node.connect(context.destination);

        let renderQuantumDuration = RENDER_QUANTUM_FRAMES / context.sampleRate;

        // Set up some automations, after one render quantum.  We want the first
        // render not to have any automations, just to be sure we handle that
        // case correctly.
        context.suspend(renderQuantumDuration)
            .then(() => {
              let now = context.currentTime;

              // Establish the first automation event.
              nodeParam.setValueAtTime(1, now);
              // The second render should be constant
              nodeParam.setValueAtTime(0, now + renderQuantumDuration);
              // The third render and part of the fourth is a linear ramp
              nodeParam.linearRampToValueAtTime(
                  1, now + 2.5 * renderQuantumDuration);
              // Everything afterwards should be constant.
            })
            .then(() => context.resume());

        context.startRendering()
            .then(renderedBuffer => {
              let data = renderedBuffer.getChannelData(0);

              // The very first render quantum should be constant, so the array
              // has length 1.
              should(
                  data.slice(0, RENDER_QUANTUM_FRAMES),
                  'Render quantum 0: array size')
                  .beConstantValueOf(1);

              should(
                  data.slice(RENDER_QUANTUM_FRAMES, 2 * RENDER_QUANTUM_FRAMES),
                  'Render quantum 1: array size')
                  .beConstantValueOf(1);

              should(
                  data.slice(
                      2 * RENDER_QUANTUM_FRAMES, 4 * RENDER_QUANTUM_FRAMES),
                  'Render quantum 2-3: array size')
                  .beConstantValueOf(RENDER_QUANTUM_FRAMES);

              should(
                  data.slice(4 * RENDER_QUANTUM_FRAMES),
                  'Remaining renders: array size')
                  .beConstantValueOf(1);
            })
            .then(() => task.done());
      });

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