2019-01-27 18:05:20 +00:00
|
|
|
import Tone from "../core/Tone";
|
|
|
|
import "../type/Type";
|
|
|
|
import "../core/Param";
|
2019-03-08 18:01:41 +00:00
|
|
|
import "../signal/SignalBase";
|
2015-10-21 14:11:34 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
/**
|
|
|
|
* @class A signal is an audio-rate value. Tone.Signal is a core component of the library.
|
|
|
|
* Unlike a number, Signals can be scheduled with sample-level accuracy. Tone.Signal
|
|
|
|
* has all of the methods available to native Web Audio
|
|
|
|
* [AudioParam](http://webaudio.github.io/web-audio-api/#the-audioparam-interface)
|
|
|
|
* as well as additional conveniences. Read more about working with signals
|
|
|
|
* [here](https://github.com/Tonejs/Tone.js/wiki/Signals).
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @extends {Tone.Param}
|
|
|
|
* @param {Number|AudioParam} [value] Initial value of the signal. If an AudioParam
|
|
|
|
* is passed in, that parameter will be wrapped
|
|
|
|
* and controlled by the Signal.
|
|
|
|
* @param {string} [units=Number] unit The units the signal is in.
|
|
|
|
* @example
|
|
|
|
* var signal = new Tone.Signal(10);
|
|
|
|
*/
|
|
|
|
Tone.Signal = function(){
|
|
|
|
|
|
|
|
var options = Tone.defaults(arguments, ["value", "units"], Tone.Signal);
|
|
|
|
Tone.Param.call(this, options);
|
2015-10-21 14:11:34 +00:00
|
|
|
|
|
|
|
/**
|
2019-01-27 18:05:20 +00:00
|
|
|
* The constant source node which generates the signal
|
|
|
|
* @type {ConstantSourceNode}
|
|
|
|
* @private
|
2015-10-21 14:11:34 +00:00
|
|
|
*/
|
2019-01-27 18:05:20 +00:00
|
|
|
this._constantSource = this.context.createConstantSource();
|
|
|
|
this._constantSource.start(0);
|
|
|
|
this._param = this._constantSource.offset;
|
|
|
|
this.value = options.value;
|
2015-10-21 14:11:34 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
/**
|
|
|
|
* The node where the constant signal value is scaled.
|
2019-03-08 18:01:41 +00:00
|
|
|
* @type {ConstantSourceNode}
|
2019-01-27 18:05:20 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.output = this._constantSource;
|
2018-02-04 21:59:39 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
/**
|
|
|
|
* The node where the value is set.
|
|
|
|
* @type {Tone.Param}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.input = this._param = this.output.offset;
|
|
|
|
};
|
2015-10-21 14:11:34 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
Tone.extend(Tone.Signal, Tone.Param);
|
2015-10-21 14:11:34 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
/**
|
|
|
|
* The default values
|
|
|
|
* @type {Object}
|
|
|
|
* @static
|
|
|
|
* @const
|
|
|
|
*/
|
|
|
|
Tone.Signal.defaults = {
|
|
|
|
"value" : 0,
|
|
|
|
"units" : Tone.Type.Default,
|
|
|
|
"convert" : true,
|
|
|
|
};
|
2015-10-21 14:11:34 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
//use SignalBase's connect/disconnect methods
|
|
|
|
Tone.Signal.prototype.connect = Tone.SignalBase.prototype.connect;
|
|
|
|
Tone.Signal.prototype.disconnect = Tone.SignalBase.prototype.disconnect;
|
2015-10-21 14:11:34 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
/**
|
|
|
|
* Return the current signal value at the given time.
|
|
|
|
* @param {Time} time When to get the signal value
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
|
|
|
Tone.Signal.prototype.getValueAtTime = function(time){
|
|
|
|
if (this._param.getValueAtTime){
|
|
|
|
return this._param.getValueAtTime(time);
|
|
|
|
} else {
|
|
|
|
return Tone.Param.prototype.getValueAtTime.call(this, time);
|
|
|
|
}
|
|
|
|
};
|
2017-12-30 02:15:30 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
/**
|
|
|
|
* dispose and disconnect
|
|
|
|
* @returns {Tone.Signal} this
|
|
|
|
*/
|
|
|
|
Tone.Signal.prototype.dispose = function(){
|
|
|
|
Tone.Param.prototype.dispose.call(this);
|
2019-03-08 18:01:41 +00:00
|
|
|
this._constantSource.stop();
|
2019-01-27 18:05:20 +00:00
|
|
|
this._constantSource.disconnect();
|
|
|
|
this._constantSource = null;
|
|
|
|
return this;
|
|
|
|
};
|
2018-06-06 02:42:17 +00:00
|
|
|
|
2019-01-27 18:05:20 +00:00
|
|
|
export default Tone.Signal;
|
2015-10-21 14:11:34 +00:00
|
|
|
|