Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
    
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
add comment

4 Answers

up vote 2 down vote accepted

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]}'
share|improve this answer
    
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 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. –  Stephane 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
add comment

If I understand you right, here an example:

Silence:

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.

share|improve this answer
    
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
add comment

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
fi

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

share|improve this answer
    
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 Jan 15 '13 at 23:15
add comment

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
else
    # unmuted
fi
share|improve this answer
    
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 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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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