tags:

views:

53

answers:

1

I have a c++ object that accepts sound requests and plays them with ALSA. There is thread that processes the sound requests. Some sounds are periodic and are rescheduled after the wav file contents have been written to the ALSA library. Is there a way I can find out when all the data has been played? The function snd_pcm_writei is a blocking write function, but it does not necessarily mean that the file has been played.

One option that I am considering is to call snd_pcm_drain after playing each sound file, then call snd_pcm_prepare when I play the next file. Would this be an good solution? Or is this inefficient?

Update: The "drain solution" seems to work, but is not very efficient. The calls takes a while to return (maybe it cleans up some resources) and adds latency to the program. The latency is seen best when I play many small files consecutively. A few seconds of silence can be heard between each file; this is snd_pcm_drain executing.

+1  A: 

Might not be correct (i've done very little work in this area), but from looking at the ALSA docs here: http://www.alsa-project.org/alsa-doc/alsa-lib/pcm.html

It looks like snd_pcm_status_t holds the status information that should give you an indication of whether the stream is currently processing data or not.

paulw1128
I looked at the PCM status a few days ago and it does not appear to have a "playing" state.
waffleman
Again - not having used it I don't know for sure, but I would've thought that `available count in samples - snd_pcm_status_get_avail()` might give you a value that will indicate whether there's anything left to play. I'd recommend throwing together some simple test cases and looking at how the various values behave.
paulw1128