2014-11-30 18:20:35 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/effect/Effect", "Tone/signal/WaveShaper"], function(Tone){
|
2014-10-19 21:54:30 +00:00
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/**
|
2015-07-04 16:40:33 +00:00
|
|
|
* @class Tone.Distortion is a simple distortion effect using Tone.WaveShaper.
|
2015-07-04 19:25:37 +00:00
|
|
|
* Algorithm from [a stackoverflow answer](http://stackoverflow.com/a/22313408).
|
2014-10-19 21:54:30 +00:00
|
|
|
*
|
|
|
|
* @extends {Tone.Effect}
|
|
|
|
* @constructor
|
2015-06-22 05:20:57 +00:00
|
|
|
* @param {Number|Object} [distortion] The amount of distortion (nominal range of 0-1)
|
2015-02-27 21:53:10 +00:00
|
|
|
* @example
|
2015-07-04 16:40:33 +00:00
|
|
|
* var dist = new Tone.Distortion(0.8).toMaster();
|
|
|
|
* var fm = new Tone.SimpleFM().connect(dist);
|
|
|
|
* //this sounds good on bass notes
|
|
|
|
* fm.triggerAttackRelease("A1", "8n");
|
2014-10-19 21:54:30 +00:00
|
|
|
*/
|
|
|
|
Tone.Distortion = function(){
|
|
|
|
|
2017-04-26 03:18:08 +00:00
|
|
|
var options = Tone.defaults(arguments, ["distortion"], Tone.Distortion);
|
2015-05-21 17:52:44 +00:00
|
|
|
Tone.Effect.call(this, options);
|
2014-10-19 21:54:30 +00:00
|
|
|
|
|
|
|
/**
|
2014-11-30 18:20:35 +00:00
|
|
|
* @type {Tone.WaveShaper}
|
2014-10-19 21:54:30 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-11-30 18:20:35 +00:00
|
|
|
this._shaper = new Tone.WaveShaper(4096);
|
2014-11-04 00:22:17 +00:00
|
|
|
|
2015-02-10 16:40:27 +00:00
|
|
|
/**
|
|
|
|
* holds the distortion amount
|
|
|
|
* @type {number}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._distortion = options.distortion;
|
|
|
|
|
2014-10-19 21:54:30 +00:00
|
|
|
this.connectEffect(this._shaper);
|
2015-02-10 16:40:27 +00:00
|
|
|
this.distortion = options.distortion;
|
|
|
|
this.oversample = options.oversample;
|
2014-10-19 21:54:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Tone.extend(Tone.Distortion, Tone.Effect);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @static
|
|
|
|
* @const
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
Tone.Distortion.defaults = {
|
|
|
|
"distortion" : 0.4,
|
|
|
|
"oversample" : "none"
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-08-26 14:00:32 +00:00
|
|
|
* The amount of distortion.
|
2015-02-10 16:40:27 +00:00
|
|
|
* @memberOf Tone.Distortion#
|
2015-08-26 14:00:32 +00:00
|
|
|
* @type {NormalRange}
|
2015-02-10 16:40:27 +00:00
|
|
|
* @name distortion
|
2014-10-19 21:54:30 +00:00
|
|
|
*/
|
2015-02-10 16:40:27 +00:00
|
|
|
Object.defineProperty(Tone.Distortion.prototype, "distortion", {
|
|
|
|
get : function(){
|
|
|
|
return this._distortion;
|
|
|
|
},
|
|
|
|
set : function(amount){
|
|
|
|
this._distortion = amount;
|
|
|
|
var k = amount * 100;
|
|
|
|
var deg = Math.PI / 180;
|
|
|
|
this._shaper.setMap(function(x){
|
|
|
|
if (Math.abs(x) < 0.001){
|
|
|
|
//should output 0 when input is 0
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return ( 3 + k ) * x * 20 * deg / ( Math.PI + k * Math.abs(x) );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2014-10-19 21:54:30 +00:00
|
|
|
|
|
|
|
/**
|
2015-02-10 16:40:27 +00:00
|
|
|
* The oversampling of the effect. Can either be "none", "2x" or "4x".
|
|
|
|
* @memberOf Tone.Distortion#
|
|
|
|
* @type {string}
|
|
|
|
* @name oversample
|
2014-10-19 21:54:30 +00:00
|
|
|
*/
|
2015-02-10 16:40:27 +00:00
|
|
|
Object.defineProperty(Tone.Distortion.prototype, "oversample", {
|
|
|
|
get : function(){
|
|
|
|
return this._shaper.oversample;
|
|
|
|
},
|
|
|
|
set : function(oversampling){
|
|
|
|
this._shaper.oversample = oversampling;
|
|
|
|
}
|
|
|
|
});
|
2014-12-19 17:20:47 +00:00
|
|
|
|
2014-10-19 21:54:30 +00:00
|
|
|
/**
|
2015-06-22 05:20:57 +00:00
|
|
|
* Clean up.
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.Distortion} this
|
2014-10-19 21:54:30 +00:00
|
|
|
*/
|
|
|
|
Tone.Distortion.prototype.dispose = function(){
|
|
|
|
Tone.Effect.prototype.dispose.call(this);
|
2014-11-30 18:20:35 +00:00
|
|
|
this._shaper.dispose();
|
2014-10-19 21:54:30 +00:00
|
|
|
this._shaper = null;
|
2015-02-02 18:22:16 +00:00
|
|
|
return this;
|
2014-10-19 21:54:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return Tone.Distortion;
|
|
|
|
});
|