mirror of
https://github.com/Tonejs/Tone.js
synced 2024-11-16 00:27:58 +00:00
can cancel scheduled stop and setTick events
This commit is contained in:
parent
373af11bbc
commit
23932cbfa6
2 changed files with 52 additions and 4 deletions
|
@ -92,6 +92,14 @@ define(["Tone/core/Tone", "Tone/signal/TickSignal", "Tone/core/TimelineState",
|
|||
*/
|
||||
Tone.TickSource.prototype.stop = function(time){
|
||||
time = this.toSeconds(time);
|
||||
//cancel the previous stop
|
||||
if (this._state.getValueAtTime(time) === Tone.State.Stopped){
|
||||
var event = this._state.get(time);
|
||||
if (event.time > this.now()){
|
||||
this._tickOffset.cancel(event.time);
|
||||
this._state.cancel(event.time);
|
||||
}
|
||||
}
|
||||
this._state.cancel(time);
|
||||
this._state.setStateAtTime(Tone.State.Stopped, time);
|
||||
this.setTicksAtTime(0, time);
|
||||
|
@ -101,7 +109,7 @@ define(["Tone/core/Tone", "Tone/signal/TickSignal", "Tone/core/TimelineState",
|
|||
/**
|
||||
* Pause the clock. Pausing does not reset the tick counter.
|
||||
* @param {Time} [time=now] The time when the clock should stop.
|
||||
* @returns {Tone.Clock} this
|
||||
* @returns {Tone.TickSource} this
|
||||
*/
|
||||
Tone.TickSource.prototype.pause = function(time){
|
||||
time = this.toSeconds(time);
|
||||
|
@ -111,6 +119,18 @@ define(["Tone/core/Tone", "Tone/signal/TickSignal", "Tone/core/TimelineState",
|
|||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Cancel start/stop/pause and setTickAtTime events scheduled after the given time.
|
||||
* @param {Time} [time=now] When to clear the events after
|
||||
* @returns {Tone.TickSource} this
|
||||
*/
|
||||
Tone.TickSource.prototype.cancel = function(time){
|
||||
time = this.toSeconds(time);
|
||||
this._state.cancel(time);
|
||||
this._tickOffset.cancel(time);
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the elapsed ticks at the given time
|
||||
* @param {Time} time When to get the tick value
|
||||
|
|
|
@ -135,17 +135,30 @@ define(["Test", "Tone/source/TickSource", "helper/Offline", "helper/Basic"], fun
|
|||
source.dispose();
|
||||
});
|
||||
|
||||
it("can invoke stop multiple times, takes the last invokation", function(){
|
||||
var source = new TickSource(1);
|
||||
source.start(0).stop(3).stop(2).stop(4);
|
||||
expect(source.getTicksAtTime(0)).to.be.closeTo(0, 0.01);
|
||||
expect(source.getTicksAtTime(1)).to.be.closeTo(1, 0.01);
|
||||
expect(source.getTicksAtTime(2)).to.be.closeTo(2, 0.01);
|
||||
expect(source.getTicksAtTime(3)).to.be.closeTo(3, 0.01);
|
||||
expect(source.getTicksAtTime(4)).to.be.closeTo(0, 0.01);
|
||||
expect(source.getTicksAtTime(5)).to.be.closeTo(0, 0.01);
|
||||
expect(source.getTicksAtTime(6)).to.be.closeTo(0, 0.01);
|
||||
source.dispose();
|
||||
});
|
||||
|
||||
it("can set multiple setTicksAtTime", function(){
|
||||
var source = new TickSource(1);
|
||||
source.start(0, 1).stop(3);
|
||||
source.start(0, 1).pause(3);
|
||||
source.setTicksAtTime(1, 4);
|
||||
source.stop(5).start(6);
|
||||
source.setTicksAtTime(2, 7);
|
||||
expect(source.getTicksAtTime(0)).to.be.closeTo(1, 0.01);
|
||||
expect(source.getTicksAtTime(1)).to.be.closeTo(2, 0.01);
|
||||
expect(source.getTicksAtTime(2)).to.be.closeTo(3, 0.01);
|
||||
expect(source.getTicksAtTime(3)).to.be.closeTo(0, 0.01);
|
||||
expect(source.getTicksAtTime(3.5)).to.be.closeTo(0, 0.01);
|
||||
expect(source.getTicksAtTime(3)).to.be.closeTo(4, 0.01);
|
||||
expect(source.getTicksAtTime(3.5)).to.be.closeTo(4, 0.01);
|
||||
expect(source.getTicksAtTime(4)).to.be.closeTo(1, 0.01);
|
||||
expect(source.getTicksAtTime(5)).to.be.closeTo(0, 0.01);
|
||||
expect(source.getTicksAtTime(6)).to.be.closeTo(0, 0.01);
|
||||
|
@ -192,6 +205,21 @@ define(["Test", "Tone/source/TickSource", "helper/Offline", "helper/Basic"], fun
|
|||
expect(source.getTimeOfTick(5, 4)).to.be.closeTo(5, 0.01);
|
||||
source.dispose();
|
||||
});
|
||||
|
||||
it("can cancel scheduled events", function(){
|
||||
var source = new TickSource(1);
|
||||
source.start(0).stop(3);
|
||||
source.setTicksAtTime(10, 2);
|
||||
source.cancel(1);
|
||||
expect(source.getTicksAtTime(0)).to.be.closeTo(0, 0.01);
|
||||
expect(source.getTicksAtTime(1)).to.be.closeTo(1, 0.01);
|
||||
expect(source.getTicksAtTime(2)).to.be.closeTo(2, 0.01);
|
||||
expect(source.getTicksAtTime(3)).to.be.closeTo(3, 0.01);
|
||||
expect(source.getTicksAtTime(4)).to.be.closeTo(4, 0.01);
|
||||
expect(source.getTicksAtTime(5)).to.be.closeTo(5, 0.01);
|
||||
expect(source.getTicksAtTime(6)).to.be.closeTo(6, 0.01);
|
||||
source.dispose();
|
||||
});
|
||||
});
|
||||
|
||||
context("forEachTickBetween", function(){
|
||||
|
|
Loading…
Reference in a new issue