Linux – Testing if audio devices / sound cards are currently playing

audiodeviceslinuxpulseaudioscripting

Is there a default program where I can check if my audio devices are in silent?

Edit: By silence, I mean that if there is something playing on that (not just activated or opened)

Something like this:

if [[ device0 is silent ]] ; then
    radio $RANDOM
fi

Edit 2: What I'm trying to achieve is a script that plays radio and can keep playing when the player fails, e.g. if the internet connection goes down and the player didn't recovery, I will kill the player and start over again

Best Answer

If you're using PulseAudio (Gnome-based Linux distributions tend to use PulseAudio, you can check if one is running with ps -C pulseaudio) and you want to know whether some applications are sending any data to any "sink", you could do:

pacmd list-sink-inputs | grep -c 'state: RUNNING'

Still with PulseAudio, if you want to check whether your sound output is muted, there might be simpler but you can get the "mute" status of the default "sink" using:

pacmd dump | awk '
  $1 == "set-sink-mute" {m[$2] = $3}
  $1 == "set-default-sink" {s = $2}
  END {print m[s]}'
Related Question