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.
14 <script src="resources/audio-testing.js"></script>
15 <script src="../resources/js-test.js"></script>
22 var sampleRate = 44100.0;
23 var lengthInSeconds = 1;
26 var constantOneBuffer = 0;
27 var linearRampBuffer = 0;
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;
35 if (n == linearRampBuffer.length) {
36 testPassed("Rendered signal is of correct length.");
38 testFailed("Rendered signal is not of correct length.");
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.
44 for (var i = 0; i < n; ++i) {
45 if (renderedData[i] != expectedData[i]) {
52 testPassed("Rendered signal exactly matches the audio-rate gain changing signal.");
54 testFailed("Rendered signal differs from the audio-rate gain changing signal.");
61 if (window.testRunner) {
62 testRunner.dumpAsText();
63 testRunner.waitUntilDone();
66 window.jsTestIsAsync = true;
68 var sampleFrameLength = sampleRate * lengthInSeconds;
70 // Create offline audio context.
71 context = new webkitOfflineAudioContext(1, sampleFrameLength, sampleRate);
73 // Create buffer used by the source which will have its gain controlled.
74 constantOneBuffer = createConstantBuffer(context, sampleFrameLength, 1);
76 // Create buffer used to control gain.
77 linearRampBuffer = createLinearRampBuffer(context, sampleFrameLength);
79 // Create the two sources.
81 var constantSource = context.createBufferSource();
82 constantSource.buffer = constantOneBuffer;
84 var gainChangingSource = context.createBufferSource();
85 gainChangingSource.buffer = linearRampBuffer;
87 // Create a gain node controlling the gain of constantSource and make the connections.
88 var gainNode = context.createGainNode();
90 // Intrinsic baseline gain of zero.
91 gainNode.gain.value = 0;
93 constantSource.connect(gainNode);
94 gainNode.connect(context.destination);
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);
100 // Start both sources at time 0.
101 constantSource.noteOn(0);
102 gainChangingSource.noteOn(0);
104 context.oncomplete = checkResult;
105 context.startRendering();
109 successfullyParsed = true;