Tone.js/Tone/effect/BitCrusher.js

86 lines
2 KiB
JavaScript
Raw Normal View History

2015-01-18 16:09:31 +00:00
define(["Tone/core/Tone", "Tone/effect/Effect", "Tone/signal/Expr"],
function(Tone){
"use strict";
/**
* @class downsample incoming signal.
*
* The algorithm to downsample the incoming signal is to scale the input
* to between [0, 2^bits) and then apply a Floor function to the scaled value,
* then scale it back to audio range [-1, 1]
*
* @constructor
* @extends {Tone.Effect}
* @param {number} bits 1-8.
*/
Tone.BitCrusher = function(){
var options = this.optionsObject(arguments, ["bits"], Tone.BitCrusher.defaults);
Tone.Effect.call(this, options);
2015-01-20 13:05:27 +00:00
this._bits = options.bits;
var invStepSize = 1 / Math.pow(2, this._bits - 1);
2015-01-18 16:09:31 +00:00
/**
* floor function
* @type {Tone.Expr}
* @private
*/
2015-01-20 13:05:27 +00:00
this._floor = new Tone.Expr("$0 - mod($0, %, %)", invStepSize, this._bits);
2015-01-18 16:09:31 +00:00
//connect it up
this.connectEffect(this._floor);
};
Tone.extend(Tone.BitCrusher, Tone.Effect);
/**
2015-01-18 16:09:31 +00:00
* the default values
* @static
* @type {Object}
*/
2015-01-18 16:09:31 +00:00
Tone.BitCrusher.defaults = {
"bits" : 4
};
/**
* set the bit rate
* @param {number} bits 1-8
*/
Tone.BitCrusher.prototype.setBits = function(bits){
2015-01-20 13:05:27 +00:00
this._bits = bits;
2015-01-18 16:09:31 +00:00
var invStepSize = 1 / Math.pow(2, bits - 1);
this._floor.dispose();
this._floor = null;
this._floor = new Tone.Expr("$0 - mod($0, %, %)", invStepSize, bits);
this.connectEffect(this._floor);
};
2015-01-20 13:05:27 +00:00
/**
* @return {number} current bits
*/
Tone.BitCrusher.prototype.getBits = function(){
return this._bits;
};
2015-01-18 16:09:31 +00:00
/**
* set multiple parameters at once with an object
* @param {Object} params the parameters as an object
*/
Tone.BitCrusher.prototype.set = function(params){
if (!this.isUndef(params.bits)) this.setBits(params.bits);
Tone.Effect.prototype.set.call(this, params);
};
/**
* clean up
*/
Tone.BitCrusher.prototype.dispose = function(){
Tone.Effect.prototype.dispose.call(this);
this._floor.dispose();
this._floor = null;
};
return Tone.BitCrusher;
});