aboutsummaryrefslogtreecommitdiffstats
path: root/bin/set_volume
blob: 58349942984467cecf0725372c57700e64bcd970 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env bash

set -euo pipefail

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()
{
  amixer $cardselector sset Master 5%+ &>/dev/null
  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()
{
  amixer $cardselector sset Master 5%- &>/dev/null
  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% Headphone: $state_headphone% Speaker: $state_speaker% BT Headphone: $state_master%"
  else
    send_notify "Master: $state_master% Headphone: $state_headphone% Speaker: $state_speaker%"
  fi
}

function toggle_volume()
{
  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]")
      amixer $cardselector sset Master mute &>/dev/null
      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
        amixer $cardselector sset Headphone mute &>/dev/null
        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
        amixer $cardselector sset Speaker mute &>/dev/null
        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]")
      amixer $cardselector sset Master unmute &>/dev/null
      state_master="[on]"
      # if headphones are meant to be off, don't unmute them again
      if [ "$state_muted_headphone" != "[off]" ];then
        amixer $cardselector sset Headphone unmute &>/dev/null
        state_headphone="[on]"
      fi
      # if headphones are meant to be off, don't unmute them again
      if [ "$state_muted_speaker" != "[off]" ];then
        amixer $cardselector sset Speaker unmute &>/dev/null
        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 Headphone: $state_headphone Speaker: $state_speaker BT Headphone: $state_master" 2000
  else
    send_notify "Master: $state_master Headphone: $state_headphone Speaker: $state_speaker" 2000
  fi
}

function send_notify()
{
  # allow timeout to be set by 2nd argument
  local timeout=1000
#  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" | "up" | "+" )
    increase_volume
    ;;
  "decrease" | "down" | "-" )
    decrease_volume
    ;;
  "toggle")
    if [ -f "$state_muted" ]; then
      set +e
      state_muted_speaker="$(cat "$state_muted" | grep speaker | cut -d' ' -f2)"
      state_muted_headphone="$(cat "$state_muted" | grep headphone | cut -d' ' -f2)"
      set -e
      rm "$state_muted"
    else
      touch "$state_muted"
    fi
    toggle_volume
    ;;
  *)
    ;;
esac

exit 0