getNextState returns the next instance of the given state after the given time

This commit is contained in:
Yotam Mann 2018-03-01 14:11:07 -05:00
parent d98b10d6f8
commit e5a6788de1
2 changed files with 35 additions and 0 deletions

View file

@ -46,6 +46,8 @@ define(["Tone/core/Tone", "Tone/core/Timeline", "Tone/type/Type"], function(Tone
* @returns {Tone.TimelineState} this
*/
Tone.TimelineState.prototype.setStateAtTime = function(state, time){
//all state changes need to be >= the previous state time
//TODO throw error if time < the previous event time
this.add({
"state" : state,
"time" : time
@ -70,5 +72,22 @@ define(["Tone/core/Tone", "Tone/core/Timeline", "Tone/type/Type"], function(Tone
}
};
/**
* Return the event after the time with the given state
* @param {Tone.State} state The state to look for
* @param {Time} time When to check from
* @return {Object} The event with the given state after the time
*/
Tone.TimelineState.prototype.getNextState = function(state, time){
time = this.toSeconds(time);
var index = this._search(time);
for (var i = index; i < this._timeline.length; i++){
var event = this._timeline[i];
if (event.state === state){
return event;
}
}
};
return Tone.TimelineState;
});

View file

@ -61,5 +61,21 @@ define(["Test", "Tone/core/TimelineState"], function(Test, TimelineState) {
sched.dispose();
});
it("gets the next 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.getNextState("B", 1)).exists;
expect(sched.getNextState("B", 1).state).is.equal("B");
expect(sched.getNextState("B", 2)).exists;
expect(sched.getNextState("B", 2).state).is.equal("B");
expect(sched.getNextState("B", 2).time).is.equal(3);
expect(sched.getNextState("B", 0.9)).exists;
expect(sched.getNextState("B", 0.9).state).is.equal("B");
expect(sched.getNextState("B", 0.9).time).is.equal(1);
sched.dispose();
});
});
});