markijbema markijbema - 2 months ago 8
Linux Question

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

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


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.