3 Tests that we are able to schedule a series of notes to playback with sample-accuracy.
4 We use an impulse so we can tell exactly where the rendering is happening.
9 sample-accurate-scheduling.html
11 <script src=
"/resources/testharness.js"></script>
12 <script src=
"/resources/testharnessreport.js"></script>
13 <script src=
"/webaudio/resources/audit-util.js"></script>
14 <script src=
"/webaudio/resources/audit.js"></script>
15 <script src=
"/webaudio/resources/buffer-loader.js"></script>
18 <script id=
"layout-test-code">
19 let audit = Audit.createTaskRunner();
21 let sampleRate =
44100.0;
22 let lengthInSeconds =
4;
28 // See if we can render at exactly these sample offsets.
29 let sampleOffsets = [
0,
3,
512,
517,
1000,
1005,
20000,
21234,
37590];
31 function createImpulse() {
32 // An impulse has a value of
1 at time
0, and is otherwise
0.
33 impulse = context.createBuffer(
2,
512, sampleRate);
34 let sampleDataL = impulse.getChannelData(
0);
35 let sampleDataR = impulse.getChannelData(
1);
40 function playNote(time) {
41 let bufferSource = context.createBufferSource();
42 bufferSource.buffer = impulse;
43 bufferSource.connect(context.destination);
44 bufferSource.start(time);
47 function checkSampleAccuracy(buffer, should) {
48 let bufferDataL = buffer.getChannelData(
0);
49 let bufferDataR = buffer.getChannelData(
1);
52 let badOffsetCount =
0;
54 // Left and right channels must be the same.
55 should(bufferDataL, 'Content of left and right channels match and')
56 .beEqualToArray(bufferDataR);
58 // Go through every sample and make sure it's
0, except at positions in
60 for (let i =
0; i < buffer.length; ++i) {
61 if (bufferDataL[i] !=
0) {
62 // Make sure this index is in sampleOffsets
64 for (let j =
0; j < sampleOffsets.length; ++j) {
65 if (sampleOffsets[j] == i) {
71 should(found, 'Non-zero sample found at sample offset ' + i)
79 should(impulseCount, 'Number of impulses found')
80 .beEqualTo(sampleOffsets.length);
82 if (impulseCount == sampleOffsets.length) {
83 should(badOffsetCount, 'bad offset').beEqualTo(
0);
88 {label: 'test', description: 'Test sample-accurate scheduling'},
89 function(task, should) {
91 // Create offline audio context.
92 context = new OfflineAudioContext(
93 2, sampleRate * lengthInSeconds, sampleRate);
96 for (let i =
0; i < sampleOffsets.length; ++i) {
97 let timeInSeconds = sampleOffsets[i] / sampleRate;
98 playNote(timeInSeconds);
101 context.startRendering().then(function(buffer) {
102 checkSampleAccuracy(buffer, should);