Tone.js/Tone/effect/BitCrusher.js

55 lines
1.2 KiB
JavaScript
Raw Normal View History

2014-10-30 22:06:13 +00:00
define(["Tone/core/Tone", "Tone/effect/Effect", "Tone/signal/Expr"],
function(Tone){
2014-04-06 00:47:59 +00:00
"use strict";
2014-06-17 16:30:45 +00:00
/**
2014-09-05 15:32:33 +00:00
* @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]
2014-06-17 16:30:45 +00:00
*
* @constructor
2014-08-23 18:23:54 +00:00
* @extends {Tone.Effect}
* @param {number} bits 1-8.
2014-06-17 16:30:45 +00:00
*/
Tone.BitCrusher = function(){
2014-06-17 16:30:45 +00:00
var options = this.optionsObject(arguments, ["bits"], Tone.BitCrusher.defaults);
Tone.Effect.call(this, options);
2014-04-06 00:47:59 +00:00
2014-11-01 20:16:14 +00:00
var invStepSize = 1 / Math.pow(2, options.bits - 1);
/**
2014-10-30 22:06:13 +00:00
* floor function
* @type {Tone.Expr}
* @private
*/
2014-11-02 01:54:53 +00:00
this._floor = new Tone.Expr("$0 - mod($0, %, %)", invStepSize, options.bits);
2014-04-06 00:47:59 +00:00
//connect it up
2014-11-01 20:16:14 +00:00
this.connectEffect(this._floor);
2014-06-17 16:30:45 +00:00
};
2014-04-06 00:47:59 +00:00
2014-08-23 18:23:54 +00:00
Tone.extend(Tone.BitCrusher, Tone.Effect);
2014-04-06 00:47:59 +00:00
/**
* the default values
* @static
* @type {Object}
*/
Tone.BitCrusher.defaults = {
"bits" : 4
2014-06-17 16:30:45 +00:00
};
2014-04-06 00:47:59 +00:00
2014-06-20 04:38:14 +00:00
/**
* clean up
*/
Tone.BitCrusher.prototype.dispose = function(){
Tone.Effect.prototype.dispose.call(this);
2014-10-30 22:06:13 +00:00
this._floor.dispose();
this._floor = null;
2014-06-20 04:38:14 +00:00
};
2014-04-06 00:47:59 +00:00
return Tone.BitCrusher;
});