Portaudio不能识别所有音频设备

时间:2019-06-28 14:27:34

标签: c++ linux audio portaudio pulseaudio

这个问题在我编写c ++程序时发生,所以我创建了代码的最小实例以更好地阐明问题:

#include <portaudio.h>
#include <iostream>

int main()
{
    Pa_Initialize();

    int devices = Pa_GetDeviceCount();
    std::cout << devices << std::endl;

    for (int i = 0; i != devices; ++i) 
    {
        auto * info = Pa_GetDeviceInfo(i);
        std::cout << info->name << std::endl;
    }

    Pa_Terminate();
}

因此,此代码应打印我拥有声音io的设备。我已经在两台linux PC上使用ubuntu 16和ubuntu 18.10测试了此代码,并进行了一些似乎不合理的更改结果的调整(我将在后面解释此调整)结果非常荒谬。我首先在Ubuntu 16上运行我的应用程序,它检测到很多设备,包括sysdefault(这是有用的设备)。然后,我在ubuntu 18上运行了同一个应用程序,但没有找到sysdefault,我用aplay -Larecord -L命令检查了Alsa是否识别sysdefault,所以我认为这是pulseaudio问题。我下载了pavucontrol,并做了一些没有运气的调整。最后这是更不合理的事情,下载并调整pavucontrol之后,我再次尝试在ubuntu 16上使用我的应用程序,但它不起作用,而且当我在ubuntu 16上删除pavucontrol时它起作用了,但是当我在ubuntu 18上做了同样的事情时没有! 值得一提的是,我的ubuntu 16是一个旧系统,我已经安装了许多cpp库,并且在最近几年中做了很多调整,另一方面,我的ubuntu 18是一台格式化的笔记本电脑,几乎没有安装任何东西在上面。 在这里,我添加了一些控制台输出以更好地理解问题:

$aplay -L
> 
> default
>     Playback/recording through the PulseAudio sound server null
>     Discard all samples (playback) or generate zero samples (capture) pulse
>     PulseAudio Sound Server hdmi:CARD=HDMI,DEV=0
>     HDA Intel HDMI, HDMI 0
>     HDMI Audio Output hdmi:CARD=HDMI,DEV=1
>     HDA Intel HDMI, HDMI 1
>     HDMI Audio Output hdmi:CARD=HDMI,DEV=2
>     HDA Intel HDMI, HDMI 2
>     HDMI Audio Output dmix:CARD=HDMI,DEV=3
>     HDA Intel HDMI, HDMI 0
>     Direct sample mixing device dmix:CARD=HDMI,DEV=7
>     HDA Intel HDMI, HDMI 1
>     Direct sample mixing device dmix:CARD=HDMI,DEV=8
>     HDA Intel HDMI, HDMI 2
>     Direct sample mixing device dsnoop:CARD=HDMI,DEV=3
>     HDA Intel HDMI, HDMI 0
>     Direct sample snooping device dsnoop:CARD=HDMI,DEV=7
>     HDA Intel HDMI, HDMI 1
>     Direct sample snooping device dsnoop:CARD=HDMI,DEV=8
>     HDA Intel HDMI, HDMI 2
>     Direct sample snooping device hw:CARD=HDMI,DEV=3
>     HDA Intel HDMI, HDMI 0
>     Direct hardware device without any conversions hw:CARD=HDMI,DEV=7
>     HDA Intel HDMI, HDMI 1
>     Direct hardware device without any conversions hw:CARD=HDMI,DEV=8
>     HDA Intel HDMI, HDMI 2
>     Direct hardware device without any conversions plughw:CARD=HDMI,DEV=3
>     HDA Intel HDMI, HDMI 0
>     Hardware device with all software conversions plughw:CARD=HDMI,DEV=7
>     HDA Intel HDMI, HDMI 1
>     Hardware device with all software conversions plughw:CARD=HDMI,DEV=8
>     HDA Intel HDMI, HDMI 2
>     Hardware device with all software conversions sysdefault:CARD=PCH
>     HDA Intel PCH, ALC3234 Analog
>     Default Audio Device front:CARD=PCH,DEV=0
>     HDA Intel PCH, ALC3234 Analog
>     Front speakers surround21:CARD=PCH,DEV=0
>     HDA Intel PCH, ALC3234 Analog
>     2.1 Surround output to Front and Subwoofer speakers surround40:CARD=PCH,DEV=0
>     HDA Intel PCH, ALC3234 Analog
>     4.0 Surround output to Front and Rear speakers surround41:CARD=PCH,DEV=0
>     HDA Intel PCH, ALC3234 Analog
>     4.1 Surround output to Front, Rear and Subwoofer speakers surround50:CARD=PCH,DEV=0
>     HDA Intel PCH, ALC3234 Analog
>     5.0 Surround output to Front, Center and Rear speakers surround51:CARD=PCH,DEV=0
>     HDA Intel PCH, ALC3234 Analog
>     5.1 Surround output to Front, Center, Rear and Subwoofer speakers surround71:CARD=PCH,DEV=0
>     HDA Intel PCH, ALC3234 Analog
>     7.1 Surround output to Front, Center, Side, Rear and Woofer speakers dmix:CARD=PCH,DEV=0
>     HDA Intel PCH, ALC3234 Analog
>     Direct sample mixing device dsnoop:CARD=PCH,DEV=0
>     HDA Intel PCH, ALC3234 Analog
>     Direct sample snooping device hw:CARD=PCH,DEV=0
>     HDA Intel PCH, ALC3234 Analog
>     Direct hardware device without any conversions plughw:CARD=PCH,DEV=0
>     HDA Intel PCH, ALC3234 Analog
>     Hardware device with all software conversions

并运行该应用程序:

$./main
6
HDA Intel HDMI: 0 (hw:0,3)
HDA Intel HDMI: 1 (hw:0,7)
HDA Intel HDMI: 2 (hw:0,8)
hdmi
pulse
default

问题/要求是:是什么造成了问题?可能不是alsa,所以是portaudio吗?但是它在第一种情况下有效,所以它是pulseaudio吗?如果是的话,现在我再现它后该如何解决该问题?

1 个答案:

答案 0 :(得分:0)

毕竟问题出在Alsa身上。我必须在Ubuntu 18机器上安装声卡(或至少要使用虚拟声卡),而Alsa默认情况下仅读取其中一种。此问题有很多解决方法,对我有用的一个方法是更改​​/创建配置。带有这两行的文件/etc/asound.conf

defaults.pcm.card 2
defaults.ctl.card 1


更改了默认卡。