Tone.js/Tone/effect/Effect.js

115 lines
2.6 KiB
JavaScript
Raw Normal View History

2015-02-02 17:48:04 +00:00
define(["Tone/core/Tone", "Tone/component/CrossFade"], function(Tone){
"use strict";
/**
2014-08-23 17:50:13 +00:00
* @class Effect is the base class for effects. connect the effect between
2014-09-05 15:32:33 +00:00
* the effectSend and effectReturn GainNodes. then control the amount of
* effect which goes to the output using the dry/wet control.
*
* @constructor
* @extends {Tone}
2015-02-02 17:48:04 +00:00
* @param {number} [initialWet=0] the starting wet value
* defaults to 100% wet
*/
Tone.Effect = function(){
Tone.call(this);
//get all of the defaults
2015-02-02 17:48:04 +00:00
var options = this.optionsObject(arguments, ["wet"], Tone.Effect.defaults);
/**
* the drywet knob to control the amount of effect
2015-02-02 17:48:04 +00:00
* @type {Tone.CrossFade}
*/
2015-02-10 16:40:27 +00:00
this.dryWet = new Tone.CrossFade(options.wet);
2015-02-02 17:48:04 +00:00
/**
2015-02-10 16:40:27 +00:00
* The wet control, i.e. how much of the effected
* will pass through to the output.
2015-02-02 17:48:04 +00:00
* @type {Tone.Signal}
*/
2015-02-02 17:48:04 +00:00
this.wet = this.dryWet.fade;
2014-08-21 00:46:57 +00:00
/**
* connect the effectSend to the input of hte effect
*
* @type {GainNode}
*/
this.effectSend = this.context.createGain();
2014-08-21 00:46:57 +00:00
/**
* connect the output of the effect to the effectReturn
*
* @type {GainNode}
*/
this.effectReturn = this.context.createGain();
//connections
2015-02-02 17:48:04 +00:00
this.input.connect(this.dryWet.a);
this.input.connect(this.effectSend);
2015-02-02 17:48:04 +00:00
this.effectReturn.connect(this.dryWet.b);
this.dryWet.connect(this.output);
};
Tone.extend(Tone.Effect);
/**
* @static
* @type {Object}
*/
Tone.Effect.defaults = {
2015-02-02 17:48:04 +00:00
"wet" : 1
};
/**
* bypass the effect
2015-02-02 17:48:04 +00:00
* @returns {Tone.Effect} `this`
*/
Tone.Effect.prototype.bypass = function(){
2015-02-02 17:48:04 +00:00
this.setWet(0);
return this;
};
/**
* chains the effect in between the effectSend and effectReturn
* @param {Tone} effect
2014-10-19 21:56:41 +00:00
* @internal
2015-02-02 17:48:04 +00:00
* @returns {Tone.Effect} `this`
*/
Tone.Effect.prototype.connectEffect = function(effect){
2014-12-01 02:32:09 +00:00
this.effectSend.chain(effect, this.effectReturn);
2015-02-02 17:48:04 +00:00
return this;
};
2014-09-04 18:04:58 +00:00
/**
* set the preset if it exists
* @param {string} presetName the name of the preset
2015-02-02 17:48:04 +00:00
* @returns {Tone.Effect} `this`
2014-09-04 18:04:58 +00:00
*/
Tone.Effect.prototype.setPreset = function(presetName){
if (!this.isUndef(this.preset) && this.preset.hasOwnProperty(presetName)){
this.set(this.preset[presetName]);
}
2015-02-02 17:48:04 +00:00
return this;
2014-09-04 18:04:58 +00:00
};
/**
* tear down
2015-02-02 17:48:04 +00:00
* @returns {Tone.Effect} `this`
*/
Tone.Effect.prototype.dispose = function(){
Tone.prototype.dispose.call(this);
this.dryWet.dispose();
this.dryWet = null;
this.effectSend.disconnect();
this.effectSend = null;
this.effectReturn.disconnect();
this.effectReturn = null;
2015-02-02 17:48:04 +00:00
this.wet = null;
return this;
};
return Tone.Effect;
});