Finding all the devices I can use to play PCM with ALSA
I think you can use snd_device_name_hint for enumerating devices.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 returnchar** 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 toosnd_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. 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.
It is:
aplay -l
Now what I did is just make a programme to give the out as like as this by alsa.
For everyone's help I have made a (.so) library and a sample Application demonstrating the use of this library in c++.
The output of my library is like-
[root@~]# ./IdeaAudioEngineTestHDA Intel plughw:0,0HDA Intel plughw:0,2USB 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
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;}