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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
// META: global=window,dedicatedworker
// META: script=/common/media.js
// META: script=/webcodecs/utils.js
var defaultInit =
{
timestamp: 1234,
channels: 2,
sampleRate: 8000,
frames: 100,
}
function
createDefaultAudioData() {
return make_audio_data(
defaultInit.timestamp, defaultInit.channels, defaultInit.sampleRate,
defaultInit.frames);
}
test(t => {
let local_data = new Float32Array(defaultInit.channels * defaultInit.frames);
let audio_data_init = {
timestamp: defaultInit.timestamp,
data: local_data,
numberOfFrames: defaultInit.frames,
numberOfChannels: defaultInit.channels,
sampleRate: defaultInit.sampleRate,
format: 'f32-planar',
}
let data = new AudioData(audio_data_init);
assert_equals(data.timestamp, defaultInit.timestamp, 'timestamp');
assert_equals(data.numberOfFrames, defaultInit.frames, 'frames');
assert_equals(data.numberOfChannels, defaultInit.channels, 'channels');
assert_equals(data.sampleRate, defaultInit.sampleRate, 'sampleRate');
assert_equals(
data.duration, defaultInit.frames / defaultInit.sampleRate * 1_000_000,
'duration');
assert_equals(data.format, 'f32-planar', 'format');
// Create an Int16 array of the right length.
let small_data = new Int16Array(defaultInit.channels * defaultInit.frames);
let wrong_format_init = {...audio_data_init};
wrong_format_init.data = small_data;
// Creating `f32-planar` AudioData from Int16 from should throw.
assert_throws_js(TypeError, () => {
let data = new AudioData(wrong_format_init);
}, `AudioDataInit.data needs to be big enough`);
var members = [
'timestamp',
'data',
'numberOfFrames',
'numberOfChannels',
'sampleRate',
'format',
];
for (const member of members) {
let incomplete_init = {...audio_data_init};
delete incomplete_init[member];
assert_throws_js(
TypeError, () => {let data = new AudioData(incomplete_init)},
'AudioData requires \'' + member + '\'');
}
let invalid_init = {...audio_data_init};
invalid_init.numberOfFrames = 0
assert_throws_js(
TypeError, () => {let data = new AudioData(invalid_init)},
'AudioData requires numberOfFrames > 0');
invalid_init = {...audio_data_init};
invalid_init.numberOfChannels = 0
assert_throws_js(
TypeError, () => {let data = new AudioData(invalid_init)},
'AudioData requires numberOfChannels > 0');
}, 'Verify AudioData constructors');
test(t => {
let data = createDefaultAudioData();
let clone = data.clone();
// Verify the parameters match.
assert_equals(data.timestamp, clone.timestamp, 'timestamp');
assert_equals(data.numberOfFrames, clone.numberOfFrames, 'frames');
assert_equals(data.numberOfChannels, clone.numberOfChannels, 'channels');
assert_equals(data.sampleRate, clone.sampleRate, 'sampleRate');
assert_equals(data.format, clone.format, 'format');
const data_copyDest = new Float32Array(defaultInit.frames);
const clone_copyDest = new Float32Array(defaultInit.frames);
// Verify the data matches.
for (var channel = 0; channel < defaultInit.channels; channel++) {
data.copyTo(data_copyDest, {planeIndex: channel});
clone.copyTo(clone_copyDest, {planeIndex: channel});
assert_array_equals(
data_copyDest, clone_copyDest, 'Cloned data ch=' + channel);
}
// Verify closing the original data doesn't close the clone.
data.close();
assert_equals(data.numberOfFrames, 0, 'data.buffer (closed)');
assert_not_equals(clone.numberOfFrames, 0, 'clone.buffer (not closed)');
clone.close();
assert_equals(clone.numberOfFrames, 0, 'clone.buffer (closed)');
// Verify closing a closed AudioData does not throw.
data.close();
}, 'Verify closing and cloning AudioData');
test(t => {
let data = make_audio_data(
-10, defaultInit.channels, defaultInit.sampleRate, defaultInit.frames);
assert_equals(data.timestamp, -10, 'timestamp');
data.close();
}, 'Test we can construct AudioData with a negative timestamp.');
// Each test vector represents two channels of data in the following arbitrary
// layout: <min, zero, max, min, max / 2, min / 2, zero, max, zero, zero>.
const testVectorFrames = 5;
const testVectorChannels = 2;
const testVectorInterleavedResult =
[[-1.0, 1.0, 0.5, 0.0, 0.0], [0.0, -1.0, -0.5, 1.0, 0.0]];
const testVectorPlanarResult =
[[-1.0, 0.0, 1.0, -1.0, 0.5], [-0.5, 0.0, 1.0, 0.0, 0.0]];
test(t => {
const INT8_MIN = (-0x7f - 1);
const INT8_MAX = 0x7f;
const UINT8_MAX = 0xff;
const testVectorUint8 = [
0, -INT8_MIN, UINT8_MAX, 0, INT8_MAX / 2 + 128, INT8_MIN / 2 + 128,
-INT8_MIN, UINT8_MAX, -INT8_MIN, -INT8_MIN
];
let data = new AudioData({
timestamp: defaultInit.timestamp,
data: new Uint8Array(testVectorUint8),
numberOfFrames: testVectorFrames,
numberOfChannels: testVectorChannels,
sampleRate: defaultInit.sampleRate,
format: 'u8'
});
const epsilon = 1.0 / (UINT8_MAX - 1);
let dest = new Float32Array(data.numberOfFrames);
data.copyTo(dest, {planeIndex: 0, format: 'f32-planar'});
assert_array_approx_equals(
dest, testVectorInterleavedResult[0], epsilon, 'interleaved channel 0');
data.copyTo(dest, {planeIndex: 1, format: 'f32-planar'});
assert_array_approx_equals(
dest, testVectorInterleavedResult[1], epsilon, 'interleaved channel 1');
data = new AudioData({
timestamp: defaultInit.timestamp,
data: new Uint8Array(testVectorUint8),
numberOfFrames: testVectorFrames,
numberOfChannels: testVectorChannels,
sampleRate: defaultInit.sampleRate,
format: 'u8-planar'
});
data.copyTo(dest, {planeIndex: 0, format: 'f32-planar'});
assert_array_approx_equals(
dest, testVectorPlanarResult[0], epsilon, 'planar channel 0');
data.copyTo(dest, {planeIndex: 1, format: 'f32-planar'});
assert_array_approx_equals(
dest, testVectorPlanarResult[1], epsilon, 'planar channel 1');
}, 'Test conversion of uint8 data to float32');
test(t => {
const INT16_MIN = (-0x7fff - 1);
const INT16_MAX = 0x7fff;
const testVectorInt16 = [
INT16_MIN, 0, INT16_MAX, INT16_MIN, INT16_MAX / 2, INT16_MIN / 2, 0,
INT16_MAX, 0, 0
];
let data = new AudioData({
timestamp: defaultInit.timestamp,
data: new Int16Array(testVectorInt16),
numberOfFrames: testVectorFrames,
numberOfChannels: testVectorChannels,
sampleRate: defaultInit.sampleRate,
format: 's16'
});
const epsilon = 1.0 / (INT16_MAX + 1);
let dest = new Float32Array(data.numberOfFrames);
data.copyTo(dest, {planeIndex: 0, format: 'f32-planar'});
assert_array_approx_equals(
dest, testVectorInterleavedResult[0], epsilon, 'interleaved channel 0');
data.copyTo(dest, {planeIndex: 1, format: 'f32-planar'});
assert_array_approx_equals(
dest, testVectorInterleavedResult[1], epsilon, 'interleaved channel 1');
data = new AudioData({
timestamp: defaultInit.timestamp,
data: new Int16Array(testVectorInt16),
numberOfFrames: testVectorFrames,
numberOfChannels: testVectorChannels,
sampleRate: defaultInit.sampleRate,
format: 's16-planar'
});
data.copyTo(dest, {planeIndex: 0, format: 'f32-planar'});
assert_array_approx_equals(
dest, testVectorPlanarResult[0], epsilon, 'planar channel 0');
data.copyTo(dest, {planeIndex: 1, format: 'f32-planar'});
assert_array_approx_equals(
dest, testVectorPlanarResult[1], epsilon, 'planar channel 1');
}, 'Test conversion of int16 data to float32');
test(t => {
const INT32_MIN = (-0x7fffffff - 1);
const INT32_MAX = 0x7fffffff;
const testVectorInt32 = [
INT32_MIN, 0, INT32_MAX, INT32_MIN, INT32_MAX / 2, INT32_MIN / 2, 0,
INT32_MAX, 0, 0
];
let data = new AudioData({
timestamp: defaultInit.timestamp,
data: new Int32Array(testVectorInt32),
numberOfFrames: testVectorFrames,
numberOfChannels: testVectorChannels,
sampleRate: defaultInit.sampleRate,
format: 's32'
});
const epsilon = 1.0 / INT32_MAX;
let dest = new Float32Array(data.numberOfFrames);
data.copyTo(dest, {planeIndex: 0, format: 'f32-planar'});
assert_array_approx_equals(
dest, testVectorInterleavedResult[0], epsilon, 'interleaved channel 0');
data.copyTo(dest, {planeIndex: 1, format: 'f32-planar'});
assert_array_approx_equals(
dest, testVectorInterleavedResult[1], epsilon, 'interleaved channel 1');
data = new AudioData({
timestamp: defaultInit.timestamp,
data: new Int32Array(testVectorInt32),
numberOfFrames: testVectorFrames,
numberOfChannels: testVectorChannels,
sampleRate: defaultInit.sampleRate,
format: 's32-planar'
});
data.copyTo(dest, {planeIndex: 0, format: 'f32-planar'});
assert_array_approx_equals(
dest, testVectorPlanarResult[0], epsilon, 'planar channel 0');
data.copyTo(dest, {planeIndex: 1, format: 'f32-planar'});
assert_array_approx_equals(
dest, testVectorPlanarResult[1], epsilon, 'planar channel 1');
}, 'Test conversion of int32 data to float32');
test(t => {
const testVectorFloat32 =
[-1.0, 0.0, 1.0, -1.0, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0];
let data = new AudioData({
timestamp: defaultInit.timestamp,
data: new Float32Array(testVectorFloat32),
numberOfFrames: testVectorFrames,
numberOfChannels: testVectorChannels,
sampleRate: defaultInit.sampleRate,
format: 'f32'
});
const epsilon = 0;
let dest = new Float32Array(data.numberOfFrames);
data.copyTo(dest, {planeIndex: 0, format: 'f32-planar'});
assert_array_approx_equals(
dest, testVectorInterleavedResult[0], epsilon, 'interleaved channel 0');
data.copyTo(dest, {planeIndex: 1, format: 'f32-planar'});
assert_array_approx_equals(
dest, testVectorInterleavedResult[1], epsilon, 'interleaved channel 1');
data = new AudioData({
timestamp: defaultInit.timestamp,
data: new Float32Array(testVectorFloat32),
numberOfFrames: testVectorFrames,
numberOfChannels: testVectorChannels,
sampleRate: defaultInit.sampleRate,
format: 'f32-planar'
});
data.copyTo(dest, {planeIndex: 0, format: 'f32-planar'});
assert_array_approx_equals(
dest, testVectorPlanarResult[0], epsilon, 'planar channel 0');
data.copyTo(dest, {planeIndex: 1, format: 'f32-planar'});
assert_array_approx_equals(
dest, testVectorPlanarResult[1], epsilon, 'planar channel 1');
}, 'Test conversion of float32 data to float32');
test(t => {
const testVectorFloat32 =
[-1.0, 0.0, 1.0, -1.0, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0];
let data = new AudioData({
timestamp: defaultInit.timestamp,
data: new Float32Array(testVectorFloat32),
numberOfFrames: testVectorFrames,
numberOfChannels: testVectorChannels,
sampleRate: defaultInit.sampleRate,
format: 'f32'
});
const epsilon = 0;
// Call copyTo() without specifying a format, for interleaved data.
let dest = new Float32Array(data.numberOfFrames * testVectorChannels);
data.copyTo(dest, {planeIndex: 0});
assert_array_approx_equals(
dest, testVectorFloat32, epsilon, 'interleaved data');
assert_throws_js(RangeError, () => {
data.copyTo(dest, {planeIndex: 1});
}, 'Interleaved AudioData cannot copy out planeIndex > 0');
data = new AudioData({
timestamp: defaultInit.timestamp,
data: new Float32Array(testVectorFloat32),
numberOfFrames: testVectorFrames,
numberOfChannels: testVectorChannels,
sampleRate: defaultInit.sampleRate,
format: 'f32-planar'
});
// Call copyTo() without specifying a format, for planar data.
dest = new Float32Array(data.numberOfFrames);
data.copyTo(dest, {planeIndex: 0});
assert_array_approx_equals(
dest, testVectorPlanarResult[0], epsilon, 'planar channel 0');
data.copyTo(dest, {planeIndex: 1});
assert_array_approx_equals(
dest, testVectorPlanarResult[1], epsilon, 'planar channel 1');
}, 'Test copying out planar and interleaved data');
|