使用ALSA [或V4L2]通过轮询读取麦克风数据

时间:2014-09-13 10:19:08

标签: linux alsa multimedia v4l2

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

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。
  3. 感谢您的关注。

1 个答案:

答案 0 :(得分:1)

要阻止snd_pcm_read*()来电阻止,请使用snd_pcm_nonblock()启用非阻止模式。

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