Mute music while something else plays audio
I wanted to mute my music when another application (mpv or browser) started playing an audio stream and unmute the music when that stream ended. To do this I had a look at the pulseaudio API and started investigating how I could go about doing this. Quickly someone pointed me to the modules page on the pulseaudio wiki, specifically the role ducking module.
"This module lowers the volume of less important streams when a more important stream appears, and raises the volume back up once the important stream has finished (this is called "ducking")."
This is exactly what I wanted. Programs run with their media.role set to "browser" will make anything run with the media.role of "music" mute themselves. Implementing it into my environment was straight forward:
- load the module using: pacmd load-module module-role-ducking trigger_roles="browser" volume=0
- load the module on boot using: echo "load-module module-role-ducking trigger_roles="browser" volume=0" >>/etc/pulse/default.pa"
- add "PULSE_PROP='media.role=browser'" to any applications environment that I want to mute my music
- run cmus with the environment "PULSE_PROP='media.role=music'"
- patch urlopener to also run programs with "PULSE_PROP='media.role=browser'", see the commit here