How to identify Alsa eLements?
Hi alsa-devel,
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?
2) How can I obtain the index? The name and numid are shown by 'amixer contents', for example.
Kind regards, Tanjeff
Hi,
Quoted 16 lines · Tanjeff Moos
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.
Quoted 2 lines
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.
Quoted 2 lines
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
Quoted 7 lines · Takashi Sakamoto
Quoted 2 lines
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:
Quoted 7 lines
1. numid matching
2. the combination matching with:
* iface
* device
* subdevice
* name
* index is within the range of set
I sent PR[1] to update the documentation in alsa-lib.
[1] https://github.com/alsa-project/alsa-lib/pull/66
Regards
Takashi Sakamoto
Hi Takashi Sakamoto,
Quoted 3 lines · Takashi Sakamoto
Hi,
On Thu, Jul 02, 2020 at 10:05:01PM +0200, Tanjeff Moos wrote:
[...]
Quoted 16 lines
Quoted 3 lines
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
Quoted 4 lines
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.
Thanks for the clarification.
Quoted 4 lines
Quoted 3 lines
2) How can I obtain the index? The name and numid are shown by 'amixer contents', for example.
[...]
Quoted 10 lines
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)
I find this quite confusing. If I could change a volume control using any of those interfaces, then I don't understand when to use which interface. I'm sure that there is good reasoning for each of them, but unfortunatly the documentation has very little information about these concepts.
Anyway, I will stick to the lower abstraction which serves my needs. In the worst case I will do more work than necessary ;-)
Quoted 5 lines
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'.
So the controls offered by CTL/HCTL are determined by the driver? And SCTL, MIXER and MIXER_SELEM are influenced by user space config files?
Quoted 5 lines
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.
qashctl is indeed very helpful, thank you! As being said, I'll stick to CTL.
Thank you very much for your advice!
- Tanjeff
Quoted 10 lines · Tanjeff Moos
Quoted 9 lines
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)
Quoted 4 lines
I find this quite confusing. If I could change a volume control using any of those interfaces, then I don't understand when to use which interface. I'm sure that there is good reasoning for each of them, but unfortunatly the documentation has very little information about these concepts.
So do I. I can imagine that the toughness of work to design good abstraction for the control feature...
Quoted 10 lines
Anyway, I will stick to the lower abstraction which serves my needs. In the worst case I will do more work than necessary ;-)
Quoted 4 lines
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'.So the controls offered by CTL/HCTL are determined by the driver? And SCTL, MIXER and
MIXER_SELEMare influenced by user space config files?
Yes, as long as I know.
HCTL abstraction nearly equals to 'CTL + cache mechanism for control elements + event notification callback'. However in some cases of addition/removal control element set from user space[1], it hits assert and aborts programs with panic.
SCTL/MIXER/MIXER_SELEM features includes extra filter logic for control elements with the configuration. They're specialized to usual channel of audio; e.g. stereo, surround sounds. It's functional as long as using usual sound devices such as stereo speakers. On the other hand, they can handle just a part of the channels when handling control elements for exceptional multi-channel of audio for recording equipments. I guess that your case is the latter.
Quoted 8 lines
Quoted 5 lines
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.
qashctl is indeed very helpful, thank you! As being said, I'll stick to CTL.
Thank you very much for your advice!
Quashctl is pretty good tool for the purpose as long as I know. I wish for someone to develop similar functional tool with CUI or TUI...
[1] for example, below test program can abort pulseaudio when removing added control element set:
https://github.com/alsa-project/alsa-lib/blob/master/test/user-ctl-element-set.c
Regards
Takashi Sakamoto