Tone.js/Tone/effect/Chorus.js

180 lines
4.2 KiB
JavaScript
Raw Normal View History

2015-01-20 12:30:31 +00:00
define(["Tone/core/Tone", "Tone/component/LFO", "Tone/effect/StereoXFeedbackEffect"],
function(Tone){
"use strict";
2014-08-23 19:19:01 +00:00
/**
* @class A Chorus effect with feedback. inspiration from https://github.com/Dinahmoe/tuna/blob/master/tuna.js
2014-08-23 19:19:01 +00:00
*
* @constructor
* @extends {Tone.StereoXFeedbackEffect}
2015-02-10 16:40:27 +00:00
* @param {number|Object} [frequency=2] the frequency of the effect
2014-12-02 06:42:08 +00:00
* @param {number} [delayTime=3.5] the delay of the chorus effect in ms
* @param {number} [depth=0.7] the depth of the chorus
2015-02-27 21:53:10 +00:00
* @example
* var chorus = new Tone.Chorus(4, 2.5, 0.5);
2014-08-23 19:19:01 +00:00
*/
Tone.Chorus = function(){
2014-08-23 19:19:01 +00:00
2015-02-10 16:40:27 +00:00
var options = this.optionsObject(arguments, ["frequency", "delayTime", "depth"], Tone.Chorus.defaults);
Tone.StereoXFeedbackEffect.call(this, options);
2014-08-23 19:19:01 +00:00
/**
* the depth of the chorus
* @type {number}
* @private
*/
this._depth = options.depth;
2014-08-23 19:19:01 +00:00
/**
* the delayTime
* @type {number}
* @private
*/
this._delayTime = options.delayTime / 1000;
2014-08-23 19:19:01 +00:00
/**
* the lfo which controls the delayTime
* @type {Tone.LFO}
* @private
*/
this._lfoL = new Tone.LFO(options.rate, 0, 1);
/**
* another LFO for the right side with a 180 degree phase diff
* @type {Tone.LFO}
* @private
*/
this._lfoR = new Tone.LFO(options.rate, 0, 1);
2015-02-10 16:40:27 +00:00
this._lfoR.phase = 180;
/**
* delay for left
* @type {DelayNode}
* @private
*/
this._delayNodeL = this.context.createDelay();
2014-08-23 19:19:01 +00:00
/**
* delay for right
2014-08-23 19:19:01 +00:00
* @type {DelayNode}
* @private
*/
this._delayNodeR = this.context.createDelay();
2014-08-23 19:19:01 +00:00
2015-02-10 16:40:27 +00:00
/**
* The frequency the chorus will modulate at.
2015-06-13 23:50:39 +00:00
* @type {Frequency}
* @signal
2015-02-10 16:40:27 +00:00
*/
this.frequency = this._lfoL.frequency;
2014-08-23 19:19:01 +00:00
//connections
this.connectSeries(this.effectSendL, this._delayNodeL, this.effectReturnL);
this.connectSeries(this.effectSendR, this._delayNodeR, this.effectReturnR);
//and pass through
this.effectSendL.connect(this.effectReturnL);
this.effectSendR.connect(this.effectReturnR);
//lfo setup
this._lfoL.connect(this._delayNodeL.delayTime);
this._lfoR.connect(this._delayNodeR.delayTime);
//start the lfo
this._lfoL.start();
this._lfoR.start();
//have one LFO frequency control the other
this._lfoL.frequency.connect(this._lfoR.frequency);
//set the initial values
2015-02-10 16:40:27 +00:00
this.depth = this._depth;
this.frequency.value = options.frequency;
this.type = options.type;
2015-04-18 14:54:08 +00:00
this._readOnly(["frequency"]);
2014-08-23 19:19:01 +00:00
};
Tone.extend(Tone.Chorus, Tone.StereoXFeedbackEffect);
2014-08-23 19:19:01 +00:00
/**
* @static
* @type {Object}
*/
Tone.Chorus.defaults = {
2015-03-05 16:06:27 +00:00
"frequency" : 1.5,
"delayTime" : 3.5,
"depth" : 0.7,
2014-12-04 02:37:58 +00:00
"feedback" : 0.1,
2014-09-05 04:36:55 +00:00
"type" : "sine"
};
2014-08-23 19:19:01 +00:00
/**
2015-02-10 16:40:27 +00:00
* The depth of the effect.
* @memberOf Tone.Chorus#
* @type {number}
* @name depth
2014-09-05 04:36:55 +00:00
*/
2015-02-10 16:40:27 +00:00
Object.defineProperty(Tone.Chorus.prototype, "depth", {
get : function(){
return this._depth;
},
set : function(depth){
this._depth = depth;
var deviation = this._delayTime * depth;
this._lfoL.min = Math.max(this._delayTime - deviation, 0);
2015-02-10 16:40:27 +00:00
this._lfoL.max = this._delayTime + deviation;
this._lfoR.min = Math.max(this._delayTime - deviation, 0);
2015-02-10 16:40:27 +00:00
this._lfoR.max = this._delayTime + deviation;
}
});
2014-09-05 04:36:55 +00:00
2015-01-20 12:30:31 +00:00
/**
2015-02-10 16:40:27 +00:00
* The delayTime in milliseconds
* @memberOf Tone.Chorus#
* @type {number}
* @name delayTime
2015-01-20 12:30:31 +00:00
*/
2015-02-10 16:40:27 +00:00
Object.defineProperty(Tone.Chorus.prototype, "delayTime", {
get : function(){
return this._delayTime * 1000;
},
set : function(delayTime){
this._delayTime = delayTime / 1000;
this.depth = this._depth;
}
});
2015-01-20 12:30:31 +00:00
2014-08-23 19:19:01 +00:00
/**
2015-02-10 16:40:27 +00:00
* The lfo type for the chorus.
* @memberOf Tone.Chorus#
* @type {string}
* @name type
2014-08-23 19:19:01 +00:00
*/
2015-02-10 16:40:27 +00:00
Object.defineProperty(Tone.Chorus.prototype, "type", {
get : function(){
return this._lfoL.type;
},
set : function(type){
this._lfoL.type = type;
this._lfoR.type = type;
}
});
2014-08-23 19:19:01 +00:00
/**
* clean up
2015-02-02 18:22:16 +00:00
* @returns {Tone.Chorus} `this`
2014-08-23 19:19:01 +00:00
*/
Tone.Chorus.prototype.dispose = function(){
Tone.StereoXFeedbackEffect.prototype.dispose.call(this);
this._lfoL.dispose();
this._lfoL = null;
2015-02-10 16:40:27 +00:00
this._lfoR.dispose();
this._lfoR = null;
2015-02-10 16:40:27 +00:00
this._delayNodeL.disconnect();
this._delayNodeL = null;
2015-02-10 16:40:27 +00:00
this._delayNodeR.disconnect();
this._delayNodeR = null;
2015-04-18 14:54:08 +00:00
this._writable("frequency");
2015-02-10 16:40:27 +00:00
this.frequency = null;
2015-02-02 18:22:16 +00:00
return this;
2014-08-23 19:19:01 +00:00
};
return Tone.Chorus;
});