aboutsummaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authorDavid Runge <dave@sleepmap.de>2015-03-25 21:12:41 +0100
committerDavid Runge <dave@sleepmap.de>2015-03-25 21:12:41 +0100
commit51f5d81a244107165457cc644237a916d603af42 (patch)
tree2611e6b597eb9837d390d80737918b490ee3d58d /bin
parentae1e10ec24c3eeaa55b549df095863ed98cb1681 (diff)
downloaddotfiles-51f5d81a244107165457cc644237a916d603af42.tar.gz
dotfiles-51f5d81a244107165457cc644237a916d603af42.tar.bz2
dotfiles-51f5d81a244107165457cc644237a916d603af42.tar.xz
dotfiles-51f5d81a244107165457cc644237a916d603af42.zip
bin/set_volume: Rewritten to also increase/decrease volume of connected Bluetooth headset and toggle mute smartly.
Diffstat (limited to 'bin')
-rwxr-xr-xbin/set_volume120
1 files changed, 106 insertions, 14 deletions
diff --git a/bin/set_volume b/bin/set_volume
index 3168870..1652312 100755
--- a/bin/set_volume
+++ b/bin/set_volume
@@ -1,40 +1,125 @@
-#!/bin/bash
+#!/usr/bin/env bash
FUNCTIONS=$HOME/bin/functions.sh
[ -e $FUNCTIONS ] || exit 1
. $FUNCTIONS
-LED=/sys/class/leds/0003:17EF:6009.0004:amber:mute/brightness
+state_muted="/tmp/$(whoami)/state_muted"
+state_muted_headphone=""
+state_muted_speaker=""
+state_bt_headphone=""
+name_bt_headphone="bluez_sink.00_1B_66_02_36_41"
+cardselector=""
+card_icon="/usr/share/icons/gnome/48x48/devices/audio-speakers.png" # gnome-icon-theme
function increase_volume()
{
- mute amixer sset Master 10%+
+ mute amixer $cardselector sset Master 5%+
+ local state_master=$(amixer $cardselector sget Master |grep "%" | cut -d'%' -f1 | cut -d '[' -f2 | uniq)
+ #update pactl bluetooth audio to same level if present
+ if [ $state_bt_headphone = "[on]" ];then
+ pactl set-sink-volume $name_bt_headphone "$state_master%"
+ fi
+ print_volumes $state_master
}
function decrease_volume()
{
- mute amixer sset Master 10%-
+ mute amixer $cardselector sset Master 5%-
+ local state_master=$(amixer $cardselector sget Master |grep "%" | cut -d'%' -f1 | cut -d '[' -f2 | uniq)
+ #update pactl bluetooth audio to same level if present
+ if [ $state_bt_headphone = "[on]" ];then
+ pactl set-sink-volume $name_bt_headphone "$state_master%"
+ fi
+ print_volumes $state_master
+}
+
+function print_volumes()
+{
+ local state_master=$1
+ local state_headphone=$(amixer $cardselector sget Headphone |grep "%" | cut -d'%' -f1 | cut -d '[' -f2 | uniq)
+ local state_speaker=$(amixer $cardselector sget Speaker |grep "%" | cut -d'%' -f1 | cut -d '[' -f2 | uniq)
+ if [ $state_bt_headphone = "[on]" ]; then
+ send_notify "Master: $state_master% \nHeadphone: $state_headphone% \nSpeaker: $state_speaker%\nBT Headphone: $state_master%"
+ else
+ send_notify "Master: $state_master% \nHeadphone: $state_headphone% \nSpeaker: $state_speaker%"
+ fi
}
function toggle_volume()
{
- state=$(amixer sget Master | grep -o '\[o[n|f]*\]' | head -n 1)
- case "$state" in
+ local state_master=$(amixer $cardselector sget Master | grep -o '\[o[n|f]*\]' | head -n 1)
+ local state_headphone=$(amixer $cardselector sget Headphone | grep -o '\[o[n|f]*\]' | head -n 1)
+ local state_speaker=$(amixer $cardselector sget Speaker | grep -o '\[o[n|f]*\]' | head -n 1)
+ case "$state_master" in
"[on]")
- mute amixer sset Master mute
- mute amixer sset Headphone mute
- mute amixer sset Speaker mute
- echo 1 > $LED
+ mute amixer $cardselector sset Master mute
+ state_master="[off]"
+ # if headphones are off already, don't mute, instead save state to tmp file
+ if [ "$state_headphone" = "[off]" ];then
+ echo "headphone [off]" >> "$state_muted"
+ else
+ mute amixer $cardselector sset Headphone mute
+ state_headphone="[off]"
+ fi
+ # if speakers are off already, don't mute, instead save state to tmp file
+ if [ "$state_speaker" = "[off]" ];then
+ echo "speaker [off]" >> "$state_muted"
+ else
+ mute amixer $cardselector sset Speaker mute
+ state_speaker="[off]"
+ fi
+ # if present, also mute bluetooth headphone
+ if [ $state_bt_headphone = "[on]" ]; then
+ pactl set-sink-mute $name_bt_headphone 1
+ fi
;;
"[off]")
- mute amixer sset Master unmute
- mute amixer sset Headphone unmute
- mute amixer sset Speaker unmute
- echo 0 > $LED
+ mute amixer $cardselector sset Master unmute
+ state_master="[on]"
+ # if headphones are meant to be off, don't unmute them again
+ if [ "$state_muted_headphone" != "[off]" ];then
+ mute amixer $cardselector sset Headphone unmute
+ state_headphone="[on]"
+ fi
+ # if headphones are meant to be off, don't unmute them again
+ if [ "$state_muted_speaker" != "[off]" ];then
+ mute amixer $cardselector sset Speaker unmute
+ state_speaker="[on]"
+ fi
+ # if present, also unmute bluetooth headphone
+ if [ $state_bt_headphone = "[on]" ]; then
+ pactl set-sink-mute $name_bt_headphone 0
+ fi
;;
esac
+ if [ $state_bt_headphone = "[on]" ]; then
+ send_notify "Master: $state_master \nHeadphone: $state_headphone \nSpeaker: $state_speaker\nBT Headphone: $state_master" 2000
+ else
+ send_notify "Master: $state_master \nHeadphone: $state_headphone \nSpeaker: $state_speaker" 2000
+ fi
}
+function send_notify()
+{
+ # allow timeout to be set by 2nd argument
+ local timeout=500
+ if [ $2 -gt 0 ]; then
+ timeout=$2
+ fi
+ notify-send -t $timeout \
+ -i /usr/share/icons/gnome/48x48/devices/audio-speakers.png \
+ "Volume" "$1"
+}
+
+if [ $HOSTNAME = "dvzrv" ];then
+ cardselector=" -M -c 1 "
+ if [ -n "$(pactl list sinks short | grep $name_bt_headphone)" ]; then
+ state_bt_headphone="[on]"
+ fi
+ #TODO: also check pactl for JACK sink
+fi
+
case "$1" in
increase)
increase_volume
@@ -43,6 +128,13 @@ case "$1" in
decrease_volume
;;
toggle)
+ if [ -f "$state_muted" ]; then
+ state_muted_headphone=$(cat "$state_muted" | grep headphone | cut -d' ' -f2)
+ state_muted_speaker=$(cat "$state_muted" | grep speaker | cut -d' ' -f2)
+ rm "$state_muted"
+ else
+ touch "$state_muted"
+ fi
toggle_volume
;;
*)