Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(494)

Side by Side Diff: third_party/WebKit/LayoutTests/webaudio/resources/panner-formulas.js

Issue 2859143004: Correctly clamp in distance formulas (Closed)
Patch Set: Address review comments. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // For the record, these distance formulas were taken from the OpenAL 1 // For the record, these distance formulas were taken from the OpenAL
2 // spec 2 // spec
3 // (http://connect.creativelabs.com/openal/Documentation/OpenAL%201.1%20Specific ation.pdf), 3 // (http://connect.creativelabs.com/openal/Documentation/OpenAL%201.1%20Specific ation.pdf),
4 // not the code. The Web Audio spec follows the OpenAL formulas. 4 // not the code. The Web Audio spec follows the OpenAL formulas.
5 5
6 function linearDistance(panner, x, y, z) { 6 function linearDistance(panner, x, y, z) {
7 var distance = Math.sqrt(x * x + y * y + z * z); 7 var distance = Math.sqrt(x * x + y * y + z * z);
8 distance = Math.min(distance, panner.maxDistance); 8 var dref = Math.min(panner.refDistance, panner.maxDistance);
9 var rolloff = panner.rolloffFactor; 9 var dmax = Math.max(panner.refDistance, panner.maxDistance);
10 var gain = (1 - rolloff * (distance - panner.refDistance) / (panner.maxDista nce - panner.refDistance)); 10 distance = Math.max(Math.min(distance, dmax), dref);
11 var rolloff = Math.max(Math.min(panner.rolloffFactor, 1), 0);
12 if (dref === dmax)
13 return 1 - rolloff;
14
15 var gain = (1 - rolloff * (distance - dref) / (dmax - dref));
11 16
12 return gain; 17 return gain;
13 } 18 }
14 19
15 function inverseDistance(panner, x, y, z) { 20 function inverseDistance(panner, x, y, z) {
16 var distance = Math.sqrt(x * x + y * y + z * z); 21 var distance = Math.sqrt(x * x + y * y + z * z);
17 distance = Math.min(distance, panner.maxDistance); 22 distance = Math.max(distance, panner.refDistance);
18 var rolloff = panner.rolloffFactor; 23 var rolloff = panner.rolloffFactor;
19 var gain = panner.refDistance / (panner.refDistance + rolloff * (distance - panner.refDistance)); 24 var gain = panner.refDistance / (panner.refDistance + rolloff * (Math.max(di stance, panner.refDistance) - panner.refDistance));
20 25
21 return gain; 26 return gain;
22 } 27 }
23 28
24 function exponentialDistance(panner, x, y, z) { 29 function exponentialDistance(panner, x, y, z) {
25 var distance = Math.sqrt(x * x + y * y + z * z); 30 var distance = Math.sqrt(x * x + y * y + z * z);
26 distance = Math.min(distance, panner.maxDistance); 31 distance = Math.max(distance, panner.refDistance);
27 var rolloff = panner.rolloffFactor; 32 var rolloff = panner.rolloffFactor;
28 var gain = Math.pow(distance / panner.refDistance, -rolloff); 33 var gain = Math.pow(distance / panner.refDistance, -rolloff);
29 34
30 return gain; 35 return gain;
31 } 36 }
32 37
33 // Simple implementations of 3D vectors implemented as a 3-element array. 38 // Simple implementations of 3D vectors implemented as a 3-element array.
34 39
35 // x - y 40 // x - y
36 function vec3Sub(x, y) { 41 function vec3Sub(x, y) {
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 outR[k] = srcR[k] * gains.right; 173 outR[k] = srcR[k] * gains.right;
169 } else { 174 } else {
170 outL[k] = srcL[k] * gains.left; 175 outL[k] = srcL[k] * gains.left;
171 outR[k] = srcR[k] + srcL[k] * gains.right; 176 outR[k] = srcR[k] + srcL[k] * gains.right;
172 } 177 }
173 } 178 }
174 } 179 }
175 180
176 return { left: outL, right: outR }; 181 return { left: outL, right: outR };
177 } 182 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698