How to find out ALSA audio device capabilities programmatically in Linux without opening the device
Asked Answered
W

4

6

How do I find out the capabilities of ALSA devices without opening the device first? Problem is, I need to supply the parameters to the snd_pcm_open() function to use the test functions which to me is silly. Why ask whether this is a playback or record device if I have to tell it to the open function first?

As an example, I would like to list all playback devices but the only way I can think of is trying to open every device I find (with snd_device_name_hint()) and try to open them in playback mode and if I manage to open, everything is fine.

Problem is, with all the possible variations (sample rate, channels, etc) there would be an awful large number of open()'s I need to perform to find out the parameters.

Wormy answered 8/12, 2009 at 12:34 Comment(0)
Z
7

Use snd_ctl_next to iterate over all devices, then snd_ctl_open to get info about the device - this is not the same as snd_pcm_open, which could fail if another program has the device open or if you have bad settings.

It's quite dense, but here's some open-source code that iterates over all ALSA devices you could look at: https://app.assembla.com/spaces/portaudio/git/source/master/src/hostapi/alsa/pa_linux_alsa.c - search for BuildDeviceList to get started.

Zigzag answered 31/12, 2009 at 1:16 Comment(1)
Unfortunately that code DOES open the pcm device to check for max channels, sample rate etc. I don't think there is anyway getting around this.Demerol
B
0

It was my first requirements to a linux/unix projects where I need to know about all of the available audio devices capability. Then I need to use these devices to capture and playback 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 program 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@~]# ./IdeaAudioEngineTest
HDA Intel plughw:0,0
HDA Intel plughw:0,2

This library can also capture and playback the real-time audio data.

It is available with documentation in IdeaAudio library with Duplex Alsa Audio

Barefoot answered 1/8, 2013 at 10:43 Comment(0)
D
0

No, there is no other way to query number of channels, sample rate etc. other than opening the card with snd_pcm_open.

Demerol answered 27/10, 2021 at 6:40 Comment(0)
L
-1

Providing you can perform a shell command. Then aplay is your friend.

http://alsa.opensrc.org/Aplay

aplay -l

list all soundcards and digital audio devices

Leonoraleonore answered 8/12, 2009 at 12:48 Comment(1)
This doesn't tell me anything I don't know with snd_device_name_hint(). It doesn't seem to tell, for instance, what sample rates each device supports. Plus I wouldn't say it's very simple to call this from C code and handle the output even if it did produce the info I need...Wormy

© 2022 - 2024 — McMap. All rights reserved.