mirror of
https://github.com/Tonejs/Tone.js
synced 2024-11-16 08:38:00 +00:00
added setCurveAtTime to Timeline
This commit is contained in:
parent
bc1070ce29
commit
04614a2948
1 changed files with 56 additions and 0 deletions
|
@ -38,11 +38,13 @@ define(["Tone/core/Tone", "Tone/signal/Signal", "Tone/core/Timeline"], function
|
|||
/**
|
||||
* The event types of a schedulable signal.
|
||||
* @enum {String}
|
||||
* @private
|
||||
*/
|
||||
Tone.TimelineSignal.Type = {
|
||||
Linear : "linear",
|
||||
Exponential : "exponential",
|
||||
Target : "target",
|
||||
Curve : "curve",
|
||||
Set : "set"
|
||||
};
|
||||
|
||||
|
@ -167,6 +169,33 @@ define(["Tone/core/Tone", "Tone/signal/Signal", "Tone/core/Timeline"], function
|
|||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Set an array of arbitrary values starting at the given time for the given duration.
|
||||
* @param {Float32Array} values
|
||||
* @param {Time} startTime
|
||||
* @param {Time} duration
|
||||
* @param {NormalRange} [scaling=1] If the values in the curve should be scaled by some value
|
||||
* @returns {Tone.TimelineSignal} this
|
||||
*/
|
||||
Tone.TimelineSignal.prototype.setValueCurveAtTime = function (values, startTime, duration, scaling) {
|
||||
scaling = this.defaultArg(scaling, 1);
|
||||
//copy the array
|
||||
var floats = new Float32Array(values);
|
||||
for (var i = 0; i < floats.length; i++){
|
||||
floats[i] = this._fromUnits(floats[i]) * scaling;
|
||||
}
|
||||
startTime = this.toSeconds(startTime);
|
||||
duration = this.toSeconds(duration);
|
||||
this._events.addEvent({
|
||||
"type" : Tone.TimelineSignal.Type.Curve,
|
||||
"value" : floats,
|
||||
"time" : startTime,
|
||||
"duration" : duration
|
||||
});
|
||||
this._param.setValueCurveAtTime(floats, startTime, duration);
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Cancels all scheduled parameter changes with times greater than or
|
||||
* equal to startTime.
|
||||
|
@ -291,6 +320,8 @@ define(["Tone/core/Tone", "Tone/signal/Signal", "Tone/core/Timeline"], function
|
|||
previouVal = previous.value;
|
||||
}
|
||||
value = this._exponentialApproach(before.time, previouVal, before.value, before.constant, time);
|
||||
} else if (before.type === Tone.TimelineSignal.Type.Curve){
|
||||
value = this._curveInterpolate(before.time, before.value, before.duration, time);
|
||||
} else if (after === null){
|
||||
value = before.value;
|
||||
} else if (after.type === Tone.TimelineSignal.Type.Linear){
|
||||
|
@ -348,6 +379,31 @@ define(["Tone/core/Tone", "Tone/signal/Signal", "Tone/core/Timeline"], function
|
|||
return v0 * Math.pow(v1 / v0, (t - t0) / (t1 - t0));
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculates the the value along the curve produced by setValueCurveAtTime
|
||||
* @private
|
||||
*/
|
||||
Tone.TimelineSignal.prototype._curveInterpolate = function (start, curve, duration, time) {
|
||||
var len = curve.length;
|
||||
// If time is after duration, return the last curve value
|
||||
if (time >= start + duration) {
|
||||
return curve[len - 1];
|
||||
} else if (time <= start){
|
||||
return curve[0];
|
||||
} else {
|
||||
var progress = (time - start) / duration;
|
||||
var lowerIndex = Math.floor((len - 1) * progress);
|
||||
var upperIndex = Math.ceil((len - 1) * progress);
|
||||
var lowerVal = curve[lowerIndex];
|
||||
var upperVal = curve[upperIndex];
|
||||
if (upperIndex === lowerIndex){
|
||||
return lowerVal;
|
||||
} else {
|
||||
return this._linearInterpolate(lowerIndex, lowerVal, upperIndex, upperVal, progress * (len - 1));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Clean up.
|
||||
* @return {Tone.TimelineSignal} this
|
||||
|
|
Loading…
Reference in a new issue