I'm trying to retrieve (and probably later set) the master sound volume in Linux. I'm using PulseAudio, but ideally it should work for ALSA too.
I found this very helpful post on how to set the volume, and from that I was able to deduce the existence of snd_mixer_selem_get_playback_volume() to retrieve the current setting. However on my system, this seems to be giving me wrong readings - where mixer programs show 100%, this tops out at about 66%.
If I open pavucontrol I can see the volume for this output device matches the readings I get from here, so I assume it's giving me the hardware volume setting, not the global master volume that I wanted.
Compile the below code with
gcc audio_volume.c -o audio_volume -lasound
or altenatively withgcc audio_volume.c -o audio_volume_oss -DOSSCONTROL
. OSS version is written very crudely and does not give precise results. Setting volume to 100 sets 97% volume in my system. ALSA version works for me and PulseAudio in openSUSE. Needs some cleanup and love but I hope it is what you need, I grant the permission to use it on the WTFPL license.