I have a sound card's control open and am polling on ALSA control events. The event is pulled from the event stream using the gtkIOStream ALSA::Control class like so :
snd_ctl_event_t *event;
snd_ctl_event_alloca(&event);
int err = snd_ctl_read(ctl, event);
if (err < 0)
return ALSADebug().evaluateError(err, "snd_ctl_read error when handling poll events\n");
I am now trying to get the matching simple mixer element from the event, but in some cases some ALSA simple mixers have duplicate elements. For example one element in the playback mixer has the same name as an element in the capture mixer section. If the name is extracted from the control event like so :
std::string elemName(snd_ctl_event_elem_get_name(event));
It is impossible to tell which mixer element (snd_mixer_elem_t) that name belongs to because there are two mixer elements with the same name. When iterating through the known set of simple mixer elements, we will return once we find the first one - another simpilar problem exists when we search for mixer elements based on a key in the mixer element name. Searching is done on each simple mixer element using the snd_mixer_selem_get_name ALSA function.
Is there a way to find the exact mixer simple mixer element which fired the ASLA Control event without using the mixer element name ? Is there for example a way to map Control numid back to the exact simple mixer element in some way using the ALSA control element's numid (snd_ctl_event_elem_get_numid) ?
unsigned int snd_ctl_event_elem_get_numid(const snd_ctl_event_t *obj)
Is there a different approach which can be used ?
The ALSA control interface can't be used to manage alsa mixer elements which have state changes. To catch alsa mixer events for elements, it is best to use the ALSA mixer element callback methodology.
A complete implementation for handling mixer element changes is implemented here
It boils down to the following C++ code, starting with opening an ALSA card :
After loading, enable callbacks on each mixer element :
The callback allows you to get the C++ ALSA::MixerEvents class instance to handle events with :
A usable application of the MixerEvents class is given here.