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

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

  • Do you mean a tool to check volume level? – Eddy_Em Jan 15 '13 at 11:56
  • no, check if there is something playing or not – RSFalcon7 Jan 15 '13 at 13:12
  • 1
    then the only wariant I guess is to check "Mix" in alsamixer and try to record a little from soundcard, then analize volume in recorded file. If it is larger than a treshold, you can mean that there's nothing playing. Try for example sox. In other case you can try lsof|grep snd. If there's nothing you can turn radio on. – Eddy_Em Jan 15 '13 at 14:03
  • Also asked on Superuser – glenn jackman Jan 15 '13 at 14:05

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]}'
  • I'm not sure what I'm using, but this script didn't work for me. (It "returns" no in any situation, playing or not) – RSFalcon7 Jan 15 '13 at 13:40
  • Sorry, I'd missed that part. I still don't think this is doing what's requested: if an application has the device open but isn't feeding it any data, wouldn't it give a false positive? – Gilles 'SO- stop being evil' Jan 16 '13 at 10:31
  • 1
    @Gilles, in my tests (pausing mplayer), it gives State: CORKED instead of State: RUNNING in that case so it would still work. If the application feeds silence data (is playing silence), then I suppose it would still show RUNNING, but in that case, I see no other alternative than using pamon on the monitor device of all the sinks and check for silence. – Stéphane Chazelas Jan 16 '13 at 10:47
  • I tested and it's working. Thanks @Stephane I would give you all my upvotes if I could! – RSFalcon7 Jan 16 '13 at 11:50

If I understand you right, here an example:


fuser /dev/snd/timer && echo "Something is playing" || echo "There's silence"
There's silence

I turn on audacious:

fuser /dev/snd/timer && echo "Something is playing" || echo "There's silence"
/dev/snd/timer:      47663
Something is playing

For OSS chande /dev/snd/timer to /dev/dsp.

This is a very dependent method.

  • From the my homework here ( linux.amazingdev.com/blog/archives/000909.html ) it seems possible that a process is holding the device but not actually play anything. Is that true? (If so, it's not useful) – RSFalcon7 Jan 15 '13 at 14:23
  • Yes, it's true. If you press "pause" or "mute" it will be so. But in this case you cannot be absolutely sure that after a couple of seconds player won't play again. Or you can combine this method & write from soundcard. – Eddy_Em Jan 15 '13 at 14:36
  • In this case I'm looking for a way to overcome the player failures, so unfortunately this won't work – RSFalcon7 Jan 15 '13 at 14:59

Assuming you are using ALSA mixer for your sound, you can use amixer to query information about audio devices. An audio device typically has several audio channels - or controls - each which can be muted and unmuted. For example to check if the Master control is muted you would do something like:

if amixer get Master | grep -q off; then
    # action

Use the set option to perform things on the controls. See man amixer for more help.

  • Perhaps there was a little ambiguity on the question, I edited. Unfortunatly amixer didn't work, I tested over 15 min with absolutely no player opened, and it is still reporting "unmuted" – RSFalcon7 Jan 15 '13 at 13:36
  • This checks whether the sound channel is muted, not whether there is something playing. – Gilles 'SO- stop being evil' Jan 15 '13 at 23:15

htor's answer will probably work, but it is entirely possible that "off" will appear somewhere else in the output, causing an incorrect result. Here is a more strict check using GNU awk:

if amixer get Master | gawk 'END{ if ( $NF != "[off]" ) { exit 1 } }'; then
    # muted
    # unmuted
  • My amixer's last line has no $6: “ Mono: Playback [on]”. (amixer version 1.0.26) Maybe $NF would be more portable. – manatwork Jan 15 '13 at 12:39
  • @manatwork Thanks for the note, the output looked portable, but I guess I am easily led ;-) – Chris Down Jan 15 '13 at 12:49
  • This checks whether the sound channel is muted, not whether there is something playing. – Gilles 'SO- stop being evil' Jan 15 '13 at 23:17
  • @Gilles - When I posted this answer, the question did not describe that problem. – Chris Down Jan 17 '13 at 7:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.