summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-analysernode-interface/test-analysernode.html
blob: e8325388d1bf01e7bebe881ad0da1583f0e2f481 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <script src="/resources/testharness.js"></script>
  <script src="/resources/testharnessreport.js"></script>
  <script>
    function testNode() {
      var context = new AudioContext();
      var buffer = context.createBuffer(1, 2048, context.sampleRate);
      for (var i = 0; i < 2048; ++i) {
        buffer.getChannelData(0)[i] = Math.sin(
          440 * 2 * Math.PI * i / context.sampleRate
        );
      }

      var destination = context.destination;

      var source = context.createBufferSource();

      var analyser = context.createAnalyser();

      source.buffer = buffer;

      source.connect(analyser);
      analyser.connect(destination);

      assert_equals(
        analyser.channelCount,
        2,
        "analyser node has 2 input channels by default"
      );
      assert_equals(
        analyser.channelCountMode,
        "max",
        "Correct channelCountMode for the analyser node"
      );
      assert_equals(
        analyser.channelInterpretation,
        "speakers",
        "Correct channelCountInterpretation for the analyser node"
      );

      assert_equals(
        analyser.fftSize,
        2048,
        "Correct default value for fftSize"
      );
      assert_equals(
        analyser.frequencyBinCount,
        1024,
        "Correct default value for frequencyBinCount"
      );
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser.fftSize = 0;
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser.fftSize = 1;
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser.fftSize = 8;
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser.fftSize = 100;
      }); // non-power of two
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser.fftSize = 2049;
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser.fftSize = 4097;
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser.fftSize = 8193;
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser.fftSize = 16385;
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser.fftSize = 32769;
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser.fftSize = 65536;
      });
      analyser.fftSize = 1024;
      assert_equals(
        analyser.frequencyBinCount,
        512,
        "Correct new value for frequencyBinCount"
      );

      assert_equals(
        analyser.minDecibels,
        -100,
        "Correct default value for minDecibels"
      );
      assert_equals(
        analyser.maxDecibels,
        -30,
        "Correct default value for maxDecibels"
      );
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser.minDecibels = -30;
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser.minDecibels = -29;
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser.maxDecibels = -100;
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser.maxDecibels = -101;
      });

      assert_true(
        Math.abs(analyser.smoothingTimeConstant - 0.8) < 0.001,
        "Correct default value for smoothingTimeConstant"
      );
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser.smoothingTimeConstant = -0.1;
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser.smoothingTimeConstant = 1.1;
      });
      analyser.smoothingTimeConstant = 0;
      analyser.smoothingTimeConstant = 1;
    }

    function testConstructor() {
      var context = new AudioContext();

      var analyser = new AnalyserNode(context);
      assert_equals(
        analyser.channelCount,
        2,
        "analyser node has 2 input channels by default"
      );
      assert_equals(
        analyser.channelCountMode,
        "max",
        "Correct channelCountMode for the analyser node"
      );
      assert_equals(
        analyser.channelInterpretation,
        "speakers",
        "Correct channelCountInterpretation for the analyser node"
      );

      assert_equals(
        analyser.fftSize,
        2048,
        "Correct default value for fftSize"
      );
      assert_equals(
        analyser.frequencyBinCount,
        1024,
        "Correct default value for frequencyBinCount"
      );
      assert_equals(
        analyser.minDecibels,
        -100,
        "Correct default value for minDecibels"
      );
      assert_equals(
        analyser.maxDecibels,
        -30,
        "Correct default value for maxDecibels"
      );
      assert_true(
        Math.abs(analyser.smoothingTimeConstant - 0.8) < 0.001,
        "Correct default value for smoothingTimeConstant"
      );

      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser = new AnalyserNode(context, { fftSize: 0 });
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser = new AnalyserNode(context, { fftSize: 1 });
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser = new AnalyserNode(context, { fftSize: 8 });
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser = new AnalyserNode(context, { fftSize: 100 });
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser = new AnalyserNode(context, { fftSize: 2049 });
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser = new AnalyserNode(context, { fftSize: 4097 });
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser = new AnalyserNode(context, { fftSize: 8193 });
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser = new AnalyserNode(context, { fftSize: 16385 });
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser = new AnalyserNode(context, { fftSize: 32769 });
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser = new AnalyserNode(context, { fftSize: 65536 });
      });
      analyser = new AnalyserNode(context, { fftSize: 1024 });
      assert_equals(
        analyser.frequencyBinCount,
        512,
        "Correct new value for frequencyBinCount"
      );

      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser = new AnalyserNode(context, { minDecibels: -30 });
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser = new AnalyserNode(context, { minDecibels: -29 });
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser = new AnalyserNode(context, { maxDecibels: -100 });
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser = new AnalyserNode(context, { maxDecibels: -101 });
      });

      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser = new AnalyserNode(context, { smoothingTimeConstant: -0.1 });
      });
      assert_throws_dom("INDEX_SIZE_ERR", function() {
        analyser = new AnalyserNode(context, { smoothingTimeConstant: -1.1 });
      });
      analyser = new AnalyserNode(context, { smoothingTimeConstant: 0 });
      analyser = new AnalyserNode(context, { smoothingTimeConstant: 1 });
    }
    test(testNode, "Test AnalyserNode API");
    test(testConstructor, "Test AnalyserNode's ctor API");
  </script>
</head>
</body>
</html>