正确调整 Alsa 缓冲区大小,奇怪的 API
我目前正在开展一个项目,需要我使用 Alsa 进行一些采样。我试图正确配置所有内容,但我坚持如何正确调整我的阅读大小。
有两个原语似乎对我的任务很有趣:
snd_pcm_hw_params_get_period_time
snd_pcm_hw_params_get_buffer_size
第一个原语的名称表明输出将是采样周期的时间长度,但这很奇怪:如果我将采样率设置为 f = 44100Hz 采样周期(以纳秒为单位)应为 T0 = 1e9 / 44100 ~= 22676 ns
而函数将回答 T1 = 725 us = 725000 ns
。
同时,即使我被要求使用非锁定原语,我也会尝试分析锁定“readi”所需的时间,结果发现该示例需要 T2 = 8028603 ns
最好情况下的时间,最坏情况下的 T3 = 12436217 ns
。
最后我不明白以下两个的含义是什么:
snd_pcm_hw_params_get_buffer_time
snd_pcm_hw_params_get_period_size
我不明白如何测量缓冲区的时间和周期的大小,但是前者返回与 get_buffer_size 相同的值,而后者返回与 get_period_time 相同的值。
有什么提示吗?
I'm currently working on a project that requires me to do some sampling with Alsa. I'm trying to configure correctly everything but I'm stuck on how to correctly size my reading.
There are two primitives that seem to be interesting for my task:
snd_pcm_hw_params_get_period_time
snd_pcm_hw_params_get_buffer_size
The name of the first one suggests that the output will be the time length of a sampling period, however that's weird: if I'm setting the sampling rate on f = 44100Hz
the sampling period (in nanoseconds) should be T0 = 1e9 / 44100 ~= 22676 ns
while the function will answer T1 = 725 us = 725000 ns
.
Meanwhile, even if I've been asked to use non-locking primitives, I'm trying to profile the time required for locking 'readi', and it turns out that the sample requires T2 = 8028603 ns
in the best case time and T3 = 12436217 ns
in the worst case.
Finally I can't figure out what's the meaning of the following two:
snd_pcm_hw_params_get_buffer_time
snd_pcm_hw_params_get_period_size
I don't get how could I measure the buffer in time and the period in size, However the former returns the same value as get_buffer_size
, while the latter returns the same value as get_period_time
.
Any hint?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ALSA 有一些奇怪的^W特定术语:
*_size 函数似乎返回帧中的大小。
华泰
ALSA has some weird^Wspecific terminology:
The *_size functions appear to return sizes in frames.
HTH
这是我的声卡初始化函数。
首先,我设置所需的参数。
当每个参数都正确设置后,参数将应用于句柄:
应用后,勇敢的程序员可以获得所需的数据,如下所示:
get_period_size_min() 给出将包含采样的缓冲区的最小大小(以帧为单位)。具有此大小的缓冲区足够宽。
这是相当违反直觉的,但正确的采样周期并不像人们想象的那样由 1/rate 给出。可以使用 get_period_time() 原语获取采样周期!
This is my soundcard initialization function.
First of all I set needed parameters
When every parameter has been setted correctly, the parameters are applied to the handle:
After it has been applied the brave programmer can obtain the required data as follows:
get_period_size_min() gives the minimum size in frames of the buffer that will contain the sampling. A buffer having this size is wide enough.
This is pretty counter-intuitive, but the correct sampling period is not given by 1/rate as one may think. One can obtain the sampling period by using the get_period_time() primitive!
根据我使用和阅读 ALSA 库的理解,这段时期与硬件中断有关。如果您使用
它,它将返回通过中断传递给硬件的帧数。同样,如果您使用
You will get the time in us (10^-6) ,这些帧将被插入。因此可以获得“真实”采样率
From what I understood using and reading the ALSA library, this period is related to the hardware interruptions being made. If you use
It will return you the number of frames passed to the hw by interruption. Similarly, if you use
You will get the time in us (10^-6) that those frames will be inserted. So the "real" sampling rate can be obtained if