4 Tests that that we can merge two mono streams into a stereo stream.
9 <script src="../fast/js/resources/js-test-pre.js"></script>
10 <script type="text/javascript" src="resources/audio-testing.js"></script>
11 <script type="text/javascript" src="resources/buffer-loader.js"></script>
16 <div id="description"></div>
17 <div id="console"></div>
20 description("Tests audio channel merging of two mono streams into a single stereo stream.");
22 var sampleRate = 44100.0;
23 var lengthInSampleFrames = 512;
33 function createBufferWithDCOffset(length, sampleRate, offset) {
34 var buffer = context.createBuffer(1, length, sampleRate);
35 var data = buffer.getChannelData(0);
36 for (var i = 0; i < buffer.length; ++i)
42 // checkResult() checks that the rendered buffer is stereo and that the left channel is all -1 and right channel all +1.
43 function checkResult(event) {
44 var buffer = event.renderedBuffer;
48 if (buffer.numberOfChannels == 2) {
49 var bufferDataL = buffer.getChannelData(0);
50 var bufferDataR = buffer.getChannelData(1);
52 // Go through every sample and make sure it's all -1 for the left-channel, and all +1 for the right-channel.
53 for (var i = 0; i < buffer.length; ++i) {
54 if (bufferDataL[i] != -1 || bufferDataR[i] != 1) {
64 testPassed("Correctly merged from two mono streams to stereo.");
66 testFailed("Merging error from two mono streams to stereo.");
73 if (window.layoutTestController) {
74 layoutTestController.dumpAsText();
75 layoutTestController.waitUntilDone();
78 window.jsTestIsAsync = true;
80 // Create stereo offline audio context.
81 context = new webkitAudioContext(2, lengthInSampleFrames, sampleRate);
83 // Create two mono buffers, one having all -1 values, the other all +1.
84 buffer1 = createBufferWithDCOffset(lengthInSampleFrames, sampleRate, -1);
85 buffer2 = createBufferWithDCOffset(lengthInSampleFrames, sampleRate, 1);
87 // Create a buffer source for each of these buffers.
88 bufferSource1 = context.createBufferSource();
89 bufferSource2 = context.createBufferSource();
90 bufferSource1.buffer = buffer1;
91 bufferSource2.buffer = buffer2;
93 // Create a channel merger and connect it so that it merges two mono streams into a single stereo stream.
95 channelMerger = context.createChannelMerger();
96 bufferSource1.connect(channelMerger, 0, 0); // connect to input 0 of the channel merger
97 bufferSource2.connect(channelMerger, 0, 1); // connect to input 1 of the channel merger
99 channelMerger.connect(context.destination);
101 // Trigger both sources to start at the beginning.
102 bufferSource1.noteOn(0);
103 bufferSource2.noteOn(0);
105 context.oncomplete = checkResult;
106 context.startRendering();
113 <script src="../fast/js/resources/js-test-post.js"></script>