问题
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