Pulseaudio with two sources, reduce volume for one

I'm watching NHL playoffs in Firefox, but during commercials I'm testing the music player I'm developing. I "unpause" my music player during commercials so there are two sound sources playing in pulseaudio. I would like my music player (python name mserve) to take "sound source priority" over Firefox.

Currently pulseaudio already seems to dim sound from Firefox a little bit when mserve is playing. The sound dimming is maybe 20 to 40%? How can I increase this such that pulseaudio is dimming Firefox say 60% to 80%?

If there is software that automatically recognizes when commercials start, eg sound gain increases or inaudible to human-ear-sounds commercial tracking software activated, that would save me from clicking "play" when commercials start and "pause" when hockey resumes.

Note: I assume it is pulseaudio and not alsa doing the heavy-lifting in the two input stream sound blender. I'm not looking for a GUI sound mixer, rather a bash or python solution.

1 Answer

Short Answer

In Unix & Linux site I found this Q&A:

First get short list of sinks:

$ pactl list short sink-inputs
1545 0 1083 protocol-native.c float32le 2ch 48000Hz
1556 0 1083 protocol-native.c float32le 2ch 48000Hz
1688 0 1083 protocol-native.c float32le 2ch 44100Hz
1698 0 1455 protocol-native.c s16le 2ch 44100Hz

Remove short from above command to confirm Firefox (in my case) is actually 1688. My music player was just loaded and is the last input: 1698. When pressing play on my music plyaer, I set Firefox volume to 40% with:

$ pactl set-sink-input-volume 1688 40%

When pressing pause on music player I reset Firefox volume to 100% with:

$ pactl set-sink-input-volume 1688 100%

Long Answer

40% volume wasn't soft enough so 25% was used.

There can be multiple instances of Firefox sound sources so all of them need to be muted.

Here's the final code:

 def hockey_commercial(self): ''' Commercial button plays music for 90 seconds then pauses. ''' if self.play_hockey_active is True: return # Already running, don't restart self.play_hockey_active = True self.play_hockey_secs = 90 self.play_hockey_remaining = self.play_hockey_secs self.play_soften_firefox() # Soften volume of commercials self.play_hockey_tstart = time.time() if self.pp_state is "Paused": self.pp_toggle() # Play music during commercials def play_soften_firefox(self): result = [] self.play_firefox_indices = [] result = os.popen('pactl list short sink-inputs') \ .read().strip().splitlines() count = len(result) if count < 2: print('result missing firefox:',result) return # Firefox may have multiple entries for line in result: sink = line.split('\t')[0] app = os.popen('pactl list sink-inputs | grep "Sink Input #' + \ sink + '" -A20 | grep application.name').read() if "Firefox" in app: self.play_firefox_indices.append(sink) result = os.popen('pactl set-sink-input-volume ' + \ sink + ' 25%') \ .read().strip().splitlines()

If you are curious what it looks like in action (be kind only my second program):

mserve commercial.gif

  • The music player is paused while watching hockey
  • When a commercial starts click the Commercial button with hockey stick character
  • The button text changes to time remaining countdown
  • Volume for Firefox is cut to 25% which is barely audible
  • Music starts playing at normal volume
  • When countdown hits zero music pauses and Firefox volume is restored
  • You can still press pause/play during countdown

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like