added setCurveAtTime to Timeline

This commit is contained in:
Yotam Mann 2016-03-05 10:44:03 -05:00
parent bc1070ce29
commit 04614a2948

View file

@ -38,11 +38,13 @@ define(["Tone/core/Tone", "Tone/signal/Signal", "Tone/core/Timeline"], function
/** /**
* The event types of a schedulable signal. * The event types of a schedulable signal.
* @enum {String} * @enum {String}
* @private
*/ */
Tone.TimelineSignal.Type = { Tone.TimelineSignal.Type = {
Linear : "linear", Linear : "linear",
Exponential : "exponential", Exponential : "exponential",
Target : "target", Target : "target",
Curve : "curve",
Set : "set" Set : "set"
}; };
@ -167,6 +169,33 @@ define(["Tone/core/Tone", "Tone/signal/Signal", "Tone/core/Timeline"], function
return this; 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 * Cancels all scheduled parameter changes with times greater than or
* equal to startTime. * equal to startTime.
@ -291,6 +320,8 @@ define(["Tone/core/Tone", "Tone/signal/Signal", "Tone/core/Timeline"], function
previouVal = previous.value; previouVal = previous.value;
} }
value = this._exponentialApproach(before.time, previouVal, before.value, before.constant, time); 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){ } else if (after === null){
value = before.value; value = before.value;
} else if (after.type === Tone.TimelineSignal.Type.Linear){ } 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)); 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. * Clean up.
* @return {Tone.TimelineSignal} this * @return {Tone.TimelineSignal} this