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
|
<!DOCTYPE html>
<html>
<head>
<title>Test Simple AudioWorklet I/O</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/webaudio/resources/audit.js"></script>
<script src="/webaudio/resources/audit-util.js"></script>
</head>
<body>
<script>
// Arbitrary sample rate
const sampleRate = 48000;
// The offset to be applied by the worklet to its inputs.
const offset = 1;
// Location of the worklet's code
const filePath = 'processors/add-offset.js';
let audit = Audit.createTaskRunner();
// Context to be used for the tests.
let context;
audit.define('Initialize worklet', (task, should) => {
// Two channels for testing. Channel 0 is the output of the
// AudioWorklet. Channel 1 is the oscillator so we can compare
// the outputs.
context = new OfflineAudioContext(
{numberOfChannels: 2, length: sampleRate, sampleRate: sampleRate});
// Load up the code for the worklet.
should(
context.audioWorklet.addModule(filePath),
'Creation of AudioWorklet')
.beResolved()
.then(() => task.done());
});
audit.define(
{label: 'test', description: 'Simple AudioWorklet I/O'},
(task, should) => {
let merger = new ChannelMergerNode(
context, {numberOfChannels: context.destination.channelCount});
merger.connect(context.destination);
let src = new OscillatorNode(context);
let worklet = new AudioWorkletNode(
context, 'add-offset-processor',
{processorOptions: {offset: offset}});
src.connect(worklet).connect(merger, 0, 0);
src.connect(merger, 0, 1);
// Start and stop the source. The stop time is fairly arbitrary,
// but use a render quantum boundary for simplicity.
const stopFrame = RENDER_QUANTUM_FRAMES;
src.start(0);
src.stop(stopFrame / context.sampleRate);
context.startRendering()
.then(buffer => {
let ch0 = buffer.getChannelData(0);
let ch1 = buffer.getChannelData(1);
let shifted = ch1.slice(0, stopFrame).map(x => x + offset);
// The initial part of the output should be the oscillator
// shifted by |offset|.
should(
ch0.slice(0, stopFrame),
`AudioWorklet output[0:${stopFrame - 1}]`)
.beCloseToArray(shifted, {absoluteThreshold: 0});
// Output should be constant after the source has stopped.
should(
ch0.slice(stopFrame),
`AudioWorklet output[${stopFrame}:]`)
.beConstantValueOf(offset);
})
.then(() => task.done());
});
audit.run();
</script>
</body>
</html>
|