微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 C++ 中使用 Alsa soundlib 获取 device.description

如何解决在 C++ 中使用 Alsa soundlib 获取 device.description

我是 alsa 的新手,pulseAudio,需要帮助解决这个问题。

这是我系统上 pacmd 命令的截断输出 pacmd 列表源:

    name: <alsa_input.pci-0000_00_1f.3.analog-stereo>
    
    properties:
        alsa.resolution_bits = "16"
        device.profile.name = "analog-stereo"
        device.profile.description = "Analog Stereo"
        device.description = "Built-in Audio Analog Stereo"
        module-udev-detect.discovered = "1"
        device.icon_name = "audio-card-pci"

我想使用简单的 C++ 程序获取字段:device.description = “内置音频模拟立体声”

当我尝试使用 desc = snd_device_name_get_hint(*n,"DESC"); 我得到相同设备的以下输出

Name of device: hw:CARD=PCH,DEV=0
Description of device: HDA Intel PCH,ALC3235 Analog
Direct hardware device without any conversions
I/O type of device: (null)

有没有办法获取类似于 pacmd 输出的描述?

如果不可能,有谁知道如何从 pacmd 输出获取 name: 字段。

我从官方页面尝试了一堆 pf API: https://www.alsa-project.org/alsa-doc/alsa-lib/group___control.html 但没有运气

我当前正在运行的代码

#include <alsa/asoundlib.h>
void listdev(char *devname)
{
    char** hints;
    int    err;
    char** n;
    char*  name;
    char*  desc;
    char*  ioid;
    /* Enumerate sound devices */
    err = snd_device_name_hint(-1,devname,(void***)&hints);
    if (err != 0) {
        fprintf(stderr,"*** Cannot get device names\n");
        exit(1);
    }
    n = hints;
    while (*n != NULL) {
        name = snd_device_name_get_hint(*n,"NAME");
        desc = snd_device_name_get_hint(*n,"DESC");
        ioid = snd_device_name_get_hint(*n,"IOID");
        printf("Name of device: %s\n",name);
        printf("Description of device: %s\n",desc);
        printf("I/O type of device: %s\n",ioid);
        printf("\n");
        if (name && strcmp("null",name)) free(name);
        if (desc && strcmp("null",desc)) free(desc);
        if (ioid && strcmp("null",ioid)) free(ioid);
        n++;
    }
    //Free hint buffer too
    snd_device_name_free_hint((void**)hints);
}

解决方法

使用 proplist 获得那些必需的值 https://freedesktop.org/software/pulseaudio/doxygen/structpa__card__info.html#a61d544035431d68f87e5e1cb27c3bf2e

关于 proplist 的更多信息:https://freedesktop.org/software/pulseaudio/doxygen/proplist_8h.html

,

如果您想使用 ALSA C++ API(来自 gtkIOStream),那么您可以这样做:

const string deviceName="hw:0";
Playback pcm(deviceName.c_str()); // You could also use the PCM class instead of Playback or Capture.
cout<<"Opened the PCM device "<<deviceName<<endl;

Info info;
info.getInfo(pcm);
cout << "Card   Number     : " << info.getCard() << endl;
cout << "Device Number     : " << info.getDevice() << endl;
cout << "Sub-device Number : " << info.getSubDevice() << endl;
cout << "Short ID          : " << info.getID() << endl;
cout << "Name              : " << info.getName() << endl;
cout << "Sub-device name   : " << info.getSubDeviceName() << endl;

您可以在 ALSAInfoTest.C file 中看到这个完整的示例。

如果你想使用 alsa-lib API,那么流程如下:

snd_pcm_t *handle;
const char *device="hw:0";
snd_pcm_open(&handle,device,SND_PCM_STREAM_PLAYBACK,0); // check for error here
snd_pcm_info_t *info;
snd_pcm_info_malloc(&info); // malloc the structure,you should check for errors
cout<<snd_pcm_info_get_name(info)<<endl; // print out the device name
snd_pcm_info_free(info);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。