From e5a6788de1fce0d11d147aa465ddc7e5f1acaacc Mon Sep 17 00:00:00 2001 From: Yotam Mann Date: Thu, 1 Mar 2018 14:11:07 -0500 Subject: [PATCH] getNextState returns the next instance of the given state after the given time --- Tone/core/TimelineState.js | 19 +++++++++++++++++++ test/core/TimelineState.js | 16 ++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/Tone/core/TimelineState.js b/Tone/core/TimelineState.js index da5d3a20..f5f09843 100644 --- a/Tone/core/TimelineState.js +++ b/Tone/core/TimelineState.js @@ -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; }); diff --git a/test/core/TimelineState.js b/test/core/TimelineState.js index 726dfea6..b6c2e5d7 100644 --- a/test/core/TimelineState.js +++ b/test/core/TimelineState.js @@ -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(); + }); }); });