Set ALSA master volume from C code
Asked Answered
S

1

35

I've been looking for a simple C code example to set the master volume of the ALSA mixer but could not find anything simple for this supposedly common operation.

I'm totally unfamiliar with ALSA, so making my own minimal example will take time. I would be happy if anyone could provide one.

Savill answered 22/7, 2011 at 8:18 Comment(0)
S
58

The following works for me. The parameter volume is to be given in the range [0, 100]. Beware, there is no error handling!

void SetAlsaMasterVolume(long volume)
{
    long min, max;
    snd_mixer_t *handle;
    snd_mixer_selem_id_t *sid;
    const char *card = "default";
    const char *selem_name = "Master";

    snd_mixer_open(&handle, 0);
    snd_mixer_attach(handle, card);
    snd_mixer_selem_register(handle, NULL, NULL);
    snd_mixer_load(handle);

    snd_mixer_selem_id_alloca(&sid);
    snd_mixer_selem_id_set_index(sid, 0);
    snd_mixer_selem_id_set_name(sid, selem_name);
    snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid);

    snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
    snd_mixer_selem_set_playback_volume_all(elem, volume * max / 100);

    snd_mixer_close(handle);
}
Savill answered 22/7, 2011 at 9:18 Comment(11)
What header file are these under?Boutique
@JonathanHenson: #include <alsa/asoundlib.h>. I think there might be one that includes less, maybe #include <alsa/mixer.h>.Savill
I guess I could have just retrieved the output from pkg-config --cflags alsa. Anyhow thanks!Boutique
@dreamlax: that's part of the mixer elements, too. I think it's the mute melemAnglicize
@trenki, Thanks for example, I have different situation, plz help...I am opening two pcm_snd_pcm_open() handles, i want to control volume of two instances. How can i set volume independently ? I means using which API? ...Any other idea?Emboly
@trenki, Master does not exist for me as elem. How can i get all the couple card/elem name ? "Default" card name seems to be ok for me for example even if aplay -l/-L does not show default as card name.... I tried a piece of code to enumerate all card/elem without success : fossies.org/linux/audacity-minsrc/lib-src/portmixer/src/…Exuviae
@Exuviae I found that if I run "alsamixer" it gives the elem names there... in my case it was "Playback" and "Headphone".Arellano
This seems to be the best documentation available on using the mixer API. Can it be improved by some comments explaining why the functions need to be called? The actual API documentation is extremely sparse.Orphaorphan
@Exuviae You can also list all the mixers by using "amixer contents"Fledgy
Good solution, similar to this example, which has error handling. github.com/OpenDingux/alsa-volume/blob/master/setvolume.cAgathaagathe
"Master" is the name of a "Control". Here is a source for the Standard Linux Control names for ALSA, from the Linux Kernel Docs docs.kernel.org/sound/designs/control-names.htmlAgathaagathe

© 2022 - 2024 — McMap. All rights reserved.