-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
161 lines (135 loc) · 5.85 KB
/
index.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* eslint-disable no-prototype-builtins */
'use strict';
module.exports = function ( context, gainNode, _globalOptions ) {
_globalOptions = _globalOptions || {};
if ( !gainNode || !context ) {
throw new Error( 'gainNode and context arguments cannot be null' );
}
_globalOptions.startValue = _globalOptions.hasOwnProperty( 'startValue' ) ? _globalOptions.startValue : gainNode.gain.value;
_globalOptions.type = _globalOptions.hasOwnProperty( 'type' ) ? _globalOptions.type : 'exponential';
_globalOptions.fadeLength = _globalOptions.hasOwnProperty( 'fadeLength' ) ? _globalOptions.fadeLength : 10;
var ALMOST_ZERO = 0.00001;
var _currentStartValue = 0;
var _currentTargetValue = _globalOptions.startValue;
var _currentStartTime = 0;
var _currentEndTime = context.currentTime;
var _currDirection = '';
var _debug = !!_globalOptions.debug;
function isFading( time ) {
return _currentEndTime > time;
}
function cauculateInterpolationAt( time ) {
if ( _debug ) {
console.log( 'calculating interpolation' );
}
if ( time <= _currentStartTime ) {
return _currentStartValue;
} else if ( time >= _currentEndTime ) {
return _currentTargetValue;
} else {
if ( _globalOptions.type === 'linear' ) {
return _currentStartValue + ( _currentTargetValue - _currentStartValue ) * ( ( time - _currentStartTime ) / ( _currentEndTime - _currentStartTime ) );
} else if ( _globalOptions.type === 'exponential' ) {
var exponent = ( ( time - _currentStartTime ) / ( _currentEndTime - _currentStartTime ) );
return _currentStartValue * Math.pow( ( _currentTargetValue / _currentStartValue ), exponent );
}
}
}
function calculateEndTime( startTime, targetValue ) {
if ( !isFading() ) {
return startTime + _globalOptions.fadeLength;
} else if ( targetValue === _currentStartValue ) {
var timeTillNow = ( context.currentTime - _currentStartTime );
if ( _debug ) {
console.log( 'end time will be now +', timeTillNow );
}
return startTime + timeTillNow;
} else {
var startValue = cauculateInterpolationAt( startTime );
var timeTaken = 0;
if ( _globalOptions.type === 'linear' ) {
var gradient = _globalOptions.fadeLength / ( ALMOST_ZERO - 1 );
timeTaken = ( ( targetValue - startValue ) * gradient );
if ( _debug ) {
console.log( 'Time taken to go linearly from ', startValue, '-', targetValue, ' is ', timeTaken );
}
} else if ( _globalOptions.type === 'exponential' ) {
var diff = Math.log( targetValue ) - Math.log( startValue );
timeTaken = ( 10 / Math.log( ALMOST_ZERO ) * diff );
if ( _debug ) {
console.log( 'Time taken to go expoentially from ', startValue, '-', targetValue, ' is ', timeTaken );
}
}
return startTime + timeTaken;
}
}
return {
valueAt: function ( time ) {
if ( !time ) {
time = context.currentTime;
}
if ( !isFading( time ) ) {
return _currentTargetValue;
} else {
return cauculateInterpolationAt( time );
}
},
fadeIn: function ( _options ) {
if ( _currDirection === 'fadein' ) {
return;
}
_options = _options || {};
_options.startTime = _options.hasOwnProperty( 'startTime' ) ? _options.startTime : context.currentTime;
_options.targetValue = _options.hasOwnProperty( 'targetValue' ) ? _options.targetValue : 1;
if ( !_options.hasOwnProperty( 'endTime' ) ) {
_options.endTime = calculateEndTime( _options.startTime, _options.targetValue );
}
var startvalue = cauculateInterpolationAt( _options.startTime );
if ( _debug ) {
console.log( 'Start value', startvalue );
}
gainNode.gain.cancelScheduledValues( _options.startTime );
gainNode.gain.setValueAtTime( startvalue, _options.startTime );
if ( _globalOptions.type === 'linear' ) {
gainNode.gain.linearRampToValueAtTime( _options.targetValue, _options.endTime );
} else if ( _globalOptions.type === 'exponential' ) {
gainNode.gain.exponentialRampToValueAtTime( _options.targetValue, _options.endTime );
}
_currentStartValue = startvalue;
_currentTargetValue = _options.targetValue;
_currentStartTime = _options.startTime;
_currentEndTime = _options.endTime;
_currDirection = 'fadein';
},
fadeOut: function ( _options ) {
if ( _currDirection === 'fadeout' ) {
return;
}
_options = _options || {};
_options.startTime = _options.hasOwnProperty( 'startTime' ) ? _options.startTime : context.currentTime;
_options.targetValue = _options.hasOwnProperty( 'targetValue' ) ? _options.targetValue : ALMOST_ZERO;
if ( !_options.hasOwnProperty( 'endTime' ) ) {
_options.endTime = calculateEndTime( _options.startTime, _options.targetValue );
}
var startvalue = cauculateInterpolationAt( _options.startTime );
if ( _debug ) {
console.log( 'Start value', startvalue );
}
gainNode.gain.cancelScheduledValues( _options.startTime );
gainNode.gain.setValueAtTime( startvalue, _options.startTime );
if ( _globalOptions.type === 'linear' ) {
gainNode.gain.linearRampToValueAtTime( _options.targetValue, _options.endTime );
} else if ( _globalOptions.type === 'exponential' ) {
gainNode.gain.exponentialRampToValueAtTime( _options.targetValue, _options.endTime );
}
if ( _debug ) {
console.log( context.currentTime, ':: Fading out to ', _options.targetValue, 'starting from', _options.startTime, 'to', _options.endTime );
}
_currentStartValue = startvalue;
_currentTargetValue = _options.targetValue;
_currentStartTime = _options.startTime;
_currentEndTime = _options.endTime;
_currDirection = 'fadeout';
}
};
};