cjcliffe / beatdetektor

BeatDetektor BPM detection / visualization library
http://www.beatdetektor.com
324 stars 54 forks source link

Audio input example? #6

Open h2o- opened 8 years ago

h2o- commented 8 years ago

Hello i'm trying to put together an audio input 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, etcs ).

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.

Thank you for such amazing project,

<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>
cjcliffe commented 8 years ago

@h2o- sorry didn't catch this issue til now -- the time is just the time in seconds (floating point) for the current spectrum frame.

For the microphone you'd probably want to use the time since recording started (sample_time - start_time) and for an audio track you can use the current playback time for the given sample. If you move the time backwards just remember to call reset() first, and starting at 0 is not necessary.

There's no built-in track change detection but it will follow tempo changes -- though it follows more slowly when it's recently had a solid BPM match and tempo changes abruptly.

If you know you're going to want to reset detection (such as a playlist change event) just use the bd_med.reset() function to reset state and start again.

rickhurkens commented 2 years ago

@h2o @cjcliffe I have finished h2o'2 code from above and committed it to a new repository: https://github.com/rickhurkens/beatdetektor_from_audioIn

Thank you people for your code and examples. This has saved me a lot of work :)