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
|
<!DOCTYPE html>
<!--
Tests that we are able to schedule a series of notes to playback with sample-accuracy.
We use an impulse so we can tell exactly where the rendering is happening.
-->
<html>
<head>
<title>
sample-accurate-scheduling.html
</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">
let audit = Audit.createTaskRunner();
let sampleRate = 44100.0;
let lengthInSeconds = 4;
let context = 0;
let bufferLoader = 0;
let impulse;
// See if we can render at exactly these sample offsets.
let sampleOffsets = [0, 3, 512, 517, 1000, 1005, 20000, 21234, 37590];
function createImpulse() {
// An impulse has a value of 1 at time 0, and is otherwise 0.
impulse = context.createBuffer(2, 512, sampleRate);
let sampleDataL = impulse.getChannelData(0);
let sampleDataR = impulse.getChannelData(1);
sampleDataL[0] = 1.0;
sampleDataR[0] = 1.0;
}
function playNote(time) {
let bufferSource = context.createBufferSource();
bufferSource.buffer = impulse;
bufferSource.connect(context.destination);
bufferSource.start(time);
}
function checkSampleAccuracy(buffer, should) {
let bufferDataL = buffer.getChannelData(0);
let bufferDataR = buffer.getChannelData(1);
let impulseCount = 0;
let badOffsetCount = 0;
// Left and right channels must be the same.
should(bufferDataL, 'Content of left and right channels match and')
.beEqualToArray(bufferDataR);
// Go through every sample and make sure it's 0, except at positions in
// sampleOffsets.
for (let i = 0; i < buffer.length; ++i) {
if (bufferDataL[i] != 0) {
// Make sure this index is in sampleOffsets
let found = false;
for (let j = 0; j < sampleOffsets.length; ++j) {
if (sampleOffsets[j] == i) {
found = true;
break;
}
}
++impulseCount;
should(found, 'Non-zero sample found at sample offset ' + i)
.beTrue();
if (!found) {
++badOffsetCount;
}
}
}
should(impulseCount, 'Number of impulses found')
.beEqualTo(sampleOffsets.length);
if (impulseCount == sampleOffsets.length) {
should(badOffsetCount, 'bad offset').beEqualTo(0);
}
}
audit.define(
{label: 'test', description: 'Test sample-accurate scheduling'},
function(task, should) {
// Create offline audio context.
context = new OfflineAudioContext(
2, sampleRate * lengthInSeconds, sampleRate);
createImpulse();
for (let i = 0; i < sampleOffsets.length; ++i) {
let timeInSeconds = sampleOffsets[i] / sampleRate;
playNote(timeInSeconds);
}
context.startRendering().then(function(buffer) {
checkSampleAccuracy(buffer, should);
task.done();
});
});
audit.run();
</script>
</body>
</html>
|