首页 > 解决方案 > Linux sound programming. How to determine a buffer size in frames?

问题描述

I'm experimenting with ALSA and came across with the following configuration parameter in this howto, Section 2:

The unit of the buffersize depends on the function. Sometimes it is given in bytes, sometimes the number of frames has to be specified. One frame is the sample data vector for all channels. For 16 Bit stereo data, one frame has a length of four bytes.

/* Set buffer size (in frames). The resulting latency is given by */
/* latency = periodsize * periods / (rate * bytes_per_frame)     */
if (snd_pcm_hw_params_set_buffer_size(pcm_handle, hwparams, (periodsize * periods)>>2) < 0) {
  fprintf(stderr, "Error setting buffersize.\n");
  return(-1);
}

I don't understand this For 16 Bit stereo data, one frame has a length of four bytes

Why is it four? Does it follow by the number of channels: 2? I mean earlier they configured it as follows:

/* Set number of channels */
if (snd_pcm_hw_params_set_channels(pcm_handle, hwparams, 2) < 0) {
  fprintf(stderr, "Error setting channels.\n");
  return(-1);
}

How about if my acoustic system contains 4 outputs? Or 6? Does it mean that I have to configre it to 16 Bit * 4 and 16 Bit * 6 correspondingly?

标签: clinuxalsa

解决方案


Why is it four? Does it follow by the number of channels: 2?

Yes, according to mentioned earlier:

One frame is the sample data vector for all channels.

So for stereo 16 bit data, there are two (left and right) channels of 16 bits (=2 bytes) each, so that totals to 4 bytes per frame.


推荐阅读