Tone.js/Tone/effect/AutoWah.js

164 lines
4.7 KiB
JavaScript
Raw Normal View History

2014-08-23 18:23:43 +00:00
define(["Tone/core/Tone", "Tone/component/Follower", "Tone/signal/ScaleExp", "Tone/effect/Effect"], function(Tone){
"use strict";
2014-08-23 18:23:43 +00:00
/**
* @class AutoWah connects an envelope follower to a bandpass filter.
* Some inspiration from Tuna.js https://github.com/Dinahmoe/tuna
*
* @constructor
* @extends {Tone.Effect}
* @param {number=} [baseFrequency=100] the frequency the filter is set
* to at the low point of the wah
* @param {number=} [octaves=5] the number of octaves above the baseFrequency
* the filter will sweep to when fully open
* @param {number=} [sensitivity=0] the decibel threshold sensitivity for
* the incoming signal. Normal range of -40 to 0.
*/
Tone.AutoWah = function(){
2014-08-23 18:23:43 +00:00
var options = this.optionsObject(arguments, ["baseFrequency", "octaves", "sensitivity"], Tone.AutoWah.defaults);
Tone.Effect.call(this, options);
2014-08-23 18:23:43 +00:00
/**
* the envelope follower
* @type {Tone.Follower}
* @private
*/
this._follower = new Tone.Follower(options.follower);
2014-08-23 18:23:43 +00:00
/**
* scales the follower value to the frequency domain
* @type {Tone}
* @private
*/
2014-09-05 03:56:48 +00:00
this._sweepRange = new Tone.ScaleExp(0, 1, 0, 1, 0.5);
2014-08-23 18:23:43 +00:00
/**
* @type {number}
* @private
*/
this._baseFrequency = options.baseFrequency;
2014-08-23 18:23:43 +00:00
/**
* @type {number}
* @private
*/
this._octaves = options.octaves;
2014-08-23 18:23:43 +00:00
/**
* @type {BiquadFilterNode}
* @private
*/
2014-09-06 19:55:43 +00:00
this._bandpass = new Tone.Filter(0, "bandpass");
2014-09-05 03:56:48 +00:00
this._bandpass.setRolloff(options.rolloff);
// this._bandpass.type = "bandpass";
// this._bandpass.Q.value = options.Q;
2014-08-23 18:23:43 +00:00
/**
* @type {BiquadFilterNode}
* @private
*/
this._peaking = this.context.createBiquadFilter();
this._peaking.type = "peaking";
2014-09-05 03:56:48 +00:00
this._peaking.gain.value = options.gain;
2014-08-23 18:23:43 +00:00
//the control signal path
this.chain(this.effectSend, this._follower, this._sweepRange);
this._sweepRange.connect(this._bandpass.frequency);
this._sweepRange.connect(this._peaking.frequency);
//the filtered path
this.chain(this.effectSend, this._bandpass, this._peaking, this.effectReturn);
//set the initial value
this._setSweepRange();
this.setSensitiviy(options.sensitivity);
2014-08-23 18:23:43 +00:00
};
Tone.extend(Tone.AutoWah, Tone.Effect);
/**
* @static
* @type {Object}
*/
Tone.AutoWah.defaults = {
"baseFrequency" : 100,
2014-09-04 04:32:44 +00:00
"octaves" : 6,
"sensitivity" : 0,
2014-09-05 03:56:48 +00:00
"Q" : 2,
"gain" : 2,
"rolloff" : -48,
/** attributes for the envelope follower */
"follower" : {
2014-09-04 04:32:44 +00:00
"attack" : 0.3,
"release" : 0.5
}
};
2014-08-23 18:23:43 +00:00
/**
* set the number of octaves that the filter will sweep
* @param {number} octaves the number of octaves above the base frequency the filter will sweep
*/
Tone.AutoWah.prototype.setOctaves = function(octaves){
this._octaves = octaves;
this._setSweepRange();
};
/**
* set the number of octaves that the filter will sweep
* @param {number} octaves the number of octaves above the base frequency the filter will sweep
*/
Tone.AutoWah.prototype.setBaseFrequency = function(baseFreq){
this._baseFrequency = baseFreq;
this._setSweepRange();
};
/**
* set the sensitivity to control how responsive to the input signal
* the wah is.
*
* @param {number} sensitivy the sensitivity to the input signal in dB
*/
Tone.AutoWah.prototype.setSensitiviy = function(sensitivy){
this._sweepRange.setInputMax(this.dbToGain(sensitivy));
};
/**
* sets the sweep range of the scaler
* @private
*/
Tone.AutoWah.prototype._setSweepRange = function(){
this._sweepRange.setOutputMin(this._baseFrequency);
this._sweepRange.setOutputMax(Math.min(this._baseFrequency * Math.pow(2, this._octaves), this.context.sampleRate / 2));
};
2014-08-25 13:57:36 +00:00
/**
* set all of the parameters with an object
* @param {Object} params
*/
Tone.AutoWah.prototype.set = function(params){
if (!this.isUndef(params.baseFrequency)) this.setBaseFrequency(params.baseFrequency);
if (!this.isUndef(params.sensitivity)) this.setSensitiviy(params.sensitivity);
if (!this.isUndef(params.octaves)) this.setOctaves(params.octaves);
if (!this.isUndef(params.follower)) this._follower.set(params.follower);
2014-09-05 03:56:48 +00:00
if (!this.isUndef(params.Q)) this._bandpass.Q.value = params.Q;
if (!this.isUndef(params.gain)) this._peaking.gain.value = params.gain;
2014-08-25 13:57:36 +00:00
Tone.Effect.prototype.set.call(this, params);
};
2014-08-23 18:23:43 +00:00
/**
* clean up
*/
Tone.AutoWah.prototype.dispose = function(){
2014-08-23 20:08:08 +00:00
Tone.Effect.prototype.dispose.call(this);
this._follower.dispose();
this._sweepRange.dispose();
this._bandpass.disconnect();
this._peaking.disconnect();
this._follower = null;
this._sweepRange = null;
this._bandpass = null;
this._peaking = null;
2014-08-23 18:23:43 +00:00
};
return Tone.AutoWah;
});