summaryrefslogtreecommitdiff
path: root/volsv
blob: de30c77f86bab144f4e1f68b0be366ae4c761658 (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
145
146
147
148
149
#!/bin/sh

re='^[0-9]+$'

#{{{ detect
detect () {
  if pgrep -x pulseaudio >/dev/null; then
    PULSE='y' ; unset PIPEWIRE SNDIO ALSA
  elif pgrep -x pipewire >/dev/null; then
    PIPEWIRE='y' ; unset PULSE SNDIO ALSA
  elif pgrep -x sndiod >/dev/null; then
    SNDIO='y' ; unset PIPEWIRE PULSE ALSA
  else
    ALSA='y' ; unset PIPEWIRE PULSE SNDIO
  fi
}
#}}}

#{{{ up
up () {
  [ -n "$SNDIO" ] && sndioctl output.level=+"$(printf "%0.02f" "$(( amt * 0.01 ))")"
  [ -n "$PULSE$PIPEWIRE" ] && pamixer -i $amt
  [ -n "$ALSA" ] && amixer sset Master "${amt}%+"
}
#}}}

#{{{ down
down () {
  [ -n "$SNDIO" ] && sndioctl output.level=-"$(printf "%0.02f" "$(( amt * 0.01 ))")"
  [ -n "$PULSE$PIPEWIRE" ] && pamixer -d $amt
  [ -n "$ALSA" ] && amixer sset Master "${amt}%-"
}
#}}}

#{{{ mute
mute () {
  case "$1" in
    spr)
      [ -n "$SNDIO" ] && sndioctl output.mute=!
      [ -n "$PULSE$PIPEWIRE" ] && pamixer -t
      [ -n "$ALSA" ] && amixer sset Master toggle
      ;;
    mic)
      [ -n "$SNDIO" ] && sndioctl input.mute=!
      [ -n "$PULSE" ] && pamixer --source 1 -t
      [ -n "$PIPEWIRE$ALSA" ] && amixer sset Capture toggle
      ;;
  esac
}
#}}}

#{{{ get
get () {
  case "$1" in
    volu)
      [ -n "$SNDIO" ] && printf "%s%%" `sndioctl output.level | rev | cut -d'.' -f1 | rev | cut -c 1,2`
      [ -n "$PULSE$PIPEWIRE" ] && printf "%s%%\n" "$(pamixer --get-volume)"
      [ -n "$ALSA" ] && amixer sget Master | grep '\[[0-9]*\%\]' | sed "s/ /\n/g" | grep '%' | sed 's/\[//;s/\]//g'
      ;;
    mute)
      [ -n "$SNDIO" ] && sndioctl output.mute | cut -d'=' -f2 | sed 's/1/[off]/;s/0/[on]/'
      [ -n "$PULSE$PIPEWIRE" ] && pamixer --get-mute | sed 's/[Ff]alse/\[on\]/;s/[Tt]rue/\[off\]/'
      [ -n "$ALSA" ] && amixer sget Master | grep '\[o[fn]' | cut -d' ' -f8 | head -1
      ;;
    both)
      [ -n "$SNDIO" ] && printf "%s%s%%" `sndioctl output.mute | cut -d'=' -f2 | sed 's/1/[off]/;s/0/[on]/'` `sndioctl output.level | rev | cut -d'.' -f1 | rev | cut -c 1,2`
      [ -n "$PULSE$PIPEWIRE" ] && printf "%s%s%%\n" `pamixer --get-mute | sed 's/[Ff]alse/\[on\]/;s/[Tt]rue/\[off\]/'` "$(pamixer --get-volume)"
      [ -n "$ALSA" ] && printf "%s%s" `amixer sget Master | grep '\[o[fn]' | cut -d' ' -f8 | head -1` `amixer sget Master | grep '\[[0-9]*\%\]' | sed "s/ /\n/g" | grep '%' | sed 's/\[//;s/\]//g'`
      ;;
  esac
}
#}}}

#{{{ setact
setact() {
  if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
    echo "Running $1 with $action..."
    echo "Incompatible options given. Only one action may be specified per run."
    return 1
  else
    action="$1"
  fi
}
#}}}

#{{{ printhelp
printhelp () { cat << EOF
volsv: volume setter v, sets the volume despite the sound server
supported sound servers are sndio, pulseaudio, pipewire, and alsa (no bluealsa)

Actions:
  -i x    Increase the volume by x (5% if not given)
  -d x    Decrease the volume by x (5% if not given)
  -t      Mute the default output
  -m      Mute the default mic
  -v      Get the volume of the default output
  -g      Get the mute status of the default output
  -a      Get the volume and mute status of the default output
EOF
}
#}}}

#{{{ getopts
while getopts "hidtmvga" o; do case "${o}" in
  i)
    eval nextopt=\${$OPTIND}
    if [ -n "$nextopt" -a "$nextopt" != "-*" ]; then
      OPTIND=$((OPTIND + 1))
      amt=$nextopt
    fi
    case "$amt" in
      ''|*[!0-9]*) amt=5 ;;
    esac
    setact up || exit 1
    ;;
  d)
    eval nextopt=\${$OPTIND}
    if [ -n "$nextopt" -a "$nextopt" != "-*" ]; then
      OPTIND=$((OPTIND + 1))
      amt=$nextopt
    fi
    case "$amt" in
      ''|*[!0-9]*) amt=5 ;;
    esac
    setact down || exit 1
    ;;
  t) setact mute || exit 1 ;;
  m) setact micm || exit 1 ;;
  v) setact getv || exit 1 ;;
  g) setact getm || exit 1 ;;
  a) setact geta || exit 1 ;;
  *) printhelp ; exit 1 ;;
esac done
#}}}

detect

#{{{ action
case "$action" in
  up) up ;;
  down) down ;;
  mute) mute spr ;;
  micm) mute mic ;;
  getv) get volu ;;
  getm) get mute ;;
  geta) get both ;;
  *) printhelp ;;
esac
#}}}