4

Is there a way to detect if any sound is playing (using a bash script)?

(I'm on Ubuntu 11.10 using pulseaudio.)

I plan on using it in an indicator to visually remind me sound is playing while the mute is on.

dgo.a
  • 831
  • 3
  • 12
  • 23

1 Answers1

6

Using pulse-audio: You can try pactl list to see what pulse audio is doing with the sound hardware. I'm leaving the specifics of pulling out the status to you. For e.g.: This command would list the Sink and Source states.

pactl list | grep State

Using procfs for ALSA (Caveat: these proc entries might go away in the future): This snippet runs through the pcm playback devices in ALSA proc heirarchy.

if grep -q RUNNING /proc/asound/card*/*p/*/status 2>&1; then
   echo "Playing"
else
   echo "Idle"
fi
Anil
  • 618
  • 5
  • 6
  • Thanks a lot! `pactl list` is working for me. Volume, mute status, etc. How did you find this out? I've been searching for "pulseaudio status" and nothing this simple came up. – dgo.a Feb 25 '12 at 13:35
  • No problem :-) Find out the tools that come along with pulseaudio, then take a guess on the which one might be appropriate, check the man pages, try running it. – Anil Feb 25 '12 at 15:14
  • `pactl list` output is locale-dependent and will be shown according to `$LANG`. – That Brazilian Guy Feb 06 '22 at 20:34
  • `pactl` has a "short" format that's meant for parsing in scripts according to `man pactl`. In my case, I can use `pactl list short sinks | cut --fields=5` to get the `RUNNING`/`IDLE` state directly. – Shane S. May 29 '22 at 18:45