2017-10-21 23:02:46 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/signal/WaveShaper", "Tone/signal/Multiply", "Tone/signal/Subtract"], function(Tone){
|
2014-09-08 15:49:21 +00:00
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/**
|
2015-06-19 04:52:04 +00:00
|
|
|
* @class Signal-rate modulo operator. Only works in AudioRange [-1, 1] and for modulus
|
2017-10-21 23:02:46 +00:00
|
|
|
* values in the NormalRange.
|
2014-09-08 15:49:21 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2014-12-01 04:26:06 +00:00
|
|
|
* @extends {Tone.SignalBase}
|
2015-06-19 04:52:04 +00:00
|
|
|
* @param {NormalRange} modulus The modulus to apply.
|
2015-02-27 18:00:20 +00:00
|
|
|
* @example
|
2015-06-14 05:17:09 +00:00
|
|
|
* var mod = new Tone.Modulo(0.2)
|
|
|
|
* var sig = new Tone.Signal(0.5).connect(mod);
|
|
|
|
* //mod outputs 0.1
|
2014-09-08 15:49:21 +00:00
|
|
|
*/
|
2015-02-27 18:00:20 +00:00
|
|
|
Tone.Modulo = function(modulus){
|
2014-09-08 15:49:21 +00:00
|
|
|
|
2017-04-26 03:45:37 +00:00
|
|
|
Tone.SignalBase.call(this);
|
2016-09-20 03:02:42 +00:00
|
|
|
this.createInsOuts(1, 0);
|
2014-09-08 15:49:21 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-21 23:02:46 +00:00
|
|
|
* A waveshaper gets the integer multiple of
|
2015-02-27 18:00:20 +00:00
|
|
|
* the input signal and the modulus.
|
2014-09-08 15:49:21 +00:00
|
|
|
* @private
|
2015-02-27 18:00:20 +00:00
|
|
|
* @type {Tone.WaveShaper}
|
2014-09-08 15:49:21 +00:00
|
|
|
*/
|
2015-02-27 18:00:20 +00:00
|
|
|
this._shaper = new Tone.WaveShaper(Math.pow(2, 16));
|
2014-09-12 05:03:48 +00:00
|
|
|
|
2014-09-08 15:49:21 +00:00
|
|
|
/**
|
2015-02-27 18:00:20 +00:00
|
|
|
* the integer multiple is multiplied by the modulus
|
|
|
|
* @type {Tone.Multiply}
|
2014-09-08 15:49:21 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2015-02-27 18:00:20 +00:00
|
|
|
this._multiply = new Tone.Multiply();
|
2014-09-08 15:49:21 +00:00
|
|
|
|
2014-11-04 00:22:17 +00:00
|
|
|
/**
|
2015-02-27 18:00:20 +00:00
|
|
|
* and subtracted from the input signal
|
|
|
|
* @type {Tone.Subtract}
|
2014-11-04 00:22:17 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2015-02-27 18:00:20 +00:00
|
|
|
this._subtract = this.output = new Tone.Subtract();
|
2014-11-04 00:22:17 +00:00
|
|
|
|
2014-09-08 15:49:21 +00:00
|
|
|
/**
|
2015-02-27 18:00:20 +00:00
|
|
|
* the modulus signal
|
|
|
|
* @type {Tone.Signal}
|
2014-09-08 15:49:21 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2015-02-27 18:00:20 +00:00
|
|
|
this._modSignal = new Tone.Signal(modulus);
|
|
|
|
|
|
|
|
//connections
|
|
|
|
this.input.fan(this._shaper, this._subtract);
|
|
|
|
this._modSignal.connect(this._multiply, 0, 0);
|
|
|
|
this._shaper.connect(this._multiply, 0, 1);
|
|
|
|
this._multiply.connect(this._subtract, 0, 1);
|
|
|
|
this._setWaveShaper(modulus);
|
2014-09-08 15:49:21 +00:00
|
|
|
};
|
|
|
|
|
2015-02-27 18:00:20 +00:00
|
|
|
Tone.extend(Tone.Modulo, Tone.SignalBase);
|
2014-09-08 15:49:21 +00:00
|
|
|
|
2014-09-12 04:55:10 +00:00
|
|
|
/**
|
2015-02-27 18:00:20 +00:00
|
|
|
* @param {number} mod the modulus to apply
|
2014-09-12 04:55:10 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2015-02-27 18:00:20 +00:00
|
|
|
Tone.Modulo.prototype._setWaveShaper = function(mod){
|
|
|
|
this._shaper.setMap(function(val){
|
|
|
|
var multiple = Math.floor((val + 0.0001) / mod);
|
|
|
|
return multiple;
|
|
|
|
});
|
2014-09-12 04:55:10 +00:00
|
|
|
};
|
|
|
|
|
2015-02-27 18:00:20 +00:00
|
|
|
/**
|
|
|
|
* The modulus value.
|
|
|
|
* @memberOf Tone.Modulo#
|
2015-06-19 04:52:04 +00:00
|
|
|
* @type {NormalRange}
|
2015-02-27 18:00:20 +00:00
|
|
|
* @name value
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Tone.Modulo.prototype, "value", {
|
|
|
|
get : function(){
|
|
|
|
return this._modSignal.value;
|
|
|
|
},
|
|
|
|
set : function(mod){
|
|
|
|
this._modSignal.value = mod;
|
|
|
|
this._setWaveShaper(mod);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clean up
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.Modulo} this
|
2015-02-27 18:00:20 +00:00
|
|
|
*/
|
|
|
|
Tone.Modulo.prototype.dispose = function(){
|
2017-04-26 03:45:37 +00:00
|
|
|
Tone.SignalBase.prototype.dispose.call(this);
|
2015-02-27 18:00:20 +00:00
|
|
|
this._shaper.dispose();
|
|
|
|
this._shaper = null;
|
|
|
|
this._multiply.dispose();
|
|
|
|
this._multiply = null;
|
|
|
|
this._subtract.dispose();
|
|
|
|
this._subtract = null;
|
|
|
|
this._modSignal.dispose();
|
|
|
|
this._modSignal = null;
|
|
|
|
return this;
|
2014-09-08 15:49:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return Tone.Modulo;
|
2017-10-21 23:02:46 +00:00
|
|
|
});
|