From 023758425bb7f1d8e86a4d4e042b351504909c49 Mon Sep 17 00:00:00 2001 From: tambien Date: Tue, 12 Mar 2019 11:25:33 -0400 Subject: [PATCH] use the playbackRate when computing the offset of a synced player fixes #349 --- Tone/source/Player.js | 8 ++++++++ package.json | 2 +- test/source/Player.js | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Tone/source/Player.js b/Tone/source/Player.js index 9ef3f85c..98a8dd75 100644 --- a/Tone/source/Player.js +++ b/Tone/source/Player.js @@ -198,8 +198,16 @@ Tone.Player.prototype._start = function(startTime, offset, duration){ //compute the values in seconds offset = this.toSeconds(offset); + + //if it's synced, it should factor in the playback rate for computing the offset + if (this._synced){ + offset *= this._playbackRate; + } + + //compute the duration which is either the passed in duration of the buffer.duration - offset var computedDuration = Tone.defaultArg(duration, Math.max(this._buffer.duration - offset, 0)); computedDuration = this.toSeconds(computedDuration); + //scale it by the playback rate computedDuration = computedDuration / this._playbackRate; diff --git a/package.json b/package.json index d8796248..5abfe4b4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tone", - "version": "13.7.0", + "version": "13.8.0", "description": "A Web Audio framework for making interactive music in the browser.", "main": "build/Tone.js", "files": [ diff --git a/test/source/Player.js b/test/source/Player.js index eaf440df..2710ace2 100644 --- a/test/source/Player.js +++ b/test/source/Player.js @@ -450,6 +450,24 @@ describe("Player", function(){ }); }); + it("starts at the correct position when Transport is offset and playbackRate is not 1", function(){ + return Offline(function(Transport){ + //make a ramp between 0-1 + var ramp = new Float32Array(Math.floor(Tone.context.sampleRate * 0.3)); + for (var i = 0; i < ramp.length; i++){ + ramp[i] = (i / (ramp.length)); + } + var buff = Buffer.fromArray(ramp); + var player = new Player(buff).toMaster(); + player.playbackRate = 0.5; + player.sync().start(0); + //start halfway through + Transport.start(0, 0.15); + }, 0.05).then(function(buffer){ + expect(buffer.getValueAtTime(0)).to.be.closeTo(0.25, 0.01); + }); + }); + it("starts with an offset when synced and started after Transport is running", function(){ return Offline(function(Transport){ var ramp = new Float32Array(Math.floor(Tone.context.sampleRate * 0.3));