Tone.js/Tone/component/Compressor.js

121 lines
2.9 KiB
JavaScript
Raw Normal View History

define(["Tone/core/Tone", "Tone/core/Param", "Tone/core/AudioNode"], function(Tone){
2014-10-20 01:55:18 +00:00
"use strict";
/**
* @class Tone.Compressor is a thin wrapper around the Web Audio
2015-07-04 19:25:37 +00:00
* [DynamicsCompressorNode](http://webaudio.github.io/web-audio-api/#the-dynamicscompressornode-interface).
* Compression reduces the volume of loud sounds or amplifies quiet sounds
* by narrowing or "compressing" an audio signal's dynamic range.
2015-07-04 19:25:37 +00:00
* Read more on [Wikipedia](https://en.wikipedia.org/wiki/Dynamic_range_compression).
2014-10-20 01:55:18 +00:00
*
* @extends {Tone.AudioNode}
2014-10-20 01:55:18 +00:00
* @constructor
2015-06-20 23:25:49 +00:00
* @param {Decibels|Object} [threshold] The value above which the compression starts to be applied.
* @param {Positive} [ratio] The gain reduction ratio.
2015-02-27 21:53:10 +00:00
* @example
2015-06-20 23:25:49 +00:00
* var comp = new Tone.Compressor(-30, 3);
2014-10-20 01:55:18 +00:00
*/
Tone.Compressor = function(){
var options = Tone.defaults(arguments, ["threshold", "ratio"], Tone.Compressor);
Tone.AudioNode.call(this);
2014-10-20 01:55:18 +00:00
/**
* the compressor node
* @type {DynamicsCompressorNode}
* @private
*/
2015-04-24 17:13:20 +00:00
this._compressor = this.input = this.output = this.context.createDynamicsCompressor();
2014-10-20 01:55:18 +00:00
/**
* the threshold vaue
2015-06-13 23:29:25 +00:00
* @type {Decibels}
* @signal
2014-10-20 01:55:18 +00:00
*/
this.threshold = new Tone.Param({
"param" : this._compressor.threshold,
"units" : Tone.Type.Decibels,
"convert" : false
});
2014-10-20 01:55:18 +00:00
/**
* The attack parameter
2015-06-13 23:29:25 +00:00
* @type {Time}
* @signal
2014-10-20 01:55:18 +00:00
*/
2015-10-21 14:34:37 +00:00
this.attack = new Tone.Param(this._compressor.attack, Tone.Type.Time);
2014-10-20 01:55:18 +00:00
/**
* The release parameter
2015-06-13 23:29:25 +00:00
* @type {Time}
* @signal
2014-10-20 01:55:18 +00:00
*/
2015-10-21 14:34:37 +00:00
this.release = new Tone.Param(this._compressor.release, Tone.Type.Time);
2014-10-20 01:55:18 +00:00
/**
* The knee parameter
2015-06-13 23:29:25 +00:00
* @type {Decibels}
* @signal
2014-10-20 01:55:18 +00:00
*/
this.knee = new Tone.Param({
"param" : this._compressor.knee,
"units" : Tone.Type.Decibels,
"convert" : false
});
2014-10-20 01:55:18 +00:00
/**
* The ratio value
2015-06-13 23:29:25 +00:00
* @type {Number}
* @signal
2014-10-20 01:55:18 +00:00
*/
this.ratio = new Tone.Param({
"param" : this._compressor.ratio,
"convert" : false
});
2014-10-20 01:55:18 +00:00
//set the defaults
2015-04-05 19:13:15 +00:00
this._readOnly(["knee", "release", "attack", "ratio", "threshold"]);
2014-10-20 01:55:18 +00:00
this.set(options);
};
Tone.extend(Tone.Compressor, Tone.AudioNode);
2014-10-20 01:55:18 +00:00
/**
* @static
* @const
* @type {Object}
*/
Tone.Compressor.defaults = {
"ratio" : 12,
"threshold" : -24,
"release" : 0.25,
"attack" : 0.003,
"knee" : 30
};
/**
* clean up
* @returns {Tone.Compressor} this
2014-10-20 01:55:18 +00:00
*/
Tone.Compressor.prototype.dispose = function(){
Tone.AudioNode.prototype.dispose.call(this);
2015-04-05 19:13:15 +00:00
this._writable(["knee", "release", "attack", "ratio", "threshold"]);
2014-10-20 01:55:18 +00:00
this._compressor.disconnect();
this._compressor = null;
this.attack.dispose();
2014-10-20 01:55:18 +00:00
this.attack = null;
this.release.dispose();
2014-10-20 01:55:18 +00:00
this.release = null;
this.threshold.dispose();
2014-10-20 01:55:18 +00:00
this.threshold = null;
this.ratio.dispose();
2014-10-20 01:55:18 +00:00
this.ratio = null;
this.knee.dispose();
2014-10-20 01:55:18 +00:00
this.knee = null;
2015-02-02 17:49:13 +00:00
return this;
2014-10-20 01:55:18 +00:00
};
return Tone.Compressor;
});