[LAD] writing a bpm counter with (or without) ALSA

Arnold Krille arnold at arnoldarts.de
Sat Sep 25 23:21:49 UTC 2010


On Sunday 26 September 2010 01:08:21 Max Payne wrote:
> 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!

Why do you part a task that belongs together?

The most trusted clock to relate samples to real world time (which you want 
for your beats/bars) is the sampling-clock. Which you get from the alsa-
thread. Why even bother to do the time-keeping in a different thread? Do all 
your audio-stuff in one thread.

You should use a different thread when disk is accessed. Or when you do long 
running math that you only need to be finished for the audio-block after the 
current (or even later). And you would have a different thread for the gui 
stuff. Most probably this thread is called the main-thread:-)

Have fun,

Arnold
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.linuxaudio.org/pipermail/linux-audio-dev/attachments/20100926/196c7b66/attachment.pgp>


More information about the Linux-audio-dev mailing list