4 Tests that we are able to schedule a series of notes to playback with sample-accuracy.
5 We use an impulse so we can tell exactly where the rendering is happening.
10 <script src=
"../resources/js-test.js"></script>
11 <script src=
"resources/compatibility.js"></script>
12 <script type=
"text/javascript" src=
"resources/audio-testing.js"></script>
13 <script type=
"text/javascript" src=
"resources/buffer-loader.js"></script>
18 <div id=
"description"></div>
19 <div id=
"console"></div>
22 description("Tests sample-accurate scheduling.");
24 var sampleRate
= 44100.0;
25 var lengthInSeconds
= 4;
31 // See if we can render at exactly these sample offsets.
32 var sampleOffsets
= [0, 3, 512, 517, 1000, 1005, 20000, 21234, 37590];
34 function createImpulse() {
35 // An impulse has a value of 1 at time 0, and is otherwise 0.
36 impulse
= context
.createBuffer(2, 512, sampleRate
);
37 var sampleDataL
= impulse
.getChannelData(0);
38 var sampleDataR
= impulse
.getChannelData(1);
43 function playNote(time
) {
44 var bufferSource
= context
.createBufferSource();
45 bufferSource
.buffer
= impulse
;
46 bufferSource
.connect(context
.destination
);
47 bufferSource
.start(time
);
50 function checkSampleAccuracy(event
) {
51 var buffer
= event
.renderedBuffer
;
53 var bufferDataL
= buffer
.getChannelData(0);
54 var bufferDataR
= buffer
.getChannelData(1);
58 var badOffsetCount
= false;
60 // Go through every sample and make sure it's 0, except at positions in sampleOffsets.
61 for (var i
= 0; i
< buffer
.length
; ++i
) {
62 // Make sure left == right
63 if (bufferDataL
[i
] != bufferDataR
[i
]) {
64 testFailed("Rendered buffer left and right channels are not identical.");
69 if (bufferDataL
[i
] != 0) {
70 // Make sure this index is in sampleOffsets
72 for (var j
= 0; j
< sampleOffsets
.length
; ++j
) {
73 if (sampleOffsets
[j
] == i
) {
80 testFailed("Non-zero sample found at sample offset " + i
);
87 if (impulseCount
== sampleOffsets
.length
) {
88 if (badOffsetCount
== 0) {
89 testPassed("Expected number of events found.");
91 testFailed("Expected number of events found, but " + badOffsetCount
+ " are at incorrect offsets.");
95 testFailed("Expected " + sampleOffsets
.length
+ " impulses but only found " + impulseCount
);
100 testPassed("All events rendered with sample-accuracy.");
102 testFailed("Events NOT rendered with sample-accuracy.");
109 if (window
.testRunner
) {
110 testRunner
.dumpAsText();
111 testRunner
.waitUntilDone();
114 window
.jsTestIsAsync
= true;
116 // Create offline audio context.
117 context
= new OfflineAudioContext(2, sampleRate
* lengthInSeconds
, sampleRate
);
120 for (var i
= 0; i
< sampleOffsets
.length
; ++i
) {
121 var timeInSeconds
= sampleOffsets
[i
] / sampleRate
;
122 playNote(timeInSeconds
);
125 context
.oncomplete
= checkSampleAccuracy
;
126 context
.startRendering();