Xfce Forum

Sub domains
 

You are not logged in.

#1 2018-01-03 00:31:24

Misko_2083
Member
Registered: 2015-10-13
Posts: 191
Website

pulseaudio volume

Happy New Year ToZ
Here is a small pulse audio volume control icon for your XFCE tray.
https://i.imgur.com/WcK42C7.gif
Works perfectly in xfce with faenza-dark icon theme.
Requires a bit newer yad, xdotool, notify-send.

#!/bin/bash
#######################################################
# Description:                                        #
#   bash script to set the pulseaudio volume          #
#   via yad UI  -  author Misko_2083                  #
#######################################################

ERR(){ echo "ERROR: $1" 1>&2; }

declare -i DEPCOUNT=0
for DEP in /usr/bin/{xdotool,yad,pactl,pacmd,notify-send}; {
	[ -x "$DEP" ] || {
		ERR "$LINENO Dependency '$DEP' not met."
		DEPCOUNT+=1
	}
}
[ $DEPCOUNT -eq 0 ] || exit 1

export YAD_PULSE=$(mktemp -u --tmpdir YAD_PULSE.XXXXXXXX)
mkfifo "$YAD_PULSE"
trap "rm -f $YAD_PULSE" EXIT

yad_pulse_audio(){
 # set window class
 YAD_CLASS="YAD_PULSE_AUDIO"

 # Unmap the window if the script is relaunched
   pids="$(xdotool search --class "$YAD_CLASS")"
   wpid="$(xdotool getwindowfocus)"

   for pid in $pids; do
        # Compares window class pid with the pid of a window in focus
        if [[ "$pid" == "$wpid" ]]; then
           xdotool windowunmap $pid
           exit 1
        fi
   done

 SINK="$(pacmd list-sinks | awk '/* index: [0-9]+/{ print $3 }')"

# current volume snippet taken
# from pulseaudio-ctl <https://github.com/graysky2/pulseaudio-ctl>
# by graysky <graysky@archlinux.us>, licensed under MIT
CURVOL="$(pacmd list-sinks | grep -A 15 '* index' | awk '/volume: /{ print $3 }' | grep -m 1 % | sed 's/[%|,]//g')"
[ -n "$CURVOL" ] || CURVOL="$(pacmd list-sinks | grep -A 15 '* index' | awk '/volume: front/{ print $5 }' | sed 's/[%|,]//g')"
[ -n "$CURVOL" ] || CURVOL=100

#printf "Current volume: %3d%%\n" "$CURVOL"
#printf "\rVolume: %3d%%" "$CURVOL"
#pactl set-sink-volume "$SINK" "$CURVOL%"

yad --scale \
    --print-partial \
    --text="  Volume Control                   " \
    --min-value=0 --max-value=100 \
    --image="preferences-desktop-sound" \
    --borders="12" \
    --no-buttons  \
    --undecorated \
    --close-on-unfocus \
    --on-top \
    --skip-taskbar \
    --mouse \
    --sticky \
    --class="$YAD_CLASS" \
    --value="$CURVOL" 2> /dev/null | \
while read VOL; do
    set_notification_icon $(printf "%3d" "$VOL")
    pactl set-sink-volume "$SINK" "$VOL%"
done
}
export -f  yad_pulse_audio

function set_notification_icon
{
    LANG=C.UTF-8

  if [[ "$1" -ge "68" ]]; then
    echo "icon:notification-audio-volume-high"

  elif [[ "$1" -ge "34" && "$1" -lt "68" ]]; then
    echo "icon:notification-audio-volume-medium" 

  elif [[ "$1" -ge "1" && "$1" -lt "34" ]]; then
    echo "icon:notification-audio-volume-low" 

  elif [[ "$1" -lt "1" ]]; then
    echo "icon:notification-audio-volume-off" 
  fi

  echo "tooltip:Volume $1 %" 
  echo "menu:About!sh -c 'notify-send -i notification-audio-volume-high -t 8000 \"Pulseaudio Volume Control\" \"by Misko_2083\"'!gtk-about||Quit!quit!gtk-quit"
}
export -f set_notification_icon

exec 3<> $YAD_PULSE

yad --notification --command="bash -c 'yad_pulse_audio >$YAD_PULSE'" \
 --listen <&3 & notifpid=$!

until xdotool getwindowname $(xdotool search --pid "$notifpid" | tail -1) &>/dev/null; do
        # sleep until the window opens
        sleep 0.5       
done

SINK="$(pacmd list-sinks | awk '/* index: [0-9]+/{ print $3 }')"

# current volume snippet taken
# from pulseaudio-ctl <https://github.com/graysky2/pulseaudio-ctl>
# by graysky <graysky@archlinux.us>, licensed under MIT
CURVOL="$(pacmd list-sinks | grep -A 15 '* index' | awk '/volume: /{ print $3 }' | grep -m 1 % | sed 's/[%|,]//g')"
[ -n "$CURVOL" ] || CURVOL="$(pacmd list-sinks | grep -A 15 '* index' | awk '/volume: front/{ print $5 }' | sed 's/[%|,]//g')"
[ -n "$CURVOL" ] || CURVOL=100

set_notification_icon $(printf "%3d" "$CURVOL") >&3

# Waits for notification to exit
wait $notifpid

exec 3>&-

exit 0

Last edited by Misko_2083 (2018-01-04 12:08:46)


Do you want to exit the Circus?
https://www.youtube.com/watch?v=ZJwQicZHp_c

Offline

#2 2018-01-03 01:31:11

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 10,985

Re: pulseaudio volume

Happy new year, Misko_2083.

This looks really interesting but how does this work? Did you create a launcher in the panel and assign it this script? It doesn't seem to do anything on my system. I've got yad 0.4.0 installed.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#3 2018-01-03 01:57:06

Misko_2083
Member
Registered: 2015-10-13
Posts: 191
Website

Re: pulseaudio volume

ToZ wrote:

This looks really interesting but how does this work? Did you create a launcher in the panel and assign it this script? It doesn't seem to do anything on my system. I've got yad 0.4.0 installed.

The script creates a notification icon and on click calls undecorated yad --scale dialog with assinged window class "YAD_PULSE_AUDIO". That dialog must have a focus because it closes if it's unfocused.
On openbox I had to use devilspie to make it work because it doesn't get focus. On xfce4 it has focus.
That dialog is unmapped with xdotool if it's called again.

# Unmap the window if the script is relaunched
 if [[ $(pgrep -c "$(basename $0)") -ne 1 ]]; then
   pids="$(xdotool search --class "$YAD_CLASS")"
   wpid="$(xdotool getwindowfocus)"

   for pid in $pids; do
        # Compares window class pid with the pid of a window in focus
        if [[ "$pid" == "$wpid" ]]; then
           xdotool windowunmap $pid
           exit 1
        fi
   done
 fi

Make sure to have pulseaudio-utils package installed and that the script name doesn't have spaces in name.
------
btw. I made a simpler script to show date and time if it's assinged to the panel launcher.

#!/bin/bash

# set window class
YAD_CLASS="YAD_CLOCK"

ERR(){ echo "ERROR: $1" 1>&2; }

declare -i DEPCOUNT=0
for DEP in /usr/bin/{xdotool,yad}; {
	[ -x "$DEP" ] || {
		ERR "$LINENO Dependency '$DEP' not met."
		DEPCOUNT+=1
	}
}
[ $DEPCOUNT -eq 0 ] || exit 1

# Ensure only one instance of this scipt can start
if [[ $(pgrep -c "$(basename $0)") -ne 1 ]]; then
   pids="$(xdotool search --class "$YAD_CLASS")"
   wpid="$(xdotool getwindowfocus)"

   for pid in $pids; do
        # Compares window class pid with the pid of a window in focus
        if [[ "$pid" == "$wpid" ]]; then
           xdotool windowunmap $pid
           exit 1
        fi
   done
fi

while true
 do
   echo "1:#$(date '+%T')"
   echo "2:#$(date '+%D %a')"
   sleep 1
 done | yad --multi-progress \
             --bar="Time":NORM \
             --bar="Date":NORM \
             --window-icon="clock" \
             --image="clock" \
             --no-buttons  \
             --undecorated \
             --close-on-unfocus \
             --on-top \
             --skip-taskbar \
             --mouse \
             --sticky \
             --class="$YAD_CLASS"

exit 0

Last edited by Misko_2083 (2018-01-03 02:05:37)


Do you want to exit the Circus?
https://www.youtube.com/watch?v=ZJwQicZHp_c

Offline

#4 2018-01-03 11:47:30

Jerry3904
Member
Registered: 2013-11-09
Posts: 853

Re: pulseaudio volume

What advantage does this have over the already available Volume Icon? We supply that in the NA by default where a click gets a slider control (without icon or label)


MX-23 (based on Debian Stable) with our flagship Xfce 4.18.

Offline

#5 2018-01-04 02:17:15

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 10,985

Re: pulseaudio volume

@Misko_2083, the date/time script works but the volume icon does not. It just hangs. Here is the output of the script with bash debugging enabled:

$ bash -x vvv
+ declare -i DEPCOUNT=0
+ for DEP in /usr/bin/{xdotool,yad,pactl,pacmd,notify-send}
+ '[' -x /usr/bin/xdotool ']'
+ for DEP in /usr/bin/{xdotool,yad,pactl,pacmd,notify-send}
+ '[' -x /usr/bin/yad ']'
+ for DEP in /usr/bin/{xdotool,yad,pactl,pacmd,notify-send}
+ '[' -x /usr/bin/pactl ']'
+ for DEP in /usr/bin/{xdotool,yad,pactl,pacmd,notify-send}
+ '[' -x /usr/bin/pacmd ']'
+ for DEP in /usr/bin/{xdotool,yad,pactl,pacmd,notify-send}
+ '[' -x /usr/bin/notify-send ']'
+ '[' 0 -eq 0 ']'
++ mktemp -u --tmpdir YAD_PULSE.XXXXXXXX
+ export YAD_PULSE=/tmp/YAD_PULSE.902trBHN
+ YAD_PULSE=/tmp/YAD_PULSE.902trBHN
+ mkfifo /tmp/YAD_PULSE.902trBHN
+ trap 'rm -f /tmp/YAD_PULSE.902trBHN' EXIT
+ export -f yad_pulse_audio
+ export -f set_notification_icon
+ exec
+ notifpid=13439
+ yad --notification '--command=bash -c '\''yad_pulse_audio &>/tmp/YAD_PULSE.902trBHN'\''' --listen --
++ xdotool search --pid 13439
++ tail -1
+ xdotool getwindowname
+ sleep 0.5
++ xdotool search --pid 13439
++ tail -1
+ xdotool getwindowname 98566147
++ pacmd list-sinks
++ awk '/* index: [0-9]+/{ print $3 }'
+ SINK=0
++ pacmd list-sinks
++ grep -A 15 '* index'
++ awk '/volume: /{ print $3 }'
++ grep -m 1 %
++ sed 's/[%|,]//g'
+ CURVOL=
+ '[' -n '' ']'
++ pacmd list-sinks
++ grep -A 15 '* index'
++ awk '/volume: front/{ print $5 }'
++ sed 's/[%|,]//g'
+ CURVOL=55
+ '[' -n 55 ']'
++ printf %3d 55
+ set_notification_icon 55
+ LANG=C.UTF-8
+ [[ 55 -ge 68 ]]
+ [[ 55 -ge 34 ]]
+ [[ 55 -lt 68 ]]
+ echo icon:notification-audio-volume-medium
+ echo 'tooltip:Volume 55 %'
+ echo 'menu:About!sh -c '\''notify-send -i notification-audio-volume-high -t 8000 "Pulseaudio Volume Control" "by Misko_2083"'\''!gtk-about||Quit!quit!gtk-quit'
+ wait 13439

I like the idea though. It's a simple way to create pseudo-tray applications.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#6 2018-01-04 11:28:54

Misko_2083
Member
Registered: 2015-10-13
Posts: 191
Website

Re: pulseaudio volume

Jerry3904 wrote:

What advantage does this have over the already available Volume Icon? We supply that in the NA by default where a click gets a slider control (without icon or label)

The only advantage is it's possible to amplify the sound output. I would not set to the levels over 150 because overamplifying causes cliping and sound distorsion and can posibly damage the speakers. smile
yad --scale --min-value=0 --max-value=150

@ToZ There is a typo  '--&' in here, also we don't need to redirect stderr to that fifo &>$YAD_PULSE

yad --notification --command="bash -c 'yad_pulse_audio &>$YAD_PULSE'" \
 --listen <&3 --& notifpid=$!

From your bash -x output I can see that the notification icon is loaded.
Let's see what is going on if there is 'bash -x' in the command notification icon launches on left-click.
change the line that launches notification icon to:

yad --notification --command="bash -x -c 'yad_pulse_audio >$YAD_PULSE'" \
 --listen <&3 & notifpid=$!

Run the scrip it in terminal and click the icon.

./snd2
+ yad_pulse_audio
+ notify-send 'opening window'
+ YAD_CLASS=YAD_PULSE_AUDIO
+++ basename bash
++ pgrep -c bash
+ [[ 4 -ne 1 ]]
++ xdotool search --class YAD_PULSE_AUDIO
+ pids=
++ xdotool getwindowfocus
+ wpid=98566148
++ pacmd list-sinks
++ awk '/* index: [0-9]+/{ print $3 }'
+ SINK=1
++ pacmd list-sinks
++ grep -m 1 %
++ grep -A 15 '* index'
++ sed 's/[%|,]//g'
++ awk '/volume: /{ print $3 }'
+ CURVOL=
+ '[' -n '' ']'
++ pacmd list-sinks
++ sed 's/[%|,]//g'
++ grep -A 15 '* index'
++ awk '/volume: front/{ print $5 }'
+ CURVOL=100
+ '[' -n 100 ']'
+ yad --scale --print-partial '--text=  Volume Control                   ' --min-value=0 --max-value=100 --image=preferences-desktop-sound --borders=12 --no-buttons --undecorated --close-on-unfocus --on-top --skip-taskbar --mouse --sticky --class=YAD_PULSE_AUDIO --value=100
+ read VOL

If you get the output like thisthe window is being misplased off the screen.


Do you want to exit the Circus?
https://www.youtube.com/watch?v=ZJwQicZHp_c

Offline

Board footer

Powered by FluxBB