Tone.js/Tone/effect/BitCrusher.js

97 lines
2.2 KiB
JavaScript
Raw Normal View History

import Tone from "../core/Tone";
import "../effect/Effect";
import "../signal/Subtract";
import "../signal/Modulo";
2015-01-18 16:09:31 +00:00
/**
* @class Tone.Bitcrusher downsamples the incoming signal to a different bitdepth.
* Lowering the bitdepth of the signal creates distortion. Read more about Bitcrushing
* on [Wikipedia](https://en.wikipedia.org/wiki/Bitcrusher).
*
* @constructor
* @extends {Tone.Effect}
* @param {Number} bits The number of bits to downsample the signal. Nominal range
* of 1 to 8.
* @example
* //initialize crusher and route a synth through it
* var crusher = new Tone.BitCrusher(4).toMaster();
* var synth = new Tone.MonoSynth().connect(crusher);
*/
Tone.BitCrusher = function(){
2015-01-18 16:09:31 +00:00
var options = Tone.defaults(arguments, ["bits"], Tone.BitCrusher);
Tone.Effect.call(this, options);
2015-01-20 13:05:27 +00:00
var invStepSize = 1 / Math.pow(2, options.bits - 1);
2014-04-06 00:47:59 +00:00
/**
* Subtract the input signal and the modulus of the input signal
* @type {Tone.Subtract}
* @private
*/
this._subtract = new Tone.Subtract();
2014-04-06 00:47:59 +00:00
/**
* The mod function
* @type {Tone.Modulo}
* @private
*/
this._modulo = new Tone.Modulo(invStepSize);
2014-06-20 04:38:14 +00:00
/**
* keeps track of the bits
* @type {number}
* @private
2014-06-20 04:38:14 +00:00
*/
this._bits = options.bits;
//connect it up
this.effectSend.fan(this._subtract, this._modulo);
this._modulo.connect(this._subtract, 0, 1);
this._subtract.connect(this.effectReturn);
};
Tone.extend(Tone.BitCrusher, Tone.Effect);
/**
* the default values
* @static
* @type {Object}
*/
Tone.BitCrusher.defaults = {
"bits" : 4
};
2015-01-18 16:09:31 +00:00
/**
* The bit depth of the effect. Nominal range of 1-8.
* @memberOf Tone.BitCrusher#
* @type {number}
* @name bits
*/
Object.defineProperty(Tone.BitCrusher.prototype, "bits", {
get : function(){
return this._bits;
},
set : function(bits){
this._bits = bits;
var invStepSize = 1 / Math.pow(2, bits - 1);
this._modulo.value = invStepSize;
}
2015-03-05 16:06:27 +00:00
});
/**
* Clean up.
* @returns {Tone.BitCrusher} this
*/
Tone.BitCrusher.prototype.dispose = function(){
Tone.Effect.prototype.dispose.call(this);
this._subtract.dispose();
this._subtract = null;
this._modulo.dispose();
this._modulo = null;
return this;
};
export default Tone.BitCrusher;