Skip to content

Commit

Permalink
added app-volume.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
ghosty-be committed Nov 28, 2022
1 parent b78c761 commit d597ef1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
This script will create a virtual microphone for PulseAudio to use and set it as the default device, playing an mp3 file in a loop
## test-sd-x.sh
This script I use for testing SD cards / USB sticks for fakes or bad blocks
## app-volume.sh
This script allows to list and set volumes for apps
63 changes: 63 additions & 0 deletions app-volume.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/bash
## script description
# this script allows to
# - list the application sinks
# - set the volume for an application sink by application name
# based on https://askubuntu.com/a/1418749

## global variables
SCRIPT=`basename $0`
DEBUG=false

(${DEBUG}) && echo "debug mode active"
(${DEBUG}) && set -x

#warning, this will terminate the script on errors
#do not use this if errors are expected behavior!
set -e

## display usage on invalid amount of arguments or help arguments
function usage() {
echo "usage: $SCRIPT {list|set <app> <volume>}"
echo "arguments"
echo " argument argument description"
echo " list list app sinks"
echo " set <app> <volume> set volume for <app> to <volume>%"
exit 1
}

function set-app-volume() {
player="$1"
volume="$2"
# get specific app sink
firstPlayerSinkIndex="$(pacmd list-sink-inputs | awk '/index:|application.name |application.process.binary / {print $0}' | sed 's/^ *//g ; s/.*= //' | grep -iB 1 "${player}" | awk '/index:/ {print $2; exit}')"
# I don't see the need to do this calculation here if pactl takes percentages direct anyway...
# 100% → 65536
#[[ $firstPlayerSinkIndex ]] && pacmd set-sink-input-volume "$firstPlayerSinkIndex" "$((volume*65536/100))"
[[ $firstPlayerSinkIndex ]] && pactl set-sink-input-volume "$firstPlayerSinkIndex" "${volume}%"
}

function list-app-sinks() {
pacmd list-sink-inputs | awk '/index:|volume: |application.name |application.process.binary / {print $0}' | sed 's/^[ \t]*//g'
}

case "$1" in
list)
list-app-sinks
;;
set)
#if arguments < 3 show usage
if [ $# -ne 3 ]
then
usage
fi
set-app-volume $2 $3
;;
*)
usage
;;
esac

(${DEBUG}) && set +x

exit 0

0 comments on commit d597ef1

Please sign in to comment.