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

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

Issue 1820403002: Implement Automations for PannerNode and AutioListener (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 4 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
(Empty)
1 // For the record, these distance formulas were taken from the OpenAL
2 // spec
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.
5
6 function linearDistance(panner, x, y, z) {
7 var distance = Math.sqrt(x * x + y * y + z * z);
8 distance = Math.min(distance, panner.maxDistance);
9 var rolloff = panner.rolloffFactor;
10 var gain = (1 - rolloff * (distance - panner.refDistance) / (panner.maxDista nce - panner.refDistance));
11
12 return gain;
13 }
14
15 function inverseDistance(panner, x, y, z) {
16 var distance = Math.sqrt(x * x + y * y + z * z);
17 distance = Math.min(distance, panner.maxDistance);
18 var rolloff = panner.rolloffFactor;
19 var gain = panner.refDistance / (panner.refDistance + rolloff * (distance - panner.refDistance));
20
21 return gain;
22 }
23
24 function exponentialDistance(panner, x, y, z) {
25 var distance = Math.sqrt(x * x + y * y + z * z);
26 distance = Math.min(distance, panner.maxDistance);
27 var rolloff = panner.rolloffFactor;
28 var gain = Math.pow(distance / panner.refDistance, -rolloff);
29
30 return gain;
31 }
32
33 // Simple implementations of 3D vectors implemented as a 3-element array.
34
35 // x - y
36 function vec3Sub(x, y) {
37 var z = new Float32Array(3);
38 z[0] = x[0] - y[0];
39 z[1] = x[1] - y[1];
40 z[2] = x[2] - y[2];
41
42 return z;
43 }
44
45 // x/|x|
46 function vec3Normalize(x) {
47 var mag = Math.hypot(...x);
48 return x.map(function (c) { return c / mag; });
49 }
50
51 // x == 0?
52 function vec3IsZero(x) {
53 return x[0] === 0 && x[1] === 0 && x[2] === 0;
54 }
55
56 // Vector cross product
57 function vec3Cross(u, v) {
58 var cross = new Float32Array(3);
59 cross[0] = u[1] * v[2] - u[2] * v[1];
60 cross[1] = u[2] * v[0] - u[0] * v[2];
61 cross[2] = u[0] * v[1] - u[1] * v[0];
62 return cross;
63 }
64
65 // Dot product
66 function vec3Dot(x, y) {
67 return x[0] * y[0] + x[1] * y[1] + x[2] * y[2];
68 }
69
70 // a*x, for scalar a
71 function vec3Scale(a, x) {
72 return x.map(function (c) { return a * c; });
73 }
74
75 function calculateAzimuth(source, listener, listenerForward, listenerUp) {
76 var sourceListener = vec3Sub(source, listener);
77
78 if (vec3IsZero(sourceListener))
79 return 0;
80
81 sourceListener = vec3Normalize(sourceListener);
82
83 var listenerRight = vec3Normalize(vec3Cross(listenerForward, listenerUp));
84 var listenerForwardNorm = vec3Normalize(listenerForward);
85
86 var up = vec3Cross(listenerRight, listenerForwardNorm);
87 var upProjection = vec3Dot(sourceListener, up);
88
89 var projectedSource = vec3Normalize(vec3Sub(sourceListener, vec3Scale(upProj ection, up)));
90
91 var azimuth = 180 / Math.PI * Math.acos(vec3Dot(projectedSource, listenerRig ht));
92
93 // Source in front or behind the listener
94 var frontBack = vec3Dot(projectedSource, listenerForwardNorm);
95 if (frontBack < 0)
96 azimuth = 360 - azimuth;
97
98 // Make azimuth relative to "front" and not "right" listener vector.
99 if (azimuth >= 0 && azimuth <= 270)
100 azimuth = 90 - azimuth;
101 else
102 azimuth = 450 - azimuth;
103
104 // We don't need elevation, so we're skipping that computation.
105 return azimuth;
106 }
107
108 // Map our position angle to the azimuth angle (in degrees).
109 //
110 // An angle of 0 corresponds to an azimuth of 90 deg; pi, to -90 deg.
111 function angleToAzimuth(angle) {
112 return 90 - angle * 180 / Math.PI;
113 }
114
115 // The gain caused by the EQUALPOWER panning model
116 function equalPowerGain(azimuth, numberOfChannels) {
117 var halfPi = Math.PI / 2;
118
119 if (azimuth < -90)
120 azimuth = -180 - azimuth;
121 else
122 azimuth = 180 - azimuth;
123
124 if (numberOfChannels == 1) {
125 var panPosition = (azimuth + 90) / 180;
126
127 var gainL = Math.cos(halfPi * panPosition);
128 var gainR = Math.sin(halfPi * panPosition);
129
130 return { left : gainL, right : gainR };
131 } else {
132 if (azimuth <= 0) {
133 var panPosition = (azimuth + 90) / 90;
134
135 var gainL = Math.cos(halfPi * panPosition);
136 var gainR = Math.sin(halfPi * panPosition);
137
138 return { left : gainL, right : gainR };
139 } else {
140 var panPosition = azimuth / 90;
141
142 var gainL = Math.cos(halfPi * panPosition);
143 var gainR = Math.sin(halfPi * panPosition);
144
145 return { left : gainL, right : gainR };
146 }
147 }
148 }
149
150 function applyPanner(azimuth, srcL, srcR, numberOfChannels)
hongchan 2016/05/06 18:41:26 Let's bring up the curly brace.
151 {
152 var length = srcL.length;
153 var outL = new Float32Array(length);
154 var outR = new Float32Array(length);
155
156 if (numberOfChannels == 1) {
hongchan 2016/05/06 18:41:26 Not sure if I am understanding this - there is not
Raymond Toy 2016/05/06 21:20:50 I think because I didn't actually implement or tes
Raymond Toy 2016/05/11 19:28:30 Implemented.
157 } else {
158 for (var k = 0; k < length; ++k) {
159 var gains = equalPowerGain(azimuth[k], numberOfChannels);
160 if (azimuth[k] <= 0) {
161 outL[k] = srcL[k] + srcR[k] * gains.left;
162 outR[k] = srcR[k] * gains.right;
163 } else {
164 outL[k] = srcL[k] * gains.left;
165 outR[k] = srcR[k] + srcL[k] * gains.right;
166 }
167 }
168 }
169
170 return { left: outL, right: outR };
171 }
172
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698