summaryrefslogtreecommitdiff
path: root/volsv
blob: 5976fcc5e1300b2a328660439f6f500bf6acdd05 (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
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/sh

#{{{ detect
detect () {
  if pgrep -x pulseaudio >/dev/null; then
    AUDIO_SERVER="PULSE"
  elif pgrep -x pipewire >/dev/null; then
    AUDIO_SERVER="PW"
  elif pgrep -x sndiod >/dev/null; then
    AUDIO_SERVER="SNDIO"
  else
    AUDIO_SERVER="ALSA"
  fi
}
#}}}

#{{{ up
up () {
  case "$AUDIO_SERVER" in
    SNDIO) sndioctl output.level=+"$(printf "%0.02f" "$(( amt * 0.01 ))")" ;;
    PULSE|PW) pactl set-sink-volume $(pactl get-default-sink) "+${amt}%" ;;
    ALSA) amixer sset Master "${amt}%+" ;;
  esac
}
#}}}

#{{{ down
down () {
  case "$AUDIO_SERVER" in
    SNDIO) sndioctl output.level=-"$(printf "%0.02f" "$(( amt * 0.01 ))")" ;;
    PULSE|PW) pactl set-sink-volume $(pactl get-default-sink) "-${amt}%" ;;
    ALSA) amixer sset Master "${amt}%-" ;;
  esac
}
#}}}

#{{{ mute
mute () {
  case "$1" in
    spr)
      case "$AUDIO_SERVER" in
        SNDIO) sndioctl output.mute=! ;;
        PULSE|PW) pactl set-sink-mute $(pactl get-default-sink) toggle ;;
        ALSA) amixer sset Master toggle ;;
      esac
      ;;
    mic)
      case "$AUDIO_SERVER" in
        SNDIO) sndioctl input.mute=! ;;
        PULSE|PW) pactl set-source-mute $(pactl get-default-source) toggle ;;
        ALSA) amixer sset Capture toggle ;;
      esac
      ;;
  esac
}
#}}}

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