How long does it take for snd_pcm_writei() (ALSA) to send data?
Please correct me if I'm wrong.

snd_pcm_hw_params_get_period_size() gives the number of times per second a chunk of data is sent, i.e. in Hz.

snd_pcm_hw_params_get_period_time() gives the number of microseconds to send such a chunk.

E.g. for my example, it takes in reality around 10 times per second, and these calls return to me 4410 Hz and 10000 usec, respectively. Hence, this appears correct.

However, it still eludes me how the buffer size plays into this. If anyone who happens to be experienced with ALSA would like to add that, that would be awesome.

Edit:

I believe it works like this:

In the same example, 4410 frames will be required for each period. This makes it 8820 samples. If you're using S16_LE, i.e. short data types, then each frame is 2 byte, hence the buffer should be 17640 bytes.

So, that is the buffer which is treated during each period.

However, there is another important concept in ALSA, the hardware buffer, which can be quite large and introduce a latency when copying it---Hence why ALSA uses the concept of periods at all.

Reference: https://www.linuxjournal.com/article/6735


  " The size of the buffer can be programmed by ALSA library calls. The
  buffer can be quite large, and transferring it in one operation could
  result in unacceptable delays, called latency. To solve this, ALSA
  splits the buffer up into a series of periods (called fragments in
  OSS/Free) and transfers the data in units of a period.
  
  A period stores frames, each of which contains the samples captured at
  one point in time. For a stereo device, the frame would contain
  samples for two channels. Figure 1 illustrates the breakdown of a
  buffer into periods, frames and samples with some hypothetical values.
  Here, left and right channel information is stored alternately within
  a frame; this is called interleaved mode. A non-interleaved mode,
  where all the sample data for one channel is stored followed by the
  data for the next channel, also is supported. "