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,4 +1,5 @@
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"; "use strict";
@ -22,8 +23,9 @@ define(["Tone/core/Tone", "Tone/signal/WaveShaper", "Tone/type/Type", "Tone/core
Tone.Signal = function(){ Tone.Signal = function(){
var options = Tone.defaults(arguments, ["value", "units"], Tone.Signal); var options = Tone.defaults(arguments, ["value", "units"], Tone.Signal);
var gainNode = Tone.context.createGain(); var constantSource = Tone.context.createConstantSource();
options.param = gainNode.gain; constantSource.start(0);
options.param = constantSource.offset;
Tone.Param.call(this, options); Tone.Param.call(this, options);
/** /**
@ -31,17 +33,14 @@ define(["Tone/core/Tone", "Tone/signal/WaveShaper", "Tone/type/Type", "Tone/core
* @type {GainNode} * @type {GainNode}
* @private * @private
*/ */
this.output = gainNode; this.output = constantSource;
/** /**
* The node where the value is set. * The node where the value is set.
* @type {Tone.Param} * @type {Tone.Param}
* @private * @private
*/ */
this.input = this._param = this.output.gain; this.input = this._param = this.output.offset;
//connect the const output to the node output
this.context.getConstant(1).connect(this.output);
}; };
Tone.extend(Tone.Signal, Tone.Param); Tone.extend(Tone.Signal, Tone.Param);