Now, if the hardware has been set to 48000Hz , 2 periods, of 1024 frames each, making a buffer size of 2048 frames. The hardware will interrupt 2 times per buffer. ALSA will endeavor to keep the buffer as full as possible. Once the first period of samples has been played, the third period of samples is transfered into the space the first one occupied while the second period of samples is being played. (normal ring buffer behaviour).

Now, if the hardware has been set to 48000Hz , 2 periods, of 1024 frames each, making a buffer size of 2048 frames. The hardware will interrupt 2 times per buffer. ALSA will endeavor to keep the buffer as full as possible. Once the first period of samples has been played, the third period of samples is transfered into the space the first one occupied while the second period of samples is being played. (normal ring buffer behaviour).

+

+

&nbsp;

+

+

== Additional example ==

+

+

Here is an alternative example for the above discussion.

+

+

Say we want to work with a stereo, 16-bit, 44.1 KHz stream, ''one-way'' (meaning, ''either'' in playback ''or'' in capture direction). Then we have:

It seems ([http://mirror.leaseweb.com/kernel/people/tiwai/docs/writing-an-alsa-driver.pdf writing-an-alsa-driver.pdf]), however, that it is the ALSA runtime that decides on the actual ''buffer_size'' and ''period_size'', depending on: the requested number of channels, and their respective properties (rate and sampling resolution) - as well as the parameters set in the ''snd_pcm_hardware'' structure (in the driver).

+

+

----

----

Revision as of 23:29, 4 November 2010

A frame is equivalent of one sample being played, irrespective of the number of channels or the number of bits. e.g.

1 frame of a Stereo 48khz 16bit PCM stream is 4 bytes.

1 frame of a 5.1 48khz 16bit PCM stream is 12 bytes.

A period is the number of frames in between each hardware interrupt. The poll() will return once a period.

The buffer is a ring buffer. The buffer size always has to be greater than one period size. Commonly this is 2*period size, but some hardware can do 8 periods per buffer. It is also possible for the buffer size to not be an integer multiple of the period size.

Now, if the hardware has been set to 48000Hz , 2 periods, of 1024 frames each, making a buffer size of 2048 frames. The hardware will interrupt 2 times per buffer. ALSA will endeavor to keep the buffer as full as possible. Once the first period of samples has been played, the third period of samples is transfered into the space the first one occupied while the second period of samples is being played. (normal ring buffer behaviour).

Additional example

Here is an alternative example for the above discussion.

Say we want to work with a stereo, 16-bit, 44.1 KHz stream, one-way (meaning, either in playback or in capture direction). Then we have:

'stereo' = number of channels: 2

1 analog sample is represented with 16 bits = 2 bytes

1 frame represents 1 analog sample from all channels; here we have 2 channels, and so:

It seems (writing-an-alsa-driver.pdf), however, that it is the ALSA runtime that decides on the actual buffer_size and period_size, depending on: the requested number of channels, and their respective properties (rate and sampling resolution) - as well as the parameters set in the snd_pcm_hardware structure (in the driver).