[alsa-devel] XRUN happens too often.

Rong-Jhe r93922118 at ntu.edu.tw
Wed Apr 9 14:06:51 CEST 2008

> I don't have a direct answer for you, but I have an application that seems
> to be able to write data using writei with no problems at rates up to
> 192 KHz.  You can download it from sourceforge and look at the source.
> http://sourceforge.net/projects/discord/ 
> I copied and adapted from other examples on the web.  Do a search for 
> alsa in
> the source code and it should take you to the initialization function 
> and the
> playing function.

Thanks for your help.
I think that XRUN may happen because the time of using snd_pcm_writei().
I have a problem about snd_pcm_write().
If the pcm is block, when does snd_pcm_writei() return?

I try to write the audio data into the dsp file directly without ALSA library.
The data will be written into DMA of the soundcard.
This action of writting is asynchronous, and the write() returns after 
queueing the data intto the DMA.

I guess the problem may be caused by the late time which the second 
snd_pcm_writei() is called.
The time between two snd_pcm_writei() may be too long, so the pcm enters XRUN 

The period time in this pcm is 21333 us, and the buffer time is 213330 us 
because there are 10 periods in a buffer.

I recorded the time timeA that snd_pcm_writei() is called, and the time timeB 
that snd_pcm_writei() returns.
I calculate the duration between timeA and timeB, and the duration is not 

If snd_pcm_writei() is synchronous, the duration may be equal to the buffer 
time( 10xperiod time).
According to the measurement, snd_pcm_writei() seems not to be synchronous.
Can anyone tell me the basic theory of snd_pcm_writei(), especially in dmix 

More information about the Alsa-devel mailing list