Detecting bpm of audio input using javascript?

谁都会走 提交于 2021-01-29 07:20:13

问题


I'm trying to put together an audio input + beatdetektor example so I could calculate the BPM of a track playing through my line in ( or even microphone input ).

Unfortunately, I didn't understand exactly how the "time" parameter from the "process" method is supposed to be used ( specially in that case, where I don't necessarily know when the track changes, etc ).

Here is some code I put together trying to solve this puzzle using the lovely p5.js library, please let me know how I could tweak this to achieve the desired effect.

<script src="js/p5-zip/p5.js"></script>
<script src="js/p5-zip/addons/p5.sound.js"></script>

<script src="js/beatdetektor-master/beatdetektor.js"></script>

<script>

  // beatdetektor stuff

  bd_med = new BeatDetektor(85,169);

  vu = new BeatDetektor.modules.vis.VU();
  kick_det = new BeatDetektor.modules.vis.BassKick();

  // p5 stuff
  mic = new p5.AudioIn();
  mic.start();
  fft = new p5.FFT();
  fft.setInput(mic);

  funk = function(){
    spectrum = fft.analyze();

    bd_med.process( (new Date).getTime(), spectrum)
  }

  // analyse with 60 frames, we could maybe use requestAnimationFrame here
  setInterval( funk, 1000/60 )

</script>

回答1:


According to the documentation on the BeatDetektor library, the process() function takes an argument in seconds.

My guess would be that the argument is the seconds since the start of the song, not an absolute date. Right now you're passing in the number of milliseconds since 1970. I don't know of any songs that are that long.

Here is an example of using the process() function to simulate 30 seconds of a song.

It looks like you're going to have to keep track of the start time of the song, then calculate how much time has passed when calling the process() function. Processing has a handy millis() function that might help you out.



来源:https://stackoverflow.com/questions/38029623/detecting-bpm-of-audio-input-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!