Bug 1459041 [wpt PR 10830] - Move AudioBufferSourceNode tests to WPT, a=testonly
[gecko.git] / testing / web-platform / tests / webaudio / the-audio-api / the-audiobuffersourcenode-interface / sample-accurate-scheduling.html
blob5fafd024eef9b476f4d97b2ebaa2190a4ca520d5
1 <!DOCTYPE html>
2 <!--
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.
5 -->
6 <html>
7 <head>
8 <title>
9 sample-accurate-scheduling.html
10 </title>
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>
16 </head>
17 <body>
18 <script id="layout-test-code">
19 let audit = Audit.createTaskRunner();
21 let sampleRate = 44100.0;
22 let lengthInSeconds = 4;
24 let context = 0;
25 let bufferLoader = 0;
26 let impulse;
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);
36 sampleDataL[0] = 1.0;
37 sampleDataR[0] = 1.0;
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);
51 let impulseCount = 0;
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
59 // sampleOffsets.
60 for (let i = 0; i < buffer.length; ++i) {
61 if (bufferDataL[i] != 0) {
62 // Make sure this index is in sampleOffsets
63 let found = false;
64 for (let j = 0; j < sampleOffsets.length; ++j) {
65 if (sampleOffsets[j] == i) {
66 found = true;
67 break;
70 ++impulseCount;
71 should(found, 'Non-zero sample found at sample offset ' + i)
72 .beTrue();
73 if (!found) {
74 ++badOffsetCount;
79 should(impulseCount, 'Number of impulses found')
80 .beEqualTo(sampleOffsets.length);
82 if (impulseCount == sampleOffsets.length) {
83 should(badOffsetCount, 'bad offset').beEqualTo(0);
87 audit.define(
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);
94 createImpulse();
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);
103 task.done();
107 audit.run();
108 </script>
109 </body>
110 </html>