From 06a3413824b6c0c7ede27fe96ca25ecc42914456 Mon Sep 17 00:00:00 2001 From: Yotam Mann Date: Sun, 28 May 2017 16:55:25 -0400 Subject: [PATCH] removing start/stop/pause events from TickSignal adding `getDurationOfTicks` --- Tone/signal/TickSignal.js | 84 ++++++--------------------------------- test/signal/TickSignal.js | 69 +++----------------------------- 2 files changed, 18 insertions(+), 135 deletions(-) diff --git a/Tone/signal/TickSignal.js b/Tone/signal/TickSignal.js index 40f0f535..93eca9a5 100644 --- a/Tone/signal/TickSignal.js +++ b/Tone/signal/TickSignal.js @@ -22,21 +22,6 @@ define(["Tone/core/Tone", "Tone/signal/TimelineSignal"], function (Tone) { //extend the memory this._events.memory = Infinity; - - /** - * The paused time - * @type {Ticks} - * @private - */ - this._pausedRate = null; - - - /** - * Keep track of state - * @type {Tone.TimelineState} - * @private - */ - // this._state = new Tone.TimelineState(Tone.State.Started); }; Tone.extend(Tone.TickSignal, Tone.TimelineSignal); @@ -50,11 +35,6 @@ define(["Tone/core/Tone", "Tone/signal/TimelineSignal"], function (Tone) { */ function _wrapScheduleMethods(method){ return function(value, time){ - //make sure it's not paused or stopped - var lastEvent = this._events.get(time); - if (lastEvent && lastEvent.type === Tone.State.Stopped && this._pausedRate !== null){ - throw new Error("Tone.TickSignal: cannot schedule automations while stopped."); - } time = this.toSeconds(time); method.apply(this, arguments); var event = this._events.get(time); @@ -91,55 +71,6 @@ define(["Tone/core/Tone", "Tone/signal/TimelineSignal"], function (Tone) { return this; }; - /** - * Stop the signal from ticking forward and reset the ticks to 0 - * until it is restarted. - * @param {Time=} time When to stop the signal - * @return {Tone.TickSignal} this - */ - Tone.TickSignal.prototype.stop = function(time){ - time = this.toSeconds(time); - this._pausedRate = this.getValueAtTime(time); - this.cancelScheduledValues(time); - this.setValueAtTime(0, time); - var event = this._events.get(time); - event.type = Tone.State.Stopped; - event.ticks = 0; - return this; - }; - - /** - * Pause the ticks from counting forward. Will remain - * at the same tick value until resumed. - * @param {Time=} time When to pause the signal - * @return {Tone.TickSignal} this - */ - Tone.TickSignal.prototype.pause = function(time){ - time = this.toSeconds(time); - this._pausedRate = this.getValueAtTime(time); - this.setValueAtTime(0, time); - var event = this._events.get(time); - event.type = Tone.State.Stopped; - return this; - }; - - /** - * Start the signal after paused or stopped. Optionally with an offset - * to start the tick counter at. - * @param {Time=} time When to pause the signal - * @param {Ticks=} offset How many ticks to offset the counter - * @return {Tone.TickSignal} this - */ - Tone.TickSignal.prototype.start = function(time, offset){ - time = this.toSeconds(time); - var resumeRate = this._pausedRate; - this._pausedRate = null; - this.setValueAtTime(resumeRate, time); - var event = this._events.get(time); - event.ticks = Tone.defaultArg(offset, this.getTickAtTime(time)); - return this; - }; - /** * Schedules an exponential continuous change in parameter value from * the previous scheduled parameter value to the given value. @@ -182,9 +113,6 @@ define(["Tone/core/Tone", "Tone/signal/TimelineSignal"], function (Tone) { "time" : 0 }; } - if (event.type === Tone.State.Stopped){ - return event.ticks; - } var val0 = this.getValueAtTime(event.time); var val1 = this.getValueAtTime(time); return 0.5 * (time - event.time) * (val0 + val1) + event.ticks; @@ -203,6 +131,18 @@ define(["Tone/core/Tone", "Tone/signal/TimelineSignal"], function (Tone) { return this._getTickUntilEvent(event, time); }; + /** + * Return the elapsed time of the number of ticks from the given time + * @param {Ticks} ticks The number of ticks to calculate + * @param {Time} time The time to get the next tick from + * @return {Seconds} The duration of the number of ticks from the given time in seconds + */ + Tone.TickSignal.prototype.getDurationOfTicks = function(ticks, time){ + time = this.toSeconds(time); + var currentTick = this.getTickAtTime(time); + return this.getTimeOfTick(currentTick + ticks) - time; + }; + /** * Given a tick, returns the time that tick occurs at. * @param {Ticks} tick diff --git a/test/signal/TickSignal.js b/test/signal/TickSignal.js index a3818f1a..a84dae31 100644 --- a/test/signal/TickSignal.js +++ b/test/signal/TickSignal.js @@ -242,71 +242,14 @@ define(["Test", "Tone/signal/TickSignal", "helper/Offline"], tickSignal.dispose(); }); - it("pause stops counting ticks", function(){ + it ("can get the duration of a tick at any point in time", function(){ var tickSignal = new TickSignal(1); tickSignal.setValueAtTime(2, 1); - expect(tickSignal.getTickAtTime(2)).to.be.closeTo(3, 0.01); - tickSignal.pause(3); - expect(tickSignal.getTickAtTime(3)).to.be.closeTo(5, 0.01); - expect(tickSignal.getTickAtTime(4)).to.be.closeTo(5, 0.01); - expect(tickSignal.getTickAtTime(5)).to.be.closeTo(5, 0.01); - tickSignal.dispose(); - }); - - it("start increments ticks forward after pause where it left off", function(){ - var tickSignal = new TickSignal(1); - tickSignal.setValueAtTime(2, 1); - tickSignal.pause(3); - tickSignal.start(4); - expect(tickSignal.getTickAtTime(2)).to.be.closeTo(3, 0.01); - expect(tickSignal.getTickAtTime(3)).to.be.closeTo(5, 0.01); - expect(tickSignal.getTickAtTime(4)).to.be.closeTo(5, 0.01); - expect(tickSignal.getTickAtTime(5)).to.be.closeTo(7, 0.01); - tickSignal.dispose(); - }); - - it("start can be passed in with an offset", function(){ - var tickSignal = new TickSignal(1); - tickSignal.setValueAtTime(2, 1); - tickSignal.pause(3); - tickSignal.start(4, 1); - expect(tickSignal.getTickAtTime(2)).to.be.closeTo(3, 0.01); - expect(tickSignal.getTickAtTime(3)).to.be.closeTo(5, 0.01); - expect(tickSignal.getTickAtTime(4)).to.be.closeTo(1, 0.01); - expect(tickSignal.getTickAtTime(5)).to.be.closeTo(3, 0.01); - tickSignal.dispose(); - }); - - it("stops values and starts the tick counter over", function(){ - var tickSignal = new TickSignal(1); - tickSignal.setValueAtTime(2, 1); - expect(tickSignal.getTickAtTime(2)).to.be.closeTo(3, 0.01); - tickSignal.stop(2); - tickSignal.start(2); - expect(tickSignal.getTickAtTime(2)).to.be.closeTo(0, 0.01); - expect(tickSignal.getTickAtTime(3)).to.be.closeTo(2, 0.01); - expect(tickSignal.getTickAtTime(4)).to.be.closeTo(4, 0.01); - tickSignal.stop(6); - tickSignal.start(6); - expect(tickSignal.getTickAtTime(5)).to.be.closeTo(6, 0.01); - expect(tickSignal.getTickAtTime(6)).to.be.closeTo(0, 0.01); - tickSignal.dispose(); - }); - - it("throws an error if events are scheduled while paused or stopped", function(){ - var tickSignal0 = new TickSignal(1); - tickSignal0.pause(0); - expect(function(){ - tickSignal0.setValueAtTime(1, 1); - }).throws(Error); - tickSignal0.dispose(); - - var tickSignal1 = new TickSignal(1); - tickSignal1.stop(0); - expect(function(){ - tickSignal1.setValueAtTime(1, 1); - }).throws(Error); - tickSignal1.dispose(); + tickSignal.setValueAtTime(10, 2); + expect(tickSignal.getDurationOfTicks(1, 0)).to.be.closeTo(1, 0.01); + expect(tickSignal.getDurationOfTicks(1, 1)).to.be.closeTo(0.5, 0.01); + expect(tickSignal.getDurationOfTicks(1, 2)).to.be.closeTo(0.1, 0.01); + expect(tickSignal.getDurationOfTicks(2, 1.5)).to.be.closeTo(0.6, 0.01); }); });