I am controlling volume of Speaker using Alsa amixer library on Linux. The problem I have is that its not controlling volume in a proper scale between [0,100] percentage.This sample code I got from here Set ALSA master volume from C code
//volume.c
#include<stdio.h>
#include<alsa/asoundlib.h>
int main(int argc, char *argv[])
{
long volume=atoi(argv[1]);
long min, max;
snd_mixer_t *handle;
snd_mixer_selem_id_t *sid;
const char *card = "hw:0";
const char *selem_name = "Speaker";
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);
}
//compiling source code
gcc volume.c -o volume -lasound
//set volume of speaker 90%
./volume 90
The above program is not setting up volume in proper percentage which I am passing as an argument.
Percentage given to volume program -> actual set volume
0->0
10->2
20->6
30->10
40->15
50->22
60->31
70->41
80->56
90->75
100->100
As shown above when I am setting volume between range [0,100]%, I am getting resulted volume in different scale.Ex. for value 40 it will set volume to 15%.
How can I setup volume in proper scale between [0,100] percentage ?