Clock uses setTimeout to invoke onended

the native onended is too flakey. addresses #48
This commit is contained in:
Yotam Mann 2015-01-04 22:19:33 -05:00
parent 0171707425
commit 693d96b1b7

View file

@ -4,15 +4,13 @@ define(["Tone/core/Tone", "Tone/signal/Signal"], function(Tone){
/**
* @class a sample accurate clock built on an oscillator.
* Invokes the onTick method at the set rate
* NB: can cause audio glitches. use sparingly.
* Invokes the tick method at the set rate
*
* @internal
* @constructor
* @extends {Tone}
* @param {number} rate the rate of the callback
* @param {function} callback the callback to be invoked with the time of the audio event
* NB: it is very important that only
*/
Tone.Clock = function(rate, callback){
@ -90,7 +88,7 @@ define(["Tone/core/Tone", "Tone/signal/Signal"], function(Tone){
* @param {Tone.Time} time the time when the clock should start
*/
Tone.Clock.prototype.start = function(time){
//reset the oscillator
if (!this._oscillator){
this._oscillator = this.context.createOscillator();
this._oscillator.type = "square";
this._oscillator.connect(this._jsNode);
@ -99,7 +97,7 @@ define(["Tone/core/Tone", "Tone/signal/Signal"], function(Tone){
this._upTick = false;
var startTime = this.toSeconds(time);
this._oscillator.start(startTime);
this._oscillator.onended = function(){};
}
};
/**
@ -108,9 +106,18 @@ define(["Tone/core/Tone", "Tone/signal/Signal"], function(Tone){
* @param {function} onend called when the oscilator stops
*/
Tone.Clock.prototype.stop = function(time, onend){
var stopTime = this.toSeconds(time);
this._oscillator.onended = onend;
if (this._oscillator){
var now = this.now();
var stopTime = this.toSeconds(time, now);
this._oscillator.stop(stopTime);
this._oscillator = null;
//set a timeout for when it stops
if (time){
setTimeout(onend, (stopTime - now) * 1000);
} else {
onend();
}
}
};
/**
@ -149,7 +156,6 @@ define(["Tone/core/Tone", "Tone/signal/Signal"], function(Tone){
this._jsNode.disconnect();
this._controlSignal.dispose();
if (this._oscillator){
this._oscillator.onended();
this._oscillator.disconnect();
}
this._jsNode.onaudioprocess = function(){};