From 753640bdeb811928a9c936a5946ec0f98e1ed6b0 Mon Sep 17 00:00:00 2001 From: Yotam Mann Date: Sun, 25 Sep 2016 22:32:54 -0400 Subject: [PATCH] seek to a position in the buffer --- Tone/source/Player.js | 24 ++++++++++++++++++++++++ test/source/Player.js | 38 ++++++++++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/Tone/source/Player.js b/Tone/source/Player.js index 96f401ff..d511450f 100644 --- a/Tone/source/Player.js +++ b/Tone/source/Player.js @@ -227,6 +227,30 @@ define(["Tone/core/Tone", "Tone/core/Buffer", "Tone/source/Source"], function(To return this; }; + + /** + * Seek to a specific time in the player's buffer. If the + * source is no longer playing at that time, it will stop. + * If you seek to a time that + * @param {Time} offset The time to seek to. + * @param {Time=} time The time for the seek event to occur. + * @return {Tone.Player} this + * @example + * source.start(0.2); + * source.stop(0.4); + */ + Tone.Player.prototype.seek = function(offset, time){ + time = this.toSeconds(time); + if (this._state.getStateAtTime(time) === Tone.State.Started){ + offset = this.toSeconds(offset); + // if it's currently playing, stop it + this._stop(time); + //restart it at the given time + this._start(time, offset); + } + return this; + }; + /** * Set the loop start and end. Will only loop if loop is * set to true. diff --git a/test/source/Player.js b/test/source/Player.js index 44f0b7ec..2bb921c2 100644 --- a/test/source/Player.js +++ b/test/source/Player.js @@ -1,5 +1,6 @@ -define(["helper/Basic", "Tone/source/Player", "helper/Offline", "helper/SourceTests", "Tone/core/Buffer", "helper/Meter"], - function (BasicTests, Player, Offline, SourceTests, Buffer, Meter) { +define(["helper/Basic", "Tone/source/Player", "helper/Offline", + "helper/SourceTests", "Tone/core/Buffer", "helper/Meter", "helper/Offline2"], + function (BasicTests, Player, Offline, SourceTests, Buffer, Meter, Offline2) { if (window.__karma__){ Buffer.baseUrl = "/base/test/"; @@ -193,8 +194,6 @@ define(["helper/Basic", "Tone/source/Player", "helper/Offline", "helper/SourceTe context("Start Scheduling", function(){ - this.timeout(3000); - it("can be start with an offset", function(done){ var player; var offline = new Offline(0.4, 1); @@ -217,6 +216,37 @@ define(["helper/Basic", "Tone/source/Player", "helper/Offline", "helper/SourceTe offline.run(); }); + it("can seek to a position at the given time", function(done){ + Offline2(function(output, test, after){ + + var ramp = new Float32Array(Math.floor(44100 * 0.3)); + for (var i = 0; i < ramp.length; i++){ + ramp[i] = (i / (ramp.length)) * 0.3; + } + + var buff = new Buffer().fromArray(ramp); + var player = new Player(buff).connect(output); + + player.start(0); + player.seek(0.2, 0.1); + + test(function(sample, time){ + if (time < 0.1){ + expect(sample).to.be.within(0, 0.1); + } else if (time > 0.1 && time < 0.2){ + expect(sample).to.be.within(0.2, 0.3); + } + }); + + after(function(){ + buff.dispose(); + player.dispose(); + done(); + }); + + }, 0.3); + }); + it("can be play for a specific duration", function(done){ var player; var meter = new Meter(0.4);