fc719882b96f1ae74b0b8ff4e2ace39e47146194
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / webaudio / audioparam-connect-audioratesignal.html
1 <!DOCTYPE html>
2
3 <!--
4 Tests that an audio-rate signal (AudioNode output) can be connected to an AudioParam.
5 Specifically, this tests that an audio-rate signal coming from an AudioBufferSourceNode
6 playing an AudioBuffer containing a specific curve can be connected to an AudioGainNode's
7 .gain attribute (an AudioParam).  Another AudioBufferSourceNode will be the audio source
8 having its gain changed.  We load this one with an AudioBuffer containing a constant value of 1.
9 Thus it's easy to check that the resultant signal should be equal to the gain-scaling curve.
10 -->
11
12 <html>
13 <head>
14 <script src="resources/audio-testing.js"></script>
15 <script src="../resources/js-test.js"></script>
16
17 </head>
18 <body>
19
20 <script>
21
22 var sampleRate = 44100.0;
23 var lengthInSeconds = 1;
24
25 var context = 0;
26 var constantOneBuffer = 0;
27 var linearRampBuffer = 0;
28
29 function checkResult(event) {
30     var renderedBuffer = event.renderedBuffer;
31     var renderedData = renderedBuffer.getChannelData(0);
32     var expectedData = linearRampBuffer.getChannelData(0);
33     var n = renderedBuffer.length;
34
35     if (n == linearRampBuffer.length) {
36         testPassed("Rendered signal is of correct length.");
37     } else {
38         testFailed("Rendered signal is not of correct length.");
39     }
40
41     // Check that the rendered result exactly matches the buffer used to control gain.
42     // This is because we're changing the gain of a signal having constant value 1.
43     var success = true;
44     for (var i = 0; i < n; ++i) {
45         if (renderedData[i] != expectedData[i]) {
46             success = false;
47             break;
48         }
49     }
50
51     if (success) {
52         testPassed("Rendered signal exactly matches the audio-rate gain changing signal.");
53     } else {
54         testFailed("Rendered signal differs from the audio-rate gain changing signal.");
55     }
56
57     finishJSTest();
58 }
59
60 function runTest() {
61     if (window.testRunner) {
62         testRunner.dumpAsText();
63         testRunner.waitUntilDone();
64     }
65
66     window.jsTestIsAsync = true;
67
68     var sampleFrameLength = sampleRate * lengthInSeconds;
69
70     // Create offline audio context.
71     context = new webkitOfflineAudioContext(1, sampleFrameLength, sampleRate);
72
73     // Create buffer used by the source which will have its gain controlled.
74     constantOneBuffer = createConstantBuffer(context, sampleFrameLength, 1);
75
76     // Create buffer used to control gain.
77     linearRampBuffer = createLinearRampBuffer(context, sampleFrameLength);
78
79     // Create the two sources.
80
81     var constantSource = context.createBufferSource();
82     constantSource.buffer = constantOneBuffer;
83
84     var gainChangingSource = context.createBufferSource();
85     gainChangingSource.buffer = linearRampBuffer;
86
87     // Create a gain node controlling the gain of constantSource and make the connections.
88     var gainNode = context.createGainNode();
89
90     // Intrinsic baseline gain of zero.
91     gainNode.gain.value = 0;
92
93     constantSource.connect(gainNode);
94     gainNode.connect(context.destination);
95
96     // Connect an audio-rate signal to control the .gain AudioParam.
97     // This is the heart of what is being tested.
98     gainChangingSource.connect(gainNode.gain);
99
100     // Start both sources at time 0.
101     constantSource.noteOn(0);
102     gainChangingSource.noteOn(0);
103
104     context.oncomplete = checkResult;
105     context.startRendering();
106 }
107
108 runTest();
109 successfullyParsed = true;
110
111 </script>
112
113 </body>
114 </html>