summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-audiobuffer-interface/audiobuffer-copy-channel.html
blob: c0cd49d32520fdf3c55248b1130da64d5845097d (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!DOCTYPE html>
<html>
  <head>
    <title>
      Test Basic Functionality of AudioBuffer.copyFromChannel and
      AudioBuffer.copyToChannel
    </title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="/webaudio/resources/audit-util.js"></script>
    <script src="/webaudio/resources/audit.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      // Define utility routines.

      // Initialize the AudioBuffer |buffer| with a ramp signal on each channel.
      // The ramp starts at channel number + 1.
      function initializeAudioBufferRamp(buffer) {
        for (let c = 0; c < buffer.numberOfChannels; ++c) {
          let d = buffer.getChannelData(c);
          for (let k = 0; k < d.length; ++k) {
            d[k] = k + c + 1;
          }
        }
      }

      // Create a Float32Array of length |length| and initialize the array to
      // -1.
      function createInitializedF32Array(length) {
        let x = new Float32Array(length);
        for (let k = 0; k < length; ++k) {
          x[k] = -1;
        }
        return x;
      }

      // Create a Float32Array of length |length| that is initialized to be a
      // ramp starting at 1.
      function createFloat32RampArray(length) {
        let x = new Float32Array(length);
        for (let k = 0; k < x.length; ++k) {
          x[k] = k + 1;
        }

        return x;
      }

      // Test that the array |x| is a ramp starting at value |start| of length
      // |length|, starting at |startIndex| in the array.  |startIndex| is
      // optional and defaults to 0. Any other values must be -1.
      function shouldBeRamp(
          should, testName, x, startValue, length, startIndex) {
        let k;
        let startingIndex = startIndex || 0;
        let expected = Array(x.length);

        // Fill the expected array with the correct results.

        // The initial part (if any) must be -1.
        for (k = 0; k < startingIndex; ++k) {
          expected[k] = -1;
        }

        // The second part should be a ramp starting with |startValue|
        for (; k < startingIndex + length; ++k) {
          expected[k] = startValue + k - startingIndex;
        }

        // The last part (if any) should be -1.
        for (; k < x.length; ++k) {
          expected[k] = -1;
        }

        should(x, testName, {numberOfArrayLog: 32}).beEqualToArray(expected);
      }

      let audit = Audit.createTaskRunner();

      let context = new AudioContext();
      // Temp array for testing exceptions for copyToChannel/copyFromChannel.
      // The length is arbitrary.
      let x = new Float32Array(8);

      // Number of frames in the AudioBuffer for testing.  This is pretty
      // arbitrary so choose a fairly small value.
      let bufferLength = 16;

      // Number of channels in the AudioBuffer.  Also arbitrary, but it should
      // be greater than 1 for test coverage.
      let numberOfChannels = 3;

      // AudioBuffer that will be used for testing copyFrom and copyTo.
      let buffer = context.createBuffer(
          numberOfChannels, bufferLength, context.sampleRate);

      let initialValues = Array(numberOfChannels);

      // Initialize things
      audit.define('initialize', (task, should) => {
        // Initialize to -1.
        initialValues.fill(-1);
        should(initialValues, 'Initialized values').beConstantValueOf(-1)
        task.done();
      });

      // Test that expected exceptions are signaled for copyFrom.
      audit.define('copyFrom-exceptions', (task, should) => {
        should(
            AudioBuffer.prototype.copyFromChannel,
            'AudioBuffer.prototype.copyFromChannel')
            .exist();

        should(
            () => {
              buffer = context.createBuffer(
                  numberOfChannels, bufferLength, context.sampleRate);
            },
            '0: buffer = context.createBuffer(' + numberOfChannels + ', ' +
                bufferLength + ', context.sampleRate)')
            .notThrow();
        should(() => {
          buffer.copyFromChannel(null, 0);
        }, '1: buffer.copyFromChannel(null, 0)').throw(TypeError);
        should(() => {
          buffer.copyFromChannel(context, 0);
        }, '2: buffer.copyFromChannel(context, 0)').throw(TypeError);
        should(() => {
          buffer.copyFromChannel(x, -1);
        }, '3: buffer.copyFromChannel(x, -1)').throw(DOMException, 'IndexSizeError');
        should(
            () => {
              buffer.copyFromChannel(x, numberOfChannels);
            },
            '4: buffer.copyFromChannel(x, ' + numberOfChannels + ')')
            .throw(DOMException, 'IndexSizeError');
        ;
        should(() => {
          buffer.copyFromChannel(x, 0, -1);
        }, '5: buffer.copyFromChannel(x, 0, -1)').notThrow();
        should(
            () => {
              buffer.copyFromChannel(x, 0, bufferLength);
            },
            '6: buffer.copyFromChannel(x, 0, ' + bufferLength + ')')
            .notThrow();

        should(() => {
          buffer.copyFromChannel(x, 3);
        }, '7: buffer.copyFromChannel(x, 3)').throw(DOMException, 'IndexSizeError');

        // See https://github.com/whatwg/html/issues/5380 for why not `new SharedArrayBuffer()`
        // WebAssembly.Memory's size is in multiples of 64 KiB
        const shared_buffer = new Float32Array(new WebAssembly.Memory({ shared:true, initial:1, maximum:1 }).buffer);
        should(
            () => {
              buffer.copyFromChannel(shared_buffer, 0);
            },
            '8: buffer.copyFromChannel(SharedArrayBuffer view, 0)')
            .throw(TypeError);

        should(
            () => {
              buffer.copyFromChannel(shared_buffer, 0, 0);
            },
            '9: buffer.copyFromChannel(SharedArrayBuffer view, 0, 0)')
            .throw(TypeError);

        task.done();
      });

      // Test that expected exceptions are signaled for copyTo.
      audit.define('copyTo-exceptions', (task, should) => {
        should(
            AudioBuffer.prototype.copyToChannel,
            'AudioBuffer.prototype.copyToChannel')
            .exist();
        should(() => {
          buffer.copyToChannel(null, 0);
        }, '0: buffer.copyToChannel(null, 0)').throw(TypeError);
        should(() => {
          buffer.copyToChannel(context, 0);
        }, '1: buffer.copyToChannel(context, 0)').throw(TypeError);
        should(() => {
          buffer.copyToChannel(x, -1);
        }, '2: buffer.copyToChannel(x, -1)').throw(DOMException, 'IndexSizeError');
        should(
            () => {
              buffer.copyToChannel(x, numberOfChannels);
            },
            '3: buffer.copyToChannel(x, ' + numberOfChannels + ')')
            .throw(DOMException, 'IndexSizeError');
        should(() => {
          buffer.copyToChannel(x, 0, -1);
        }, '4: buffer.copyToChannel(x, 0, -1)').notThrow();
        should(
            () => {
              buffer.copyToChannel(x, 0, bufferLength);
            },
            '5: buffer.copyToChannel(x, 0, ' + bufferLength + ')')
            .notThrow();

        should(() => {
          buffer.copyToChannel(x, 3);
        }, '6: buffer.copyToChannel(x, 3)').throw(DOMException, 'IndexSizeError');

        // See https://github.com/whatwg/html/issues/5380 for why not `new SharedArrayBuffer()`
        // WebAssembly.Memory's size is in multiples of 64 KiB
        const shared_buffer = new Float32Array(new WebAssembly.Memory({ shared:true, initial:1, maximum:1 }).buffer);
        should(
            () => {
              buffer.copyToChannel(shared_buffer, 0);
            },
            '7: buffer.copyToChannel(SharedArrayBuffer view, 0)')
            .throw(TypeError);

        should(
            () => {
              buffer.copyToChannel(shared_buffer, 0, 0);
            },
            '8: buffer.copyToChannel(SharedArrayBuffer view, 0, 0)')
            .throw(TypeError);

        task.done();
      });

      // Test copyFromChannel
      audit.define('copyFrom-validate', (task, should) => {
        // Initialize the AudioBuffer to a ramp for testing copyFrom.
        initializeAudioBufferRamp(buffer);

        // Test copyFrom operation with a short destination array, filling the
        // destination completely.
        for (let c = 0; c < numberOfChannels; ++c) {
          let dst8 = createInitializedF32Array(8);
          buffer.copyFromChannel(dst8, c);
          shouldBeRamp(
              should, 'buffer.copyFromChannel(dst8, ' + c + ')', dst8, c + 1, 8)
        }

        // Test copyFrom operation with a short destination array using a
        // non-zero start index that still fills the destination completely.
        for (let c = 0; c < numberOfChannels; ++c) {
          let dst8 = createInitializedF32Array(8);
          buffer.copyFromChannel(dst8, c, 1);
          shouldBeRamp(
              should, 'buffer.copyFromChannel(dst8, ' + c + ', 1)', dst8, c + 2,
              8)
        }

        // Test copyFrom operation with a short destination array using a
        // non-zero start index that does not fill the destinatiom completely.
        // The extra elements should be unchanged.
        for (let c = 0; c < numberOfChannels; ++c) {
          let dst8 = createInitializedF32Array(8);
          let startInChannel = bufferLength - 5;
          buffer.copyFromChannel(dst8, c, startInChannel);
          shouldBeRamp(
              should,
              'buffer.copyFromChannel(dst8, ' + c + ', ' + startInChannel + ')',
              dst8, c + 1 + startInChannel, bufferLength - startInChannel);
        }

        // Copy operation with the destination longer than the buffer, leaving
        // the trailing elements of the destination untouched.
        for (let c = 0; c < numberOfChannels; ++c) {
          let dst26 = createInitializedF32Array(bufferLength + 10);
          buffer.copyFromChannel(dst26, c);
          shouldBeRamp(
              should, 'buffer.copyFromChannel(dst26, ' + c + ')', dst26, c + 1,
              bufferLength);
        }

        task.done();
      });

      // Test copyTo
      audit.define('copyTo-validate', (task, should) => {
        // Create a source consisting of a ramp starting at 1, longer than the
        // AudioBuffer
        let src = createFloat32RampArray(bufferLength + 10);

        // Test copyTo with AudioBuffer shorter than Float32Array. The
        // AudioBuffer should be completely filled with the Float32Array.
        should(
            () => {
              buffer =
                  createConstantBuffer(context, bufferLength, initialValues);
            },
            'buffer = createConstantBuffer(context, ' + bufferLength + ', [' +
                initialValues + '])')
            .notThrow();

        for (let c = 0; c < numberOfChannels; ++c) {
          buffer.copyToChannel(src, c);
          shouldBeRamp(
              should, 'buffer.copyToChannel(src, ' + c + ')',
              buffer.getChannelData(c), 1, bufferLength);
        }

        // Test copyTo with AudioBuffer longer than the Float32Array.  The tail
        // of the AudioBuffer should be unchanged.
        buffer = createConstantBuffer(context, bufferLength, initialValues);
        let src10 = createFloat32RampArray(10);
        for (let c = 0; c < numberOfChannels; ++c) {
          buffer.copyToChannel(src10, c);
          shouldBeRamp(
              should, 'buffer.copyToChannel(src10, ' + c + ')',
              buffer.getChannelData(c), 1, 10);
        }

        // Test copyTo with non-default startInChannel.  Part of the AudioBuffer
        // should filled with the beginning and end sections untouched.
        buffer = createConstantBuffer(context, bufferLength, initialValues);
        for (let c = 0; c < numberOfChannels; ++c) {
          let startInChannel = 5;
          buffer.copyToChannel(src10, c, startInChannel);

          shouldBeRamp(
              should,
              'buffer.copyToChannel(src10, ' + c + ', ' + startInChannel + ')',
              buffer.getChannelData(c), 1, src10.length, startInChannel);
        }
        task.done();
      });

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