summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-audioworklet-interface/audioworklet-audioparam.https.html
blob: 8e51470f646f63abaed321c3efd3e9e25906edea (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
<!DOCTYPE html>
<html>
  <head>
    <title>
      Test AudioWorkletNode's basic AudioParam features
    </title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="/webaudio/resources/audit.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      let audit = Audit.createTaskRunner();

      let sampleRate = 48000;
      let renderLength = 48000 * 0.6;
      let context;

      let filePath = 'processors/gain-processor.js';

      // Sets up AudioWorklet and OfflineAudioContext.
      audit.define('Initializing AudioWorklet and Context', (task, should) => {
        context = new OfflineAudioContext(1, renderLength, sampleRate);
        context.audioWorklet.addModule(filePath).then(() => {
          task.done();
        });
      });

      // Verifies the functionality of AudioParam in AudioWorkletNode by
      // comparing (canceling out) values from GainNode and AudioWorkletNode
      // with simple gain computation code by AudioParam.
      audit.define(
          'Verifying AudioParam in AudioWorkletNode',
          (task, should) => {
            let constantSourceNode = new ConstantSourceNode(context);
            let gainNode = new GainNode(context);
            let inverterNode = new GainNode(context, {gain: -1});
            let gainWorkletNode = new AudioWorkletNode(context, 'gain');
            let gainWorkletParam = gainWorkletNode.parameters.get('gain');

            // Test default value and setter/getter functionality.
            should(gainWorkletParam.value,
                   'Default gain value of gainWorkletNode')
                .beEqualTo(Math.fround(0.707));
            gainWorkletParam.value = 0.1;
            should(gainWorkletParam.value,
                   'Value of gainWorkletParam after setter = 0.1')
                .beEqualTo(Math.fround(0.1));

            constantSourceNode.connect(gainNode)
                .connect(inverterNode)
                .connect(context.destination);
            constantSourceNode.connect(gainWorkletNode)
                .connect(context.destination);

            // With arbitrary times and values, test all possible AudioParam
            // automations.
            [gainNode.gain, gainWorkletParam].forEach((param) => {
              param.setValueAtTime(0, 0);
              param.linearRampToValueAtTime(1, 0.1);
              param.exponentialRampToValueAtTime(0.5, 0.2);
              param.setValueCurveAtTime([0, 2, 0.3], 0.2, 0.1);
              param.setTargetAtTime(0.01, 0.4, 0.5);
            });

            // Test if the setter works correctly in the middle of rendering.
            context.suspend(0.5).then(() => {
              gainNode.gain.value = 1.5;
              gainWorkletParam.value = 1.5;
              context.resume();
            });

            constantSourceNode.start();
            context.startRendering().then((renderedBuffer) => {
              should(renderedBuffer.getChannelData(0),
                     'The rendered buffer')
                  .beConstantValueOf(0);
              task.done();
            });
          });

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