Browse Source

Initial files

master
Burathar 4 years ago
commit
8ffd810a4f
  1. 1
      .gitignore
  2. 11
      README.txt
  3. 40
      set_all_sink.sh
  4. 67
      set_current_sink_volume.sh
  5. 32
      stdlib.sh

1
.gitignore vendored

@ -0,0 +1 @@ @@ -0,0 +1 @@
sink_names.sh

11
README.txt

@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
This is a small pulseaudio script library, written to be used with keyboard shortcuts. However, it can also help in command line audio control.
IMPORTANT: set_all_sink.sh sources a file called sink_names.sh, this file should define a variable named $bluetooth_sink_name containing the identifying string for a specific device (i.e. the MAC address) like this:
#sink_names.sh
#!/bin/bash
bluetooth_sink_name="00_00_00_00_00_00"
To change the audio output device, use set_all_sink.sh
To change the master volume, use set_current_sink_volume.sh

40
set_all_sink.sh

@ -0,0 +1,40 @@ @@ -0,0 +1,40 @@
#!/bin/bash
source stdlib.sh
source sink_names.sh
while getopts 'vs' opt ; do
case "$opt" in
v) verbosity=VERBOSE ;;
s) verbosity=SILENT ;;
esac
done
newSink=${@:$OPTIND:1}
if [ -z "$newSink" ]; then
error "Usage: $0 [OPTION] <sinkId/sinkName>"
error " -v print verbose output"
error " -s don't print any output"
error "Valid sinks:"
error "$(pactl list short sinks)"
exit 1
fi
case "$newSink" in
'bluetooth')
newSink=`pactl list short sinks | grep "$bluetooth_sink_name" | cut -f 1` ;;
'analog')
newSink=`pactl list short sinks | grep analog-stereo | cut -f 1` ;;
'hdmi')
newSink=`pactl list short sinks | grep hdmi-stereo-extra1 | cut -f 1` ;;
esac
pactl list short sink-inputs 2> >(error) |while read stream; do
streamId=$(echo $stream|cut '-d ' -f1)
log "moving stream $streamId"
pactl move-sink-input "$streamId" "$newSink" > >(log) 2> >(error)
done
pacmd set-default-sink "$newSink" > >(log) 2> >(error) # to change toolbar item

67
set_current_sink_volume.sh

@ -0,0 +1,67 @@ @@ -0,0 +1,67 @@
#!/bin/bash
source stdlib.sh
while getopts 'vsasp' opt ; do
case "$opt" in
v) verbosity=VERBOSE ;;
q) verbosity=QUIET;;
a) relative=ADD;;
s) relative=SUBTRACT;;
p) percentage=true ;;
esac
done
value=${@:$OPTIND:1}
log "value: ${value}"
if [ -z "$value" ]; then
error "Usage: $0 [OPTION] <volume>"
error " -v print verbose output"
error " -q quiet; don't print any output"
error " -a relative; add value to current volume"
error " -s relative; subtract value from current volume"
error " -p interpret value as a percentage instead of an abosolute value"
exit 1
fi
default_sink=`pactl info 2> >(error) | grep "Default Sink" | cut -d ' ' -f 3`
default_sink_id=`pactl list sinks short | grep "$default_sink" | cut -f 1`
log "default sink: #${default_sink_id}: ${default_sink}"
if [ -n "$relative" ]; then
if [ "$percentage" = 'true' ]; then
current_percentage=`pactl list sinks 2> >(error) | grep '^[[:space:]]Volume:' | head -n $(( ${default_sink_id} + 1 )) | tail -n 1 | sed -e 's,.* \([0-9][0-9]*\)%.*,\1,'`
log "current percentage: ${current_percentage}"
value_percentage=$(echo "scale=2; "$value" / 65536 * 100" | bc 2> >(error) )
log "value percentage: ${value_percentage}"
if [ "$relative" = 'ADD' ]; then
value=$((current_percentage + value ))
elif [ "$relative" = 'SUBTRACT' ]; then
value=$((current_percentage - value ))
fi
log "new percentage: ${value}"
else
current_volume=`pactl list sinks 2> >(error) | grep '^[[:space:]]Volume:' | head -n $(( ${default_sink_id} + 1 )) | tail -n 1 | cut -d ' ' -f 3`
log "current volume: ${current_volume}"
if [ "$relative" = 'ADD' ]; then
value=$((current_volume + value))
elif [ "$relative" = 'SUBTRACT' ]; then
value=$((current_volume - value))
fi
log "new volume: ${value}"
fi
fi
if [ "$percentage" = 'true' ]; then
value=$(echo "scale=2; 65536 * ("$value" / 100)" | bc 2> >(error) )
log "new volume: ${value}"
fi
value=$( printf "%.0f" "$value" 2> >(error) )
if [ "$value" -lt 0 ]; then value=0; fi
if [ "$value" -gt 98304 ]; then value=98304; fi
log "rounded value: ${value}"
pactl set-sink-volume "$default_sink" "$value" > >(log) 2> >(error)

32
stdlib.sh

@ -0,0 +1,32 @@ @@ -0,0 +1,32 @@
#!/bin/bash
function log () {
# if argument is given, echo argument, if stream is piped, echo each line of stream
if [ "$verbosity" != 'VERBOSE' ] ; then return 0; fi
if [ -n "$1" ]; then
echo "$1"
else
while read line
do
if [ -n "$line" ]; then
echo "$line"
fi
done
fi
}
function error () {
# if argument is given, echo argument to stderr, if stream is piped, echo each line of stream to stderr
if [ "$verbosity" = 'SILENT' ]; then return 0; fi
if [ -n "$1" ]; then
echo "$1" >&2
else
while read line
do
if [ -n "$line" ]; then
echo "$line" >&2
fi
done
fi
}
Loading…
Cancel
Save