-
Notifications
You must be signed in to change notification settings - Fork 965
/
Normalize.js
110 lines (96 loc) · 2.28 KB
/
Normalize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
define(["Tone/core/Tone", "Tone/signal/Add", "Tone/signal/Multiply"], function(Tone){
"use strict";
/**
* @class Normalize takes an input min and max and maps it linearly to NormalRange [0,1]
*
* @extends {Tone.SignalBase}
* @constructor
* @param {number} inputMin the min input value
* @param {number} inputMax the max input value
* @example
* var norm = new Tone.Normalize(2, 4);
* var sig = new Tone.Signal(3).connect(norm);
* //output of norm is 0.5.
*/
Tone.Normalize = function(inputMin, inputMax){
Tone.SignalBase.call(this);
/**
* the min input value
* @type {number}
* @private
*/
this._inputMin = Tone.defaultArg(inputMin, 0);
/**
* the max input value
* @type {number}
* @private
*/
this._inputMax = Tone.defaultArg(inputMax, 1);
/**
* subtract the min from the input
* @type {Tone.Add}
* @private
*/
this._sub = this.input = new Tone.Add(0);
/**
* divide by the difference between the input and output
* @type {Tone.Multiply}
* @private
*/
this._div = this.output = new Tone.Multiply(1);
this._sub.connect(this._div);
this._setRange();
};
Tone.extend(Tone.Normalize, Tone.SignalBase);
/**
* The minimum value the input signal will reach.
* @memberOf Tone.Normalize#
* @type {number}
* @name min
*/
Object.defineProperty(Tone.Normalize.prototype, "min", {
get : function(){
return this._inputMin;
},
set : function(min){
this._inputMin = min;
this._setRange();
}
});
/**
* The maximum value the input signal will reach.
* @memberOf Tone.Normalize#
* @type {number}
* @name max
*/
Object.defineProperty(Tone.Normalize.prototype, "max", {
get : function(){
return this._inputMax;
},
set : function(max){
this._inputMax = max;
this._setRange();
}
});
/**
* set the values
* @private
*/
Tone.Normalize.prototype._setRange = function() {
this._sub.value = -this._inputMin;
this._div.value = 1 / (this._inputMax - this._inputMin);
};
/**
* clean up
* @returns {Tone.Normalize} this
*/
Tone.Normalize.prototype.dispose = function(){
Tone.SignalBase.prototype.dispose.call(this);
this._sub.dispose();
this._sub = null;
this._div.dispose();
this._div = null;
return this;
};
return Tone.Normalize;
});