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