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/audit-util.js"></script> | |
7 <script src="resources/audio-testing.js"></script> | |
8 <script src="resources/panner-formulas.js"></script> | |
9 <title>Test Automation of PannerNode Positions</title> | |
10 </head> | |
11 | |
12 <body> | |
13 <script> | |
14 description("Test Automation of PannerNode Positions."); | |
15 window.jsTestIsAsync = true; | |
16 | |
17 var sampleRate = 48000; | |
18 // These tests are quite slow, so don't run for many frames. 256 frames s
hould be enough to | |
19 // demonstrate that automations are working. | |
20 var renderFrames = 256; | |
21 var renderDuration = renderFrames / sampleRate; | |
22 | |
23 var context; | |
24 var panner; | |
25 | |
26 var audit = Audit.createTaskRunner(); | |
27 | |
28 // Set of tests for the panner node with automations applied to the positi
on of the source. | |
29 var testConfigs = [{ | |
30 // Distance model parameters for the panner | |
31 distanceModel: { | |
32 model: "inverse", | |
33 rolloff: 1 | |
34 }, | |
35 // Initial location of the source | |
36 startPosition: [0, 0, 1], | |
37 // Final position of the source. For this test, we only want to move on
the z axis which | |
38 // doesn't change the azimuth angle. | |
39 endPosition: [0, 0, 10000], | |
40 }, { | |
41 distanceModel: { | |
42 model: "inverse", | |
43 rolloff: 1 | |
44 }, | |
45 startPosition: [0, 0, 1], | |
46 // An essentially random end position, but it should be such that azimut
h angle changes as | |
47 // we move from the start to the end. | |
48 endPosition: [20000, 30000, 10000], | |
49 errorThreshold: [{ | |
50 // Error threshold for 1-channel case | |
51 relativeThreshold: 4.8124e-7 | |
52 }, { | |
53 // Error threshold for 2-channel case | |
54 relativeThreshold: 4.3267e-7 | |
55 }], | |
56 }, { | |
57 distanceModel: { | |
58 model: "exponential", | |
59 rolloff: 1.5 | |
60 }, | |
61 startPosition: [0, 0, 1], | |
62 endPosition: [20000, 30000, 10000], | |
63 errorThreshold: [{ | |
64 relativeThreshold: 5.0783e-7 | |
65 }, { | |
66 relativeThreshold: 5.2180e-7 | |
67 }] | |
68 }, { | |
69 distanceModel: { | |
70 model: "linear", | |
71 rolloff: 1 | |
72 }, | |
73 startPosition: [0, 0, 1], | |
74 endPosition: [20000, 30000, 10000], | |
75 errorThreshold: [{ | |
76 relativeThreshold: 6.5324e-6 | |
77 }, { | |
78 relativeThreshold: 6.5756e-6 | |
79 }] | |
80 }]; | |
81 | |
82 for (var k = 0; k < testConfigs.length; ++k) { | |
83 var config = testConfigs[k]; | |
84 var tester = function (c, channelCount) { | |
85 return function (done) { | |
86 runTest(c, channelCount).then(done); | |
87 } | |
88 }; | |
89 | |
90 var baseTestName = config.distanceModel.model + " rolloff: " + config.di
stanceModel.rolloff; | |
91 | |
92 // Define tasks for both 1-channel and 2-channel | |
93 audit.defineTask(k + ": 1-channel " + baseTestName, tester(config, 1)); | |
94 audit.defineTask(k + ": 2-channel " + baseTestName, tester(config, 2)); | |
95 } | |
96 | |
97 audit.defineTask("finish", function (done) { | |
98 finishJSTest(); | |
99 done(); | |
100 }); | |
101 | |
102 audit.runTasks(); | |
103 | |
104 function runTest(options, channelCount) { | |
105 // Output has 5 channels: channels 0 and 1 are for the stereo output of
the panner node. | |
106 // Channels 2-5 are the for automation of the x,y,z coordinate so that w
e have actual | |
107 // coordinates used for the panner automation. | |
108 context = new OfflineAudioContext(5, renderFrames, sampleRate); | |
109 | |
110 // Stereo source for the panner. | |
111 var source = context.createBufferSource(); | |
112 source.buffer = createConstantBuffer(context, renderFrames, channelCount
== 1 ? 1 : [1, 2]); | |
113 | |
114 panner = context.createPanner(); | |
115 panner.distanceModel = options.distanceModel.model; | |
116 panner.rolloffFactor = options.distanceModel.rolloff; | |
117 panner.panningModel = "equalpower"; | |
118 | |
119 // Source and gain node for the z-coordinate calculation. | |
120 var dist = context.createBufferSource(); | |
121 dist.buffer = createConstantBuffer(context, 1, 1); | |
122 dist.loop = true; | |
123 var gainX = context.createGain(); | |
124 var gainY = context.createGain(); | |
125 var gainZ = context.createGain(); | |
126 dist.connect(gainX); | |
127 dist.connect(gainY); | |
128 dist.connect(gainZ); | |
129 | |
130 // Set the gain automation to match the z-coordinate automation of the p
anner. | |
131 | |
132 // End the automation some time before the end of the rendering so we ca
n verify that | |
133 // automation has the correct end time and value. | |
134 var endAutomationTime = 0.75 * renderDuration; | |
135 | |
136 gainX.gain.setValueAtTime(options.startPosition[0], 0); | |
137 gainX.gain.linearRampToValueAtTime(options.endPosition[0], endAutomation
Time); | |
138 gainY.gain.setValueAtTime(options.startPosition[1], 0); | |
139 gainY.gain.linearRampToValueAtTime(options.endPosition[1], endAutomation
Time); | |
140 gainZ.gain.setValueAtTime(options.startPosition[2], 0); | |
141 gainZ.gain.linearRampToValueAtTime(options.endPosition[2], endAutomation
Time); | |
142 | |
143 dist.start(); | |
144 | |
145 // Splitter and merger to map the panner output and the z-coordinate aut
omation to the | |
146 // correct channels in the destination. | |
147 var splitter = context.createChannelSplitter(2); | |
148 var merger = context.createChannelMerger(5); | |
149 | |
150 source.connect(panner); | |
151 // Split the output of the panner to separate channels | |
152 panner.connect(splitter); | |
153 | |
154 // Merge the panner outputs and the z-coordinate output to the correct d
estination channels. | |
155 splitter.connect(merger, 0, 0); | |
156 splitter.connect(merger, 1, 1); | |
157 gainX.connect(merger, 0, 2); | |
158 gainY.connect(merger, 0, 3); | |
159 gainZ.connect(merger, 0, 4); | |
160 | |
161 merger.connect(context.destination); | |
162 | |
163 // Initialize starting point of the panner. | |
164 panner.positionX.setValueAtTime(options.startPosition[0], 0); | |
165 panner.positionY.setValueAtTime(options.startPosition[1], 0); | |
166 panner.positionZ.setValueAtTime(options.startPosition[2], 0); | |
167 | |
168 // Automate z coordinate to move away from the listener | |
169 panner.positionX.linearRampToValueAtTime(options.endPosition[0], 0.75 *
renderDuration); | |
170 panner.positionY.linearRampToValueAtTime(options.endPosition[1], 0.75 *
renderDuration); | |
171 panner.positionZ.linearRampToValueAtTime(options.endPosition[2], 0.75 *
renderDuration); | |
172 | |
173 source.start(); | |
174 | |
175 // Go! | |
176 return context.startRendering() | |
177 .then(function (renderedBuffer) { | |
178 // Get the panner outputs | |
179 var data0 = renderedBuffer.getChannelData(0); | |
180 var data1 = renderedBuffer.getChannelData(1); | |
181 var xcoord = renderedBuffer.getChannelData(2); | |
182 var ycoord = renderedBuffer.getChannelData(3); | |
183 var zcoord = renderedBuffer.getChannelData(4); | |
184 | |
185 // We're doing a linear ramp on the Z axis with the equalpower panne
r, so the equalpower | |
186 // panning gain remains constant. We only need to model the distanc
e effect. | |
187 | |
188 // Compute the distance gain | |
189 var distanceGain = new Float32Array(xcoord.length);; | |
190 | |
191 if (panner.distanceModel === "inverse") { | |
192 for (var k = 0; k < distanceGain.length; ++k) { | |
193 distanceGain[k] = inverseDistance(panner, xcoord[k], ycoord[k],
zcoord[k]) | |
194 } | |
195 } else if (panner.distanceModel === "linear") { | |
196 for (var k = 0; k < distanceGain.length; ++k) { | |
197 distanceGain[k] = linearDistance(panner, xcoord[k], ycoord[k], z
coord[k]) | |
198 } | |
199 } else if (panner.distanceModel === "exponential") { | |
200 for (var k = 0; k < distanceGain.length; ++k) { | |
201 distanceGain[k] = exponentialDistance(panner, xcoord[k], ycoord[
k], zcoord[k]) | |
202 } | |
203 } | |
204 | |
205 // Compute the expected result. Since we're on the z-axis, the left
and right channels | |
206 // pass through the equalpower panner unchanged. Only need to apply
the distance gain. | |
207 var buffer0 = source.buffer.getChannelData(0); | |
208 var buffer1 = channelCount == 2 ? source.buffer.getChannelData(1) :
buffer0; | |
209 | |
210 var azimuth = new Float32Array(buffer0.length); | |
211 | |
212 for (var k = 0; k < data0.length; ++k) { | |
213 azimuth[k] = calculateAzimuth([ | |
214 xcoord[k], | |
215 ycoord[k], | |
216 zcoord[k] | |
217 ], [ | |
218 context.listener.positionX.value, | |
219 context.listener.positionY.value, | |
220 context.listener.positionZ.value | |
221 ], [ | |
222 context.listener.forwardX.value, | |
223 context.listener.forwardY.value, | |
224 context.listener.forwardZ.value | |
225 ], [ | |
226 context.listener.upX.value, | |
227 context.listener.upY.value, | |
228 context.listener.upZ.value | |
229 ]); | |
230 } | |
231 | |
232 var expected = applyPanner(azimuth, buffer0, buffer1, channelCount); | |
233 var expected0 = expected.left; | |
234 var expected1 = expected.right; | |
235 | |
236 for (var k = 0; k < expected0.length; ++k) { | |
237 expected0[k] *= distanceGain[k]; | |
238 expected1[k] *= distanceGain[k]; | |
239 } | |
240 | |
241 var info = options.distanceModel.model + ", rolloff: " + options.dis
tanceModel.rolloff; | |
242 var prefix = channelCount + "-channel " | |
243 + "[" + options.startPosition[0] + ", " | |
244 + options.startPosition[1] + ", " | |
245 + options.startPosition[2] + "] -> [" | |
246 + options.endPosition[0] + ", " | |
247 + options.endPosition[1] + ", " | |
248 + options.endPosition[2] + "]: "; | |
249 | |
250 var errorThreshold = 0; | |
251 | |
252 if (options.errorThreshold) | |
253 errorThreshold = options.errorThreshold[channelCount - 1] | |
254 | |
255 Should(prefix + "distanceModel: " + info + ", left channel", data0,
{ | |
256 precision: 5 | |
257 }) | |
258 .beCloseToArray(expected0, errorThreshold); | |
259 Should(prefix + "distanceModel: " + info + ", right channel", data1,
{ | |
260 precision: 5 | |
261 }) | |
262 .beCloseToArray(expected1, errorThreshold); | |
263 }); | |
264 } | |
265 </script> | |
266 </body> | |
267 </html> | |
OLD | NEW |