Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ALSA: How to tell when a sound is finished playing

Tags:

c++

alsa

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.

like image 473
waffleman Avatar asked Oct 14 '10 18:10

waffleman


1 Answers

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.

like image 113
paulw1128 Avatar answered Sep 30 '22 16:09

paulw1128