seek to a position in the buffer

This commit is contained in:
Yotam Mann 2016-09-25 22:32:54 -04:00
parent 2474312a4e
commit 753640bdeb
2 changed files with 58 additions and 4 deletions

View file

@ -227,6 +227,30 @@ define(["Tone/core/Tone", "Tone/core/Buffer", "Tone/source/Source"], function(To
return this; 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 the loop start and end. Will only loop if loop is
* set to true. * set to true.

View file

@ -1,5 +1,6 @@
define(["helper/Basic", "Tone/source/Player", "helper/Offline", "helper/SourceTests", "Tone/core/Buffer", "helper/Meter"], define(["helper/Basic", "Tone/source/Player", "helper/Offline",
function (BasicTests, Player, Offline, SourceTests, Buffer, Meter) { "helper/SourceTests", "Tone/core/Buffer", "helper/Meter", "helper/Offline2"],
function (BasicTests, Player, Offline, SourceTests, Buffer, Meter, Offline2) {
if (window.__karma__){ if (window.__karma__){
Buffer.baseUrl = "/base/test/"; Buffer.baseUrl = "/base/test/";
@ -193,8 +194,6 @@ define(["helper/Basic", "Tone/source/Player", "helper/Offline", "helper/SourceTe
context("Start Scheduling", function(){ context("Start Scheduling", function(){
this.timeout(3000);
it("can be start with an offset", function(done){ it("can be start with an offset", function(done){
var player; var player;
var offline = new Offline(0.4, 1); var offline = new Offline(0.4, 1);
@ -217,6 +216,37 @@ define(["helper/Basic", "Tone/source/Player", "helper/Offline", "helper/SourceTe
offline.run(); 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){ it("can be play for a specific duration", function(done){
var player; var player;
var meter = new Meter(0.4); var meter = new Meter(0.4);