本文介绍了期间在ALSA的意义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ALSA并在Linux上的音频应用程序,我发现伟大的文档解释了如何使用它:的和。虽然我有一些问题了解安装的这一部分:

I'm using ALSA for and audio application on linux, I found great docs explain how to use it : 1 and this one. although I have some issues to understand this part of the setup :

 /* Set number of periods. Periods used to be called fragments. */
if (snd_pcm_hw_params_set_periods(pcm_handle, hwparams, periods, 0) < 0) {
  fprintf(stderr, "Error setting periods.\n");
  return(-1);
}

这是什么意思设置了一些时间,当我使用的播放模式
和:

what does mean set a number of period when I'm using the PLAYBACK modeand :

/* 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);
}

和这里有关延迟同样的问题,我应该怎么理解呢?
在此先感谢您的帮助!

and the same question here about the latency , how should I understand it ?thanks in advance for any help !

推荐答案

我假设你已经阅读并理解的的这个部分。你也可能会发现澄清相对于期大小选择的东西在ALSA的上下文中(或在博客片段)。引用:

I assume you've read and understood this section of linux-journal. You may also find that this blog clarify things with respect to period size selection (or fragment in the blog) in the context of ALSA. To quote:

您不要滥用声音设备的碎片逻辑。就像是
  这样的:

的延迟由缓冲器大小限定。结果
       唤醒间隔由片段大小来定义。

该缓冲器填充水平将全缓冲和全之间振荡
  缓冲减去1个片段大小减去OS调度延迟。设置
  更小的片段大小会增加CPU的负荷,减少电池
  时间,因为你使CPU更经常醒来。 OTOH它增加
  辍学的安全,因为你前面填写的播放缓冲区。选择
  片段大小,因此一些东西,你应该做的出来平衡
  功耗和辍学安全性之间您的需求。随着现代
  处理器和良好的OS调度器,如Linux的一个设置的
  片段大小以外的任何超过一半的缓冲区大小不
  太大的意义。

The buffer fill level will oscillate between 'full buffer' and 'full buffer minus 1x fragment size minus OS scheduling latency'. Setting smaller fragment sizes will increase the CPU load and decrease battery time since you force the CPU to wake up more often. OTOH it increases drop out safety, since you fill up playback buffer earlier. Choosing the fragment size is hence something which you should do balancing out your needs between power consumption and drop-out safety. With modern processors and a good OS scheduler like the Linux one setting the fragment size to anything other than half the buffer size does not make much sense.

...
  (呵呵,ALSA使用的术语是时期什么我称之为片段
  以上。它的同义)

... (Oh, ALSA uses the term 'period' for what I call 'fragment' above. It's synonymous)

所以基本上,通常你会设置周期 2(因为是在的你引用)。然后 periodsize *期是以字节为单位的总缓冲区大小。最后,是由许多样本的缓冲引起的延迟,并且可以通过在该样品回放(速率除以缓冲器尺寸被计算,即,根据下式等待时间= periodsize *周期/(速率* bytes_per_frame)在code注释)。

So essentially, typically you would set period to 2 (as was done in the howto you referenced). Then periodsize * period is your total buffer size in bytes. Finally, the latency is the delay that is induced by the buffering of that many samples, and can be computed by dividing the buffer size by the rate at which samples are played back (ie. according to the formula latency = periodsize * periods / (rate * bytes_per_frame) in the code comments).

例如,从参数:


  • 周期= 2

  • periodsize = 8192字节

  • 率= 44100Hz

  • 16位立体声数据(每帧4个字节)

对应于期间* periodsize = 2 * 8192 = 16384 字节的总缓冲器大小,和16384 /(44100 * 4)一个延迟〜0.093`秒。

correspond to a total buffer size of period * periodsize = 2 * 8192 = 16384 bytes, and a latency of 16384 / (44100 * 4) ~ 0.093` seconds.

另请注意,你的硬件可能对支持的时期一定规模的大小限制(见此故障排除指南

Note also that your hardware may have some size limitations for the supported period size (see this trouble shooting guide)

这篇关于期间在ALSA的意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 16:29