简体   繁体   English

找到我可以使用ALSA播放PCM的所有设备

[英]Finding all the devices I can use to play PCM with ALSA

I use ALSA to play PCM samples. 我使用ALSA播放PCM样本。 I open the PCM stream with this function: 我用这个函数打开PCM流:

int snd_pcm_open(snd_pcm_t** pcmp,
        const char* name,
        snd_pcm_stream_t stream,
        int mode);

I'm currently using "default" as the name parameter. 我目前正在使用“default”作为名称参数。 I would like to be able to choose other devices. 我希望能够选择其他设备。 What I cannot understand is how I can determine what are the names of the other available devices. 我无法理解的是如何确定其他可用设备的名称。

I attached a USB microphone to my system and aplay and amixer seems to detect the new device. 我将USB麦克风连接到我的系统,aplay和amixer似乎检测到了新设备。 How do I determine the name of that device? 如何确定该设备的名称? Is there any ALSA function to get a list of available devices with their respective names? 是否有任何ALSA功能可以获取具有各自名称的可用设备列表?

I think you can use snd_device_name_hint for enumerating devices. 我想你可以使用snd_device_name_hint来枚举设备。 Here is an example. 这是一个例子。 Beware that I haven't compiled it ! 请注意,我还没有编译它!

char **hints;
/* Enumerate sound devices */
int err = snd_device_name_hint(-1, "pcm", (void***)&hints);
if (err != 0)
   return;//Error! Just return

char** n = hints;
while (*n != NULL) {

    char *name = snd_device_name_get_hint(*n, "NAME");

    if (name != NULL && 0 != strcmp("null", name)) {
        //Copy name to another buffer and then free it
        free(name);
    }
    n++;
}//End of while

//Free hint buffer too
snd_device_name_free_hint((void**)hints);

It was my first requirements to a linux/unix projects where I need to know about all of the available audio devices capability and name. 这是我对linux / unix项目的第一个要求,我需要知道所有可用的音频设备功能和名称。 Then I need to use these devices to capture and plaback the audio. 然后我需要使用这些设备来捕获和回放音频。 What I have done is pretty simple. 我所做的很简单。 There is a linux/unix command which is used to find the devices through alsa utility in linux. 有一个linux / unix命令用于通过linux中的alsa实用程序查找设备。

It is: 它是:

aplay -l

Now what I did is just make a programme to give the out as like as this by alsa. 现在我所做的只是制作一个程序,像alsa一样给出这个。

For everyone's help I have made a (.so) library and a sample Application demonstrating the use of this library in c++. 为了大家的帮助,我创建了一个(.so)库和一个示例应用程序,演示了如何在c ++中使用这个库。

The output of my library is like- 我的图书馆的输出像 -

[root@~]# ./IdeaAudioEngineTest
HDA Intel plughw:0,0
HDA Intel plughw:0,2
USB Audio Device plughw:1,0

This library can also capture and playback the real-time audio data. 该库还可以捕获和回放实时音频数据。

It is available with documentation in IdeaAudio library with Duplex Alsa Audio 它与带有Duplex Alsa Audio的IdeaAudio库中的文档一起提供

Just for grins, your program reformatted: 只是为了笑容,你的程序重新格式化:

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>

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

}

int main(void)

{

    printf("PCM devices:\n");
    printf("\n");
    listdev("pcm");

    printf("MIDI devices:\n");
    printf("\n");
    listdev("rawmidi");

    return 0;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM