| 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 |
| 11 // How many panner nodes to create for the test | 11 // How many panner nodes to create for the test |
| 12 var nodesToCreate = 100; | 12 var nodesToCreate = 100; |
| 13 | 13 |
| 14 // Be sure we render long enough for all of our nodes. | 14 // Be sure we render long enough for all of our nodes. |
| 15 var renderLengthSeconds = timeStep * (nodesToCreate + 1); | 15 var renderLengthSeconds = timeStep * (nodesToCreate + 1); |
| 16 | 16 |
| 17 // These are global mostly for debugging. | 17 // These are global mostly for debugging. |
| 18 var context; | 18 var context; |
| 19 var impulse; | 19 var impulse; |
| 20 var bufferSource; | 20 var bufferSource; |
| 21 var panner; | 21 var panner; |
| 22 var position; | 22 var position; |
| 23 var time; | 23 var time; |
| 24 | 24 |
| 25 var renderedBuffer; | 25 var renderedBuffer; |
| 26 var renderedLeft; | 26 var renderedLeft; |
| 27 var renderedRight; | 27 var renderedRight; |
| 28 | 28 |
| 29 function createGraph(context, nodeCount, positionSetter) { | 29 function createGraph(context, nodeCount, positionSetter) { |
| 30 bufferSource = new Array(nodeCount); | 30 bufferSource = new Array(nodeCount); |
| 31 panner = new Array(nodeCount); | 31 panner = new Array(nodeCount); |
| 32 position = new Array(nodeCount); | 32 position = new Array(nodeCount); |
| 33 time = new Array(nodeCount); | 33 time = new Array(nodeCount); |
| 34 // Angle between panner locations. (nodeCount - 1 because we want | 34 // Angle between panner locations. (nodeCount - 1 because we want |
| 35 // to include both 0 and 180 deg. | 35 // to include both 0 and 180 deg. |
| 36 var angleStep = Math.PI / (nodeCount - 1); | 36 let angleStep = Math.PI / (nodeCount - 1); |
| 37 | 37 |
| 38 if (numberOfChannels == 2) { | 38 if (numberOfChannels == 2) { |
| 39 impulse = createStereoImpulseBuffer(context, pulseLengthFrames); | 39 impulse = createStereoImpulseBuffer(context, pulseLengthFrames); |
| 40 } | 40 } else |
| 41 else | 41 impulse = createImpulseBuffer(context, pulseLengthFrames); |
| 42 impulse = createImpulseBuffer(context, pulseLengthFrames); | |
| 43 | 42 |
| 44 for (var k = 0; k < nodeCount; ++k) { | 43 for (let k = 0; k < nodeCount; ++k) { |
| 45 bufferSource[k] = context.createBufferSource(); | 44 bufferSource[k] = context.createBufferSource(); |
| 46 bufferSource[k].buffer = impulse; | 45 bufferSource[k].buffer = impulse; |
| 47 | 46 |
| 48 panner[k] = context.createPanner(); | 47 panner[k] = context.createPanner(); |
| 49 panner[k].panningModel = "equalpower"; | 48 panner[k].panningModel = 'equalpower'; |
| 50 panner[k].distanceModel = "linear"; | 49 panner[k].distanceModel = 'linear'; |
| 51 | 50 |
| 52 var angle = angleStep * k; | 51 let angle = angleStep * k; |
| 53 position[k] = {angle : angle, x : Math.cos(angle), z : Math.sin(angle)}; | 52 position[k] = {angle: angle, x: Math.cos(angle), z: Math.sin(angle)}; |
| 54 positionSetter(panner[k], position[k].x, 0, position[k].z); | 53 positionSetter(panner[k], position[k].x, 0, position[k].z); |
| 55 | 54 |
| 56 bufferSource[k].connect(panner[k]); | 55 bufferSource[k].connect(panner[k]); |
| 57 panner[k].connect(context.destination); | 56 panner[k].connect(context.destination); |
| 58 | 57 |
| 59 // Start the source | 58 // Start the source |
| 60 time[k] = k * timeStep; | 59 time[k] = k * timeStep; |
| 61 bufferSource[k].start(time[k]); | 60 bufferSource[k].start(time[k]); |
| 62 } | 61 } |
| 63 } | 62 } |
| 64 | 63 |
| 65 function createTestAndRun(context, should, nodeCount, numberOfSourceChannels, | 64 function createTestAndRun( |
| 66 positionSetter) { | 65 context, should, nodeCount, numberOfSourceChannels, positionSetter) { |
| 67 numberOfChannels = numberOfSourceChannels; | 66 numberOfChannels = numberOfSourceChannels; |
| 68 | 67 |
| 69 createGraph(context, nodeCount, positionSetter); | 68 createGraph(context, nodeCount, positionSetter); |
| 70 | 69 |
| 71 return context.startRendering() | 70 return context.startRendering().then(buffer => checkResult(buffer, should)); |
| 72 .then(buffer => checkResult(buffer, should)); | |
| 73 } | 71 } |
| 74 | 72 |
| 75 // Map our position angle to the azimuth angle (in degrees). | 73 // Map our position angle to the azimuth angle (in degrees). |
| 76 // | 74 // |
| 77 // An angle of 0 corresponds to an azimuth of 90 deg; pi, to -90 deg. | 75 // An angle of 0 corresponds to an azimuth of 90 deg; pi, to -90 deg. |
| 78 function angleToAzimuth(angle) { | 76 function angleToAzimuth(angle) { |
| 79 return 90 - angle * 180 / Math.PI; | 77 return 90 - angle * 180 / Math.PI; |
| 80 } | 78 } |
| 81 | 79 |
| 82 // The gain caused by the EQUALPOWER panning model | 80 // The gain caused by the EQUALPOWER panning model |
| 83 function equalPowerGain(angle) { | 81 function equalPowerGain(angle) { |
| 84 var azimuth = angleToAzimuth(angle); | 82 let azimuth = angleToAzimuth(angle); |
| 85 | 83 |
| 86 if (numberOfChannels == 1) { | 84 if (numberOfChannels == 1) { |
| 87 var panPosition = (azimuth + 90) / 180; | 85 let panPosition = (azimuth + 90) / 180; |
| 88 | 86 |
| 89 var gainL = Math.cos(0.5 * Math.PI * panPosition); | 87 let gainL = Math.cos(0.5 * Math.PI * panPosition); |
| 90 var gainR = Math.sin(0.5 * Math.PI * panPosition); | 88 let gainR = Math.sin(0.5 * Math.PI * panPosition); |
| 91 | 89 |
| 92 return { left : gainL, right : gainR }; | 90 return {left: gainL, right: gainR}; |
| 91 } else { |
| 92 if (azimuth <= 0) { |
| 93 let panPosition = (azimuth + 90) / 90; |
| 94 |
| 95 let gainL = 1 + Math.cos(0.5 * Math.PI * panPosition); |
| 96 let gainR = Math.sin(0.5 * Math.PI * panPosition); |
| 97 |
| 98 return {left: gainL, right: gainR}; |
| 93 } else { | 99 } else { |
| 94 if (azimuth <= 0) { | 100 let panPosition = azimuth / 90; |
| 95 var panPosition = (azimuth + 90) / 90; | 101 |
| 96 | 102 let gainL = Math.cos(0.5 * Math.PI * panPosition); |
| 97 var gainL = 1 + Math.cos(0.5 * Math.PI * panPosition); | 103 let gainR = 1 + Math.sin(0.5 * Math.PI * panPosition); |
| 98 var gainR = Math.sin(0.5 * Math.PI * panPosition); | 104 |
| 99 | 105 return {left: gainL, right: gainR}; |
| 100 return { left : gainL, right : gainR }; | |
| 101 } else { | |
| 102 var panPosition = azimuth / 90; | |
| 103 | |
| 104 var gainL = Math.cos(0.5 * Math.PI * panPosition); | |
| 105 var gainR = 1 + Math.sin(0.5 * Math.PI * panPosition); | |
| 106 | |
| 107 return { left : gainL, right : gainR }; | |
| 108 } | |
| 109 } | 106 } |
| 107 } |
| 110 } | 108 } |
| 111 | 109 |
| 112 function checkResult(renderedBuffer, should) { | 110 function checkResult(renderedBuffer, should) { |
| 113 renderedLeft = renderedBuffer.getChannelData(0); | 111 renderedLeft = renderedBuffer.getChannelData(0); |
| 114 renderedRight = renderedBuffer.getChannelData(1); | 112 renderedRight = renderedBuffer.getChannelData(1); |
| 115 | 113 |
| 116 // The max error we allow between the rendered impulse and the | 114 // The max error we allow between the rendered impulse and the |
| 117 // expected value. This value is experimentally determined. Set | 115 // expected value. This value is experimentally determined. Set |
| 118 // to 0 to make the test fail to see what the actual error is. | 116 // to 0 to make the test fail to see what the actual error is. |
| 119 var maxAllowedError = 1.3e-6; | 117 let maxAllowedError = 1.3e-6; |
| 120 | |
| 121 var success = true; | |
| 122 | 118 |
| 123 // Number of impulses found in the rendered result. | 119 let success = true; |
| 124 var impulseCount = 0; | |
| 125 | 120 |
| 126 // Max (relative) error and the index of the maxima for the left | 121 // Number of impulses found in the rendered result. |
| 127 // and right channels. | 122 let impulseCount = 0; |
| 128 var maxErrorL = 0; | |
| 129 var maxErrorIndexL = 0; | |
| 130 var maxErrorR = 0; | |
| 131 var maxErrorIndexR = 0; | |
| 132 | 123 |
| 133 // Number of impulses that don't match our expected locations. | 124 // Max (relative) error and the index of the maxima for the left |
| 134 var timeCount = 0; | 125 // and right channels. |
| 126 let maxErrorL = 0; |
| 127 let maxErrorIndexL = 0; |
| 128 let maxErrorR = 0; |
| 129 let maxErrorIndexR = 0; |
| 135 | 130 |
| 136 // Locations of where the impulses aren't at the expected locations. | 131 // Number of impulses that don't match our expected locations. |
| 137 var timeErrors = new Array(); | 132 let timeCount = 0; |
| 138 | 133 |
| 139 for (var k = 0; k < renderedLeft.length; ++k) { | 134 // Locations of where the impulses aren't at the expected locations. |
| 140 // We assume that the left and right channels start at the same instant. | 135 let timeErrors = new Array(); |
| 141 if (renderedLeft[k] != 0 || renderedRight[k] != 0) { | |
| 142 // The expected gain for the left and right channels. | |
| 143 var pannerGain = equalPowerGain(position[impulseCount].angle); | |
| 144 var expectedL = pannerGain.left; | |
| 145 var expectedR = pannerGain.right; | |
| 146 | 136 |
| 147 // Absolute error in the gain. | 137 for (let k = 0; k < renderedLeft.length; ++k) { |
| 148 var errorL = Math.abs(renderedLeft[k] - expectedL); | 138 // We assume that the left and right channels start at the same instant. |
| 149 var errorR = Math.abs(renderedRight[k] - expectedR); | 139 if (renderedLeft[k] != 0 || renderedRight[k] != 0) { |
| 140 // The expected gain for the left and right channels. |
| 141 let pannerGain = equalPowerGain(position[impulseCount].angle); |
| 142 let expectedL = pannerGain.left; |
| 143 let expectedR = pannerGain.right; |
| 150 | 144 |
| 151 if (Math.abs(errorL) > maxErrorL) { | 145 // Absolute error in the gain. |
| 152 maxErrorL = Math.abs(errorL); | 146 let errorL = Math.abs(renderedLeft[k] - expectedL); |
| 153 maxErrorIndexL = impulseCount; | 147 let errorR = Math.abs(renderedRight[k] - expectedR); |
| 154 } | |
| 155 if (Math.abs(errorR) > maxErrorR) { | |
| 156 maxErrorR = Math.abs(errorR); | |
| 157 maxErrorIndexR = impulseCount; | |
| 158 } | |
| 159 | 148 |
| 160 // Keep track of the impulses that didn't show up where we | 149 if (Math.abs(errorL) > maxErrorL) { |
| 161 // expected them to be. | 150 maxErrorL = Math.abs(errorL); |
| 162 var expectedOffset = timeToSampleFrame(time[impulseCount], sampleRat
e); | 151 maxErrorIndexL = impulseCount; |
| 163 if (k != expectedOffset) { | 152 } |
| 164 timeErrors[timeCount] = { actual : k, expected : expectedOffset}
; | 153 if (Math.abs(errorR) > maxErrorR) { |
| 165 ++timeCount; | 154 maxErrorR = Math.abs(errorR); |
| 166 } | 155 maxErrorIndexR = impulseCount; |
| 167 ++impulseCount; | 156 } |
| 168 } | 157 |
| 158 // Keep track of the impulses that didn't show up where we |
| 159 // expected them to be. |
| 160 let expectedOffset = timeToSampleFrame(time[impulseCount], sampleRate); |
| 161 if (k != expectedOffset) { |
| 162 timeErrors[timeCount] = {actual: k, expected: expectedOffset}; |
| 163 ++timeCount; |
| 164 } |
| 165 ++impulseCount; |
| 169 } | 166 } |
| 167 } |
| 170 | 168 |
| 171 should(impulseCount, "Number of impulses found") | 169 should(impulseCount, 'Number of impulses found').beEqualTo(nodesToCreate); |
| 172 .beEqualTo(nodesToCreate); | |
| 173 | 170 |
| 174 should(timeErrors.map(x => x.actual), "Offsets of impulses at the wrong posi
tion") | 171 should( |
| 175 .beEqualToArray(timeErrors.map(x => x.expected)); | 172 timeErrors.map(x => x.actual), |
| 173 'Offsets of impulses at the wrong position') |
| 174 .beEqualToArray(timeErrors.map(x => x.expected)); |
| 176 | 175 |
| 177 should(maxErrorL, "Error in left channel gain values") | 176 should(maxErrorL, 'Error in left channel gain values') |
| 178 .beLessThanOrEqualTo(maxAllowedError); | 177 .beLessThanOrEqualTo(maxAllowedError); |
| 179 | 178 |
| 180 should(maxErrorR, "Error in right channel gain values") | 179 should(maxErrorR, 'Error in right channel gain values') |
| 181 .beLessThanOrEqualTo(maxAllowedError); | 180 .beLessThanOrEqualTo(maxAllowedError); |
| 182 } | 181 } |
| OLD | NEW |