2014-10-30 22:06:13 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/effect/Effect", "Tone/signal/Expr"],
|
2014-09-08 15:54:03 +00:00
|
|
|
function(Tone){
|
2014-04-06 00:47:59 +00:00
|
|
|
|
2014-09-04 04:41:40 +00:00
|
|
|
"use strict";
|
|
|
|
|
2014-06-17 16:30:45 +00:00
|
|
|
/**
|
2014-09-05 15:32:33 +00:00
|
|
|
* @class downsample incoming signal.
|
2014-09-08 15:54:03 +00:00
|
|
|
*
|
|
|
|
* 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}
|
2014-09-08 15:54:03 +00:00
|
|
|
* @param {number} bits 1-8.
|
2014-06-17 16:30:45 +00:00
|
|
|
*/
|
2014-08-25 14:23:37 +00:00
|
|
|
Tone.BitCrusher = function(){
|
2014-06-17 16:30:45 +00:00
|
|
|
|
2014-09-08 15:54:03 +00:00
|
|
|
var options = this.optionsObject(arguments, ["bits"], Tone.BitCrusher.defaults);
|
2014-08-25 14:23:37 +00:00
|
|
|
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-09-08 15:54:03 +00:00
|
|
|
/**
|
2014-10-30 22:06:13 +00:00
|
|
|
* floor function
|
|
|
|
* @type {Tone.Expr}
|
2014-09-08 15:54:03 +00:00
|
|
|
* @private
|
2014-06-18 21:39:05 +00:00
|
|
|
*/
|
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
|
|
|
|
2014-08-25 14:23:37 +00:00
|
|
|
/**
|
|
|
|
* the default values
|
|
|
|
* @static
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
Tone.BitCrusher.defaults = {
|
2014-09-08 15:54:03 +00:00
|
|
|
"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(){
|
2014-08-23 19:51:21 +00:00
|
|
|
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;
|
|
|
|
});
|