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";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class A simple distortion effect using the waveshaper node
|
|
|
|
* algorithm from http://stackoverflow.com/a/22313408
|
|
|
|
*
|
|
|
|
* @extends {Tone.Effect}
|
|
|
|
* @constructor
|
|
|
|
* @param {number} distortion the amount of distortion (nominal range of 0-1)
|
|
|
|
*/
|
|
|
|
Tone.Distortion = function(){
|
|
|
|
|
|
|
|
var options = this.optionsObject(arguments, ["distortion"], Tone.Distortion.defaults);
|
|
|
|
|
|
|
|
Tone.Effect.call(this);
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
2014-10-19 21:54:30 +00:00
|
|
|
this.connectEffect(this._shaper);
|
|
|
|
this.setDistortion(options.distortion);
|
|
|
|
this.setOversample(options.oversample);
|
|
|
|
};
|
|
|
|
|
|
|
|
Tone.extend(Tone.Distortion, Tone.Effect);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @static
|
|
|
|
* @const
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
Tone.Distortion.defaults = {
|
|
|
|
"distortion" : 0.4,
|
|
|
|
"oversample" : "none"
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the amount of distortion
|
|
|
|
* @param {number} amount amount of distortion, nominal range of 0-1.
|
|
|
|
*/
|
|
|
|
Tone.Distortion.prototype.setDistortion = function(amount) {
|
|
|
|
var k = amount * 100;
|
|
|
|
var deg = Math.PI / 180;
|
2014-11-30 18:20:35 +00:00
|
|
|
this._shaper.setMap(function(x){
|
2014-11-30 22:36:56 +00:00
|
|
|
if (Math.abs(x) < 0.001){
|
2014-10-19 21:54:30 +00:00
|
|
|
//should output 0 when input is 0
|
2014-11-30 18:20:35 +00:00
|
|
|
return 0;
|
2014-10-19 21:54:30 +00:00
|
|
|
} else {
|
2014-11-30 18:20:35 +00:00
|
|
|
return ( 3 + k ) * x * 20 * deg / ( Math.PI + k * Math.abs(x) );
|
2014-10-19 21:54:30 +00:00
|
|
|
}
|
2014-11-30 18:20:35 +00:00
|
|
|
});
|
2014-10-19 21:54:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the oversampling
|
|
|
|
* @param {string} oversampling can either be "none", "2x" or "4x"
|
|
|
|
*/
|
|
|
|
Tone.Distortion.prototype.setOversample = function(oversampling) {
|
|
|
|
this._shaper.oversample = oversampling;
|
|
|
|
};
|
|
|
|
|
2014-12-19 17:20:47 +00:00
|
|
|
/**
|
|
|
|
* set in bulk
|
|
|
|
* @param {Object} params
|
|
|
|
*/
|
|
|
|
Tone.Distortion.prototype.setOversample = function(params) {
|
|
|
|
if (!this.isUndef(params.distortion)) this.setDistortion(params.distortion);
|
|
|
|
if (!this.isUndef(params.oversample)) this.setOversample(params.oversample);
|
|
|
|
Tone.Effect.prototype.set.call(this, params);
|
|
|
|
};
|
|
|
|
|
2014-10-19 21:54:30 +00:00
|
|
|
/**
|
|
|
|
* clean up
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
return Tone.Distortion;
|
|
|
|
});
|