signal uses ConstantSourceNode (shimmed when not available)

This commit is contained in:
Yotam Mann 2017-10-26 11:33:42 -04:00
parent 7a870296bc
commit 255b20cebb
2 changed files with 70 additions and 17 deletions

View file

@ -0,0 +1,54 @@
define(["Tone/core/Tone", "Tone/shim/AudioContext", "Tone/core/Context", "Tone/core/Gain"], function(Tone){
if (Tone.supported && !window.ConstantSourceNode){
var ConstantSourceNode = function(context){
this.context = context;
var buffer = context.createBuffer(1, 128, context.sampleRate);
var arr = buffer.getChannelData(0);
for (var i = 0; i < arr.length; i++){
arr[i] = 1;
}
this._bufferSource = context.createBufferSource();
this._bufferSource.channelCount = 1;
this._bufferSource.channelCountMode = "explicit";
this._bufferSource.buffer = buffer;
this._bufferSource.loop = true;
var gainNode = this._output = context.createGain();
this.offset = gainNode.gain;
this._bufferSource.connect(gainNode);
};
ConstantSourceNode.prototype.start = function(time){
this._bufferSource.start(time);
return this;
};
ConstantSourceNode.prototype.stop = function(time){
this._bufferSource.stop(time);
return this;
};
ConstantSourceNode.prototype.connect = function(to){
this._output.connect.apply(this._output, arguments);
return this;
};
ConstantSourceNode.prototype.disconnect = function(){
this._output.disconnect.apply(this._output, arguments);
return this;
};
AudioContext.prototype.createConstantSource = function(){
return new ConstantSourceNode(this);
};
Tone.Context.prototype.createConstantSource = function(){
return new ConstantSourceNode(this);
};
}
});

View file

@ -1,29 +1,31 @@
define(["Tone/core/Tone", "Tone/signal/WaveShaper", "Tone/type/Type", "Tone/core/Param", "Tone/core/Gain"], function(Tone){
define(["Tone/core/Tone", "Tone/signal/WaveShaper", "Tone/type/Type", "Tone/core/Param",
"Tone/shim/ConstantSourceNode", "Tone/core/Gain"], function(Tone){
"use strict";
/**
* @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
* 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
* 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.
* 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);
var gainNode = Tone.context.createGain();
options.param = gainNode.gain;
var constantSource = Tone.context.createConstantSource();
constantSource.start(0);
options.param = constantSource.offset;
Tone.Param.call(this, options);
/**
@ -31,17 +33,14 @@ define(["Tone/core/Tone", "Tone/signal/WaveShaper", "Tone/type/Type", "Tone/core
* @type {GainNode}
* @private
*/
this.output = gainNode;
this.output = constantSource;
/**
* The node where the value is set.
* @type {Tone.Param}
* @private
*/
this.input = this._param = this.output.gain;
//connect the const output to the node output
this.context.getConstant(1).connect(this.output);
this.input = this._param = this.output.offset;
};
Tone.extend(Tone.Signal, Tone.Param);
@ -59,12 +58,12 @@ define(["Tone/core/Tone", "Tone/signal/WaveShaper", "Tone/type/Type", "Tone/core
};
/**
* When signals connect to other signals or AudioParams,
* they take over the output value of that signal or AudioParam.
* For all other nodes, the behavior is the same as a default <code>connect</code>.
* When signals connect to other signals or AudioParams,
* they take over the output value of that signal or AudioParam.
* For all other nodes, the behavior is the same as a default <code>connect</code>.
*
* @override
* @param {AudioParam|AudioNode|Tone.Signal|Tone} node
* @param {AudioParam|AudioNode|Tone.Signal|Tone} node
* @param {number} [outputNumber=0] The output number to connect from.
* @param {number} [inputNumber=0] The input number to connect to.
* @returns {Tone.SignalBase} this
@ -82,4 +81,4 @@ define(["Tone/core/Tone", "Tone/signal/WaveShaper", "Tone/type/Type", "Tone/core
};
return Tone.Signal;
});
});