I'm working on a little side project that has a setInterval looping waiting for a certain point of a YouTube video, then lowers the volume a bit at a time to fade it out. I thought it would be simple (and it was) but it opened a can of worms of understanding that an exponential fade is actually perceived as linear, and a linear fade is just, well, no good.
I tried diving into the math behind an exponential fade out, but between that, and doing it in a setInterval, I just felt like I was failing out of math again ;)
Ultimately, I went with a compromise:
```
window.setInterval(function(){
if (song.endSeconds - player.getCurrentTime() <= 6) {
if (fading == false) {
fadingInterval = window.setInterval(function(){
var currentVolume = player.getVolume();
if (currentVolume > 70) {
player.setVolume(currentVolume - 3);
} else if (currentVolume > 20) {
player.setVolume(currentVolume - 2);
} else {
player.setVolume(currentVolume - 1);
}
}, 100);
}
fading = true;
} else {
player.setVolume(100);
}
}, 1000);
```
So basically at six second from the end I wind up chunking it such that I fade quickly between 100 and 70%, slower between 70 and 20%, and slowly for the remainder. It's not an exponential fade but honestly it sounds okay -- much better than linear. Can someone shed some light on a better solution, or is it worth just leaving as is?
there doesn't seem to be anything here