Find next beats sample

Hi guys!
I’m still kinda new to JUCE and C++.
I’m looking for a way to find out if one of the samples in the audio buffer is the start of a beat (quarter note)
What would be the best way to do this?
I tried using an AudioPlayHead but I figure out how to do it.
I also tried using MidiMessage::isQuarterFrame() but somehow I’m too dumb to iterate the midiBuffer.

usually you can calculate the number of samples per beat as:

double samples_per_single_beat = ((60.0 / current_bpm) * current_samplerate);

Then you can use this value to get the rest of the information – if you want samples per 1 bar multiply it by 4 etc.

You can then check a counter in your processing to determine if the sample you’re on is a beat - I believe audio playhead has a current sample in timeline or something so in your for loop you can start a count like:

if ((daw_sample + sample_index) % samples_per_single_beat == 0) { then this sample is on a beat } 

Thanks for your reply.
I had this idea first, but I thought the inaccuracy of the double might cause the values to get inaccurate on longer projects.
If for example, the speed is something like 133 the samples_per_beat will be rounded to 0.45112781954.
My fear was the inaccuracy will add up after a while and cause the start of the beat to slowly drift away.
Do you think that matters in reality?

It’s rounding somewhere! Just the nature of samples — you’ll have to test that : )

here’s a link to some code where i interpreted ppq and bpm data to make a temposync LFO. it’s a bit more than what you’re asking for so sry if this is not the best example, but you can see the basics in it as well. basically you get the ppq at each start of the block and the bpm as well, then you calculate the rest of the curve with that information for the given amount of samples. you see there terms like “phasor” because i implemented this as a phasor (saw wave oscillating between 0 and 1), because then that moment where it jumps from 1 to 0 is where one measure (defined by the rate parameter) has finished, like 1/16th or 2bars etc.

ignore everything that says “latency” in that code. that’s just needed for the latency compensation of the whole plugin. also ignore all this “shifted” and “syncer” stuff. that is just used to put an offset to the phase value and to enable smooth rate changes but it makes the code look more complicated.

1 Like