Tone.js/Tone/signal/Pow.js

77 lines
1.5 KiB
JavaScript
Raw Normal View History

import Tone from "../core/Tone";
import "../signal/WaveShaper";
2014-10-31 01:34:11 +00:00
/**
* @class Pow applies an exponent to the incoming signal. The incoming signal
* must be AudioRange.
*
* @extends {Tone.SignalBase}
* @constructor
* @param {Positive} exp The exponent to apply to the incoming signal, must be at least 2.
* @example
* var pow = new Tone.Pow(2);
* var sig = new Tone.Signal(0.5).connect(pow);
* //output of pow is 0.25.
*/
Tone.Pow = function(exp){
2014-10-31 01:34:11 +00:00
Tone.SignalBase.call(this);
2014-10-31 01:34:11 +00:00
/**
* the exponent
* @private
* @type {number}
2014-11-02 01:53:14 +00:00
*/
this._exp = Tone.defaultArg(exp, 1);
/**
* @type {WaveShaperNode}
* @private
*/
this._expScaler = this.input = this.output = new Tone.WaveShaper(this._expFunc(this._exp), 8192);
};
2014-11-02 01:53:14 +00:00
Tone.extend(Tone.Pow, Tone.SignalBase);
2014-10-31 01:34:11 +00:00
/**
* The value of the exponent.
* @memberOf Tone.Pow#
* @type {number}
* @name value
*/
Object.defineProperty(Tone.Pow.prototype, "value", {
get : function(){
return this._exp;
},
set : function(exp){
this._exp = exp;
this._expScaler.setMap(this._expFunc(this._exp));
}
2017-10-26 20:02:01 +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);
};
};
/**
* Clean up.
* @returns {Tone.Pow} this
*/
Tone.Pow.prototype.dispose = function(){
Tone.SignalBase.prototype.dispose.call(this);
this._expScaler.dispose();
this._expScaler = null;
return this;
};
export default Tone.Pow;