ALSA中句号的含义 [英] The meaning of period in ALSA

查看:43
本文介绍了ALSA中句号的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Linux 上使用 ALSA 和音频应用程序,我发现很好的文档解释了如何使用它:1这个.虽然我在理解这部分设置方面有一些问题:

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.
");
  return(-1);
}

当我使用播放模式时设置多个时间段是什么意思和:

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

/* 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.
");
  return(-1);
}

关于延迟的同样问题,我应该如何理解?

and the same question here about the latency , how should I understand it?

推荐答案

我假设您已经阅读并理解 linux-journal 的这一部分.您可能还会发现 此博客 阐明了有关周期大小选择的内容(或博客中的片段)在 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:

您不应该滥用声音设备的片段逻辑.就像是这个:

You shouldn't misuse the fragments logic of sound devices. It's like this:

延迟由缓冲区大小定义.
唤醒间隔由片段大小定义.

The latency is defined by the buffer size.
The wakeup interval is defined by the fragment size.

缓冲区填充水平将在完整缓冲区"和完整缓冲区"之间振荡缓冲区减去 1x 片段大小减去操作系统调度延迟'.环境较小的片段大小会增加 CPU 负载并减少电池电量时间,因为您强制 CPU 更频繁地唤醒.OTOH 它增加退出安全性,因为您更早填满了播放缓冲区.选择因此,片段大小是您应该平衡的东西您在功耗和辍学安全之间的需求.与现代处理器和良好的操作系统调度程序,如 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)

基本上,通常您会将 period 设置为 2(就像在 howto 你引用了).然后 periodsize * period 是您的总缓冲区大小(以字节为单位).最后,延迟是缓冲这么多样本引起的延迟,并且可以通过将缓冲区大小除以播放样本的速率来计算(即,根据代码注释中的公式latency = periodsize * period/(rate * bytes_per_frame)).

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).

例如,howto 中的参数:

  • 周期 = 2
  • 周期大小 = 8192 字节
  • 速率 = 44100Hz
  • 16 位立体声数据(每帧 4 个字节)

对应于 period * 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中句号的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆