| OLD | NEW |
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 | 2 |
| 3 <!-- | 3 <!-- |
| 4 Tests that multiple audio-rate signals (AudioNode outputs) can be connected to a
n AudioParam | 4 Tests that multiple audio-rate signals (AudioNode outputs) can be connected to a
n AudioParam |
| 5 and that these signals are summed, along with the AudioParams intrinsic value. | 5 and that these signals are summed, along with the AudioParams intrinsic value. |
| 6 --> | 6 --> |
| 7 | 7 |
| 8 <html> | 8 <html> |
| 9 <head> | 9 <head> |
| 10 <script src="../../resources/js-test.js"></script> | 10 <script src="../../resources/testharness.js"></script> |
| 11 <script src="../../resources/testharnessreport.js"></script> |
| 11 <script src="../resources/audit-util.js"></script> | 12 <script src="../resources/audit-util.js"></script> |
| 12 <script src="../resources/audio-testing.js"></script> | 13 <script src="../resources/audit.js"></script> |
| 13 <script src="../resources/mix-testing.js"></script> | 14 <script src="../resources/mix-testing.js"></script> |
| 14 | 15 |
| 15 </head> | 16 </head> |
| 16 <body> | 17 <body> |
| 17 | 18 |
| 18 <script> | 19 <script> |
| 20 let audit = Audit.createTaskRunner(); |
| 19 | 21 |
| 20 var sampleRate = 44100.0; | 22 let sampleRate = 44100.0; |
| 21 var lengthInSeconds = 1; | 23 let lengthInSeconds = 1; |
| 22 | 24 |
| 23 var context = 0; | 25 let context = 0; |
| 24 | 26 |
| 25 // Buffers used by the two gain controlling sources. | 27 // Buffers used by the two gain controlling sources. |
| 26 var linearRampBuffer; | 28 let linearRampBuffer; |
| 27 var toneBuffer; | 29 let toneBuffer; |
| 28 var toneFrequency = 440; | 30 let toneFrequency = 440; |
| 29 | 31 |
| 30 // Arbitrary non-zero value. | 32 // Arbitrary non-zero value. |
| 31 var baselineGain = 5; | 33 let baselineGain = 5; |
| 32 | 34 |
| 33 // Allow for a small round-off error. | 35 // Allow for a small round-off error. |
| 34 var maxAllowedError = 1e-6; | 36 let maxAllowedError = 1e-6; |
| 35 | 37 |
| 36 function checkResult(event) { | 38 function checkResult(renderedBuffer, should) { |
| 37 var renderedBuffer = event.renderedBuffer; | 39 let renderedData = renderedBuffer.getChannelData(0); |
| 38 var renderedData = renderedBuffer.getChannelData(0); | |
| 39 | 40 |
| 40 // Get buffer data from the two sources used to control gain. | 41 // Get buffer data from the two sources used to control gain. |
| 41 var linearRampData = linearRampBuffer.getChannelData(0); | 42 let linearRampData = linearRampBuffer.getChannelData(0); |
| 42 var toneData = toneBuffer.getChannelData(0); | 43 let toneData = toneBuffer.getChannelData(0); |
| 43 | 44 |
| 44 var n = renderedBuffer.length; | 45 let n = renderedBuffer.length; |
| 45 | 46 |
| 46 if (n == linearRampBuffer.length) { | 47 should(n, 'Rendered signal length').beEqualTo(linearRampBuffer.length); |
| 47 testPassed("Rendered signal is of correct length."); | 48 |
| 48 } else { | 49 // Check that the rendered result exactly matches the sum of the intrinsic |
| 49 testFailed("Rendered signal is not of correct length."); | 50 // gain plus the two sources used to control gain. |
| 51 // This is because we're changing the gain of a signal having constant value |
| 52 // 1. |
| 53 let success = true; |
| 54 for (let i = 0; i < n; ++i) { |
| 55 let expectedValue = baselineGain + linearRampData[i] + toneData[i]; |
| 56 let error = Math.abs(expectedValue - renderedData[i]); |
| 57 |
| 58 if (error > maxAllowedError) { |
| 59 success = false; |
| 60 break; |
| 50 } | 61 } |
| 62 } |
| 51 | 63 |
| 52 // Check that the rendered result exactly matches the sum of the intrinsic g
ain plus the two sources used to control gain. | 64 should( |
| 53 // This is because we're changing the gain of a signal having constant value
1. | 65 success, |
| 54 var success = true; | 66 'Rendered signal matches sum of two audio-rate gain changing signals plus
baseline gain') |
| 55 for (var i = 0; i < n; ++i) { | 67 .beTrue(); |
| 56 var expectedValue = baselineGain + linearRampData[i] + toneData[i]; | |
| 57 var error = Math.abs(expectedValue - renderedData[i]); | |
| 58 | |
| 59 if (error > maxAllowedError) { | |
| 60 success = false; | |
| 61 break; | |
| 62 } | |
| 63 } | |
| 64 | |
| 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."); | |
| 69 } | |
| 70 | |
| 71 finishJSTest(); | |
| 72 } | 68 } |
| 73 | 69 |
| 74 function runTest() { | 70 audit.define('test', function(task, should) { |
| 75 if (window.testRunner) { | 71 let sampleFrameLength = sampleRate * lengthInSeconds; |
| 76 testRunner.dumpAsText(); | |
| 77 testRunner.waitUntilDone(); | |
| 78 } | |
| 79 | 72 |
| 80 window.jsTestIsAsync = true; | 73 // Create offline audio context. |
| 74 context = new OfflineAudioContext(1, sampleFrameLength, sampleRate); |
| 81 | 75 |
| 82 var sampleFrameLength = sampleRate * lengthInSeconds; | 76 // Create buffer used by the source which will have its gain controlled. |
| 77 let constantOneBuffer = createConstantBuffer(context, sampleFrameLength, 1); |
| 78 let constantSource = context.createBufferSource(); |
| 79 constantSource.buffer = constantOneBuffer; |
| 83 | 80 |
| 84 // Create offline audio context. | 81 // Create 1st buffer used to control gain (a linear ramp). |
| 85 context = new OfflineAudioContext(1, sampleFrameLength, sampleRate); | 82 linearRampBuffer = createLinearRampBuffer(context, sampleFrameLength); |
| 83 let gainSource1 = context.createBufferSource(); |
| 84 gainSource1.buffer = linearRampBuffer; |
| 86 | 85 |
| 87 // Create buffer used by the source which will have its gain controlled. | 86 // Create 2st buffer used to control gain (a simple sine wave tone). |
| 88 var constantOneBuffer = createConstantBuffer(context, sampleFrameLength, 1); | 87 toneBuffer = createToneBuffer(context, toneFrequency, lengthInSeconds, 1); |
| 89 var constantSource = context.createBufferSource(); | 88 let gainSource2 = context.createBufferSource(); |
| 90 constantSource.buffer = constantOneBuffer; | 89 gainSource2.buffer = toneBuffer; |
| 91 | 90 |
| 92 // Create 1st buffer used to control gain (a linear ramp). | 91 // Create a gain node controlling the gain of constantSource and make the |
| 93 linearRampBuffer = createLinearRampBuffer(context, sampleFrameLength); | 92 // connections. |
| 94 var gainSource1 = context.createBufferSource(); | 93 let gainNode = context.createGain(); |
| 95 gainSource1.buffer = linearRampBuffer; | |
| 96 | 94 |
| 97 // Create 2st buffer used to control gain (a simple sine wave tone). | 95 // Intrinsic baseline gain. |
| 98 toneBuffer = createToneBuffer(context, toneFrequency, lengthInSeconds, 1); | 96 // This gain value should be summed with gainSource1 and gainSource2. |
| 99 var gainSource2 = context.createBufferSource(); | 97 gainNode.gain.value = baselineGain; |
| 100 gainSource2.buffer = toneBuffer; | |
| 101 | 98 |
| 102 // Create a gain node controlling the gain of constantSource and make the co
nnections. | 99 constantSource.connect(gainNode); |
| 103 var gainNode = context.createGain(); | 100 gainNode.connect(context.destination); |
| 104 | 101 |
| 105 // Intrinsic baseline gain. | 102 // Connect two audio-rate signals to control the .gain AudioParam. |
| 106 // This gain value should be summed with gainSource1 and gainSource2. | 103 gainSource1.connect(gainNode.gain); |
| 107 gainNode.gain.value = baselineGain; | 104 gainSource2.connect(gainNode.gain); |
| 108 | 105 |
| 109 constantSource.connect(gainNode); | 106 // Start all sources at time 0. |
| 110 gainNode.connect(context.destination); | 107 constantSource.start(0); |
| 108 gainSource1.start(0); |
| 109 gainSource2.start(0); |
| 111 | 110 |
| 112 // Connect two audio-rate signals to control the .gain AudioParam. | 111 context.startRendering().then(buffer => { |
| 113 gainSource1.connect(gainNode.gain); | 112 checkResult(buffer, should); |
| 114 gainSource2.connect(gainNode.gain); | 113 task.done(); |
| 114 }); |
| 115 }); |
| 115 | 116 |
| 116 // Start all sources at time 0. | 117 audit.run(); |
| 117 constantSource.start(0); | |
| 118 gainSource1.start(0); | |
| 119 gainSource2.start(0); | |
| 120 | |
| 121 context.oncomplete = checkResult; | |
| 122 context.startRendering(); | |
| 123 } | |
| 124 | |
| 125 runTest(); | |
| 126 successfullyParsed = true; | |
| 127 | |
| 128 </script> | 118 </script> |
| 129 | 119 |
| 130 </body> | 120 </body> |
| 131 </html> | 121 </html> |
| OLD | NEW |