Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio input example? #6

Open
h2o- opened this issue Jun 25, 2016 · 2 comments
Open

Audio input example? #6

h2o- opened this issue Jun 25, 2016 · 2 comments

Comments

@h2o-
Copy link

h2o- commented Jun 25, 2016

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
Copy link
Owner

@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
Copy link

@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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants