Fetching device.description using Alsa soundlib in C++

915 views Asked by At

I am new to alsa, pulseAudio, needed help with this issue.

This is the truncated output of pacmd command on my system pacmd list-sources:

    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"

I want to fetch the field: device.description = "Built-in Audio Analog Stereo" using a simple C++ program

When I try fetching description using desc = snd_device_name_get_hint(*n, "DESC"); I get the following output for the same device

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)

Is there a way I can fetch the description similar to pacmd output?

If it is not possible does anyone know how to fetch the name: <alsa_input.pci-0000_00_1f.3.analog-stereo> field from the pacmd output.

I tried a bunch pf APIs from the official page: https://www.alsa-project.org/alsa-doc/alsa-lib/group___control.html but no luck

Code that I am currently running:

#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);
}
2

There are 2 answers

0
Matt On

If you want to use the ALSA C++ API (from gtkIOStream) then you can do it like so :

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;

You can see this full example in the ALSAInfoTest.C file.

If you want to use the alsa-lib API, then this is the process :

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);