Tone.js/Tone/signal/Pow.js

78 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
2015-06-19 04:52:04 +00:00
* must be AudioRange.
2014-10-31 01:34:11 +00:00
*
* @extends {Tone.SignalBase}
2014-10-31 01:34:11 +00:00
* @constructor
2015-08-24 21:29:52 +00:00
* @param {Positive} exp The exponent to apply to the incoming signal, must be at least 2.
2015-02-27 18:40:35 +00:00
* @example
2015-06-14 05:17:09 +00:00
* 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){
Tone.SignalBase.call(this);
2015-02-02 17:50:18 +00:00
/**
* the exponent
* @private
* @type {number}
*/
2017-04-30 19:03:49 +00:00
this._exp = Tone.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
/**
2015-06-19 04:52:04 +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
/**
2015-06-19 04:52:04 +00:00
* Clean up.
* @returns {Tone.Pow} this
2014-10-31 01:34:11 +00:00
*/
Tone.Pow.prototype.dispose = function(){
Tone.SignalBase.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;
});