ALSA: How to tell when a sound is finished playing
Asked Answered
S

1

9

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.

Sumo answered 14/10, 2010 at 18:39 Comment(0)
E
4

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.

Endorse answered 19/10, 2010 at 17:16 Comment(5)
I looked at the PCM status a few days ago and it does not appear to have a "playing" state.Sumo
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.Endorse
how to use snd_pcm_status_get_avail() , there is not proper documentation i am using snd_pcm_status_get_avail(status) but it is giving error pcm.c:6725: snd_pcm_status_get_avail: Assertion `obj' failed.Nagana
@ParagJain I suggest you to use: snd_pcm_status_get_delay(). This gives you a positive number if the playback is going on and zero if playback is complete. Hope this helps.Oxen
@Sumo snd_pcm_state() can be used. It gives the value SND_PCM_STATE_RUNNING if the stream is currently running.Oxen

© 2022 - 2024 — McMap. All rights reserved.