Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 <!doctype html> | |
| 2 <html> | |
| 3 <head> | |
| 4 <script src="../resources/js-test.js"></script> | |
| 5 <script src="resources/compatibility.js"></script> | |
| 6 <script src="resources/audio-testing.js"></script> | |
| 7 <script src="resources/panner-formulas.js"></script> | |
| 8 <title>Test Automation of SpatialPanner Position</title> | |
| 9 </head> | |
| 10 | |
| 11 <body> | |
| 12 <script> | |
| 13 description("Test Automation of SpatialPannerNode Position."); | |
| 14 window.jsTestIsAsync = true; | |
| 15 | |
| 16 var sampleRate = 48000; | |
| 17 // These tests are quite slow, so don't run for many frames. 256 frames s hould be enough to | |
| 18 // demonstrate that automations are working. | |
| 19 var renderFrames = 256; | |
| 20 var renderDuration = renderFrames / sampleRate; | |
| 21 | |
| 22 var context; | |
| 23 var panner; | |
| 24 | |
| 25 var audit = Audit.createTaskRunner(); | |
| 26 | |
| 27 audit.defineTask("z only", function (done) { | |
| 28 runTest({ | |
| 29 distanceModel: { | |
| 30 model: "inverse", | |
| 31 rolloff: 1 | |
| 32 }, | |
| 33 startPosition: [0, 0, 1], | |
| 34 endPosition: [0, 0, 10000], | |
| 35 }) | |
| 36 .then(done); | |
| 37 }); | |
| 38 | |
| 39 audit.defineTask("inverse", function (done) { | |
| 40 runTest({ | |
| 41 distanceModel: { | |
| 42 model: "inverse", | |
| 43 rolloff: 1 | |
| 44 }, | |
| 45 startPosition: [0, 0, 1], | |
| 46 endPosition: [20000, 20000, 20000], | |
| 47 errorThreshold: { relativeThreshold: 4.0842e-7} | |
| 48 }) | |
| 49 .then(done); | |
| 50 }); | |
| 51 | |
| 52 audit.defineTask("exponential", function (done) { | |
| 53 runTest({ | |
| 54 distanceModel: { | |
| 55 model: "exponential", | |
| 56 rolloff: 1.5 | |
| 57 }, | |
| 58 startPosition: [0, 0, 1], | |
| 59 endPosition: [20000, 20000, 20000], | |
| 60 errorThreshold: { relativeThreshold: 3.4117e-7} | |
| 61 }) | |
| 62 .then(done); | |
| 63 }); | |
| 64 | |
| 65 audit.defineTask("linear", function (done) { | |
| 66 runTest({ | |
| 67 distanceModel: { | |
| 68 model: "linear", | |
| 69 rolloff: 1 | |
| 70 }, | |
| 71 startPosition: [0, 0, 1], | |
| 72 endPosition: [20000, 20000, 20000], | |
| 73 errorThreshold: { relativeThreshold: 6.5756e-6} | |
| 74 }) | |
| 75 .then(done); | |
| 76 }); | |
| 77 | |
| 78 audit.defineTask("finish", function (done) { | |
| 79 finishJSTest(); | |
| 80 done(); | |
| 81 }); | |
| 82 | |
| 83 audit.runTasks(); | |
| 84 | |
| 85 function runTest(options) { | |
| 86 // Output has 5 channels: channels 0 and 1 are for the stereo output of the panner node. | |
| 87 // Channels 2-5 are the for automation of the x,y,z coordinate so that w e have actual | |
| 88 // coordinates used for the panner automation. | |
| 89 context = new OfflineAudioContext(5, renderFrames, sampleRate); | |
| 90 | |
| 91 // Stereo source for the panner. | |
| 92 var source = context.createBufferSource(); | |
| 93 source.buffer = createConstantBuffer(context, renderFrames, [1, 2]); | |
| 94 | |
| 95 panner = context.createPanner(); | |
| 96 panner.distanceModel = options.distanceModel.model; | |
| 97 panner.rolloffFactor = options.distanceModel.rolloff; | |
| 98 panner.panningModel = "equalpower"; | |
| 99 //console.log("distanceModel = " + panner.distanceModel); | |
| 100 | |
| 101 // Source and gain node for the z-coordinate calculation. | |
| 102 var dist = context.createBufferSource(); | |
| 103 dist.buffer = createConstantBuffer(context, 1, 1); | |
| 104 dist.loop = true; | |
| 105 var gainX = context.createGain(); | |
| 106 var gainY = context.createGain(); | |
| 107 var gainZ = context.createGain(); | |
| 108 dist.connect(gainX); | |
| 109 dist.connect(gainY); | |
| 110 dist.connect(gainZ); | |
| 111 | |
| 112 // Set the gain automation to match the z-coordinate automation of the p anner. | |
| 113 gainX.gain.setValueAtTime(options.startPosition[0], 0); | |
| 114 gainX.gain.linearRampToValueAtTime(options.endPosition[0], 0.75 * render Duration); | |
|
hongchan
2016/05/06 18:41:26
Let's set a variable for 0.75 * renderDuration.
Raymond Toy
2016/05/11 19:28:30
Done.
| |
| 115 gainY.gain.setValueAtTime(options.startPosition[1], 0); | |
| 116 gainY.gain.linearRampToValueAtTime(options.endPosition[1], 0.75 * render Duration); | |
| 117 gainZ.gain.setValueAtTime(options.startPosition[2], 0); | |
| 118 gainZ.gain.linearRampToValueAtTime(options.endPosition[2], 0.75 * render Duration); | |
| 119 | |
| 120 dist.start(); | |
| 121 | |
| 122 // Splitter and merger to map the panner output and the z-coordinate aut omation to the | |
| 123 // correct channels in the destination. | |
| 124 var splitter = context.createChannelSplitter(2); | |
| 125 var merger = context.createChannelMerger(5); | |
| 126 | |
| 127 source.connect(panner); | |
| 128 // Split the output of the panner to separate channels | |
| 129 panner.connect(splitter); | |
| 130 | |
| 131 // Merge the panner outputs and the z-coordinate output to the correct d estination channels. | |
| 132 splitter.connect(merger, 0, 0); | |
| 133 splitter.connect(merger, 1, 1); | |
| 134 gainX.connect(merger, 0, 2); | |
| 135 gainY.connect(merger, 0, 3); | |
| 136 gainZ.connect(merger, 0, 4); | |
| 137 | |
| 138 merger.connect(context.destination); | |
| 139 | |
| 140 // Initialize starting point of the panner. | |
| 141 panner.positionX.setValueAtTime(options.startPosition[0], 0); | |
| 142 panner.positionY.setValueAtTime(options.startPosition[1], 0); | |
| 143 panner.positionZ.setValueAtTime(options.startPosition[2], 0); | |
| 144 | |
| 145 // Automate z coordinate to move away from the listener | |
| 146 panner.positionX.linearRampToValueAtTime(options.endPosition[0], 0.75 * renderDuration); | |
| 147 panner.positionY.linearRampToValueAtTime(options.endPosition[1], 0.75 * renderDuration); | |
| 148 panner.positionZ.linearRampToValueAtTime(options.endPosition[2], 0.75 * renderDuration); | |
| 149 | |
| 150 source.start(); | |
| 151 | |
| 152 // Go! | |
| 153 return context.startRendering() | |
| 154 .then(function (renderedBuffer) { | |
| 155 // Get the panner outputs | |
| 156 var data0 = renderedBuffer.getChannelData(0); | |
| 157 var data1 = renderedBuffer.getChannelData(1); | |
| 158 var xcoord = renderedBuffer.getChannelData(2); | |
| 159 var ycoord = renderedBuffer.getChannelData(3); | |
| 160 var zcoord = renderedBuffer.getChannelData(4); | |
| 161 | |
| 162 //console.log("data0"); | |
| 163 //console.log(data0); | |
|
hongchan
2016/05/06 18:41:26
Remove console.log()
Raymond Toy
2016/05/11 19:28:30
Done.
| |
| 164 | |
| 165 // We're doing a linear ramp on the Z axis with the equalpower panne r, so the equalpower | |
| 166 // panning gain remains constant. We only need to model the distanc e effect. | |
| 167 | |
| 168 //console.log("zcoord"); | |
| 169 //console.log(zcoord); | |
|
hongchan
2016/05/06 18:41:26
Ditto.
Raymond Toy
2016/05/11 19:28:30
Done.
| |
| 170 | |
| 171 // Compute the distance gain | |
| 172 var distanceGain = new Float32Array(xcoord.length);; | |
| 173 | |
| 174 if (panner.distanceModel === "inverse") { | |
| 175 for (var k = 0; k < distanceGain.length; ++k) { | |
| 176 distanceGain[k] = inverseDistance(panner, xcoord[k], ycoord[k], zcoord[k]) | |
| 177 } | |
| 178 } else if (panner.distanceModel === "linear") { | |
| 179 for (var k = 0; k < distanceGain.length; ++k) { | |
| 180 distanceGain[k] = linearDistance(panner, xcoord[k], ycoord[k], z coord[k]) | |
| 181 } | |
| 182 } else if (panner.distanceModel === "exponential") { | |
| 183 for (var k = 0; k < distanceGain.length; ++k) { | |
| 184 distanceGain[k] = exponentialDistance(panner, xcoord[k], ycoord[ k], zcoord[k]) | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 //console.log("distanceGain"); | |
| 189 //console.log(distanceGain); | |
|
hongchan
2016/05/06 18:41:26
Ditto.
Raymond Toy
2016/05/11 19:28:30
Done.
| |
| 190 // Compute the expected result. Since we're on the z-axis, the left and right channels | |
| 191 // pass through the equalpower panner unchanged. Only need to apply the distance gain. | |
| 192 var buffer0 = source.buffer.getChannelData(0); | |
| 193 var buffer1 = source.buffer.getChannelData(1); | |
| 194 | |
| 195 var azimuth = new Float32Array(buffer0.length); | |
| 196 | |
| 197 for (var k = 0; k < data0.length; ++k) { | |
| 198 azimuth[k] = calculateAzimuth( | |
| 199 [xcoord[k], ycoord[k], zcoord[k]], [context.listener.positionX.v alue, | |
| 200 context.listener.positionY.value, | |
| 201 context.listener.positionZ.value | |
| 202 ], [context.listener.forwardX.value, | |
| 203 context.listener.forwardY.value, | |
| 204 context.listener.forwardZ.value | |
| 205 ], [context.listener.upX.value, | |
| 206 context.listener.upY.value, | |
| 207 context.listener.upZ.value | |
| 208 ]); | |
|
hongchan
2016/05/06 18:41:26
The convention is:
calculateAzimuth([
item1,
Raymond Toy
2016/05/06 21:20:50
This is what js-beautify returns; I'm ok with chan
Raymond Toy
2016/05/11 19:28:30
Done.
| |
| 209 } | |
| 210 | |
| 211 var expected = applyPanner(azimuth, buffer0, buffer1, 2); | |
| 212 var expected0 = expected.left; | |
| 213 var expected1 = expected.right; | |
| 214 | |
| 215 for (var k = 0; k < expected0.length; ++k) { | |
| 216 expected0[k] *= distanceGain[k]; | |
| 217 expected1[k] *= distanceGain[k]; | |
| 218 } | |
| 219 | |
| 220 var info = options.distanceModel.model + ", rolloff: " + options.dis tanceModel.rolloff; | |
| 221 var prefix = "[" + options.startPosition[0] + ", "; | |
| 222 prefix += options.startPosition[1] + ", "; | |
| 223 prefix += options.startPosition[2]; | |
| 224 prefix += "] -> ["; | |
| 225 prefix += options.endPosition[0] + ", "; | |
| 226 prefix += options.endPosition[1] + ", "; | |
| 227 prefix += options.endPosition[2] + "]: "; | |
|
hongchan
2016/05/06 18:41:26
var prefix = "string"
+ "string"
+ "string"
Raymond Toy
2016/05/11 19:28:30
Done.
| |
| 228 | |
| 229 | |
| 230 | |
| 231 Should(prefix + "distanceModel: " + info + ", left channel", data0, { | |
| 232 verbose: true | |
| 233 }) | |
| 234 .beCloseToArray(expected0, options.errorThreshold || 0); | |
| 235 Should(prefix + "distanceModel: " + info + ", right channel", data1, { | |
| 236 verbose: true | |
| 237 }) | |
| 238 .beCloseToArray(expected1, options.errorThreshold || 0); | |
| 239 }); | |
| 240 } | |
| 241 </script> | |
| 242 </body> | |
| 243 </html> | |
| OLD | NEW |