how to read out volume level of clients of pulseaudio in the console

7.3k views Asked by At

I would like to read out the volume of the audio played at the moment for the several clients of pulseaudio.

The problem I like to solve is the following: I'm listening to music in xmms, then I put it on pause, to listen to a song a friend sent me on youtube. After an hour, I suddenly discover I am not listening to any music!

The (very basic) solution I was thinking of is a bash scripts which just checks the volume of all apps other than xmms every second, if any application is making sound, xmms is paused, if there is no sound, and xmms is silent, xmms is enabled. (I do want to be able to do this app-wise, for instance, pidgin should be ignored)

I could only find graphical tools to read out the volume, like pavucontrol, which displays it nicely. I really would not like to code all kinds of C programs to do such a simple thing, so:

  1. Am I thinking in the right direction, or is there a simpler solution
  2. If there isn't, how do I readout the current level of the volume for the seperate apps
3

There are 3 answers

6
Thomas On BEST ANSWER

Perhaps you can record one sample of audio from the output stream and see if it is (close to) 0. This pipeline gives you a single sample, in the form of a number between -32768 and 32767 (inclusive):

parec --raw --channels=1 --latency=2 2>/dev/null | od -N2 -td2 | head -n1 | cut -d' ' -f2- | tr -d ' '

You'll need to adjust the parec arguments, and possibly the PulseAudio configuration, to tap into the output stream and record from that.

1
Ignacio Vazquez-Abrams On

I wouldn't even bother reading the volumes. I'd write a module which contains a sink which detects the volume of apps connected to it and can perform actions based on that as well as a virtual application that you can direct to an existing sink for eventual audio output.

1
Thomas On

pactl list dumps a list of... well, it seems pretty much everything. With a running mplayer -ao pulse instance, I get the following among the output:

Sink Input #2
        Driver: protocol-native.c
        Owner Module: 8
        Client: 10
        Sink: 0
        Sample Specification: s16le 2ch 44100Hz
        Channel Map: front-left,front-right
        Mute: no
        Volume: 0: 100% 1: 100%
                0: 0.00 dB 1: 0.00 dB
                balance 0.00
        ...
        Properties:
                media.name = "audio stream"
                application.name = "MPlayer"
                native-protocol.peer = "UNIX socket client"
                ....
                application.process.binary = "mplayer"
                ...

It doesn't give you the current monitor levels (volume of currently playing audio), though, but maybe it's sufficient?