mirror of
https://github.com/Tonejs/Tone.js
synced 2024-11-16 08:38:00 +00:00
getLastState returns the last event with the given state
This commit is contained in:
parent
5f1ae523f9
commit
ebb7aac407
2 changed files with 35 additions and 1 deletions
|
@ -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;
|
||||
});
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue