Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / webaudio / audioparam-summingjunction.html
blob60bf7fa077b07f2b08c5e6445eb208816dded852
1 <!DOCTYPE html>
3 <!--
4 Tests that multiple audio-rate signals (AudioNode outputs) can be connected to an AudioParam
5 and that these signals are summed, along with the AudioParams intrinsic value.
6 -->
8 <html>
9 <head>
10 <script src="resources/compatibility.js"></script>
11 <script src="resources/audio-testing.js"></script>
12 <script src="resources/mix-testing.js"></script>
13 <script src="../resources/js-test.js"></script>
15 </head>
16 <body>
18 <script>
20 var sampleRate = 44100.0;
21 var lengthInSeconds = 1;
23 var context = 0;
25 // Buffers used by the two gain controlling sources.
26 var linearRampBuffer;
27 var toneBuffer;
28 var toneFrequency = 440;
30 // Arbitrary non-zero value.
31 var baselineGain = 5;
33 // Allow for a small round-off error.
34 var maxAllowedError = 1e-6;
36 function checkResult(event) {
37 var renderedBuffer = event.renderedBuffer;
38 var renderedData = renderedBuffer.getChannelData(0);
40 // Get buffer data from the two sources used to control gain.
41 var linearRampData = linearRampBuffer.getChannelData(0);
42 var toneData = toneBuffer.getChannelData(0);
44 var n = renderedBuffer.length;
46 if (n == linearRampBuffer.length) {
47 testPassed("Rendered signal is of correct length.");
48 } else {
49 testFailed("Rendered signal is not of correct length.");
52 // Check that the rendered result exactly matches the sum of the intrinsic gain plus the two sources used to control gain.
53 // This is because we're changing the gain of a signal having constant value 1.
54 var success = true;
55 for (var i = 0; i < n; ++i) {
56 var expectedValue = baselineGain + linearRampData[i] + toneData[i];
57 var error = Math.abs(expectedValue - renderedData[i]);
59 if (error > maxAllowedError) {
60 success = false;
61 break;
65 if (success) {
66 testPassed("Rendered signal matches sum of two audio-rate gain changing signals plus baseline gain.");
67 } else {
68 testFailed("Rendered signal differs from the sum of two audio-rate gain changing signals plus baseline gain.");
71 finishJSTest();
74 function runTest() {
75 if (window.testRunner) {
76 testRunner.dumpAsText();
77 testRunner.waitUntilDone();
80 window.jsTestIsAsync = true;
82 var sampleFrameLength = sampleRate * lengthInSeconds;
84 // Create offline audio context.
85 context = new OfflineAudioContext(1, sampleFrameLength, sampleRate);
87 // Create buffer used by the source which will have its gain controlled.
88 var constantOneBuffer = createConstantBuffer(context, sampleFrameLength, 1);
89 var constantSource = context.createBufferSource();
90 constantSource.buffer = constantOneBuffer;
92 // Create 1st buffer used to control gain (a linear ramp).
93 linearRampBuffer = createLinearRampBuffer(context, sampleFrameLength);
94 var gainSource1 = context.createBufferSource();
95 gainSource1.buffer = linearRampBuffer;
97 // Create 2st buffer used to control gain (a simple sine wave tone).
98 toneBuffer = createToneBuffer(context, toneFrequency, lengthInSeconds, 1);
99 var gainSource2 = context.createBufferSource();
100 gainSource2.buffer = toneBuffer;
102 // Create a gain node controlling the gain of constantSource and make the connections.
103 var gainNode = context.createGain();
105 // Intrinsic baseline gain.
106 // This gain value should be summed with gainSource1 and gainSource2.
107 gainNode.gain.value = baselineGain;
109 constantSource.connect(gainNode);
110 gainNode.connect(context.destination);
112 // Connect two audio-rate signals to control the .gain AudioParam.
113 gainSource1.connect(gainNode.gain);
114 gainSource2.connect(gainNode.gain);
116 // Start all sources at time 0.
117 constantSource.start(0);
118 gainSource1.start(0);
119 gainSource2.start(0);
121 context.oncomplete = checkResult;
122 context.startRendering();
125 runTest();
126 successfullyParsed = true;
128 </script>
130 </body>
131 </html>