Tone.js/Tone/signal/Pow.js

76 lines
1.6 KiB
JavaScript
Raw Normal View History

define(["Tone/core/Tone", "Tone/signal/WaveShaper"], function(Tone){
2014-10-31 01:34:11 +00:00
"use strict";
/**
2014-11-02 01:53:14 +00:00
* @class Pow applies an exponent to the incoming signal. The incoming signal
* must be in the range -1,1
2014-10-31 01:34:11 +00:00
*
* @extends {Tone.SignalBase}
2014-10-31 01:34:11 +00:00
* @constructor
* @param {number} exp the exponent to apply to the incoming signal, must be at least 2.
2015-02-27 18:40:35 +00:00
* @example
* var pow = new Tone.Pow(2);
* var sig = new Tone.Signal(0.5).connect(pow);
* //output of pow is 0.25.
2014-10-31 01:34:11 +00:00
*/
Tone.Pow = function(exp){
2015-02-02 17:50:18 +00:00
/**
* the exponent
* @private
* @type {number}
*/
this._exp = this.defaultArg(exp, 1);
2014-11-02 01:53:14 +00:00
/**
* @type {WaveShaperNode}
* @private
*/
2015-02-02 17:50:18 +00:00
this._expScaler = this.input = this.output = new Tone.WaveShaper(this._expFunc(this._exp), 8192);
2014-10-31 01:34:11 +00:00
};
Tone.extend(Tone.Pow, Tone.SignalBase);
2014-10-31 01:34:11 +00:00
2014-11-02 01:53:14 +00:00
/**
* The value of the exponent
* @memberOf Tone.Pow#
* @type {number}
* @name value
2014-11-02 01:53:14 +00:00
*/
Object.defineProperty(Tone.Pow.prototype, "value", {
get : function(){
return this._exp;
},
set : function(exp){
this._exp = exp;
this._expScaler.setMap(this._expFunc(this._exp));
}
});
2015-02-02 17:50:18 +00:00
/**
* the function which maps the waveshaper
* @param {number} exp
* @return {function}
* @private
*/
Tone.Pow.prototype._expFunc = function(exp){
return function(val){
return Math.pow(Math.abs(val), exp);
};
2014-11-02 01:53:14 +00:00
};
2014-10-31 01:34:11 +00:00
/**
* clean up
2015-02-02 03:56:33 +00:00
* @returns {Tone.Pow} `this`
2014-10-31 01:34:11 +00:00
*/
Tone.Pow.prototype.dispose = function(){
Tone.prototype.dispose.call(this);
this._expScaler.dispose();
2014-11-02 01:53:14 +00:00
this._expScaler = null;
2015-02-02 03:56:33 +00:00
return this;
2014-10-31 01:34:11 +00:00
};
return Tone.Pow;
});