-
Notifications
You must be signed in to change notification settings - Fork 209
/
kick.js
70 lines (63 loc) · 2.01 KB
/
kick.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
(function ( undefined ) {
var Kick = function ( dancer, o ) {
o = o || {};
this.dancer = dancer;
this.frequency = o.frequency !== undefined ? o.frequency : [ 0, 10 ];
this.threshold = o.threshold !== undefined ? o.threshold : 0.3;
this.decay = o.decay !== undefined ? o.decay : 0.02;
this.onKick = o.onKick;
this.offKick = o.offKick;
this.isOn = false;
this.currentThreshold = this.threshold;
var _this = this;
this.dancer.bind( 'update', function () {
_this.onUpdate();
});
};
Kick.prototype = {
on : function () {
this.isOn = true;
return this;
},
off : function () {
this.isOn = false;
return this;
},
set : function ( o ) {
o = o || {};
this.frequency = o.frequency !== undefined ? o.frequency : this.frequency;
this.threshold = o.threshold !== undefined ? o.threshold : this.threshold;
this.decay = o.decay !== undefined ? o.decay : this.decay;
this.onKick = o.onKick || this.onKick;
this.offKick = o.offKick || this.offKick;
},
onUpdate : function () {
if ( !this.isOn ) { return; }
var magnitude = this.maxAmplitude( this.frequency );
if ( magnitude >= this.currentThreshold &&
magnitude >= this.threshold ) {
this.currentThreshold = magnitude;
this.onKick && this.onKick.call( this.dancer, magnitude );
} else {
this.offKick && this.offKick.call( this.dancer, magnitude );
this.currentThreshold -= this.decay;
}
},
maxAmplitude : function ( frequency ) {
var
max = 0,
fft = this.dancer.getSpectrum();
// Sloppy array check
if ( !frequency.length ) {
return frequency < fft.length ?
fft[ ~~frequency ] :
null;
}
for ( var i = frequency[ 0 ], l = frequency[ 1 ]; i <= l; i++ ) {
if ( fft[ i ] > max ) { max = fft[ i ]; }
}
return max;
}
};
window.Dancer.Kick = Kick;
})();