summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-audioworklet-interface/audioworklet-audioparam-iterable.https.html
blob: 9e93f48ab878c5f9208dae7a6d2278cd98ab4529 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>
      Test get parameterDescriptor as various iterables
    </title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="/webaudio/js/helpers.js"></script>
  </head>

  <body>
    <script id="params">
      // A series of AudioParamDescriptors, copied one by one into various iterable
      // data structures. This is used by both the processor side and the main
      // thread side, so is in a different script tag.
      const PARAMS = [
        {
          name: "a control-rate parameter",
          defaultValue: 0.5,
          minValue: 0,
          maxValue: 1,
          automationRate: "a-rate",
        },
        {
          name: "你好",
          defaultValue: 2.5,
          minValue: 0,
          maxValue: 7,
          automationRate: "a-rate",
        },
        {
          name: "🎶",
          defaultValue: 8.5,
          minValue: 0,
          maxValue: 11115,
          automationRate: "k-rate",
        },
      ];
    </script>
    <script id="processors" type="worklet">
      registerProcessor("set",
      class SetParamProcessor extends AudioWorkletProcessor {
        static get parameterDescriptors() {
          var s = new Set();
          s.add(PARAMS[0]);
          s.add(PARAMS[1]);
          s.add(PARAMS[2]);
          return s;
        }
        constructor() { super(); }
        process() {
        }
      });

      registerProcessor("array",
      class ArrayParamProcessor extends AudioWorkletProcessor {
        static get parameterDescriptors() {
          return PARAMS;
        }
        constructor() { super(); }
        process() { }
      });

      function* gen() {
        yield PARAMS[0];
        yield PARAMS[1];
        yield PARAMS[2];
      }
      registerProcessor("generator",
      class GeneratorParamProcessor extends AudioWorkletProcessor {
        static get parameterDescriptors() {
          return gen();
        }
        constructor() { super(); }
        process() { }
      });
      // Test a processor that has a get parameterDescriptors, but it returns
      // something that is not iterable.
      try {
        registerProcessor("invalid",
        class InvalidParamProcessor extends AudioWorkletProcessor {
          static get parameterDescriptors() {
            return 4;
          }
          constructor() { super(); }
          process() { }
        });
        throw "This should not have been reached.";
      } catch (e) {
        // unclear how to signal success here, but we can signal failure in the
        // developer console
        if (e.name != "TypeError") {
          throw "This should be TypeError";
        }
      }
      // Test a processor that has a get parameterDescriptors, with a duplicate
      // param name something that is not iterable.
      try {
        registerProcessor("duplicate-param-name",
        class DuplicateParamProcessor extends AudioWorkletProcessor {
          static get parameterDescriptors() {
            var p = {
                name: "a",
                defaultValue: 1,
                minValue: 0,
                maxValue: 1,
                automationRate: "k-rate",
            };
            return [p,p];
          }
          constructor() { super(); }
          process() { }
        });
        throw "This should not have been reached.";
      } catch (e) {
        // unclear how to signal success here, but we can signal failure in the
        // developer console
        if (e.name != "NotSupportedError") {
          throw "This should be NotSupportedError";
        }
      }
      // Test a processor that has a no get parameterDescriptors.
      try {
        registerProcessor("no-params",
        class NoParamProcessor extends AudioWorkletProcessor {
          constructor() { super(); }
          process() { }
        });
      } catch (e) {
        throw "Construction should have worked.";
      }
    </script>
    <script>
      setup({ explicit_done: true });
      // Mangle the PARAMS object into a map that has the same shape as what an
      // AudioWorkletNode.parameter property would
      var PARAMS_MAP = new Map();
      for (var param of PARAMS) {
        var o = param;
        var name = o.name;
        delete o.name;
        PARAMS_MAP.set(name, o);
      }

      // This compares `lhs` and `rhs`, that are two maplike with the same shape
      // as PARAMS_MAP.
      function compare(testname, lhs, rhs) {
        equals(lhs.size, rhs.size, "Map match in size for " + testname);
        var i = 0;
        for (var [k, v] of lhs) {
          is_true(rhs.has(k), testname + ": " + k + " exists in both maps");
          var vrhs = rhs.get(k);
          ["defaultValue", "minValue", "maxValue", "automationRate"].forEach(
            paramKey => {
              equals(
                v[paramKey],
                vrhs[paramKey],
                `Values for ${k}.${paramKey} match for ${testname}`
              );
            }
          );
        }
      }
      var ac = new AudioContext();
      var url = URLFromScriptsElements(["params", "processors"]);
      ac.audioWorklet
        .addModule(url)
        .then(() => {
              ["set", "array", "generator"].forEach(iterable => {
                test(() => {
                  var node = new AudioWorkletNode(ac, iterable);
                  compare(iterable, node.parameters, PARAMS_MAP);
                }, `Creating an AudioWorkletNode with a ${iterable} for
                    parameter descriptor worked`);
          });
        })
        .then(function() {
          test(function() {
            assert_throws_dom("InvalidStateError", function() {
              new AudioWorkletNode(ac, "invalid");
            });
          }, `Attempting to create an AudioWorkletNode with an non
              iterable for parameter descriptor should not work`);
        })
      .then(function() {
        test(() => {
          new AudioWorkletNode(ac, "no-params");
        }, `Attempting to create an AudioWorkletNode from a processor
            that does not have a parameterDescriptors getter should work`);
      })
      .then(function() {
        test(function() {
          assert_throws_dom("InvalidStateError", function() {
            new AudioWorkletNode(ac, "duplicate-param-name");
          });
        }, `Attempting to create an AudioWorkletNode with two parameter
            descriptor with the same name should not work`);
      }).then(function() {
        done();
      });
    </script>
  </body>
</html>