upload webkit/tizen 2.0_beta source.
[framework/web/webkit-efl.git] / LayoutTests / webaudio / audiochannelmerger-stereo.html
1 <!DOCTYPE html>
2
3 <!--
4 Tests that that we can merge two mono streams into a stereo stream.
5 -->
6
7 <html>
8 <head>
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>
12 </head>
13
14 <body>
15
16 <div id="description"></div>
17 <div id="console"></div>
18
19 <script>
20 description("Tests audio channel merging of two mono streams into a single stereo stream.");
21
22 var sampleRate = 44100.0;
23 var lengthInSampleFrames = 512;
24
25 var context = 0;
26 var bufferLoader = 0;
27 var buffer1;
28 var buffer2;
29 var bufferSource1;
30 var bufferSource2;
31 var channelMerger;
32
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)
37         data[i] = offset;
38     
39     return buffer;
40 }
41
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;
45
46     var success = true;
47
48     if (buffer.numberOfChannels == 2) {
49         var bufferDataL = buffer.getChannelData(0);
50         var bufferDataR = buffer.getChannelData(1);
51     
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) {
55                 success = false;
56                 break;
57             }
58         }
59     } else {
60         success = false;
61     }
62
63     if (success) {
64         testPassed("Correctly merged from two mono streams to stereo.");
65     } else {
66         testFailed("Merging error from two mono streams to stereo.");
67     }
68
69     finishJSTest();
70 }
71
72 function runTest() {
73     if (window.layoutTestController) {
74         layoutTestController.dumpAsText();
75         layoutTestController.waitUntilDone();
76     }
77     
78     window.jsTestIsAsync = true;
79         
80     // Create stereo offline audio context.
81     context = new webkitAudioContext(2, lengthInSampleFrames, sampleRate);
82
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);
86
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;
92
93     // Create a channel merger and connect it so that it merges two mono streams into a single stereo stream.
94     
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
98     
99     channelMerger.connect(context.destination);
100
101     // Trigger both sources to start at the beginning.
102     bufferSource1.noteOn(0);
103     bufferSource2.noteOn(0);
104     
105     context.oncomplete = checkResult;
106     context.startRendering();
107 }
108
109 runTest();
110
111 </script>
112
113 <script src="../fast/js/resources/js-test-post.js"></script>
114 </body>
115 </html>