簡體   English   中英

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

[英]Fetching device.description using Alsa soundlib in C++

我是 alsa 的新手,pulsAudio,需要幫助解決這個問題。

這是我的系統 pacmd list-sources 上 pacmd 命令的截斷 output:

    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 = "Built-in Audio Analog Stereo"

當我嘗試使用desc = snd_device_name_get_hint(*n, "DESC");獲取描述時對於同一設備,我得到以下 output

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 output 的描述?

如果不可能,有人知道如何從 pacmd output 中獲取名稱:<alsa_input.pci-0000_00_1f.3.analog-stereo>字段。

我從官方頁面嘗試了一堆 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);
}

如果你想使用 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 文件中查看此完整示例。

如果你想使用 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM