| OLD | NEW |
| 1 var sampleRate = 44100.0; | 1 var sampleRate = 44100.0; |
| 2 | 2 |
| 3 var numberOfChannels = 1; | 3 var numberOfChannels = 1; |
| 4 | 4 |
| 5 // Time step when each panner node starts. | 5 // Time step when each panner node starts. |
| 6 var timeStep = 0.001; | 6 var timeStep = 0.001; |
| 7 | 7 |
| 8 // Length of the impulse signal. | 8 // Length of the impulse signal. |
| 9 var pulseLengthFrames = Math.round(timeStep * sampleRate); | 9 var pulseLengthFrames = Math.round(timeStep * sampleRate); |
| 10 | 10 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 | 55 |
| 56 bufferSource[k].connect(panner[k]); | 56 bufferSource[k].connect(panner[k]); |
| 57 panner[k].connect(context.destination); | 57 panner[k].connect(context.destination); |
| 58 | 58 |
| 59 // Start the source | 59 // Start the source |
| 60 time[k] = k * timeStep; | 60 time[k] = k * timeStep; |
| 61 bufferSource[k].start(time[k]); | 61 bufferSource[k].start(time[k]); |
| 62 } | 62 } |
| 63 } | 63 } |
| 64 | 64 |
| 65 function createTestAndRun(context, nodeCount, numberOfSourceChannels, positionSe
tter) { | 65 function createTestAndRun(context, should, nodeCount, numberOfSourceChannels, |
| 66 positionSetter) { |
| 66 numberOfChannels = numberOfSourceChannels; | 67 numberOfChannels = numberOfSourceChannels; |
| 67 | 68 |
| 68 createGraph(context, nodeCount, positionSetter); | 69 createGraph(context, nodeCount, positionSetter); |
| 69 | 70 |
| 70 context.oncomplete = checkResult; | 71 return context.startRendering() |
| 71 context.startRendering(); | 72 .then(buffer => checkResult(buffer, should)); |
| 72 } | 73 } |
| 73 | 74 |
| 74 // Map our position angle to the azimuth angle (in degrees). | 75 // Map our position angle to the azimuth angle (in degrees). |
| 75 // | 76 // |
| 76 // An angle of 0 corresponds to an azimuth of 90 deg; pi, to -90 deg. | 77 // An angle of 0 corresponds to an azimuth of 90 deg; pi, to -90 deg. |
| 77 function angleToAzimuth(angle) { | 78 function angleToAzimuth(angle) { |
| 78 return 90 - angle * 180 / Math.PI; | 79 return 90 - angle * 180 / Math.PI; |
| 79 } | 80 } |
| 80 | 81 |
| 81 // The gain caused by the EQUALPOWER panning model | 82 // The gain caused by the EQUALPOWER panning model |
| (...skipping 19 matching lines...) Expand all Loading... |
| 101 var panPosition = azimuth / 90; | 102 var panPosition = azimuth / 90; |
| 102 | 103 |
| 103 var gainL = Math.cos(0.5 * Math.PI * panPosition); | 104 var gainL = Math.cos(0.5 * Math.PI * panPosition); |
| 104 var gainR = 1 + Math.sin(0.5 * Math.PI * panPosition); | 105 var gainR = 1 + Math.sin(0.5 * Math.PI * panPosition); |
| 105 | 106 |
| 106 return { left : gainL, right : gainR }; | 107 return { left : gainL, right : gainR }; |
| 107 } | 108 } |
| 108 } | 109 } |
| 109 } | 110 } |
| 110 | 111 |
| 111 function checkResult(event) { | 112 function checkResult(renderedBuffer, should) { |
| 112 renderedBuffer = event.renderedBuffer; | |
| 113 renderedLeft = renderedBuffer.getChannelData(0); | 113 renderedLeft = renderedBuffer.getChannelData(0); |
| 114 renderedRight = renderedBuffer.getChannelData(1); | 114 renderedRight = renderedBuffer.getChannelData(1); |
| 115 | 115 |
| 116 // The max error we allow between the rendered impulse and the | 116 // The max error we allow between the rendered impulse and the |
| 117 // expected value. This value is experimentally determined. Set | 117 // expected value. This value is experimentally determined. Set |
| 118 // to 0 to make the test fail to see what the actual error is. | 118 // to 0 to make the test fail to see what the actual error is. |
| 119 var maxAllowedError = 1.3e-6; | 119 var maxAllowedError = 1.3e-6; |
| 120 | 120 |
| 121 var success = true; | 121 var success = true; |
| 122 | 122 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 // expected them to be. | 161 // expected them to be. |
| 162 var expectedOffset = timeToSampleFrame(time[impulseCount], sampleRat
e); | 162 var expectedOffset = timeToSampleFrame(time[impulseCount], sampleRat
e); |
| 163 if (k != expectedOffset) { | 163 if (k != expectedOffset) { |
| 164 timeErrors[timeCount] = { actual : k, expected : expectedOffset}
; | 164 timeErrors[timeCount] = { actual : k, expected : expectedOffset}
; |
| 165 ++timeCount; | 165 ++timeCount; |
| 166 } | 166 } |
| 167 ++impulseCount; | 167 ++impulseCount; |
| 168 } | 168 } |
| 169 } | 169 } |
| 170 | 170 |
| 171 if (impulseCount == nodesToCreate) { | 171 should(impulseCount, "Number of impulses found") |
| 172 testPassed("Number of impulses matches the number of panner nodes."); | 172 .beEqualTo(nodesToCreate); |
| 173 } else { | |
| 174 testFailed("Number of impulses is incorrect. (Found " + impulseCount +
" but expected " + nodesToCreate + ")"); | |
| 175 success = false; | |
| 176 } | |
| 177 | 173 |
| 178 if (timeErrors.length > 0) { | 174 should(timeErrors.map(x => x.actual), "Offsets of impulses at the wrong posi
tion") |
| 179 success = false; | 175 .beEqualToArray(timeErrors.map(x => x.expected)); |
| 180 testFailed(timeErrors.length + " timing errors found in " + nodesToCreat
e + " panner nodes."); | |
| 181 for (var k = 0; k < timeErrors.length; ++k) { | |
| 182 testFailed("Impulse at sample " + timeErrors[k].actual + " but expec
ted " + timeErrors[k].expected); | |
| 183 } | |
| 184 } else { | |
| 185 testPassed("All impulses at expected offsets."); | |
| 186 } | |
| 187 | 176 |
| 188 if (maxErrorL <= maxAllowedError) { | 177 should(maxErrorL, "Error in left channel gain values") |
| 189 testPassed("Left channel gain values are correct."); | 178 .beLessThanOrEqualTo(maxAllowedError); |
| 190 } else { | |
| 191 testFailed("Left channel gain values are incorrect. Max error = " + max
ErrorL + " at time " + time[maxErrorIndexL] + " (threshold = " + maxAllowedError
+ ")"); | |
| 192 success = false; | |
| 193 } | |
| 194 | |
| 195 if (maxErrorR <= maxAllowedError) { | |
| 196 testPassed("Right channel gain values are correct."); | |
| 197 } else { | |
| 198 testFailed("Right channel gain values are incorrect. Max error = " + ma
xErrorR + " at time " + time[maxErrorIndexR] + " (threshold = " + maxAllowedErro
r + ")"); | |
| 199 success = false; | |
| 200 } | |
| 201 | 179 |
| 202 if (success) { | 180 should(maxErrorR, "Error in right channel gain values") |
| 203 testPassed("EqualPower panner test passed"); | 181 .beLessThanOrEqualTo(maxAllowedError); |
| 204 } else { | |
| 205 testFailed("EqualPower panner test failed"); | |
| 206 } | |
| 207 | |
| 208 finishJSTest(); | |
| 209 } | 182 } |
| OLD | NEW |