panner-formulas.js (5126B)
1 // For the record, these distance formulas were taken from the OpenAL 2 // spec 3 // (http://connect.creativelabs.com/openal/Documentation/OpenAL%201.1%20Specification.pdf), 4 // not the code. The Web Audio spec follows the OpenAL formulas. 5 6 function linearDistance(panner, x, y, z) { 7 let distance = Math.sqrt(x * x + y * y + z * z); 8 let dref = Math.min(panner.refDistance, panner.maxDistance); 9 let dmax = Math.max(panner.refDistance, panner.maxDistance); 10 distance = Math.max(Math.min(distance, dmax), dref); 11 let rolloff = Math.max(Math.min(panner.rolloffFactor, 1), 0); 12 if (dref === dmax) 13 return 1 - rolloff; 14 15 let gain = (1 - rolloff * (distance - dref) / (dmax - dref)); 16 17 return gain; 18 } 19 20 function inverseDistance(panner, x, y, z) { 21 let distance = Math.sqrt(x * x + y * y + z * z); 22 distance = Math.max(distance, panner.refDistance); 23 let rolloff = panner.rolloffFactor; 24 let gain = panner.refDistance / 25 (panner.refDistance + 26 rolloff * (Math.max(distance, panner.refDistance) - panner.refDistance)); 27 28 return gain; 29 } 30 31 function exponentialDistance(panner, x, y, z) { 32 let distance = Math.sqrt(x * x + y * y + z * z); 33 distance = Math.max(distance, panner.refDistance); 34 let rolloff = panner.rolloffFactor; 35 let gain = Math.pow(distance / panner.refDistance, -rolloff); 36 37 return gain; 38 } 39 40 // Simple implementations of 3D vectors implemented as a 3-element array. 41 42 // x - y 43 function vec3Sub(x, y) { 44 let z = new Float32Array(3); 45 z[0] = x[0] - y[0]; 46 z[1] = x[1] - y[1]; 47 z[2] = x[2] - y[2]; 48 49 return z; 50 } 51 52 // x/|x| 53 function vec3Normalize(x) { 54 let mag = Math.hypot(...x); 55 return x.map(function(c) { 56 return c / mag; 57 }); 58 } 59 60 // x == 0? 61 function vec3IsZero(x) { 62 return x[0] === 0 && x[1] === 0 && x[2] === 0; 63 } 64 65 // Vector cross product 66 function vec3Cross(u, v) { 67 let cross = new Float32Array(3); 68 cross[0] = u[1] * v[2] - u[2] * v[1]; 69 cross[1] = u[2] * v[0] - u[0] * v[2]; 70 cross[2] = u[0] * v[1] - u[1] * v[0]; 71 return cross; 72 } 73 74 // Dot product 75 function vec3Dot(x, y) { 76 return x[0] * y[0] + x[1] * y[1] + x[2] * y[2]; 77 } 78 79 // a*x, for scalar a 80 function vec3Scale(a, x) { 81 return x.map(function(c) { 82 return a * c; 83 }); 84 } 85 86 function calculateAzimuth(source, listener, listenerForward, listenerUp) { 87 let sourceListener = vec3Sub(source, listener); 88 89 if (vec3IsZero(sourceListener)) 90 return 0; 91 92 sourceListener = vec3Normalize(sourceListener); 93 94 let listenerRight = vec3Normalize(vec3Cross(listenerForward, listenerUp)); 95 let listenerForwardNorm = vec3Normalize(listenerForward); 96 97 let up = vec3Cross(listenerRight, listenerForwardNorm); 98 let upProjection = vec3Dot(sourceListener, up); 99 100 let projectedSource = 101 vec3Normalize(vec3Sub(sourceListener, vec3Scale(upProjection, up))); 102 103 let azimuth = 104 180 / Math.PI * Math.acos(vec3Dot(projectedSource, listenerRight)); 105 106 // Source in front or behind the listener 107 let frontBack = vec3Dot(projectedSource, listenerForwardNorm); 108 if (frontBack < 0) 109 azimuth = 360 - azimuth; 110 111 // Make azimuth relative to "front" and not "right" listener vector. 112 if (azimuth >= 0 && azimuth <= 270) 113 azimuth = 90 - azimuth; 114 else 115 azimuth = 450 - azimuth; 116 117 // We don't need elevation, so we're skipping that computation. 118 return azimuth; 119 } 120 121 // Map our position angle to the azimuth angle (in degrees). 122 // 123 // An angle of 0 corresponds to an azimuth of 90 deg; pi, to -90 deg. 124 function angleToAzimuth(angle) { 125 return 90 - angle * 180 / Math.PI; 126 } 127 128 // The gain caused by the EQUALPOWER panning model 129 function equalPowerGain(azimuth, numberOfChannels) { 130 let halfPi = Math.PI / 2; 131 132 if (azimuth < -90) 133 azimuth = -180 - azimuth; 134 else 135 azimuth = 180 - azimuth; 136 137 if (numberOfChannels == 1) { 138 let panPosition = (azimuth + 90) / 180; 139 140 let gainL = Math.cos(halfPi * panPosition); 141 let gainR = Math.sin(halfPi * panPosition); 142 143 return {left: gainL, right: gainR}; 144 } else { 145 if (azimuth <= 0) { 146 let panPosition = (azimuth + 90) / 90; 147 148 let gainL = Math.cos(halfPi * panPosition); 149 let gainR = Math.sin(halfPi * panPosition); 150 151 return {left: gainL, right: gainR}; 152 } else { 153 let panPosition = azimuth / 90; 154 155 let gainL = Math.cos(halfPi * panPosition); 156 let gainR = Math.sin(halfPi * panPosition); 157 158 return {left: gainL, right: gainR}; 159 } 160 } 161 } 162 163 function applyPanner(azimuth, srcL, srcR, numberOfChannels) { 164 let length = srcL.length; 165 let outL = new Float32Array(length); 166 let outR = new Float32Array(length); 167 168 if (numberOfChannels == 1) { 169 for (let k = 0; k < length; ++k) { 170 let gains = equalPowerGain(azimuth[k], numberOfChannels); 171 172 outL[k] = srcL[k] * gains.left; 173 outR[k] = srcR[k] * gains.right; 174 } 175 } else { 176 for (let k = 0; k < length; ++k) { 177 let gains = equalPowerGain(azimuth[k], numberOfChannels); 178 179 if (azimuth[k] <= 0) { 180 outL[k] = srcL[k] + srcR[k] * gains.left; 181 outR[k] = srcR[k] * gains.right; 182 } else { 183 outL[k] = srcL[k] * gains.left; 184 outR[k] = srcR[k] + srcL[k] * gains.right; 185 } 186 } 187 } 188 189 return {left: outL, right: outR}; 190 }