c – Local (per application) setting volume with ALSA API
I use snd_pcm_* functions in my application to control sound. I know that I am able to change volume by multiplying samples by constant value (representing volume) but it is not professional. Setting volume in this manner result in delayed volume change (due to buffering). To prevent it I can clear buffer but it produces pauses in playback. It is unacceptable.
I found that I can change global setting by this code:
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);
}
But… it controls GLOBAL volume (per system or per user) not local (for process, for thread or for a PCM stream). I need to control local volume (per stream or per thread or per process) in my application for linux.
Probably I need to use other parameters in snd_mixer_selem_id_set_index or snd_mixer_selem_id_set_name call but I cannot found decent manual for it. The ALSA project documentation sucks so good.
How to modify above code to control local (per stream / per thread / per process) volume?
Read more here: Source link
