OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <html> |
| 3 |
| 4 <head> |
| 5 <script src="resources/compatibility.js"></script> |
| 6 <script src="resources/audio-testing.js"></script> |
| 7 <script src="../resources/js-test.js"></script> |
| 8 </head> |
| 9 |
| 10 <body> |
| 11 <div id="description"></div> |
| 12 <div id="console"></div> |
| 13 <script> |
| 14 description("Test panning model of StereoPannerNode."); |
| 15 |
| 16 window.jsTestIsAsync = true; |
| 17 |
| 18 var sampleRate = 44100; |
| 19 |
| 20 // Number of nodes to create for testing. |
| 21 var nodesToCreate = 100; |
| 22 |
| 23 // Interval between the onset of each impulse. |
| 24 var timeStep = 0.001; |
| 25 |
| 26 // Total render length. Should be long enough for all panner nodes. |
| 27 var renderLength = timeStep * (nodesToCreate + 1); |
| 28 |
| 29 var impulse; |
| 30 var impulseLength = Math.round(timeStep * sampleRate); |
| 31 |
| 32 // Pan step unit for each test trial. Note that panStep spans over 0 to 4. |
| 33 var panStep = 4 / (nodesToCreate - 1); |
| 34 |
| 35 var sources = []; |
| 36 var panners = []; |
| 37 var panPositions = []; |
| 38 var onsets = []; |
| 39 |
| 40 var context = new OfflineAudioContext(2, sampleRate * renderLength, sampleRa
te); |
| 41 |
| 42 // Calculates channel gains based on equal power panning model. The internal |
| 43 // panning clipped the pan value between -1, 1. |
| 44 function getChannelGains (pan) { |
| 45 pan = Math.min(Math.max(pan, -1), 1); |
| 46 var normalized = 0.5 * Math.PI * (pan * 0.5 + 0.5); |
| 47 return { |
| 48 gainL: Math.cos(normalized), |
| 49 gainR: Math.sin(normalized) |
| 50 }; |
| 51 } |
| 52 |
| 53 function prepareTest() { |
| 54 impulse = createImpulseBuffer(context, impulseLength); |
| 55 |
| 56 // Create multiple buffer source nodes and panner nodes for testing. |
| 57 // Each source node plays the same impulse buffer and goes through a |
| 58 // panner node. Each trial is performed sequentially with the interval of |
| 59 // 'timeStep' and a different pan position. |
| 60 for (var i = 0; i < nodesToCreate; i++) { |
| 61 sources[i] = context.createBufferSource(); |
| 62 panners[i] = context.createStereoPanner(); |
| 63 sources[i].connect(panners[i]); |
| 64 panners[i].connect(context.destination); |
| 65 |
| 66 sources[i].buffer = impulse; |
| 67 |
| 68 // Moves the pan value for each panner by pan step unit from -2 to 2. |
| 69 // This is to check if the internal panning value is clipped properly. |
| 70 panners[i].pan.value = panPositions[i] = panStep * i - 2; |
| 71 |
| 72 onsets[i] = timeStep * i; |
| 73 sources[i].start(onsets[i]); |
| 74 } |
| 75 } |
| 76 |
| 77 // To verify the result, check if each source is an impulse starting at a |
| 78 // different time and the rendered impulse has the expected gain. |
| 79 function verifyResult(event) { |
| 80 |
| 81 var success = true; |
| 82 |
| 83 // The max error we allow between the rendered impulse and the |
| 84 // expected value. This value is experimentally determined. Set |
| 85 // to 0 to make the test fail to see what the actual error is. |
| 86 var maxAllowedError = 1.3e-6; |
| 87 |
| 88 var chanL = event.renderedBuffer.getChannelData(0), |
| 89 chanR = event.renderedBuffer.getChannelData(1); |
| 90 |
| 91 // Number of impulses found in the rendered result. |
| 92 var impulseIndex = 0; |
| 93 |
| 94 // Max (relative) error and the index of the maxima for the left |
| 95 // and right channels. |
| 96 var maxErrorL = 0, |
| 97 maxErrorR = 0, |
| 98 maxErrorIndexL = 0, |
| 99 maxErrorIndexR = 0; |
| 100 |
| 101 // Locations of where the impulses aren't at the expected locations. |
| 102 var errors = []; |
| 103 |
| 104 for (var i = 0; i < chanL.length; i++) { |
| 105 |
| 106 // We assume that the left and right channels start at the same instant. |
| 107 if (chanL[i] !== 0 || chanR[i] !== 0) { |
| 108 |
| 109 // Get amount of error between actual and expected gain. |
| 110 var expected = getChannelGains(panPositions[impulseIndex]), |
| 111 errorL = Math.abs(chanL[i] - expected.gainL), |
| 112 errorR = Math.abs(chanR[i] - expected.gainR); |
| 113 |
| 114 if (errorL > maxErrorL) { |
| 115 maxErrorL = errorL; |
| 116 maxErrorIndexL = impulseIndex; |
| 117 } |
| 118 |
| 119 if (errorR > maxErrorR) { |
| 120 maxErrorR = errorR; |
| 121 maxErrorIndexR = impulseIndex; |
| 122 } |
| 123 |
| 124 // Keep track of the impulses that didn't show up where we expected |
| 125 // them to be. |
| 126 var expectedOffset = timeToSampleFrame(onsets[impulseIndex], sampleRat
e); |
| 127 if (i != expectedOffset) { |
| 128 errors.push({ |
| 129 actual: i, |
| 130 expected: expectedOffset |
| 131 }); |
| 132 } |
| 133 |
| 134 impulseIndex++; |
| 135 } |
| 136 } |
| 137 |
| 138 if (impulseIndex === nodesToCreate) { |
| 139 testPassed('Number of impulses matches the number of panner nodes.'); |
| 140 } else { |
| 141 testFailed('Number of impulses is incorrect. (Found ' + impulseIndex + '
but expected ' + nodesToCreate + ')'); |
| 142 sucess = false; |
| 143 } |
| 144 |
| 145 if (errors.length === 0) { |
| 146 testPassed("All impulses at expected offsets."); |
| 147 } else { |
| 148 testFailed(errors.length + " timing errors found in " + nodesToCreate +
" panner nodes."); |
| 149 for (var i = 0; i < errors.length; i++) { |
| 150 testFailed("Impulse at sample " + errors[i].actual + " but expected "
+ errors[i].expected); |
| 151 } |
| 152 success = false; |
| 153 } |
| 154 |
| 155 if (maxErrorL <= maxAllowedError) { |
| 156 testPassed("Left channel gain values are correct."); |
| 157 } else { |
| 158 testFailed("Left channel gain values are incorrect. Max error = " + max
ErrorL + " at time " + onsets[maxErrorIndexL] + " (threshold = " + maxAllowedErr
or + ")"); |
| 159 success = false; |
| 160 } |
| 161 |
| 162 if (maxErrorR <= maxAllowedError) { |
| 163 testPassed("Right channel gain values are correct."); |
| 164 } else { |
| 165 testFailed("Right channel gain values are incorrect. Max error = " + ma
xErrorR + " at time " + onsets[maxErrorIndexR] + " (threshold = " + maxAllowedEr
ror + ")"); |
| 166 success = false; |
| 167 } |
| 168 |
| 169 if (success) |
| 170 testPassed("StereoPannerNode test passed."); |
| 171 else |
| 172 testFailed("StereoPannerNode test failed."); |
| 173 |
| 174 finishJSTest(); |
| 175 } |
| 176 |
| 177 prepareTest(); |
| 178 context.oncomplete = verifyResult; |
| 179 context.startRendering(); |
| 180 |
| 181 successfullyParsed = true; |
| 182 </script> |
| 183 </body> |
| 184 |
| 185 </html> |
OLD | NEW |