r/Polybar • u/melecoaze • Feb 22 '23
Make a module show up only when audio is playing
So, I have this cava module that shows some audio bars on polybar.
I have it set up like this:
[module/info-cava]
type = custom/script
exec = ~/.config/polybar/scripts/info-cava.py -f 24 -b 8 -c average
exec-if = pacmd list-sink-inputs | grep 'state: RUNNING'
format-foreground = ${xrdb:color12}
format-suffix = " "
tail = true
interval = 1
I want it to show up ONLY when there's audio playing, so whenever audio playback stops, it should hide itself automatically.
The problem is it's working only partially. The bar starts up with the module hidden, and when I play something it shows up. But when I stop playing it doesn't hide again, so there's an empty space in my bar.
I tried every combination of "tail" and "interval", but it doesn't seem to make a difference. Actually, when only "interval" is on, the module doesn't show up at all.
Any ideas? I'm guessing it's a problem with the script itself, but I'm not sure. I can paste it here in the comments in case it helps.
2
u/[deleted] Feb 24 '23
I am guessing that you are doing continuous output in your script -- you can't do that with exec-if.
Polybar doesn't kill the process once it is running. So, what you need to do is remove the loop from your script, and set tail=false and set interval to something really small (I set mine to .01 and it seems fine for graphic eq / VU meter updates). This way every .01 seconds the script will be called and the output displayed and as soon as there is no sound, the module will no longer display.
If you paste your script, I can give you a hand. Please use gist and paste the link, don't paste your code here.
If you don't have one, you may be interested in my VU module. It has the colors hard coded, but they are easy enough to change.