I run into the same problem than exposed in this post: ALSA volume ignored when beginning playback
Here is the context:
- OS: raspbian
- Kernel 3.10.36+ #665
- just updated the firmware with rpi-update
- pulse-audio not installed, only ALSA
So the problem occurs when setting the volume level with alsa-lib, and then running an audio player in a C++ program.
Here is what I do (in my C++ program), and the behaviour I get:
- set the volume at a low level (ex: 30%) with alsa-lib. Code to set the volume is provided later in the post
- Alsamixer (monitored in another ssh session) shows that the volume has been modified (or at least the indicated level...)
- fork in order to run my audio player (VLC in my case). The playback starts at an incorrect volume level (100%?). But alsamixer still displays the previously set volume
- after 5 seconds, call again the alsa-lib to change the volume
- the volume is modified when looking at alsamixer and the sound is played at a lower level (whereas I increased the volume, meaning that the volume was incorrect when starting the playback)
So it seems that the initial volume level is set but ignored when starting the playback.
In the post mentionned at the beginning of this post, there is a reply about a patch for ALSA driver. But I think it is already applied in my case, as the modifications on source were 2 years ago (and I just updated my OS).
But do I really get the patch? How can I know that?
Here is the result of "modinfo" command:
pi@rasp /home/pi/ $ modinfo snd_bcm2835
filename: /lib/modules/3.10.36+/kernel/sound/arm/snd-bcm2835.ko
alias: platform:bcm2835_alsa license: GPL description:
Alsa driver for BCM2835 chip author: Dom Cobley srcversion:
0D8794C6E1A26AEFC7F8534 depends: snd-pcm,snd intree: Y
vermagic: 3.10.36+ preempt mod_unload modversions ARMv6 parm:
force_bulk:Force use of vchiq bulk for audio (bool)
If I already have the patch what can I do to solve this issue?
Here is the code use to set the volume with alsa-lib:
// ALSA mixer handle
snd_mixer_t *m_handle;
snd_mixer_elem_t* m_elem;
long volume = -1000; // This volume is in the allowed range specified by "amixer"
// Open an empty mixer
snd_mixer_open(&m_handle), SND_MIXER_ELEM_SIMPLE);
snd_mixer_attach(m_handle, "hw:0");
snd_mixer_selem_register(m_handle, NULL, NULL);
// Load the mixer elements
snd_mixer_load(m_handle);
// Configure the simple element we are looking for
snd_mixer_selem_id_t *simpleElemId; // mixer simple element
snd_mixer_selem_id_alloca(&simpleElemId);
snd_mixer_selem_id_set_index(simpleElemId, 0);
snd_mixer_selem_id_set_name(simpleElemId, "PCM");
m_elem = snd_mixer_find_selem(m_handle, simpleElemId);
// Here is set the global system volume
snd_mixer_selem_set_playback_volume_all(m_elem, volume);
snd_mixer_detach(m_handle, "hw:0");
snd_mixer_close(m_handle);
Any help or suggestion would be very appreciated :) Thank you