Hi guys,
first of all forgive my not-so-perfect English :-)
I'm writing down some code for a minimal loop player based on two threads: one handles a beat counter, the other feeds the soundcard with audio frames, through ALSA. When the beat counter has completed a full cycle (e.g. 4/4) it simply rewinds the PCM data to byte 0 making a seamless loop. Really straightforward.
Now, I'm wondering how to implement the metronome side: should I rely on something like usleep/nanosleep or ALSA layer could offer an advanced timer? Another potential issue would come from latency, obviously present within the audio thread (due to ALSA): what happens when the beat counter restarts the audio sample but an alsa frame is still being written to the soundcard?
Thank you in advance for any suggestion!

Tb