mirror of
https://github.com/Tonejs/Tone.js
synced 2024-11-16 16:48:00 +00:00
Clock uses setTimeout to invoke onended
the native onended is too flakey. addresses #48
This commit is contained in:
parent
0171707425
commit
693d96b1b7
1 changed files with 23 additions and 17 deletions
|
@ -4,15 +4,13 @@ define(["Tone/core/Tone", "Tone/signal/Signal"], function(Tone){
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class a sample accurate clock built on an oscillator.
|
* @class a sample accurate clock built on an oscillator.
|
||||||
* Invokes the onTick method at the set rate
|
* Invokes the tick method at the set rate
|
||||||
* NB: can cause audio glitches. use sparingly.
|
|
||||||
*
|
*
|
||||||
* @internal
|
* @internal
|
||||||
* @constructor
|
* @constructor
|
||||||
* @extends {Tone}
|
* @extends {Tone}
|
||||||
* @param {number} rate the rate of the callback
|
* @param {number} rate the rate of the callback
|
||||||
* @param {function} callback the callback to be invoked with the time of the audio event
|
* @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){
|
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
|
* @param {Tone.Time} time the time when the clock should start
|
||||||
*/
|
*/
|
||||||
Tone.Clock.prototype.start = function(time){
|
Tone.Clock.prototype.start = function(time){
|
||||||
//reset the oscillator
|
if (!this._oscillator){
|
||||||
this._oscillator = this.context.createOscillator();
|
this._oscillator = this.context.createOscillator();
|
||||||
this._oscillator.type = "square";
|
this._oscillator.type = "square";
|
||||||
this._oscillator.connect(this._jsNode);
|
this._oscillator.connect(this._jsNode);
|
||||||
|
@ -99,7 +97,7 @@ define(["Tone/core/Tone", "Tone/signal/Signal"], function(Tone){
|
||||||
this._upTick = false;
|
this._upTick = false;
|
||||||
var startTime = this.toSeconds(time);
|
var startTime = this.toSeconds(time);
|
||||||
this._oscillator.start(startTime);
|
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
|
* @param {function} onend called when the oscilator stops
|
||||||
*/
|
*/
|
||||||
Tone.Clock.prototype.stop = function(time, onend){
|
Tone.Clock.prototype.stop = function(time, onend){
|
||||||
var stopTime = this.toSeconds(time);
|
if (this._oscillator){
|
||||||
this._oscillator.onended = onend;
|
var now = this.now();
|
||||||
|
var stopTime = this.toSeconds(time, now);
|
||||||
this._oscillator.stop(stopTime);
|
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._jsNode.disconnect();
|
||||||
this._controlSignal.dispose();
|
this._controlSignal.dispose();
|
||||||
if (this._oscillator){
|
if (this._oscillator){
|
||||||
this._oscillator.onended();
|
|
||||||
this._oscillator.disconnect();
|
this._oscillator.disconnect();
|
||||||
}
|
}
|
||||||
this._jsNode.onaudioprocess = function(){};
|
this._jsNode.onaudioprocess = function(){};
|
||||||
|
|
Loading…
Reference in a new issue