How to identify Alsa eLements?

Takashi Sakamoto o-takashi at sakamocchi.jp
Fri Jul 3 02:34:20 CEST 2020


Hi,

On Thu, Jul 02, 2020 at 10:05:01PM +0200, Tanjeff Moos wrote:
> I'm programming a GUI for the mixer in my Focusrite Scarlett 6i6 interface.
> The Alsa driver supports that functionality, i.e. I can control the mixer
> using alsamixer, for example. Now I'm writing a Qt GUI for it.
> 
> I wonder how I can reliably identify a mixer control element (e.g. the
> master volume control). Each element has an ID (struct snd_ctl_elem_id),
> consisting of multiple elements:
> - numid (seems to change at each boot)
> - iface
> - device
> - subdevice
> - name (is always constant)
> - index
> The docs says I can identify an element by the combination of name and
> index.
> 
> My questions:
> 1) Are name and index enough to identify an element?

In ALSA control core, 'snd_ctl_find_id()' helper function[1] is used to
find control element set by the given information from userspace
application. You can see the algorithm supports two cases:

1. numid matching
2. the combination matching with:
  * iface
  * device
  * subdevice
  * name
  * index is within the range of set

As you know, the numid is not constant between every bootup since it's
assigned dynamically (see __snd_ctl_add_replace() helper function[2]).
Thus the way 2 is available in the case to hard-code in userspace
application.

> 2) How can I obtain the index? The name and numid are shown by 'amixer
>    contents', for example.

The amixer program uses 'snd_ctl_ascii_elem_id_get()' API in alsa-lib to
show identifier information. The API is programmed not to output the
index value if it equals to zero[3]. Potentially you can retrieve the
index value by a call of 'snd_ctl_elem_id_get_index()'.


Anyway, when using alsa-lib application for the purpose, you should pay
enough attention to which API is used since alsa-lib includes several
abstractions of API for control element set in each level:

 * Lower abstraction (snd_ctl_xxx)
 * Higher abstraction (snd_hctl_xxx)
 * Setup control interface (snd_sctl_xxx)
 * Mixer interface (snd_mixer_xxx)
   * Simple Mixer interface (snd_mixer_selem_xxx)

The configuration space of alsa-lib affects Setup control interface
and Mixer interface. On the other hand, it doesn't affect the
lower/higher abstraction. The amixer is a kind of application to use
'snd_hctl_xxx', 'snd_mixer_xxx', and 'snd_mixer_selem_xxx'.


When you'd like to communicate to kernel land implementation without any
effects of alsa-lib's configuration space. it's better to use the lower/higher
abstractions. As long as I've used, 'qashctl' in QasTools[4] is good GUI
application for this purpose. It's written with Qt5 and seems to be helpful
for your work in both of GUI programming and control elements handling.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound.git/tree/sound/core/control.c#n653
[2] https://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound.git/tree/sound/core/control.c#n338
[3] https://github.com/alsa-project/alsa-lib/blob/master/src/control/ctlparse.c#L110
[4] https://gitlab.com/sebholt/qastools


Regards

Takashi Sakamoto


More information about the Alsa-devel mailing list