underrun problems after setting parameters with snd_pcm_set_params()

Kai Vehmanen kai.vehmanen at linux.intel.com
Tue Apr 28 17:52:02 CEST 2020


Hey,


On Tue, 28 Apr 2020, robert rozee wrote:

> the problem only occurs with SHORT sound clips. i found that clips longer than 
> 1111ms played without flaw. clips less that 500ms generally failed to play to 
> the end, but there was no error code returned.

in general, given you have sources available to all the popular apps, it's 
good to check how other apps use the API. I.e. aplay.c may be useful 
simple additional reference for you to see how to use the interfaces.

I think you are missing draining the samples at the end, and then 
your latency is setting is incorrect. I.e.

> snd_pcm_set_params(handle, SND_PCM_FORMAT_U8,
>                            SND_PCM_ACCESS_RW_INTERLEAVED,
>                            1,                           // number of channels
>                            48000,                       // bitrate (bps)
>                            1,                           // resampling on/off
>                            min(500000, sample_length)); // latency

That sample_length does not look, the unit is usecs here. Please try
just putting latency of 500000 (i.e. 0.5sec).

> // send out current buffer content
>   frames=snd_pcm_writei(handle, @buffer, frames_to_write_now);
>   if frames<0 then frames=snd_pcm_recover(handle, frames, 0);
>   if frames<0 then break;
> }

When you have finished writing the audio samples to the ALSA device, you 
need to wait until ALSA has a chance to play all samples out. If you look 
at playback_go() in aplay.c, you'll see:

       snd_pcm_nonblock(handle, 0);
       snd_pcm_drain(handle);
       snd_pcm_nonblock(handle, nonblock);

... at the end. 

Br, Kai


More information about the Alsa-devel mailing list