4 Tests that that we can merge two mono streams into a stereo stream.
9 <link rel="stylesheet" href="../fast/js/resources/js-test-style.css"/>
10 <script src="../fast/js/resources/js-test-pre.js"></script>
11 <script type="text/javascript" src="resources/audio-testing.js"></script>
12 <script type="text/javascript" src="resources/buffer-loader.js"></script>
17 <div id="description"></div>
18 <div id="console"></div>
21 description("Tests audio channel merging of two mono streams into a single stereo stream.");
23 var sampleRate = 44100.0;
24 var lengthInSampleFrames = 512;
34 function createBufferWithDCOffset(length, sampleRate, offset) {
35 var buffer = context.createBuffer(1, length, sampleRate);
36 var data = buffer.getChannelData(0);
37 for (var i = 0; i < buffer.length; ++i)
43 // checkResult() checks that the rendered buffer is stereo and that the left channel is all -1 and right channel all +1.
44 function checkResult(event) {
45 var buffer = event.renderedBuffer;
49 if (buffer.numberOfChannels == 2) {
50 var bufferDataL = buffer.getChannelData(0);
51 var bufferDataR = buffer.getChannelData(1);
53 // Go through every sample and make sure it's all -1 for the left-channel, and all +1 for the right-channel.
54 for (var i = 0; i < buffer.length; ++i) {
55 if (bufferDataL[i] != -1 || bufferDataR[i] != 1) {
65 testPassed("Correctly merged from two mono streams to stereo.");
67 testFailed("Merging error from two mono streams to stereo.");
74 if (window.layoutTestController) {
75 layoutTestController.dumpAsText();
76 layoutTestController.waitUntilDone();
79 window.jsTestIsAsync = true;
81 // Create stereo offline audio context.
82 context = new webkitAudioContext(2, lengthInSampleFrames, sampleRate);
84 // Create two mono buffers, one having all -1 values, the other all +1.
85 buffer1 = createBufferWithDCOffset(lengthInSampleFrames, sampleRate, -1);
86 buffer2 = createBufferWithDCOffset(lengthInSampleFrames, sampleRate, 1);
88 // Create a buffer source for each of these buffers.
89 bufferSource1 = context.createBufferSource();
90 bufferSource2 = context.createBufferSource();
91 bufferSource1.buffer = buffer1;
92 bufferSource2.buffer = buffer2;
94 // Create a channel merger and connect it so that it merges two mono streams into a single stereo stream.
96 channelMerger = context.createChannelMerger();
97 bufferSource1.connect(channelMerger, 0, 0); // connect to input 0 of the channel merger
98 bufferSource2.connect(channelMerger, 0, 1); // connect to input 1 of the channel merger
100 channelMerger.connect(context.destination);
102 // Trigger both sources to start at the beginning.
103 bufferSource1.noteOn(0);
104 bufferSource2.noteOn(0);
106 context.oncomplete = checkResult;
107 context.startRendering();
111 successfullyParsed = true;
115 <script src="../fast/js/resources/js-test-post.js"></script>