如何在ALSA中设置周期和缓冲区大小? [英] How to set periods and buffer size in ALSA?

查看:599
本文介绍了如何在ALSA中设置周期和缓冲区大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为类似Sip的应用程序捕获音频.

I'm trying to capture audio for a sip like application.

我想以8khz单声道获得20毫秒的音频.

I want to get 20 milliseconds of audio at 8khz mono.

我需要应用程序每20毫秒准确获取一次音频,以避免抖动.

I need the application to get audio exactly every 20 milliseconds to avoid jitter.

我已经设置了以下参数

  • 访问权限:SND_PCM_ACCESS_RW_INTERLEAVED
  • 格式:SND_PCM_FORMAT_S16_LE
  • 费率:8000
  • 渠道:1
  • 期间大小:160

我希望周期为2,缓冲区为320(period_size * periods).但是,如果我尝试使用以下任一设置:

I want the periods to be 2 and the buffer to be 320 (period_size*periods). However if I try to set either of these using:

  • snd_pcm_hw_params_set_periods
  • snd_pcm_hw_params_set_buffer_size

然后我得到-22,即-EINVAL

Then I get -22 returned, which is -EINVAL

推荐答案

周期大小指定硬件通知您的应用程序已捕获完整周期的频率.这是一个硬件参数,表示该硬件可能不支持所需的值.

The period size specifies how often the hardware notifies your application that a complete period has been captured. It is a hardware parameter, which means that the hardware might not support the value that you want.

要获取最接近所需值的周期大小,请使用 snd_pcm_hw_params_set_period_size_near().

To get the period size that is nearest to your desired value, use snd_pcm_hw_params_set_period_size_near().

如果要读取160个样本,只需告诉 snd_pcm_read *()即可读取160帧.但是,如果这与周期大小不匹配,则会产生抖动.如果降低抖动很重要,则必须将样本放在自己的队列中,然后将其与用合适的计时器.

If you want to read 160 samples, just tell snd_pcm_read*() to read 160 frames. However, if this does not match the period size, you will get jitter. If reducing jitter is important, you have to put the samples in your own queue and take them with out with an appropriate timer.

请注意,捕获延迟仅取决于周期大小,而不取决于缓冲区大小,因此,应将缓冲区设置得尽可能大,以减少发生超限的风险.

Please note that capture latency depends only on the period size, not on the buffer size, so you should make the buffer as large as possible to reduce the risk of overruns.

这篇关于如何在ALSA中设置周期和缓冲区大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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