| OLD | NEW |
| (Empty) |
| 1 <!doctype html> | |
| 2 <html> | |
| 3 <head> | |
| 4 <title>Test Clamping of Distance for PannerNode</title> | |
| 5 <script src="../resources/testharness.js"></script> | |
| 6 <script src="../resources/testharnessreport.js"></script> | |
| 7 <script src="resources/audit-util.js"></script> | |
| 8 <script src="resources/audio-testing.js"></script> | |
| 9 </head> | |
| 10 | |
| 11 <body> | |
| 12 <script> | |
| 13 // Arbitrary sample rate and render length. | |
| 14 var sampleRate = 48000; | |
| 15 var renderFrames = 128; | |
| 16 | |
| 17 var audit = Audit.createTaskRunner(); | |
| 18 | |
| 19 audit.defineTask("ref-distance-error", function (taskDone) { | |
| 20 testDistanceLimits({name: "refDistance"}); | |
| 21 taskDone(); | |
| 22 }); | |
| 23 | |
| 24 audit.defineTask("max-distance-error", function (taskDone) { | |
| 25 testDistanceLimits({name: "maxDistance"}); | |
| 26 taskDone(); | |
| 27 }); | |
| 28 | |
| 29 function testDistanceLimits(options) { | |
| 30 // Verify that exceptions are thrown for invalid values of refDistance. | |
| 31 var context = new OfflineAudioContext(1, renderFrames, sampleRate); | |
| 32 | |
| 33 var attrName = options.name; | |
| 34 var prefix = "new PannerNode(c, {" + attrName + ": "; | |
| 35 | |
| 36 success = Should(prefix + "-1})", function () { | |
| 37 var nodeOptions = {}; | |
| 38 nodeOptions[attrName] = -1; | |
| 39 new PannerNode(context, nodeOptions); | |
| 40 }).throw("RangeError"); | |
| 41 | |
| 42 success = Should(prefix + "0})", function () { | |
| 43 var nodeOptions = {}; | |
| 44 nodeOptions[attrName] = 0; | |
| 45 new PannerNode(context, nodeOptions); | |
| 46 }).throw("RangeError") && success; | |
| 47 | |
| 48 // The smallest representable positive single float. | |
| 49 var leastPositiveDoubleFloat = 4.9406564584124654e-324; | |
| 50 | |
| 51 success = Should(prefix + leastPositiveDoubleFloat + "})", | |
| 52 function () { | |
| 53 var nodeOptions = {}; | |
| 54 nodeOptions[attrName] = leastPositiveDoubleFloat; | |
| 55 new PannerNode(context, nodeOptions); | |
| 56 }).notThrow() && success; | |
| 57 | |
| 58 prefix = "panner." + attrName + " = "; | |
| 59 panner = new PannerNode(context); | |
| 60 success = Should(prefix + "-1", function () { | |
| 61 panner[attrName] = -1; | |
| 62 }).throw("RangeError") && success; | |
| 63 | |
| 64 success = Should(prefix + "0", function () { | |
| 65 panner[attrName] = 0; | |
| 66 }).throw("RangeError") && success; | |
| 67 | |
| 68 success = Should(prefix + leastPositiveDoubleFloat, function () { | |
| 69 panner[attrName] = leastPositiveDoubleFloat; | |
| 70 }).notThrow() && success; | |
| 71 | |
| 72 Should("Invalid " + attrName + " values handled", success) | |
| 73 .summarize("correctly", "incorrectly"); | |
| 74 | |
| 75 } | |
| 76 | |
| 77 audit.defineTask("min-distance", function (taskDone) { | |
| 78 // Test clamping of panner distance to refDistance for all of the | |
| 79 // distance models. The actual distance is arbitrary as long as it's | |
| 80 // less than refDistance. We test default and non-default values for | |
| 81 // the panner's refDistance and maxDistance. | |
| 82 // correctly. | |
| 83 Promise.all([ | |
| 84 runTest({ | |
| 85 distance: 0.01, | |
| 86 distanceModel: "linear", | |
| 87 }), | |
| 88 runTest({ | |
| 89 distance: 0.01, | |
| 90 distanceModel: "exponential", | |
| 91 }), | |
| 92 runTest({ | |
| 93 distance: 0.01, | |
| 94 distanceModel: "inverse", | |
| 95 }), | |
| 96 runTest({ | |
| 97 distance: 2, | |
| 98 distanceModel: "linear", | |
| 99 maxDistance: 1000, | |
| 100 refDistance: 10, | |
| 101 }), | |
| 102 runTest({ | |
| 103 distance: 2, | |
| 104 distanceModel: "exponential", | |
| 105 maxDistance: 1000, | |
| 106 refDistance: 10, | |
| 107 }), | |
| 108 runTest({ | |
| 109 distance: 2, | |
| 110 distanceModel: "inverse", | |
| 111 maxDistance: 1000, | |
| 112 refDistance: 10, | |
| 113 }), | |
| 114 ]).then(taskDone); | |
| 115 }); | |
| 116 | |
| 117 audit.defineTask("max-distance", function (taskDone) { | |
| 118 // Like the "min-distance" task, but for clamping to the max | |
| 119 // distance. The actual distance is again arbitrary as long as it is | |
| 120 // greater than maxDistance. | |
| 121 Promise.all([ | |
| 122 runTest({ | |
| 123 distance: 20000, | |
| 124 distanceModel: "linear", | |
| 125 }), | |
| 126 runTest({ | |
| 127 distance: 21000, | |
| 128 distanceModel: "exponential", | |
| 129 }), | |
| 130 runTest({ | |
| 131 distance: 23000, | |
| 132 distanceModel: "inverse", | |
| 133 }), | |
| 134 runTest({ | |
| 135 distance: 5000, | |
| 136 distanceModel: "linear", | |
| 137 maxDistance: 1000, | |
| 138 refDistance: 10, | |
| 139 }), | |
| 140 runTest({ | |
| 141 distance: 5000, | |
| 142 distanceModel: "exponential", | |
| 143 maxDistance: 1000, | |
| 144 refDistance: 10, | |
| 145 }), | |
| 146 runTest({ | |
| 147 distance: 5000, | |
| 148 distanceModel: "inverse", | |
| 149 maxDistance: 1000, | |
| 150 refDistance: 10, | |
| 151 }), | |
| 152 ]).then(taskDone); | |
| 153 }); | |
| 154 | |
| 155 function runTest(options) { | |
| 156 var context = new OfflineAudioContext(2, renderFrames, sampleRate); | |
| 157 var src = new OscillatorNode(context, { | |
| 158 type: "sawtooth", | |
| 159 frequency: 20*440, | |
| 160 }); | |
| 161 | |
| 162 // Set panner options. Use a non-default rolloffFactor so that the | |
| 163 // various distance models look distinctly different. | |
| 164 var pannerOptions = {}; | |
| 165 Object.assign(pannerOptions, options, {rolloffFactor: 0.5}); | |
| 166 | |
| 167 var pannerRef = new PannerNode(context, pannerOptions); | |
| 168 var pannerTest = new PannerNode(context, pannerOptions); | |
| 169 | |
| 170 // Split the panner output so we can grab just one of the output | |
| 171 // channels. | |
| 172 var splitRef = new ChannelSplitterNode(context, {numberOfOutputs: 2}); | |
| 173 var splitTest = new ChannelSplitterNode(context, {numberOfOutputs: 2}); | |
| 174 | |
| 175 // Merge the panner outputs back into one stereo stream for the | |
| 176 // destination. | |
| 177 var merger = new ChannelMergerNode(context, {numberOfInputs: 2}); | |
| 178 | |
| 179 src.connect(pannerTest).connect(splitTest).connect(merger, 0, 0);; | |
| 180 src.connect(pannerRef).connect(splitRef).connect(merger, 0, 1); | |
| 181 | |
| 182 merger.connect(context.destination); | |
| 183 | |
| 184 // Move the panner some distance away. Arbitrarily select the x | |
| 185 // direction. For the reference panner, manually clamp the distance to | |
| 186 // lie between refDistance and maxDistance. | |
| 187 var xRef = Math.min(Math.max(options.distance, pannerRef.refDistance), | |
| 188 pannerRef.maxDistance); | |
| 189 | |
| 190 var xTest = options.distance; | |
| 191 | |
| 192 pannerRef.positionZ.setValueAtTime(xRef, 0); | |
| 193 pannerTest.positionZ.setValueAtTime(xTest, 0); | |
| 194 | |
| 195 src.start(); | |
| 196 | |
| 197 return context.startRendering().then(function (resultBuffer) { | |
| 198 var actual = resultBuffer.getChannelData(0); | |
| 199 var expected = resultBuffer.getChannelData(1); | |
| 200 | |
| 201 Should("Distance (" + xTest + ") is outside the range [" + | |
| 202 pannerRef.refDistance + ", " + pannerRef.maxDistance + "]", | |
| 203 xTest < pannerRef.refDistance || xTest > pannerRef.maxDistance) | |
| 204 .beEqualTo(true); | |
| 205 Should("Test panner output " + JSON.stringify(options), actual) | |
| 206 .beEqualToArray(expected); | |
| 207 }); | |
| 208 } | |
| 209 | |
| 210 audit.runTasks(); | |
| 211 </script> | |
| 212 </body> | |
| 213 </html> | |
| OLD | NEW |