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.
10 <link rel="stylesheet" href="../fast/js/resources/js-test-style.css"/>
11 <script src="resources/audio-testing.js"></script>
12 <script src="resources/mix-testing.js"></script>
13 <script src="../fast/js/resources/js-test-pre.js"></script>
20 var sampleRate = 44100.0;
21 var lengthInSeconds = 1;
25 // Buffers used by the two gain controlling sources.
28 var toneFrequency = 440;
30 // Arbitrary non-zero value.
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.");
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.
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) {
66 testPassed("Rendered signal matches sum of two audio-rate gain changing signals plus baseline gain.");
68 testFailed("Rendered signal differs from the sum of two audio-rate gain changing signals plus baseline gain.");
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 webkitOfflineAudioContext(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.createGainNode();
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.noteOn(0);
118 gainSource1.noteOn(0);
119 gainSource2.noteOn(0);
121 context.oncomplete = checkResult;
122 context.startRendering();
126 successfullyParsed = true;
129 <script src="../fast/js/resources/js-test-post.js"></script>