-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfader.js
32 lines (25 loc) · 908 Bytes
/
fader.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
let Gain = require('audio-gain');
// const schedule = require('node-schedule');
Gain.prototype.fadeTo = function(targetVol, duration) {
const startVol = this.volume,
startTime = (new Date()).getTime(),
endTime = startTime + duration,
self = this;
console.info(`Fading to ${targetVol}`);
// change volume every 50ms until finished
const interval = 50;
const rampVol = function() {
const timeNow = (new Date()).getTime();
if (timeNow >= endTime) {
self.setVolume(targetVol);
console.info('Finished fading');
return;
}
const newVol = targetVol + ( ( ( endTime - timeNow ) / duration ) * ( startVol - targetVol ) );
self.setVolume(newVol);
// call next volume change
setTimeout(rampVol, interval);
};
rampVol();
};
module.exports = Gain;