使用ALSA [或V4L2]通过轮询读取麦克风数据 [英] Reading Microphone Data by Polling using ALSA [or V4L2]

查看:935
本文介绍了使用ALSA [或V4L2]通过轮询读取麦克风数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Linux(ubuntu 14.04)中的多个麦克风读取数据。我有一个特定的约束,那就是应该通过轮询来读取麦克风(因此,尽管数据是高频的,所以不要等到有数据时才等待)。我想知道在Linux中是否可行?不幸的是,音频捕获不是我的专长,我想知道使用Alsa的选择是否合适。为了更好地理解问题,这里有一个我想到的伪代码:

I am trying to read data from multiple microphones in Linux (ubuntu 14.04). I have a specific constraint that the reading from microphones should be via polling(so no waiting until there is data, although the data comes in high frequency). I wanted to know if that is possible in Linux? Unfortunately audio capture is not the area of my expertise and I would like to know if the choice of using Alsa is a good one. To better understand the problem, here is a pseudo-code that I had in mind:

open_the_audio_device();
set_the_parameters_of_the_audio_device();
while (!done)
{
    poll_result=poll_the_devices(); //other non-audio devices are also polled here preferably, something like using select on all different file descriptors of audio, video, socket, etc.      
    if(poll_success_for_audio_device)
          receive_audio_from_the_device_that_has_data();
    else
          do_some_other_very_fast_stuff_and_start_loop_again();
}
close_the_device();

我的问题有2折:


  1. Alsa是否是一个不错的选择?

  2. 是否可以通过某些库为我提供文件描述符,以便可以将其与select函数一起使用?如果是这样,那是最佳选择,因为还有其他非音频设备也可以使用select。

感谢您的关注。

推荐答案

为防止 snd_pcm_read *()调用被阻止,请启用非阻止 snd_pcm_nonblock()

To prevent the snd_pcm_read*() calls from blocking, enable non-blocking mode with snd_pcm_nonblock().

要获取可轮询的文件描述符,请调用 snd_pcm_poll_descriptors_count() snd_pcm_poll_descriptors()
可能有多个描述符,因为某些插件实现通知的方式可能不同。
将这些描述符上的 poll()的结果转换回 POLLIN / POLLOUT 值,调用 snd_pcm_poll_descriptors_revents()

To get pollable file descriptors, call snd_pcm_poll_descriptors_count() and snd_pcm_poll_descriptors(). It is possible to have multiple descriptors because some plugins might implement notifications differently. To translate the result of a poll() on those descriptors back into a POLLIN/POLLOUT value, call snd_pcm_poll_descriptors_revents().

这篇关于使用ALSA [或V4L2]通过轮询读取麦克风数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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