ALSA: 全双工C语言示例?

12

有没有一个用C语言实现全双工ALSA连接的例子?我已经了解到它是被支持的,但是我看到的所有介绍性例子都只是录制或播放声音样本,而我想要一个可以处理我的VoIP应用程序中的录制和播放两个功能的处理器。

非常感谢帮助, Jens


3
+1因为我也想知道答案。如果您使用OSS API用于ALSA设备,是否仅使用“O_RDWR”打开即可实现全双工,或者需要使用旧式的OSS全双工设置“ioctl”等复杂方法,或者甚至根本无法实现... - R.. GitHub STOP HELPING ICE
我在此问题的链接中编写了一个答案: https://unix.stackexchange.com/a/504526/235261,试图提供全双工alsa。 - EsmaeelE
4个回答

6

有个名叫Alan的人发表了这篇不错(但有些老旧)的教程,全双工ALSA,是用C语言编写的。


3
您提供了一个链接,依次操纵它们。以下是Alan的代码(已省略并加上注释)。
// the device plughw handle dynamic sample rate and type conversion.
// there are a range of alternate devices defined in your alsa.conf
// try: 
// locate alsa.conf
// and check out what devices you have in there
//  
// The following device is PLUG:HW:Device:0:Subdevice:0
// Often simply plug, plughw, plughw:0, will have the same effect 
//
char           *snd_device_in  = "plughw:0,0";
char           *snd_device_out = "plughw:0,0";

// handle constructs to populate with our links
snd_pcm_t      *playback_handle;
snd_pcm_t      *capture_handle;

//this is the usual construct... If not fail BLAH
if ((err = snd_pcm_open(&playback_handle, snd_device_out, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
fprintf(stderr, "cannot open output audio device %s: %s\n", snd_device_in, snd_strerror(err));
exit(1);
}

// And now the CAPTURE
if ((err = snd_pcm_open(&capture_handle, snd_device_in, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
fprintf(stderr, "cannot open input audio device %s: %s\n", snd_device_out, snd_strerror(err));
exit(1);
}

然后配置并启动它们。
一个环形调制器可以完成这项工作:http://soundprogramming.net/programming_and_apis/creating_a_ring_buffer,或者您可以使用上面概述的Alan的方法。

1
一个带有预定项限制的FIFO堆栈比环形缓冲区更简单、更安全。 - slashmais

3
这是我在 Linux/Unix VoIP 项目中的首要需求,需要了解所有可用音频设备的功能和名称。然后我需要使用这些设备来捕获和播放音频。
为了帮助大家,我制作了一个 (.so) 库和一个演示如何在 c++ 中使用该库的示例应用程序。
我的库的输出如下:
[root@~]# ./IdeaAudioEngineTest
HDA Intel plughw:0,0
HDA Intel plughw:0,2
USB Audio Device plughw:1,0

该库提供了捕获和播放实时音频数据的功能。

完整的源代码和文档可以在IdeaAudio library with Duplex Alsa Audio中找到。

该库的源代码现在已经在github.com开源。


2

2
不理解为什么会被投反对票 - 你提供的示例根据其文档应该使用全双工。 - slashmais

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接