audio-param.js (1658B)
1 // Define functions that implement the formulas for AudioParam automations. 2 3 // AudioParam linearRamp value at time t for a linear ramp between (t0, v0) and 4 // (t1, v1). It is assumed that t0 <= t. Results are undefined otherwise. 5 function audioParamLinearRamp(t, v0, t0, v1, t1) { 6 if (t >= t1) 7 return v1; 8 return (v0 + (v1 - v0) * (t - t0) / (t1 - t0)) 9 } 10 11 // AudioParam exponentialRamp value at time t for an exponential ramp between 12 // (t0, v0) and (t1, v1). It is assumed that t0 <= t. Results are undefined 13 // otherwise. 14 function audioParamExponentialRamp(t, v0, t0, v1, t1) { 15 if (t >= t1) 16 return v1; 17 return v0 * Math.pow(v1 / v0, (t - t0) / (t1 - t0)); 18 } 19 20 // AudioParam setTarget value at time t for a setTarget curve starting at (t0, 21 // v0) with a final value of vFainal and a time constant of timeConstant. It is 22 // assumed that t0 <= t. Results are undefined otherwise. 23 function audioParamSetTarget(t, v0, t0, vFinal, timeConstant) { 24 return vFinal + (v0 - vFinal) * Math.exp(-(t - t0) / timeConstant); 25 } 26 27 // AudioParam setValueCurve value at time t for a setValueCurve starting at time 28 // t0 with curve, curve, and duration duration. The sample rate is sampleRate. 29 // It is assumed that t0 <= t. 30 function audioParamSetValueCurve(t, curve, t0, duration) { 31 if (t > t0 + duration) 32 return curve[curve.length - 1]; 33 34 let curvePointsPerSecond = (curve.length - 1) / duration; 35 36 let virtualIndex = (t - t0) * curvePointsPerSecond; 37 let index = Math.floor(virtualIndex); 38 39 let delta = virtualIndex - index; 40 41 let c0 = curve[index]; 42 let c1 = curve[Math.min(index + 1, curve.length - 1)]; 43 return c0 + (c1 - c0) * delta; 44 }