getLastState returns the last event with the given state

This commit is contained in:
Yotam Mann 2018-02-18 16:01:27 -05:00
parent 5f1ae523f9
commit ebb7aac407
2 changed files with 35 additions and 1 deletions

View file

@ -53,5 +53,22 @@ define(["Tone/core/Tone", "Tone/core/Timeline", "Tone/type/Type"], function(Tone
return this;
};
/**
* Return the event before the time with the given state
* @param {Tone.State} state The state to look for
* @param {Time} time When to check before
* @return {Object} The event with the given state before the time
*/
Tone.TimelineState.prototype.getLastState = function(state, time){
time = this.toSeconds(time);
var index = this._search(time);
for (var i = index; i >= 0; i--){
var event = this._timeline[i];
if (event.state === state){
return event;
}
}
};
return Tone.TimelineState;
});

View file

@ -1,4 +1,4 @@
define(["Test", "Tone/core/TimelineState"], function (Test, TimelineState) {
define(["Test", "Tone/core/TimelineState"], function(Test, TimelineState) {
describe("TimelineState", function(){
@ -43,6 +43,23 @@ define(["Test", "Tone/core/TimelineState"], function (Test, TimelineState) {
expect(sched.getValueAtTime(0)).is.equal("initial");
sched.dispose();
});
it("gets the last occurance of the state at or before the given time", function(){
var sched = new TimelineState();
sched.setStateAtTime("A", 0);
sched.setStateAtTime("B", 1);
sched.setStateAtTime("C", 2);
sched.setStateAtTime("B", 3);
expect(sched.getLastState("B", 1)).exists;
expect(sched.getLastState("B", 1).state).is.equal("B");
expect(sched.getLastState("B", 2)).exists;
expect(sched.getLastState("B", 2).state).is.equal("B");
expect(sched.getLastState("B", 2).time).is.equal(1);
expect(sched.getLastState("B", 0.9)).not.exists;
expect(sched.getLastState("B", 4).state).is.equal("B");
expect(sched.getLastState("B", 4).time).is.equal(3);
sched.dispose();
});
});
});