[alsa-devel] My understanding of ALSA playback mechanism

Yu Hang hangy at fortemedia.com.cn
Fri Apr 30 10:53:32 CEST 2010

Hi, I'm a newbie of ALSA.

I've wrote some audio applications under Windows OS with DirectSound and 
Waveform Audio API. And I also have some knowledge about Sound 
Architecture of Windows Vista/Windows 7. All my experience about audio 
architecture was from Windows.
I'm currently working on a ALSA plugin for AEC function. What I have to 
do is to retrieve audio data being played (after mixing) as reference to 
cancel the echo captured by MIC.
According to my previous experience about Sound Architecture of 
Win7/Vista, the audio data is sending from OS component to my own 
component: no matter for capture or playback. All I have to do is just 
simply modify the input data and put them into output buffer. If my 
component is under OS mixer, the received audio data is after mixing.
When I read source code of ALSA lib, I found the mechanism between 
Windows and ALSA lib are quite different.

Following was my understanding about ALSA lib mechanism during playback.

There is a hardware buffer. ALSA driver takes data from the buffer and 
sends it to HW. The driver also update the play position on the fly.
dmix receive the data from application and mix them into a shared 
memory, and then copy it into hardware buffer. Each of dmix instance 
keep a pointer to indicate current mixing position which might different 
from other instance.

[play pos]    [1st mixed data]
      |               |
+----------------------------+  hardware buffer
                     [2nd mixed data]

The figure shows, the data mixed from 1st dmix instance lasts to [1st 
mixed data] pos, and data mixed from 2nd dmix instance lasts to [2nd 
mixed data]. The buffer between [1st mixed data] and [2nd mixed data] 
contains only data of 2nd dmix instance, but it does not matter, ALSA 
just guarantees the data of 1st dmix instance can be mixed up before the 
[play pos] reaches [1st mixed data] pos.

Of course the hardware buffer is not unlimited (it's a ring buffer), so 
before dmix mix data to hardware buffer, it has to check if there is 
enough space.

Is my understanding of ALSA playback mechanism correct?

More information about the Alsa-devel mailing list