summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-iirfilternode-interface/iirfilter-basic.html
blob: 7828f05226151ebf7d6c7ec984cae1f90ef848d2 (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
<!DOCTYPE html>
<html>
  <head>
    <title>
      Test Basic IIRFilterNode Properties
    </title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="../../resources/audit-util.js"></script>
    <script src="../../resources/audit.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      let sampleRate = 48000;
      let testFrames = 100;

      // Global context that can be used by the individual tasks. It must be
      // defined by the initialize task.
      let context;

      let audit = Audit.createTaskRunner();

      audit.define('initialize', (task, should) => {
        should(() => {
          context = new OfflineAudioContext(1, testFrames, sampleRate);
        }, 'Initialize context for testing').notThrow();
        task.done();
      });

      audit.define('existence', (task, should) => {
        should(context.createIIRFilter, 'context.createIIRFilter').exist();
        task.done();
      });

      audit.define('parameters', (task, should) => {
        // Create a really simple IIR filter. Doesn't much matter what.
        let coef = Float32Array.from([1]);

        let f = context.createIIRFilter(coef, coef);

        should(f.numberOfInputs, 'numberOfInputs').beEqualTo(1);
        should(f.numberOfOutputs, 'numberOfOutputs').beEqualTo(1);
        should(f.channelCountMode, 'channelCountMode').beEqualTo('max');
        should(f.channelInterpretation, 'channelInterpretation')
            .beEqualTo('speakers');

        task.done();
      });

      audit.define('exceptions-createIIRFilter', (task, should) => {
        should(function() {
          // Two args are required.
          context.createIIRFilter();
        }, 'createIIRFilter()').throw(TypeError);

        should(function() {
          // Two args are required.
          context.createIIRFilter(new Float32Array(1));
        }, 'createIIRFilter(new Float32Array(1))').throw(TypeError);

        should(function() {
          // null is not valid
          context.createIIRFilter(null, null);
        }, 'createIIRFilter(null, null)').throw(TypeError);

        should(function() {
          // There has to be at least one coefficient.
          context.createIIRFilter([], []);
        }, 'createIIRFilter([], [])').throw(DOMException, 'NotSupportedError');

        should(function() {
          // There has to be at least one coefficient.
          context.createIIRFilter([1], []);
        }, 'createIIRFilter([1], [])').throw(DOMException, 'NotSupportedError');

        should(function() {
          // There has to be at least one coefficient.
          context.createIIRFilter([], [1]);
        }, 'createIIRFilter([], [1])').throw(DOMException, 'NotSupportedError');

        should(
            function() {
              // Max allowed size for the coefficient arrays.
              let fb = new Float32Array(20);
              fb[0] = 1;
              context.createIIRFilter(fb, fb);
            },
            'createIIRFilter(new Float32Array(20), new Float32Array(20))')
            .notThrow();

        should(
            function() {
              // Max allowed size for the feedforward coefficient array.
              let coef = new Float32Array(21);
              coef[0] = 1;
              context.createIIRFilter(coef, [1]);
            },
            'createIIRFilter(new Float32Array(21), [1])')
            .throw(DOMException, 'NotSupportedError');

        should(
            function() {
              // Max allowed size for the feedback coefficient array.
              let coef = new Float32Array(21);
              coef[0] = 1;
              context.createIIRFilter([1], coef);
            },
            'createIIRFilter([1], new Float32Array(21))')
            .throw(DOMException, 'NotSupportedError');

        should(
            function() {
              // First feedback coefficient can't be 0.
              context.createIIRFilter([1], new Float32Array(2));
            },
            'createIIRFilter([1], new Float32Array(2))')
            .throw(DOMException, 'InvalidStateError');

        should(
            function() {
              // feedforward coefficients can't all be zero.
              context.createIIRFilter(new Float32Array(10), [1]);
            },
            'createIIRFilter(new Float32Array(10), [1])')
            .throw(DOMException, 'InvalidStateError');

        should(function() {
          // Feedback coefficients must be finite.
          context.createIIRFilter([1], [1, Infinity, NaN]);
        }, 'createIIRFilter([1], [1, NaN, Infinity])').throw(TypeError);

        should(function() {
          // Feedforward coefficients must be finite.
          context.createIIRFilter([1, Infinity, NaN], [1]);
        }, 'createIIRFilter([1, NaN, Infinity], [1])').throw(TypeError);

        should(function() {
          // Test that random junk in the array is converted to NaN.
          context.createIIRFilter([1, 'abc', []], [1]);
        }, 'createIIRFilter([1, \'abc\', []], [1])').throw(TypeError);

        task.done();
      });

      audit.define('exceptions-getFrequencyData', (task, should) => {
        // Create a really simple IIR filter. Doesn't much matter what.
        let coef = Float32Array.from([1]);

        let f = context.createIIRFilter(coef, coef);

        should(
            function() {
              // frequencyHz can't be null.
              f.getFrequencyResponse(
                  null, new Float32Array(1), new Float32Array(1));
            },
            'getFrequencyResponse(null, new Float32Array(1), new Float32Array(1))')
            .throw(TypeError);

        should(
            function() {
              // magResponse can't be null.
              f.getFrequencyResponse(
                  new Float32Array(1), null, new Float32Array(1));
            },
            'getFrequencyResponse(new Float32Array(1), null, new Float32Array(1))')
            .throw(TypeError);

        should(
            function() {
              // phaseResponse can't be null.
              f.getFrequencyResponse(
                  new Float32Array(1), new Float32Array(1), null);
            },
            'getFrequencyResponse(new Float32Array(1), new Float32Array(1), null)')
            .throw(TypeError);

        should(
            function() {
              // magResponse array must the same length as frequencyHz
              f.getFrequencyResponse(
                  new Float32Array(10), new Float32Array(1),
                  new Float32Array(20));
            },
            'getFrequencyResponse(new Float32Array(10), new Float32Array(1), new Float32Array(20))')
            .throw(DOMException, 'InvalidAccessError');

        should(
            function() {
              // phaseResponse array must be the same length as frequencyHz
              f.getFrequencyResponse(
                  new Float32Array(10), new Float32Array(20),
                  new Float32Array(1));
            },
            'getFrequencyResponse(new Float32Array(10), new Float32Array(20), new Float32Array(1))')
            .throw(DOMException, 'InvalidAccessError');

        task.done();
      });

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