From: Clemens Ladisch [mailto:cladisch@googlemail.com]
#include <stdio.h> #include <stdlib.h> #include <alsa/asoundlib.h>
static void check(int err, const char *f) { if (err < 0) { fprintf(stderr, "%s failed: %s\n", f, snd_strerror(err)); exit(EXIT_FAILURE); } } #define CHECK(f) check(f, #f)
int main() { snd_ctl_t *ctl; snd_ctl_elem_value_t *value;
CHECK(snd_ctl_open(&ctl, "default", 0)); snd_ctl_elem_value_alloca(&value); snd_ctl_elem_value_set_interface(value,
SND_CTL_ELEM_IFACE_MIXER); snd_ctl_elem_value_set_name(value, "Master Playback Volume"); snd_ctl_elem_value_set_integer(value, 0, 42); CHECK(snd_ctl_elem_write(ctl, value));
snd_ctl_close(ctl); return 0;
}
Well, I finally got a chance to try this. It "works", but the snd_ctl_elem_write call takes a couple of milliseconds (that's around a million instructions on my Atom), and it apparently spends this time in kernel state, because it causes my sound thread to underrun, so it's not usable.
If I use alsamixer, I don't have this problem; I can manipulate the master volume smoothly while I'm playing my synth app. So I went through the source, and found that it's using snd_mixer_selem_set_playback_volume. This is apparently part of something called the "Simple Mixer Interface", yet another section of ALSA which is completely undocumented, other than the Doxygen stuff (which is only meaningful as a reference for someone who already knows it inside out, but can't remember e.g. whether playback_pcm comes before or after capture_pcm in the snd_mixer_selem_regopt structure).
So what's the minimum I need to do in order to get at the Master Playback Volume, via the Simple Mixer Interface?