2014-06-19 05:40:16 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/core/Transport"], function(Tone){
|
2014-06-16 01:00:22 +00:00
|
|
|
/**
|
|
|
|
* base class for sources
|
|
|
|
*
|
2014-06-19 05:40:16 +00:00
|
|
|
* sources have start/stop/pause
|
|
|
|
*
|
|
|
|
* they also have the ability to be synced to the
|
|
|
|
* start/stop/pause of Tone.Transport
|
|
|
|
*
|
2014-06-16 01:00:22 +00:00
|
|
|
* @constructor
|
|
|
|
* @extends {Tone}
|
|
|
|
*/
|
|
|
|
Tone.Source = function(){
|
2014-06-16 23:38:46 +00:00
|
|
|
/**
|
|
|
|
* unlike most ToneNodes, Sources only have an output and no input
|
|
|
|
*
|
|
|
|
* @type {GainNode}
|
|
|
|
*/
|
|
|
|
this.output = this.context.createGain();
|
2014-06-19 05:40:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {Tone.Source.State}
|
|
|
|
*/
|
|
|
|
this.state = Tone.Source.State.STOPPED;
|
2014-06-16 01:00:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Tone.extend(Tone.Source);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @abstract
|
|
|
|
* @param {Tone.Time} time
|
|
|
|
*/
|
|
|
|
Tone.Source.prototype.start = function(){};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @abstract
|
|
|
|
* @param {Tone.Time} time
|
|
|
|
*/
|
|
|
|
Tone.Source.prototype.stop = function(){};
|
|
|
|
|
2014-06-19 05:40:16 +00:00
|
|
|
|
2014-06-16 01:00:22 +00:00
|
|
|
/**
|
|
|
|
* @abstract
|
|
|
|
* @param {Tone.Time} time
|
|
|
|
*/
|
2014-06-19 05:40:16 +00:00
|
|
|
Tone.Source.prototype.pause = function(time){
|
|
|
|
//if there is no pause, just stop it
|
|
|
|
this.stop(time);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sync the source to the Transport
|
|
|
|
*/
|
|
|
|
Tone.Source.prototype.sync = function(){
|
|
|
|
if (this.state !== Tone.Source.State.SYNCED){
|
|
|
|
this.state = Tone.Source.State.SYNCED;
|
|
|
|
Tone.Transport.sync(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* unsync the source to the Transport
|
|
|
|
*/
|
|
|
|
Tone.Source.prototype.unsync = function(){
|
|
|
|
if (this.state === Tone.Source.State.SYNCED){
|
|
|
|
Tone.Transport.unsync(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-16 01:00:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} value
|
2014-06-21 17:04:22 +00:00
|
|
|
* @param {Tone.Time=} fadeTime (optional) time it takes to reach the value
|
2014-06-16 01:00:22 +00:00
|
|
|
*/
|
2014-06-21 17:04:22 +00:00
|
|
|
Tone.Source.prototype.setVolume = function(value, fadeTime){
|
2014-06-16 01:00:22 +00:00
|
|
|
var now = this.now();
|
2014-06-21 17:04:22 +00:00
|
|
|
if (fadeTime){
|
|
|
|
var currentVolume = this.output.gain.value;
|
|
|
|
this.output.gain.cancelScheduledValues(now);
|
|
|
|
this.output.gain.setValueAtTime(currentVolume, now);
|
|
|
|
this.output.gain.linearRampToValueAtTime(value, now + this.toSeconds(time));
|
|
|
|
} else {
|
|
|
|
this.output.gain.setValueAtTime(value, now);
|
|
|
|
}
|
2014-06-16 01:00:22 +00:00
|
|
|
};
|
|
|
|
|
2014-06-19 05:40:16 +00:00
|
|
|
/**
|
|
|
|
* @enum {string}
|
|
|
|
*/
|
|
|
|
Tone.Source.State = {
|
|
|
|
STARTED : "started",
|
|
|
|
PAUSED : "paused",
|
|
|
|
STOPPED : "stopped",
|
|
|
|
SYNCED : "synced"
|
|
|
|
};
|
|
|
|
|
2014-06-16 01:00:22 +00:00
|
|
|
return Tone.Source;
|
|
|
|
});
|