4 <title>Test AudioBufferSource.onended
</title>
5 <style type=
"text/css">
11 font-family: monospace
;
17 <h1>Test AudioBufferSource.onended
</h1>
19 <p>Tests that the onended event is called. This test cannot be run in an offline context
20 because the onended event is always called.
23 <p>Press
"Test" button to run the test. You should hear two tones, each lasting
1/
2 second. If
24 you do not, the test failed and onended was not correctly fired to generate the second tone.
25 There should also be messages displayed for each tone played.
28 <button onclick=
"runTest()">Test
</button>
30 <header>Results
</header>
31 <div id=
"results"></div>
34 // This is a slightly modified version of http://jsfiddle.net/ep4zm233/
36 var context
= new AudioContext();
41 // Create two buffers at a sample rate of 8000. We're assuming 8000 is not the actual
42 // context sampleRate so that resampling happens during the play back of the buffers.
43 var bufferRate
= 8000;
44 var bufferSeconds
= 0.5;
45 var bufferFrames
= bufferSeconds
* bufferRate
;
47 // The tone buffers at 400Hz and 600 Hz.
48 var sin400
= context
.createBuffer(1, bufferFrames
, bufferRate
);
49 var sin600
= context
.createBuffer(1, bufferFrames
, bufferRate
);
51 var d400
= sin400
.getChannelData(0);
52 var d600
= sin600
.getChannelData(0);
54 var omega
= 2*Math
.PI
/bufferRate
;
56 for (var k
= 0; k
< bufferFrames
; ++k
) {
57 d400
[k
] = Math
.sin(omega
* 400 * k
);
58 d600
[k
] = Math
.sin(omega
* 600 * k
);
61 var s1
= context
.createBufferSource();
63 s1
.onended = function () {
64 // Create a new source using the 600Hz buffer and play it as soon as the onended event for
66 var s2
= context
.createBufferSource();
67 s2
.connect(context
.destination
);
73 // Set up the 400 Hz buffer and play it.
76 s1
.connect(context
.destination
);
81 function clearResults() {
82 var results
= document
.querySelector("#results");
83 results
.textContent
= "";
86 function log(message
) {
88 var results
= document
.querySelector("#results");
89 results
.textContent
+= message
+ "\n";