From 8ffd810a4fab5fd35e6b7c637820b4737cf3e9b3 Mon Sep 17 00:00:00 2001 From: Burathar Date: Mon, 16 Nov 2020 14:16:02 +0100 Subject: [PATCH] Initial files --- .gitignore | 1 + README.txt | 11 +++++++ set_all_sink.sh | 40 +++++++++++++++++++++++ set_current_sink_volume.sh | 67 ++++++++++++++++++++++++++++++++++++++ stdlib.sh | 32 ++++++++++++++++++ 5 files changed, 151 insertions(+) create mode 100644 .gitignore create mode 100644 README.txt create mode 100755 set_all_sink.sh create mode 100755 set_current_sink_volume.sh create mode 100644 stdlib.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..daa0f24 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +sink_names.sh diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..94f3376 --- /dev/null +++ b/README.txt @@ -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 diff --git a/set_all_sink.sh b/set_all_sink.sh new file mode 100755 index 0000000..77710c1 --- /dev/null +++ b/set_all_sink.sh @@ -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] " + 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 diff --git a/set_current_sink_volume.sh b/set_current_sink_volume.sh new file mode 100755 index 0000000..585445a --- /dev/null +++ b/set_current_sink_volume.sh @@ -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] " + 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) diff --git a/stdlib.sh b/stdlib.sh new file mode 100644 index 0000000..41a0701 --- /dev/null +++ b/stdlib.sh @@ -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 +} +