How do you sync JavaScript animations with the tempo of a song, without building an “audio visualizer”?

前端 未结 2 1394
被撕碎了的回忆
被撕碎了的回忆 2021-02-10 16:46

From my basic understanding, JavaScript audio visualizers are reflecting the music based on the actual sound waves. I would like to build something like a metronome (http://bl.

2条回答
  •  -上瘾入骨i
    2021-02-10 17:08

    This post might be relevant:

    • Is there a more accurate way to create a Javascript timer than setTimeout?

    The gist is that you run a function in your setInterval() slightly faster than your tempo, for example, every 100ms. Long example short, you can track whether or not it's time to play a "beat" by checking the value of (new Date()).getMilliseconds() and seeing if the equivalent of one beat in milliseconds has passed instead of relying on the not-so-accurate setTimeout or setInterval functions.

    Even with that, music itself, unless generated by a computer, might not have perfect or consistent tempo, so accounting for mistimed beats could be a hurdle for you, which may be why using audio analysis to find where the actual beats are going to happen could be a better route.

提交回复
热议问题