[alsa-devel] snd_pcm_wait function

Trent Piepho tpiepho at gmail.com
Tue Nov 13 05:45:10 CET 2012

snd_pcm_drain() will drain all the pending periods.  In effect, it
rounds up however many frames are left in the buffer to the next whole
period.  There is no way to just drain a partially full period.

If you always wrote full periods, and didn't use non-blocking mode so
that each writei call always wrote everything before returning, then
you shouldn't have a partially filled period at the end of the buffer.

ALSA has the exact buffer pointer value of the app pointer and also
when a period should elapse, and could easily tell you the buffer fill
value mod period size, but I don't think there is any API for that.

On Mon, Nov 12, 2012 at 7:15 PM, Daniel Sanz <daniellsanz2 at gmail.com> wrote:
> Can I expect that there won't be any pending frames (so no need to
> call snd_pcm_drain) if I always use snd_pcm_writei to write multiples
> of the period size?
> And what if the sound is very short or the period size is big? the
> latency of hardware is negligible?
> Thanks,
> On Mon, Nov 12, 2012 at 9:57 PM, Trent Piepho <tpiepho at gmail.com> wrote:
>> You do have to fill to a full period before calling drain, or it won't
>> work in a very useful way.  It only stops when periods elapse.  So if
>> you have a half full period of audio at the end of the buffer you'll
>> get garbage when it plays to the end of that period before stopping.
>> On Mon, Nov 12, 2012 at 2:36 AM, Clemens Ladisch <clemens at ladisch.de> wrote:
>>> Enno Fennema wrote:
>>>> I want to close the pcm AFTER the sound finished.
>>> Then close it after snd_pcm_drain() has returned.
>>>> The docs say calling drain() puts the pcm in the DRAINING state.
>>> It also waits until the device has drained (unless the device is in
>>> non-blocking mode).
>>>> When I obtain the state immediately after drain() it is already SETUP.
>>>> I hoped a state change from DRAINING to SETUP  would be the right time
>>>> to close the pcm but there appears to be no such state change.
>>> That change happens immediately before snd_pcm_drain() returns.
>>> Regards,
>>> Clemens
>>> _______________________________________________
>>> Alsa-devel mailing list
>>> Alsa-devel at alsa-project.org
>>> http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

More information about the Alsa-devel mailing list